r94482 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94481‎ | r94482 | r94483 >
Date:22:17, 14 August 2011
Author:robin
Status:resolved (Comments)
Tags:
Comment:
Follow-up r92559:
* Use preg_quote() for arrays going to regexes
* Proper escaping for InfoPage->mFormatTitle and status message
* Use wgOut->addModuleStyles() to prevent flash of unstyled content
* Use $wgExtensionAssetsPath to form extension directory
* Remove unneeded isset()s
* Also, prevent php notices in onShowMissingArticle()
Modified paths:
  • /trunk/extensions/WikimediaIncubator/IncubatorTest.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/InfoPage.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/WikimediaIncubator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.php
@@ -14,7 +14,7 @@
1515 'path' => __FILE__,
1616 'name' => 'Wikimedia Incubator',
1717 'author' => 'SPQRobin',
18 - 'version' => '4.1',
 18+ 'version' => '4.2',
1919 'url' => 'http://www.mediawiki.org/wiki/Extension:WikimediaIncubator',
2020 'descriptionmsg' => 'wminc-desc',
2121 );
@@ -22,7 +22,7 @@
2323 /* General (globals and/or configuration) */
2424 $wmincPref = 'incubatortestwiki'; // Name of the preference
2525 $dir = dirname( __FILE__ ) . '/';
26 -$wmincScriptDir = $wgScriptPath . '/extensions/WikimediaIncubator/';
 26+$wmincScriptDir = $wgExtensionAssetsPath . '/WikimediaIncubator/';
2727 # only one-letter codes can be used for projects
2828 $wmincProjects = array(
2929 'p' => 'Wikipedia',
@@ -133,7 +133,7 @@
134134 $wgResourceModules['WikimediaIncubator.InfoPage'] = array(
135135 'styles' => 'InfoPage.css',
136136 'localBasePath' => dirname(__FILE__),
137 - 'remoteExtPath' => $wmincScriptDir,
 137+ 'remoteExtPath' => 'WikimediaIncubator',
138138 );
139139
140140 /* Possibility to set a logo per test wiki */
Index: trunk/extensions/WikimediaIncubator/IncubatorTest.php
@@ -114,11 +114,13 @@
115115 }
116116 }
117117 global $wmincProjects, $wmincSisterProjects;
118 - $listProjects = implode( '', array_keys( $wmincProjects ) ); # project codes like: pbtqn
 118+ $listProjects = array_map( 'preg_quote', array_keys( $wmincProjects ) );
119119 if( $allowSister && is_array( $wmincSisterProjects ) ) {
120 - # join the project codes with those of the sister projects, like: pbtqnsv
121 - $listProjects = $listProjects . implode( '', array_keys( $wmincSisterProjects ) );
 120+ # join the project codes with those of the sister projects
 121+ $listSister = array_map( 'preg_quote', array_keys( $wmincSisterProjects ) );
 122+ $listProjects = array_merge( $listProjects, $listSister );
122123 }
 124+ $listProjects = implode( '|', $listProjects );
