r65584 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65583‎ | r65584 | r65585 >
Date:20:56, 27 April 2010
Author:yaron
Status:deferred
Tags:
Comment:
Fixed formatting with stylize.php, added 'static' declaration for all functions, replaced deprecated SMW getXSDValue() with getValueKey()
Modified paths:
  • /trunk/extensions/SemanticDrilldown/includes/SD_Utils.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticDrilldown/includes/SD_Utils.inc
@@ -5,7 +5,7 @@
66 * @author Yaron Koren
77 */
88
9 -if (!defined('MEDIAWIKI')) die();
 9+if ( !defined( 'MEDIAWIKI' ) ) die();
1010
1111 class SDUtils {
1212
@@ -15,81 +15,81 @@
1616 * and excludes, categories that are manually set with
1717 * 'SHOWINDRILLDOWN' and 'HIDEFROMDRILLDOWN', respectively.
1818 */
19 - function getTopLevelCategories() {
 19+ static function getTopLevelCategories() {
2020 $categories = array();
2121 $dbr = wfGetDB( DB_SLAVE );
22 - extract($dbr->tableNames('page', 'categorylinks', 'page_props'));
 22+ extract( $dbr->tableNames( 'page', 'categorylinks', 'page_props' ) );
2323 $cat_ns = NS_CATEGORY;
2424 $sql = "SELECT page_title FROM $page p LEFT OUTER JOIN $categorylinks cl ON p.page_id = cl.cl_from WHERE p.page_namespace = $cat_ns AND cl.cl_to IS NULL";
25 - $res = $dbr->query($sql);
26 - if ($dbr->numRows( $res ) > 0) {
27 - while ($row = $dbr->fetchRow($res)) {
28 - $categories[] = str_replace('_', ' ', $row[0]);
 25+ $res = $dbr->query( $sql );
 26+ if ( $dbr->numRows( $res ) > 0 ) {
 27+ while ( $row = $dbr->fetchRow( $res ) ) {
 28+ $categories[] = str_replace( '_', ' ', $row[0] );
2929 }
3030 }
31 - $dbr->freeResult($res);
 31+ $dbr->freeResult( $res );
3232
3333 // get 'hide' and 'show' categories
3434 $hidden_cats = $shown_cats = array();
3535 $sql2 = "SELECT p.page_title, pp.pp_propname FROM $page p JOIN $page_props pp ON p.page_id = pp.pp_page WHERE p.page_namespace = $cat_ns AND (pp.pp_propname = 'hidefromdrilldown' OR pp.pp_propname = 'showindrilldown') AND pp.pp_value = 'y'";
36 - $res2 = $dbr->query($sql2);
37 - if ($dbr->numRows( $res2 ) > 0) {
38 - while ($row = $dbr->fetchRow($res2)) {
39 - if ($row[1] == 'hidefromdrilldown')
40 - $hidden_cats[] = str_replace('_', ' ', $row[0]);
 36+ $res2 = $dbr->query( $sql2 );
 37+ if ( $dbr->numRows( $res2 ) > 0 ) {
 38+ while ( $row = $dbr->fetchRow( $res2 ) ) {
 39+ if ( $row[1] == 'hidefromdrilldown' )
 40+ $hidden_cats[] = str_replace( '_', ' ', $row[0] );
4141 else
42 - $shown_cats[] = str_replace('_', ' ', $row[0]);
 42+ $shown_cats[] = str_replace( '_', ' ', $row[0] );
4343 }
4444 }
45 - $dbr->freeResult($res2);
46 - $categories = array_merge($categories, $shown_cats);
47 - foreach ($hidden_cats as $hidden_cat) {
48 - foreach ($categories as $i => $cat) {
49 - if ($cat == $hidden_cat) {
50 - unset($categories[$i]);
 45+ $dbr->freeResult( $res2 );
 46+ $categories = array_merge( $categories, $shown_cats );
 47+ foreach ( $hidden_cats as $hidden_cat ) {
 48+ foreach ( $categories as $i => $cat ) {
 49+ if ( $cat == $hidden_cat ) {
 50+ unset( $categories[$i] );
5151 }
5252 }
5353 }
54 - sort($categories);
 54+ sort( $categories );
5555 return $categories;
5656 }
5757
5858 /**
5959 * Gets a list of the names of all properties in the wiki
6060 */
61 - function getSemanticProperties() {
 61+ static function getSemanticProperties() {
6262 global $smwgContLang;
6363 $smw_namespace_labels = $smwgContLang->getNamespaces();
6464 $all_properties = array();
6565
66 - //$options = new SMWRequestOptions();
67 - //$options->limit = 10000;
68 - //$used_properties = smwfGetStore()->getPropertiesSpecial($options);
 66+ // $options = new SMWRequestOptions();
 67+ // $options->limit = 10000;
 68+ // $used_properties = smwfGetStore()->getPropertiesSpecial($options);
6969 $used_properties = smwfGetStore()->getPropertiesSpecial();
70 - foreach ($used_properties as $property) {
 70+ foreach ( $used_properties as $property ) {
7171 $all_properties[] = $property[0]->getWikiValue();
7272 }
73 - $unused_properties = smwfGetStore()->getUnusedPropertiesSpecial($options);
74 - foreach ($unused_properties as $property) {
 73+ $unused_properties = smwfGetStore()->getUnusedPropertiesSpecial( $options );
 74+ foreach ( $unused_properties as $property ) {
7575 $all_properties[] = $property->getWikiValue();
7676 }
7777 // remove the special properties of Semantic Drilldown from this list...
7878 global $sdgContLang;
7979 $sd_props = $sdgContLang->getPropertyLabels();
8080 $sd_prop_aliases = $sdgContLang->getPropertyAliases();
81 - foreach ($all_properties as $i => $prop_name) {
82 - foreach ($sd_props as $prop => $label) {
83 - if ($prop_name == $label) {
84 - unset($all_properties[$i]);
 81+ foreach ( $all_properties as $i => $prop_name ) {
 82+ foreach ( $sd_props as $prop => $label ) {
 83+ if ( $prop_name == $label ) {
 84+ unset( $all_properties[$i] );
8585 }
8686 }
87 - foreach ($sd_prop_aliases as $alias => $cur_prop) {
88 - if ($prop_name == $alias) {
89 - unset($all_properties[$i]);
 87+ foreach ( $sd_prop_aliases as $alias => $cur_prop ) {
 88+ if ( $prop_name == $alias ) {
 89+ unset( $all_properties[$i] );
9090 }
9191 }
9292 }
93 - sort($all_properties);
 93+ sort( $all_properties );
9494 return $all_properties;
9595 }
9696
@@ -97,31 +97,31 @@
9898 * Gets the names of all the filter pages, i.e. pages in the Filter
9999 * namespace
100100 */
101 - function getFilters() {
 101+ static function getFilters() {
102102 $dbr = wfGetDB( DB_SLAVE );
103 - $res = $dbr->select('page', 'page_title', array('page_namespace' => SD_NS_FILTER));
 103+ $res = $dbr->select( 'page', 'page_title', array( 'page_namespace' => SD_NS_FILTER ) );
104104 $filters = array();
105 - while ($row = $dbr->fetchRow($res)) {
 105+ while ( $row = $dbr->fetchRow( $res ) ) {
106106 $filters[] = $row[0];
107107 }
108 - $dbr->freeResult($res);
 108+ $dbr->freeResult( $res );
109109 return $filters;
110110 }
111111
112112 /**
113 - * Generic function - gets all the values that a specific page
 113+ * Generic static function - gets all the values that a specific page
114114 * points to with a specific property
115115 * ($special_prop and $prop represent the same value, depending on
116116 * whether we're using SMW 1.4 or an earlier version)
117117 */
118 - function getValuesForProperty($subject, $subject_namespace, $special_prop) {
 118+ static function getValuesForProperty( $subject, $subject_namespace, $special_prop ) {
119119 $store = smwfGetStore();
120 - $subject_title = Title::newFromText($subject, $subject_namespace);
121 - $property = SMWPropertyValue::makeProperty($special_prop);
122 - $res = $store->getPropertyValues($subject_title, $property);
 120+ $subject_title = Title::newFromText( $subject, $subject_namespace );
 121+ $property = SMWPropertyValue::makeProperty( $special_prop );
 122+ $res = $store->getPropertyValues( $subject_title, $property );
123123 $values = array();
124 - foreach ($res as $prop_val) {
125 - $values[] = html_entity_decode(str_replace('_', ' ', $prop_val->getXSDValue()));
 124+ foreach ( $res as $prop_val ) {
 125+ $values[] = html_entity_decode( str_replace( '_', ' ', $prop_val->getValueKey() ) );
126126 }
127127 return $values;
128128 }
@@ -129,11 +129,11 @@
130130 /**
131131 * Gets all the filters specified for a category.
132132 */
133 - function loadFiltersForCategory($category) {
 133+ static function loadFiltersForCategory( $category ) {
134134 $filters = array();
135 - $filter_names = SDUtils::getValuesForProperty(str_replace(' ', '_', $category), NS_CATEGORY, '_SD_F');
136 - foreach ($filter_names as $filter_name) {
137 - $filters[] = SDFilter::load($filter_name);
 135+ $filter_names = SDUtils::getValuesForProperty( str_replace( ' ', '_', $category ), NS_CATEGORY, '_SD_F' );
 136+ foreach ( $filter_names as $filter_name ) {
 137+ $filters[] = SDFilter::load( $filter_name );
138138 }
139139 return $filters;
140140 }
@@ -141,122 +141,122 @@
142142 /**
143143 * Gets all the display parameters defined for a category
144144 */
145 - function getDisplayParamsForCategory($category) {
146 - $all_display_params = SDUtils::getValuesForProperty(str_replace(' ', '_', $category), NS_CATEGORY, '_SD_DP');
 145+ static function getDisplayParamsForCategory( $category ) {
 146+ $all_display_params = SDUtils::getValuesForProperty( str_replace( ' ', '_', $category ), NS_CATEGORY, '_SD_DP' );
147147
148148 $return_display_params = array();
149 - foreach ($all_display_params as $display_params) {
150 - $return_display_params[] = explode(';', $display_params);
 149+ foreach ( $all_display_params as $display_params ) {
 150+ $return_display_params[] = explode( ';', $display_params );
151151 }
152152 return $return_display_params;
153153 }
154154
155 - function getCategoryChildren($category_name, $get_categories, $levels) {
156 - if ($levels == 0) {
 155+ static function getCategoryChildren( $category_name, $get_categories, $levels ) {
 156+ if ( $levels == 0 ) {
157157 return array();
158158 }
159159 $pages = array();
160160 $subcategories = array();
161161 $dbr = wfGetDB( DB_SLAVE );
162 - extract($dbr->tableNames('page', 'categorylinks'));
 162+ extract( $dbr->tableNames( 'page', 'categorylinks' ) );
163163 $cat_ns = NS_CATEGORY;
164 - $query_category = str_replace(' ', '_', $category_name);
165 - $query_category = str_replace("'", "\'", $query_category);
 164+ $query_category = str_replace( ' ', '_', $category_name );
 165+ $query_category = str_replace( "'", "\'", $query_category );
166166 $sql = "SELECT p.page_title, p.page_namespace FROM $categorylinks cl
167167 JOIN $page p on cl.cl_from = p.page_id
168168 WHERE cl.cl_to = '$query_category'\n";
169 - if ($get_categories)
 169+ if ( $get_categories )
170170 $sql .= "AND p.page_namespace = $cat_ns\n";
171171 $sql .= "ORDER BY cl.cl_sortkey";
172 - $res = $dbr->query($sql);
173 - while ($row = $dbr->fetchRow($res)) {
174 - if ($get_categories) {
 172+ $res = $dbr->query( $sql );
 173+ while ( $row = $dbr->fetchRow( $res ) ) {
 174+ if ( $get_categories ) {
175175 $subcategories[] = $row[0];
176176 $pages[] = $row[0];
177177 } else {
178 - if ($row[1] == $cat_ns)
 178+ if ( $row[1] == $cat_ns )
179179 $subcategories[] = $row[0];
180180 else
181181 $pages[] = $row[0];
182182 }
183183 }
184 - $dbr->freeResult($res);
185 - foreach ($subcategories as $subcategory) {
186 - $pages = array_merge($pages, SDUtils::getCategoryChildren($subcategory, $get_categories, $levels - 1));
 184+ $dbr->freeResult( $res );
 185+ foreach ( $subcategories as $subcategory ) {
 186+ $pages = array_merge( $pages, SDUtils::getCategoryChildren( $subcategory, $get_categories, $levels - 1 ) );
187187 }
188188 return $pages;
189189 }
190190
191 - function monthToString($month) {
192 - if ($month == 1) {
193 - return wfMsg('january');
194 - } elseif ($month == 2) {
195 - return wfMsg('february');
196 - } elseif ($month == 3) {
197 - return wfMsg('march');
198 - } elseif ($month == 4) {
199 - return wfMsg('april');
200 - } elseif ($month == 5) {
201 - return wfMsg('may');
202 - } elseif ($month == 6) {
203 - return wfMsg('june');
204 - } elseif ($month == 7) {
205 - return wfMsg('july');
206 - } elseif ($month == 8) {
207 - return wfMsg('august');
208 - } elseif ($month == 9) {
209 - return wfMsg('september');
210 - } elseif ($month == 10) {
211 - return wfMsg('october');
212 - } elseif ($month == 11) {
213 - return wfMsg('november');
214 - } else { //if ($month == 12) {
215 - return wfMsg('december');
 191+ static function monthToString( $month ) {
 192+ if ( $month == 1 ) {
 193+ return wfMsg( 'january' );
 194+ } elseif ( $month == 2 ) {
 195+ return wfMsg( 'february' );
 196+ } elseif ( $month == 3 ) {
 197+ return wfMsg( 'march' );
 198+ } elseif ( $month == 4 ) {
 199+ return wfMsg( 'april' );
 200+ } elseif ( $month == 5 ) {
 201+ return wfMsg( 'may' );
 202+ } elseif ( $month == 6 ) {
 203+ return wfMsg( 'june' );
 204+ } elseif ( $month == 7 ) {
 205+ return wfMsg( 'july' );
 206+ } elseif ( $month == 8 ) {
 207+ return wfMsg( 'august' );
 208+ } elseif ( $month == 9 ) {
 209+ return wfMsg( 'september' );
 210+ } elseif ( $month == 10 ) {
 211+ return wfMsg( 'october' );
 212+ } elseif ( $month == 11 ) {
 213+ return wfMsg( 'november' );
 214+ } else { // if ($month == 12) {
 215+ return wfMsg( 'december' );
216216 }
217217 }
218218
219 - function stringToMonth($str) {
220 - if ($str == wfMsg('january')) {
 219+ static function stringToMonth( $str ) {
 220+ if ( $str == wfMsg( 'january' ) ) {
221221 return 1;
222 - } elseif ($str == wfMsg('february')) {
 222+ } elseif ( $str == wfMsg( 'february' ) ) {
223223 return 2;
224 - } elseif ($str == wfMsg('march')) {
 224+ } elseif ( $str == wfMsg( 'march' ) ) {
225225 return 3;
226 - } elseif ($str == wfMsg('april')) {
 226+ } elseif ( $str == wfMsg( 'april' ) ) {
227227 return 4;
228 - } elseif ($str == wfMsg('may')) {
 228+ } elseif ( $str == wfMsg( 'may' ) ) {
229229 return 5;
230 - } elseif ($str == wfMsg('june')) {
 230+ } elseif ( $str == wfMsg( 'june' ) ) {
231231 return 6;
232 - } elseif ($str == wfMsg('july')) {
 232+ } elseif ( $str == wfMsg( 'july' ) ) {
233233 return 7;
234 - } elseif ($str == wfMsg('august')) {
 234+ } elseif ( $str == wfMsg( 'august' ) ) {
235235 return 8;
236 - } elseif ($str == wfMsg('september')) {
 236+ } elseif ( $str == wfMsg( 'september' ) ) {
237237 return 9;
238 - } elseif ($str == wfMsg('october')) {
 238+ } elseif ( $str == wfMsg( 'october' ) ) {
239239 return 10;
240 - } elseif ($str == wfMsg('november')) {
 240+ } elseif ( $str == wfMsg( 'november' ) ) {
241241 return 11;
242 - } else { //if ($strmonth == wfMsg('december')) {
 242+ } else { // if ($strmonth == wfMsg('december')) {
243243 return 12;
244244 }
245245 }
246246
247 - function booleanToString($bool_value) {
248 - wfLoadExtensionMessages('SemanticMediaWiki');
249 - $words_field_name = ($bool_value == true) ? 'smw_true_words' : 'smw_false_words';
250 - $words_array = explode(',', wfMsgForContent($words_field_name));
 247+ static function booleanToString( $bool_value ) {
 248+ wfLoadExtensionMessages( 'SemanticMediaWiki' );
 249+ $words_field_name = ( $bool_value == true ) ? 'smw_true_words' : 'smw_false_words';
 250+ $words_array = explode( ',', wfMsgForContent( $words_field_name ) );
251251 // go with the value in the array that tends to be "yes" or
252252 // "no", which is the 3rd
253253 $index_of_word = 2;
254254 // capitalize first letter of word
255 - if (count($words_array) > $index_of_word) {
256 - $string_value = ucwords($words_array[$index_of_word]);
257 - } elseif (count($words_array) == 0) {
 255+ if ( count( $words_array ) > $index_of_word ) {
 256+ $string_value = ucwords( $words_array[$index_of_word] );
 257+ } elseif ( count( $words_array ) == 0 ) {
258258 $string_value = $bool_value; // a safe value if no words are found
259259 } else {
260 - $string_value = ucwords($words_array[0]);
 260+ $string_value = ucwords( $words_array[0] );
261261 }
262262 return $string_value;
263263 }
@@ -266,25 +266,25 @@
267267 * allows pages to spoof a normal edit page, that can preview, save,
268268 * etc.
269269 */
270 - function printRedirectForm($title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this) {
271 - $article = new Article($title);
272 - $new_url = $title->getLocalURL('action=submit');
 270+ static function printRedirectForm( $title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this ) {
 271+ $article = new Article( $title );
 272+ $new_url = $title->getLocalURL( 'action=submit' );
273273 $starttime = wfTimestampNow();
274274 $edittime = $article->getTimestamp();
275275 global $wgUser;
276276 if ( $wgUser->isLoggedIn() )
277 - $token = htmlspecialchars($wgUser->editToken());
 277+ $token = htmlspecialchars( $wgUser->editToken() );
278278 else
279279 $token = EDIT_TOKEN_SUFFIX;
280280
281 - if ($is_save)
 281+ if ( $is_save )
282282 $action = "wpSave";
283 - elseif ($is_preview)
 283+ elseif ( $is_preview )
284284 $action = "wpPreview";
285285 else // $is_diff
286286 $action = "wpDiff";
287287
288 - $text =<<<END
 288+ $text = <<<END
289289 <form id="editform" name="editform" method="post" action="$new_url">
290290 <input type="hidden" name="wpTextbox1" id="wpTextbox1" value="$page_contents" />
291291 <input type="hidden" name="wpSummary" value="$edit_summary" />
@@ -294,11 +294,11 @@
295295 <input type="hidden" name="$action" />
296296
297297 END;
298 - if ($is_minor_edit)
 298+ if ( $is_minor_edit )
299299 $text .= ' <input type="hidden" name="wpMinoredit">' . "\n";
300 - if ($watch_this)
 300+ if ( $watch_this )
301301 $text .= ' <input type="hidden" name="wpWatchthis">' . "\n";
302 - $text .=<<<END
 302+ $text .= <<<END
303303 </form>
304304 <script type="text/javascript">
305305 document.editform.submit();
@@ -311,7 +311,7 @@
312312 /**
313313 * Register magic-word variable IDs
314314 */
315 - function addMagicWordVariableIDs(&$magicWordVariableIDs) {
 315+ static function addMagicWordVariableIDs( &$magicWordVariableIDs ) {
316316 $magicWordVariableIDs[] = 'MAG_HIDEFROMDRILLDOWN';
317317 $magicWordVariableIDs[] = 'MAG_SHOWINDRILLDOWN';
318318 return true;
@@ -320,11 +320,11 @@
321321 /**
322322 * Set the actual value of the magic words
323323 */
324 - function addMagicWordLanguage(&$magicWords, $langCode) {
325 - switch($langCode) {
 324+ static function addMagicWordLanguage( &$magicWords, $langCode ) {
 325+ switch( $langCode ) {
326326 default:
327 - $magicWords['MAG_HIDEFROMDRILLDOWN'] = array(0, '__HIDEFROMDRILLDOWN__');
328 - $magicWords['MAG_SHOWINDRILLDOWN'] = array(0, '__SHOWINDRILLDOWN__');
 327+ $magicWords['MAG_HIDEFROMDRILLDOWN'] = array( 0, '__HIDEFROMDRILLDOWN__' );
 328+ $magicWords['MAG_SHOWINDRILLDOWN'] = array( 0, '__SHOWINDRILLDOWN__' );
329329 }
330330 return true;
331331 }
@@ -333,14 +333,14 @@
334334 * Set values in the page_props table based on the presence of the
335335 * 'HIDEFROMDRILLDOWN' and 'SHOWINDRILLDOWN' magic words in a page
336336 */
337 - function handleShowAndHide(&$parser, &$text) {
 337+ static function handleShowAndHide( &$parser, &$text ) {
338338 global $wgOut, $wgAction;
339 - $mw_hide = MagicWord::get('MAG_HIDEFROMDRILLDOWN');
340 - if ($mw_hide->matchAndRemove($text)) {
 339+ $mw_hide = MagicWord::get( 'MAG_HIDEFROMDRILLDOWN' );
 340+ if ( $mw_hide->matchAndRemove( $text ) ) {
341341 $parser->mOutput->setProperty( 'hidefromdrilldown', 'y' );
342342 }
343 - $mw_show = MagicWord::get('MAG_SHOWINDRILLDOWN');
344 - if ($mw_show->matchAndRemove($text)) {
 343+ $mw_show = MagicWord::get( 'MAG_SHOWINDRILLDOWN' );
 344+ if ( $mw_show->matchAndRemove( $text ) ) {
345345 $parser->mOutput->setProperty( 'showindrilldown', 'y' );
346346 }
347347 return true;

Status & tagging log