Index: trunk/extensions/WikiCategoryTagCloud/WikiCategoryTagCloud.php |
— | — | @@ -1,6 +1,8 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Wiki Category Tag Cloud |
| 5 | + * @file |
| 6 | + * @ingroup Extensions |
5 | 7 | * @author Daniel Friesen http://daniel.friesen.name |
6 | 8 | * @version 1.0.1 |
7 | 9 | * |
— | — | @@ -23,27 +25,37 @@ |
24 | 26 | |
25 | 27 | if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
26 | 28 | |
27 | | -$wgExtensionCredits['parserhook'][] = array ( |
| 29 | +$wgExtensionCredits['parserhook'][] = array( |
28 | 30 | 'name' => 'Wiki Category Tag Cloud', |
29 | | - 'url' => 'http://mediawiki.org/wiki/Extension:WikiCategoryTagCloud', |
30 | 31 | 'version' => '1.0.1', |
31 | 32 | 'author' => "[http://mediawiki.org/wiki/User:Dantman Daniel Friesen]", |
32 | | - 'description' => "A Category Tag Cloud derived, improved, and fixed from the YetAnotherTagCloud Extension", |
| 33 | + 'description' => 'A Category Tag Cloud derived, improved, and fixed from the YetAnotherTagCloud Extension', |
| 34 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:WikiCategoryTagCloud', |
33 | 35 | ); |
34 | 36 | |
35 | | -$wgExtensionFunctions[] = 'registerTagCloudExtension'; |
| 37 | +// Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980 |
| 38 | +if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { |
| 39 | + $wgHooks['ParserFirstCallInit'][] = 'registerTagCloudExtension'; |
| 40 | +} else { |
| 41 | + $wgExtensionFunctions[] = 'registerTagCloudExtension'; |
| 42 | +} |
| 43 | + |
| 44 | +// Hooked function |
| 45 | +$wgHooks['ArticleSave'][] = 'invalidateCache'; |
| 46 | + |
36 | 47 | function registerTagCloudExtension() { |
37 | 48 | global $wgParser; |
38 | | - $wgParser->setHook('tagcloud', 'renderTagCloud'); |
39 | | - $wgHooks['ArticleSave'][] = 'invalidateCache()'; |
| 49 | + $wgParser->setHook( 'tagcloud', 'renderTagCloud' ); |
| 50 | + return true; |
40 | 51 | } |
41 | 52 | |
42 | 53 | function invalidateCache() { |
43 | | - $titles[0] = explode( "\n", wfMsg('tagcloudpages') ); |
| 54 | + $titles[0] = explode( "\n", wfMsg( 'tagcloudpages' ) ); |
44 | 55 | |
45 | | - for ($i = 0; $i < count($titles); $i++) { |
46 | | - Title :: newFromText($titles[$i])->invalidateCache(); |
| 56 | + for( $i = 0; $i < count( $titles ); $i++) { |
| 57 | + Title::newFromText( $titles[$i] )->invalidateCache(); |
47 | 58 | } |
| 59 | + return true; |
48 | 60 | } |
49 | 61 | |
50 | 62 | function renderTagCloud( $input, $params, $parser ) { |
— | — | @@ -51,72 +63,69 @@ |
52 | 64 | $INCREASE_FACTOR = 100; |
53 | 65 | |
54 | 66 | global $wgScript; |
55 | | - $dbr = &wfGetDB(DB_SLAVE); |
56 | | - extract($dbr->tableNames('categorylinks')); |
| 67 | + $dbr = wfGetDB( DB_SLAVE ); |
| 68 | + extract( $dbr->tableNames( 'categorylinks' ) ); |
57 | 69 | |
58 | 70 | $cloud_style = @$params['style']; |
59 | 71 | $cloud_classes = preg_split( '/\s+/', @$params['class'] ); |
60 | | - array_unshift( $cloud_classes, "tagcloud" ); |
| 72 | + array_unshift( $cloud_classes, 'tagcloud' ); |
61 | 73 | $link_style = $params['linkstyle']; |
62 | 74 | $link_classes = preg_split( '/\s+/', @$params['linkclass'] ); |
63 | | - $min_count_input = getBoxExtensionOption($input, "min_count"); |
64 | | - $min_size_input = getBoxExtensionOption($input, "min_size"); |
65 | | - $increase_factor_input = getBoxExtensionOption($input, "increase_factor"); |
66 | | - if ($min_size_input != null) { |
| 75 | + $min_count_input = getBoxExtensionOption( $input, 'min_count' ); |
| 76 | + $min_size_input = getBoxExtensionOption( $input, 'min_size' ); |
| 77 | + $increase_factor_input = getBoxExtensionOption( $input, 'increase_factor' ); |
| 78 | + if( $min_size_input != null ) { |
67 | 79 | $MIN_SIZE = $min_size_input; |
68 | 80 | } |
69 | | - if ($increase_factor_input != null) { |
| 81 | + if( $increase_factor_input != null ) { |
70 | 82 | $INCREASE_FACTOR = $increase_factor_input; |
71 | 83 | } |
72 | | - if ($min_count_input == null) { |
| 84 | + if( $min_count_input == null ) { |
73 | 85 | $min_count_input = 0; |
74 | 86 | } |
75 | 87 | |
76 | | - $excluded_input = getBoxExtensionOption($input, "exclude"); |
| 88 | + $excluded_input = getBoxExtensionOption( $input, 'exclude' ); |
77 | 89 | |
78 | | - $exclude_condition = ""; |
79 | | - if (strlen($excluded_input) > 0) { |
80 | | - $excluded_categories = explode(",", $excluded_input); |
81 | | - if (count($excluded_categories) > 0) { |
| 90 | + $exclude_condition = ''; |
| 91 | + if( strlen( $excluded_input ) > 0 ) { |
| 92 | + $excluded_categories = explode( ",", $excluded_input ); |
| 93 | + if( count( $excluded_categories ) > 0 ) { |
82 | 94 | $exclude_condition = " WHERE cl_to NOT IN ("; |
83 | | - for ($i = 0; $i < count($excluded_categories); $i++) { |
84 | | - $exclude_condition = $exclude_condition . "'" . trim($excluded_categories[$i]) . "'"; |
85 | | - if ($i < count($excluded_categories) - 1) { |
| 95 | + for( $i = 0; $i < count( $excluded_categories ); $i++ ) { |
| 96 | + $exclude_condition = $exclude_condition . "'" . trim( $excluded_categories[$i] ) . "'"; |
| 97 | + if( $i < count( $excluded_categories ) - 1 ) { |
86 | 98 | $exclude_condition = $exclude_condition . ","; |
87 | 99 | } |
88 | 100 | } |
89 | | - $exclude_condition = $exclude_condition . ")"; |
| 101 | + $exclude_condition = $exclude_condition . ")"; |
90 | 102 | } |
91 | 103 | } |
92 | 104 | |
93 | | - //$exclude_condition = mysql_real_escape_string($exclude_condition); |
94 | | - //$min_count_input = mysql_real_escape_string($min_count_input); |
95 | | - |
96 | 105 | $sql = "SELECT cl_to as title, COUNT(*) as count FROM $categorylinks " . $exclude_condition . " GROUP BY cl_to HAVING count >= $min_count_input ORDER BY cl_to ASC"; |
97 | 106 | |
98 | | - $res = $dbr->query($sql); |
99 | | - $count = $dbr->numRows($res); |
| 107 | + $res = $dbr->query( $sql ); |
| 108 | + $count = $dbr->numRows( $res ); |
100 | 109 | |
101 | | - $htmlOut = ""; |
| 110 | + $htmlOut = ''; |
102 | 111 | $htmlOut = $htmlOut . "<div class=\"" . implode( ' ', $cloud_classes ) . "\" style=\"{$cloud_style}\">"; |
103 | 112 | |
104 | 113 | $min = 1000000; |
105 | 114 | $max = -1; |
106 | 115 | |
107 | | - for ($i = 0; $i < $count; $i++) { |
108 | | - $obj = $dbr->fetchObject($res); |
| 116 | + for( $i = 0; $i < $count; $i++ ) { |
| 117 | + $obj = $dbr->fetchObject( $res ); |
109 | 118 | $tags[$i][0] = $obj->title; |
110 | 119 | $tags[$i][1] = $obj->count; |
111 | | - if ($obj->count < $min) { |
| 120 | + if( $obj->count < $min ) { |
112 | 121 | $min = $obj->count; |
113 | 122 | } |
114 | | - if ($obj->count > $max) { |
| 123 | + if( $obj->count > $max ) { |
115 | 124 | $max = $obj->count; |
116 | 125 | } |
117 | 126 | } |
118 | 127 | |
119 | | - for ($i = 0; $i < $count; $i++) { |
120 | | - $textSize = $MIN_SIZE + ($INCREASE_FACTOR * ($tags[$i][1])) / ($max); |
| 128 | + for( $i = 0; $i < $count; $i++ ) { |
| 129 | + $textSize = $MIN_SIZE + ( $INCREASE_FACTOR * ( $tags[$i][1] ) ) / ( $max ); |
121 | 130 | $title = Title::makeTitle( NS_CATEGORY, $tags[$i][0] ); |
122 | 131 | $style = $link_style; |
123 | 132 | if( $style != '' && $style{-1} != ';' ) $style .= ';'; |
— | — | @@ -124,16 +133,16 @@ |
125 | 134 | $currentRow = "<a class=\"" . implode( ' ', $link_classes ) . "\" style=\"{$style}\" href=\"" . $title->getLocalURL() . "\">" . $title->getText() . "</a> "; |
126 | 135 | $htmlOut = $htmlOut . $currentRow; |
127 | 136 | } |
128 | | - $htmlOut = $htmlOut . "</div>"; |
| 137 | + $htmlOut = $htmlOut . '</div>'; |
129 | 138 | return $htmlOut; |
130 | 139 | } |
131 | 140 | |
132 | | -function getBoxExtensionOption($input, $name, $isNumber = false) { |
133 | | - if (preg_match("/^\s*$name\s*=\s*(.*)/mi", $input, $matches)) { |
134 | | - if ($isNumber) { |
135 | | - return intval($matches[1]); |
| 141 | +function getBoxExtensionOption( $input, $name, $isNumber = false ) { |
| 142 | + if( preg_match( "/^\s*$name\s*=\s*(.*)/mi", $input, $matches ) ) { |
| 143 | + if( $isNumber ) { |
| 144 | + return intval( $matches[1] ); |
136 | 145 | } else { |
137 | | - return htmlspecialchars($matches[1]); |
| 146 | + return htmlspecialchars( $matches[1] ); |
138 | 147 | } |
139 | 148 | } |
140 | | -} |
| 149 | +} |
\ No newline at end of file |