123125 if( !preg_match( '/^W['.$listProjects.']\/[a-z-]+' .
124126 ($onlyInfoPage ? '$/' : '(\/.+)?$/' ), $title ) ) {
125127 $data['error'] = 'invalidprefix';
@@ -278,9 +280,10 @@
279281 * @return Boolean
280282 */
281283 static function shouldWeShowUnprefixedError( $title ) {
282 - global $wmincTestWikiNamespaces, $wmincProjectSite;
 284+ global $wmincTestWikiNamespaces, $wmincProjectSite, $wmincPseudoCategoryNSes;
283285 $prefixdata = self::analyzePrefix( $title->getText() );
284286 $ns = $title->getNamespace();
 287+ $categories = array_map( 'preg_quote', $wmincPseudoCategoryNSes );
285288 if( !$prefixdata['error'] ) {
286289 # no error in prefix -> no error to show
287290 return false;
@@ -291,7 +294,7 @@
292295 # OK if it's not in one of the content namespaces
293296 return false;
294297 } elseif( ( $ns == NS_CATEGORY || $ns == NS_CATEGORY_TALK ) &&
295 - preg_match( '/^(' . implode( '|', $wmincPseudoCategoryNSes ) .'):.+$/', $title->getText() ) ) {
 298+ preg_match( '/^(' . implode( '|', $categories ) .'):.+$/', $title->getText() ) ) {
296299 # whitelisted unprefixed categories
297300 return false;
298301 }
@@ -411,7 +414,7 @@
412415 static function getDB( $prefix ) {
413416 if( !self::canWeCheckDB() ) {
414417 return false;
415 - } elseif( !isset( $prefix ) || $prefix['error'] ) {
 418+ } elseif( !$prefix || $prefix['error'] ) {
416419 return false; # shouldn't be, but you never know
417420 }
418421 global $wmincProjectDatabases;
@@ -471,11 +474,10 @@
472475 global $wmincSisterProjects;
473476 $prefix2 = self::analyzePrefix( $title->getText(), false, true );
474477 $linker = class_exists( 'DummyLinker' ) ? new DummyLinker : new Linker;
475 - $p = $prefix2['project'];
476 - $link = self::getSubdomain( $prefix2['lang'], $p,
477 - ( $title->getNsText() ? $title->getNsText() . ':' : '' ) .
478 - $prefix2['realtitle'] );
 478+ $p = isset( $prefix2['project' ] ) ? $prefix2['project'] : '';
479479 if( self::getDBState( $prefix2 ) == 'existing' ) {
 480+ $link = self::getSubdomain( $prefix2['lang'], $p,
 481+ ( $title->getNsText() ? $title->getNsText() . ':' : '' ) . $prefix2['realtitle'] );
480482 if( self::displayPrefix() == $prefix2['prefix'] ) {
481483 # Redirect to the existing wiki if the user has this wiki as preference
482484 $wgOut->redirect( $link );
@@ -489,6 +491,8 @@
490492 }
491493 } elseif( array_key_exists( $p, $wmincSisterProjects ) ) {
492494 # A sister project is not hosted here, so direct the user to the relevant wiki
 495+ $link = self::getSubdomain( $prefix2['lang'], $p,
 496+ ( $title->getNsText() ? $title->getNsText() . ':' : '' ) . $prefix2['realtitle'] );
493497 $showLink = $linker->makeExternalLink( $link, $link );
494498 $wgOut->addHtml( '<div class="wminc-wiki-sister">' .
495499 wfMsgHtml( 'wminc-error-wiki-sister', $showLink ) .
@@ -529,7 +533,7 @@
530534 $wgOut->addHtml( $infopage->showMissingWiki() );
531535 }
532536 # Set the page title from "Wx/xyz - Incubator" to "Wikiproject Language - Incubator"
533 - $wgOut->setHTMLTitle( wfMsg( 'pagetitle', htmlspecialchars( $infopage->mFormatTitle ) ) );
 537+ $wgOut->setHTMLTitle( wfMsg( 'pagetitle', $infopage->mFormatTitle ) );
534538 return true;
535539 }
536540
@@ -579,7 +583,7 @@
580584 public static function getMainPage( $langCode, $prefix = null ) {
581585 # Take the "mainpage" msg in the given language
582586 $msg = wfMsgExt( 'mainpage', array( 'language' => $langCode ) );
583 - return isset( $prefix ) ? $prefix . '/' . $msg : $msg;
 587+ return $prefix !== null ? $prefix . '/' . $msg : $msg;
584588 }
585589
586590 /**
Index: trunk/extensions/WikimediaIncubator/InfoPage.php
@@ -39,7 +39,7 @@
4040 $this->mLangNames = IncubatorTest::getLanguageNames();
4141 $this->mLangName = ( isset( $this->mLangNames[$this->mLangCode] ) ?
4242 $this->mLangNames[$this->mLangCode] : wfMsg( 'wminc-unknownlang', $this->mLangCode ) );
43 - $this->mFormatTitle = wfMsg( 'wminc-infopage-title', $this->mProjectName, $this->mLangName );
 43+ $this->mFormatTitle = wfMsgHtml( 'wminc-infopage-title', $this->mProjectName, $this->mLangName );
4444 return;
4545 }
4646
@@ -117,7 +117,7 @@
118118 */
119119 public function StandardInfoPage( $beforetitle, $aftertitle, $content ) {
120120 global $wgLang, $wgOut;
121 - $wgOut->addModules( 'WikimediaIncubator.InfoPage' );
 121+ $wgOut->addModuleStyles( 'WikimediaIncubator.InfoPage' );
122122 return Html::rawElement( 'div', array( 'class' => 'wminc-infopage plainlinks',
123123 'lang' => $wgLang->getCode(), 'dir' => $wgLang->getDir() ),
124124 $beforetitle .
@@ -190,8 +190,8 @@
191191 */
192192 public function showExistingWiki() {
193193 global $wgLang, $wgUser;
194 - $created = isset( $this->mCreated ) ? $this->mCreated : '';
195 - $bug = isset( $this->mBug ) ? $this->mBug : '';
 194+ $created = isset( $this->mCreated ) ? $this->mCreated : ''; # for future use
 195+ $bug = isset( $this->mBug ) ? $this->mBug : ''; # for future use
196196 $subdomain = IncubatorTest::getSubdomain( $this->mLangCode, $this->mProjectCode );
197197 $subdomainLink = $wgUser->getSkin()->makeExternalLink( $subdomain, $subdomain );
198198 if( $this->mThisLangData['type'] != 'invalid' ) {
@@ -201,7 +201,7 @@
202202 }
203203 $content = Html::rawElement( 'div',
204204 array( 'class' => 'wminc-infopage-status' ),
205 - wfMsg( 'wminc-infopage-status-' . $this->mSubStatus, $subdomainLink )
 205+ wfMsgWikiHtml( 'wminc-infopage-status-' . $this->mSubStatus, $subdomainLink )
206206 ) . Html::rawElement( 'ul', array( 'class' => 'wminc-infopage-options' ),
207207 Html::rawElement( 'li', null, wfMsgWikiHtml( 'wminc-infopage-option-sisterprojects-other' ) .
208208 $this->listOtherProjects() ) .

Sign-offs

UserFlagDate
Nikerabbitinspected19:12, 15 August 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r94674Use slash as second paramter for preg_quote(), per CR on r94482robin18:41, 16 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r92559Major update of stable features from development code....robin19:51, 19 July 2011

Comments

#Comment by Nikerabbit (talk | contribs)   19:12, 15 August 2011

You should pass second parameter (the used delimeter) to preg_quote.

#Comment by Catrope (talk | contribs)   12:19, 16 August 2011
+				$link = self::getSubdomain( $prefix2['lang'], $p,
+					( $title->getNsText() ? $title->getNsText() . ':' : '' ) . $prefix2['realtitle'] );

Why did you intentionally duplicate this code?

Per Nikerabbit, you should pass '/' as the second param to preg_quote(). You can do this in two ways: either create a private static wrapper function that returns preg_quote( $foo, '/' ), or use array_map( 'preg_quote', $arr, array_fill( 0, count( $arr ), '/' ) ); .

#Comment by SPQRobin (talk | contribs)   18:47, 16 August 2011

preg_quote() done in r94674.

I duplicated the code so it doesn't give php notices if 'lang' and 'realtitle' aren't defined (using isset() wouldn't give much shorter code). The onShowMissingArticle function should need a bit of cleanup/restructure anyway.

Status & tagging log