-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddLinkClasses.php
More file actions
75 lines (61 loc) · 2.34 KB
/
AddLinkClasses.php
File metadata and controls
75 lines (61 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* AddLinkClasses extension - Adds class attributes to links based on their category
*
* @package MediaWiki
* @subpackage Extensions
* @author [http://www.mediawiki.org/wiki/User:Nad User:Nad] for Dave Booker (RAC bid 1239559)
* @licence GNU General Public Licence 2.0 or later
*
*/
if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
define( 'ADDLINKCLASSES_VERSION', '2.1.1, 2025-04-27' );
$wgAddLinkClassesPattern = false;
$wgExtensionFunctions[] = 'efSetupAddLinkClasses';
$wgExtensionCredits['other'][] = array(
'name' => 'AddLinkClasses',
'author' => '[http://www.mediawiki.org/wiki/User:Nad User:Nad]',
'description' => 'Adds class attributes to links based on their category',
'url' => 'http://www.rentacoder.com/RentACoder/misc/BidRequests/ShowBidRequest.asp?lngBidRequestId=1239559',
'version' => ADDLINKCLASSES_VERSION
);
class AddLinkClasses {
function __construct() {
global $wgHooks;
$wgHooks['HtmlPageLinkRendererEnd'][] = $this;
}
function onHtmlPageLinkRendererEnd( $linkRenderer, $target, $isKnown, &$text, &$attribs, &$ret ) {
global $wgAddLinkClassesPattern;
$title = Title::newFromLinkTarget( $target );
if( $isKnown && $title->exists() ) {
// Get cats
$cats = array();
$dbr = wfGetDB( DB_REPLICA );
//$cl = $dbr->tableName( 'categorylinks' );
$cl = 'categorylinks';
$id = $title->getArticleID();
$res = $dbr->select( $cl, 'cl_to', "cl_from = $id", __METHOD__, array( 'ORDER BY' => 'cl_sortkey' ) );
foreach ( $res as $row ) {
$cats[] = 'cat-' . preg_replace( '|\W|', '', strtolower( $row->cl_to ) );
}
// Add cat classes if any
if( count( $cats ) > 0 ) {
$classes = join( ' ', $cats );
$attribs['class'] = array_key_exists( 'class', $attribs ) ? $attribs['class'] . " $classes" : $classes;
}
// If a pattern has been set apply it to the text
if( is_array( $wgAddLinkClassesPattern ) ) {
if ( $text instanceof HtmlArmor ) {
$text = new HtmlArmor( preg_replace_callback( $wgAddLinkClassesPattern[0], $wgAddLinkClassesPattern[1] , HtmlArmor::getHtml( $text ) ) );
} else {
$text = preg_replace_callback( $wgAddLinkClassesPattern[0], $wgAddLinkClassesPattern[1] , $text );
}
}
}
return true;
}
}
function efSetupAddLinkClasses() {
global $egAddLinkClasses;
$egAddLinkClasses = new AddLinkClasses();
}