r106527 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106526‎ | r106527 | r106528 >
Date:20:14, 17 December 2011
Author:ialex
Status:resolved
Tags:
Comment:
* Removed usage of $wgTitle where possible
* Use Title::getDBkey() instead of Title::getText() and replacing spaces with underscores
* Use DatabaseBase::selectRow() instead of select() with a single call to fetchObject()
* Fixed return value of hooks
* Fixed fnProtectHelpCommons() to correctly handle $wgHelpCommonsProtect and $wgHelpCommonsProtectAll
Modified paths:
  • /trunk/extensions/HelpCommons/HelpCommons.php (modified) (history)

Diff [purge]

Index: trunk/extensions/HelpCommons/HelpCommons.php
@@ -50,25 +50,26 @@
5151 * @return bool
5252 */
5353 function wfHelpCommonsLoad( $helppage ) {
54 - global $wgTitle, $wgOut, $wgContLang, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
 54+ global $wgOut, $wgContLang, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
5555
56 - if ( $wgTitle->getNamespace() != NS_HELP ) {
57 - return false;
 56+ $title = $helppage->getTitle();
 57+
 58+ if ( $title->getNamespace() != NS_HELP ) {
 59+ return true;
5860 }
5961
60 - $title = str_replace( ' ', '_', $helppage->mTitle->getText() );
 62+ $dbkey = $title->getDBkey();
6163
6264 foreach ( $wgHelpCommonsFetchingWikis as $language => $urls ) {
6365 foreach ( $urls as $helpwiki ) {
6466 if ( $wgLanguageCode == $language && $wgDBname != $helpwiki ) {
6567 $dbr = wfGetDB( DB_SLAVE, array(), $helpwiki );
66 - $page = $dbr->select(
 68+ $page = $dbr->selectRow(
6769 'page',
6870 array( 'page_title', 'page_namespace', 'page_latest' ),
69 - array( 'page_namespace' => NS_HELP, 'page_title' => $title ),
 71+ array( 'page_namespace' => NS_HELP, 'page_title' => $dbkey ),
7072 __METHOD__
7173 );
72 - $page = $dbr->fetchObject( $page );
7374 }
7475 }
7576 }
@@ -80,7 +81,7 @@
8182 );
8283 $rev = $dbr->fetchObject( $rev );
8384 } else {
84 - return false;
 85+ return true;
8586 }
8687 $text = $dbr->select( 'text',
8788 array( 'old_id', 'old_text' ),
@@ -92,9 +93,9 @@
9394 if ( !empty( $text->old_text ) ) {
9495 $wgOut->addWikiText( $text->old_text );
9596 $wgOut->addScript('<style type="text/css">div.noarticletext { display: none; } div.mw-warning-with-logexcerpt { display: none; } #contentSub, #contentSub2 { display: none; }</style>');
 97+ return false;
 98+ } else {
9699 return true;
97 - } else {
98 - return false;
99100 }
100101 }
101102
@@ -104,34 +105,35 @@
105106 * @return bool
106107 */
107108 function wfHelpCommonsRedirectTalks( &$helppage, &$outputDone, &$pcache ) {
108 - global $wgTitle, $wgOut, $wgContLang, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
 109+ global $wgOut, $wgContLang, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
109110
110 - if ( $wgTitle->getNamespace() != NS_HELP_TALK ) {
111 - return false;
 111+ $title = $helppage->getTitle();
 112+
 113+ if ( $title->getNamespace() != NS_HELP_TALK ) {
 114+ return true;
112115 }
113116
114 - $title = str_replace( ' ', '_', $helppage->mTitle->getText() );
 117+ $dbkey = $title->getDBkey();
115118
116119 foreach ( $wgHelpCommonsFetchingWikis as $language => $urls ) {
117120 foreach ( $urls as $url => $helpwiki ) {
118121 if ( $wgLanguageCode == $language && $wgDBname != $helpwiki ) {
119122 $dbr = wfGetDB( DB_SLAVE, array(), $helpwiki );
120 - $page = $dbr->select(
 123+ $page = $dbr->selectRow(
121124 'page',
122125 array( 'page_title', 'page_namespace', 'page_latest' ),
123 - array( 'page_namespace' => NS_HELP, 'page_title' => $title ),
 126+ array( 'page_namespace' => NS_HELP, 'page_title' => $dbkey ),
124127 __METHOD__
125128 );
126 - $page = $dbr->fetchObject( $page );
127129 }
128130 if ( !empty( $page->page_title ) ) {
129 - if ( $page->page_title == $title && !$wgTitle->exists() ) {
 131+ if ( $page->page_title == $title && !$title->exists() ) {
130132 $helpCommonsRedirectTalk = Title::newFromText( $url . '/index.php?title=' . str_replace( ' ', '_', $wgOut->getTitle() ) );
131133 $redirectTalkPage = $helpCommonsRedirectTalk->getFullText();
132134 $wgOut->redirect( $redirectTalkPage );
 135+ return false;
 136+ } else {
133137 return true;
134 - } else {
135 - return false;
136138 }
137139 }
138140 }
@@ -239,19 +241,23 @@
240242 * @return bool
241243 */
242244 function wfHelpCommonsChangeEditSectionLink( $skin, $title, $section, $tooltip, $result, $lang = false ) {
243 - global $wgTitle, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
 245+ global $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
244246
245 - if ( $wgTitle->getNamespace() != NS_HELP ) {
 247+ $baseTitle = $skin->getTitle();
 248+
 249+ if ( $baseTitle->getNamespace() != NS_HELP ) {
246250 return false;
247251 }
248252
249253 foreach ( $wgHelpCommonsFetchingWikis as $language => $urls ) {
250254 foreach ( $urls as $url => $helpwiki ) {
251 - if ( $wgLanguageCode == $language && $wgDBname != $helpwiki && !$wgTitle->exists() ) {
 255+ if ( $wgLanguageCode == $language && $wgDBname != $helpwiki && !$baseTitle->exists() ) {
252256 // FIXME: $result is unused
253 - $result = '<span class="editsection">[<a href="' . $url . '/index.php?title=' .
254 - str_replace( ' ', '_', $title ) . '&amp;action=edit&amp;section=' . $section .
255 - '" title="' . wfMsg( 'editsectionhint', $tooltip ) . '">' . wfMsg( 'editsection' ) . '</a>]</span>';
 257+ $result = '<span class="editsection">[' . Xml::element( 'a', array(
 258+ 'href' => $url . '/index.php?title=' . str_replace( ' ', '_', $title ) . '&action=edit&section=' . $section,
 259+ 'title' => wfMsg( 'editsectionhint', $tooltip ) ),
 260+ wfMsg( 'editsection' )
 261+ ) . ']</span>';
256262 }
257263 }
258264 }
@@ -268,35 +274,36 @@
269275 function fnProtectHelpCommons( &$title, &$user, $action, &$result ) {
270276 global $wgHelpCommonsProtect, $wgHelpCommonsProtectAll, $wgHelpCommonsFetchingWikis, $wgLanguageCode, $wgDBname;
271277
272 - if ( !$wgHelpCommonsProtect && !$wgHelpCommonsProtectAll ) {
273 - return false;
 278+ $ns = $title->getNamespace();
 279+ if ( ( $ns !== NS_HELP && $ns !== NS_HELP_TALK ) || $title->exists() ) {
 280+ return true;
274281 }
275282
 283+ // block actions 'create', 'edit' and 'protect'
 284+ if ( $action != 'create' && $action != 'edit' && $action != 'protect' ) {
 285+ return true;
 286+ }
 287+
 288+ if ( $wgHelpCommonsProtectAll ) {
 289+ $result = array( 'protectedpagetext' );
 290+ return true;
 291+ } elseif ( !$wgHelpCommonsProtect ) {
 292+ return true;
 293+ }
 294+
276295 foreach ( $wgHelpCommonsFetchingWikis as $language => $urls ) {
277296 foreach ( $urls as $url => $helpwiki ) {
278297 // only protect non-existent help pages on non-help-page-fetching wikis
279 - if ( $wgLanguageCode == $language && $wgDBname != $helpwiki && !$title->exists() ) {
280 - // block actions 'create', 'edit' and 'protect'
281 - if ( $action != 'create' && $action != 'edit' && $action != 'protect' ) {
282 - return true;
283 - }
284 -
 298+ if ( $wgLanguageCode == $language && $wgDBname != $helpwiki ) {
285299 $dbr = wfGetDB( DB_SLAVE, array(), $helpwiki );
286 - $res = $dbr->select(
 300+ $res = $dbr->selectField(
287301 'page',
288 - array( 'page_title', 'page_namespace', 'page_latest' ),
289 - array( 'page_namespace' => NS_HELP, 'page_title' => str_replace( ' ', '_', $title->getText() ) ),
 302+ '1',
 303+ array( 'page_namespace' => NS_HELP, 'page_title' => $title->getDBkey() ),
290304 __METHOD__
291305 );
292306
293 - if ( $dbr->numRows( $res ) < 1 && $wgHelpCommonsProtect && !$wgHelpCommonsProtectAll ) {
294 - return true;
295 - }
296 -
297 - $ns = $title->getNamespace();
298 -
299 - // check namespaces
300 - if( $ns == NS_HELP || $ns == NS_HELP_TALK ) {
 307+ if ( $res ) {
301308 // error message if action is blocked
302309 $result = array( 'protectedpagetext' );
303310 // bail, and stop the request

Sign-offs

UserFlagDate
SVGtested10:34, 18 December 2011 (struck 10:34, 18 December 2011)

Follow-up revisions

RevisionCommit summaryAuthorDate
r106555Fixing minor bugs in follow-up to r106527cervidae09:38, 18 December 2011

Status & tagging log