r52676 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52675‎ | r52676 | r52677 >
Date:05:06, 2 July 2009
Author:ashley
Status:deferred (Comments)
Tags:
Comment:
cleanup to WikiCategoryTagCloud
Modified paths:
  • /trunk/extensions/WikiCategoryTagCloud/WikiCategoryTagCloud.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiCategoryTagCloud/WikiCategoryTagCloud.php
@@ -1,6 +1,8 @@
22 <?php
33 /**
44 * Wiki Category Tag Cloud
 5+ * @file
 6+ * @ingroup Extensions
57 * @author Daniel Friesen http://daniel.friesen.name
68 * @version 1.0.1
79 *
@@ -23,27 +25,37 @@
2426
2527 if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
2628
27 -$wgExtensionCredits['parserhook'][] = array (
 29+$wgExtensionCredits['parserhook'][] = array(
2830 'name' => 'Wiki Category Tag Cloud',
29 - 'url' => 'http://mediawiki.org/wiki/Extension:WikiCategoryTagCloud',
3031 'version' => '1.0.1',
3132 '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',
3335 );
3436
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+
3647 function registerTagCloudExtension() {
3748 global $wgParser;
38 - $wgParser->setHook('tagcloud', 'renderTagCloud');
39 - $wgHooks['ArticleSave'][] = 'invalidateCache()';
 49+ $wgParser->setHook( 'tagcloud', 'renderTagCloud' );
 50+ return true;
4051 }
4152
4253 function invalidateCache() {
43 - $titles[0] = explode( "\n", wfMsg('tagcloudpages') );
 54+ $titles[0] = explode( "\n", wfMsg( 'tagcloudpages' ) );
4455
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();
4758 }
 59+ return true;
4860 }
4961
5062 function renderTagCloud( $input, $params, $parser ) {
@@ -51,72 +63,69 @@
5264 $INCREASE_FACTOR = 100;
5365
5466 global $wgScript;
55 - $dbr = &wfGetDB(DB_SLAVE);
56 - extract($dbr->tableNames('categorylinks'));
 67+ $dbr = wfGetDB( DB_SLAVE );
 68+ extract( $dbr->tableNames( 'categorylinks' ) );
5769
5870 $cloud_style = @$params['style'];
5971 $cloud_classes = preg_split( '/\s+/', @$params['class'] );
60 - array_unshift( $cloud_classes, "tagcloud" );
 72+ array_unshift( $cloud_classes, 'tagcloud' );
6173 $link_style = $params['linkstyle'];
6274 $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 ) {
6779 $MIN_SIZE = $min_size_input;
6880 }
69 - if ($increase_factor_input != null) {
 81+ if( $increase_factor_input != null ) {
7082 $INCREASE_FACTOR = $increase_factor_input;
7183 }
72 - if ($min_count_input == null) {
 84+ if( $min_count_input == null ) {
7385 $min_count_input = 0;
7486 }
7587
76 - $excluded_input = getBoxExtensionOption($input, "exclude");
 88+ $excluded_input = getBoxExtensionOption( $input, 'exclude' );
7789
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 ) {
8294 $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 ) {
8698 $exclude_condition = $exclude_condition . ",";
8799 }
88100 }
89 - $exclude_condition = $exclude_condition . ")";
 101+ $exclude_condition = $exclude_condition . ")";
90102 }
91103 }
92104
93 - //$exclude_condition = mysql_real_escape_string($exclude_condition);
94 - //$min_count_input = mysql_real_escape_string($min_count_input);
95 -
96105 $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";
97106
98 - $res = $dbr->query($sql);
99 - $count = $dbr->numRows($res);
 107+ $res = $dbr->query( $sql );
 108+ $count = $dbr->numRows( $res );
100109
101 - $htmlOut = "";
 110+ $htmlOut = '';
102111 $htmlOut = $htmlOut . "<div class=\"" . implode( ' ', $cloud_classes ) . "\" style=\"{$cloud_style}\">";
103112
104113 $min = 1000000;
105114 $max = -1;
106115
107 - for ($i = 0; $i < $count; $i++) {
108 - $obj = $dbr->fetchObject($res);
 116+ for( $i = 0; $i < $count; $i++ ) {
 117+ $obj = $dbr->fetchObject( $res );
109118 $tags[$i][0] = $obj->title;
110119 $tags[$i][1] = $obj->count;
111 - if ($obj->count < $min) {
 120+ if( $obj->count < $min ) {
112121 $min = $obj->count;
113122 }
114 - if ($obj->count > $max) {
 123+ if( $obj->count > $max ) {
115124 $max = $obj->count;
116125 }
117126 }
118127
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 );
121130 $title = Title::makeTitle( NS_CATEGORY, $tags[$i][0] );
122131 $style = $link_style;
123132 if( $style != '' && $style{-1} != ';' ) $style .= ';';
@@ -124,16 +133,16 @@
125134 $currentRow = "<a class=\"" . implode( ' ', $link_classes ) . "\" style=\"{$style}\" href=\"" . $title->getLocalURL() . "\">" . $title->getText() . "</a>&nbsp; ";
126135 $htmlOut = $htmlOut . $currentRow;
127136 }
128 - $htmlOut = $htmlOut . "</div>";
 137+ $htmlOut = $htmlOut . '</div>';
129138 return $htmlOut;
130139 }
131140
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] );
136145 } else {
137 - return htmlspecialchars($matches[1]);
 146+ return htmlspecialchars( $matches[1] );
138147 }
139148 }
140 -}
 149+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r52694follow-up to r52676: remove unnecessary use of extract(), per code reviewashley14:03, 2 July 2009

Comments

#Comment by Nikerabbit (talk | contribs)   08:41, 2 July 2009

While you are at it, extract( $dbr->tableNames( 'categorylinks' ) ); looks like unnecessary use of extract.

#Comment by Jack Phoenix (talk | contribs)   14:04, 2 July 2009

Done in r52694.

Status & tagging log