r51123 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51122‎ | r51123 | r51124 >
Date:17:08, 28 May 2009
Author:robin
Status:ok (Comments)
Tags:
Comment:
Update per review on r50188:
* better function names
* use getOption() instead of mOptions[]
* specific messages instead of general 'go' and 'username', and fix escaping and parsing
* fix security vulnerability in RC form
Modified paths:
  • /trunk/extensions/WikimediaIncubator/CreateAccountTestWiki.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/IncubatorTest.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/SpecialViewUserLang.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/TestWikiRC.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/WikimediaIncubator.i18n.php (modified) (history)
  • /trunk/extensions/WikimediaIncubator/WikimediaIncubator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.php
@@ -10,19 +10,18 @@
1111 'path' => __FILE__,
1212 'name' => 'Wikimedia Incubator',
1313 'author' => 'SPQRobin',
14 - 'version' => '2.1.4',
 14+ 'version' => '2.2.0',
1515 'url' => 'http://www.mediawiki.org/wiki/Extension:WikimediaIncubator',
1616 'description' => 'Test wiki features for Wikimedia Incubator',
1717 'descriptionmsg' => 'wminc-desc',
1818 );
1919
2020 /* Config */
21 -$wmincEnableAutoTestWiki = true;
22 -$wmincPref = 'incubatortestwiki'; /* Name of the preference */
2321 $wgGroupPermissions['*']['viewuserlang'] = false;
2422 $wgGroupPermissions['sysop']['viewuserlang'] = true;
2523
2624 /* General */
 25+$wmincPref = 'incubatortestwiki'; // Name of the preference
2726 $dir = dirname( __FILE__ ) . '/';
2827 $wgExtensionMessagesFiles['WikimediaIncubator'] = $dir . 'WikimediaIncubator.i18n.php';
2928
@@ -35,22 +34,21 @@
3635
3736 /* TestWiki preference */
3837 $wgAutoloadClasses['IncubatorTest'] = $dir . 'IncubatorTest.php';
39 -$wgHooks['GetPreferences'][] = 'IncubatorTest::AddPreferences';
40 -$wgHooks['MagicWordwgVariableIDs'][] = 'IncubatorTest::MagicWordVariable';
41 -$wgHooks['LanguageGetMagic'][] = 'IncubatorTest::MagicWord';
42 -$wgHooks['ParserGetVariableValueSwitch'][] = 'IncubatorTest::MagicWordValue';
 38+$wgHooks['GetPreferences'][] = 'IncubatorTest::onGetPreferences';
 39+$wgHooks['MagicWordwgVariableIDs'][] = 'IncubatorTest::magicWordVariable';
 40+$wgHooks['LanguageGetMagic'][] = 'IncubatorTest::magicWord';
 41+$wgHooks['ParserGetVariableValueSwitch'][] = 'IncubatorTest::magicWordValue';
4342
4443 /* Edit page */
45 -$wgHooks['EditPage::showEditForm:initial'][] = 'IncubatorTest::EditPageCheckPrefix';
 44+$wgHooks['EditPage::showEditForm:initial'][] = 'IncubatorTest::editPageCheckPrefix';
4645
4746 /* Recent Changes */
4847 $wgAutoloadClasses['TestWikiRC'] = $dir . 'TestWikiRC.php';
49 -$wgHooks['SpecialRecentChangesQuery'][] = 'TestWikiRC::RcQuery';
50 -$wgHooks['SpecialRecentChangesPanel'][] = 'TestWikiRC::RcForm';
 48+$wgHooks['SpecialRecentChangesQuery'][] = 'TestWikiRC::onRcQuery';
 49+$wgHooks['SpecialRecentChangesPanel'][] = 'TestWikiRC::onRcForm';
5150
5251 /* Automatic pref on account creation */
53 -if ( $wmincEnableAutoTestWiki == true ) {
54 - $wgAutoloadClasses['AutoTestWiki'] = $dir . 'CreateAccountTestWiki.php';
55 - $wgHooks['UserCreateForm'][] = 'AutoTestWiki::UserCreateForm';
56 - $wgHooks['AddNewAccount'][] = 'AutoTestWiki::AddNewAccount';
57 -}
 52+$wgAutoloadClasses['AutoTestWiki'] = $dir . 'CreateAccountTestWiki.php';
 53+$wgHooks['UserCreateForm'][] = 'AutoTestWiki::onUserCreateForm';
 54+$wgHooks['AddNewAccount'][] = 'AutoTestWiki::onAddNewAccount';
 55+
Index: trunk/extensions/WikimediaIncubator/SpecialViewUserLang.php
@@ -54,9 +54,9 @@
5555 $title = htmlspecialchars( $this->getTitle()->getPrefixedText() );
5656 $action = htmlspecialchars( $wgScript );
5757 $target = htmlspecialchars( $target );
58 - $ok = wfMsg( 'go' );
59 - $username = wfMsg( 'username' );
60 - $inputformtop = wfMsg( 'wminc-viewuserlang' );
 58+ $ok = wfMsgHtml( 'go' );
 59+ $username = wfMsgHtml( 'username' );
 60+ $inputformtop = wfMsgHtml( 'wminc-viewuserlang' );
6161
6262 $wgOut->addHTML( <<<EOT
6363 <fieldset>
@@ -84,7 +84,7 @@
8585 global $wgOut, $wgLang, $wgLanguageNames, $wmincPref;
8686 $user = User::newFromName( $target );
8787 if ( $user == null || $user->getId() == 0 ) {
88 - $wgOut->addWikiText( '<span class="error">' . wfMsg( 'wminc-viewuserlang-unexisting', $target ) . '</span>' );
 88+ $wgOut->addWikiText( '<span class="error">' . wfMsgNoTrans( 'wminc-viewuserlang-unexisting', $target ) . '</span>' );
8989 } else {
9090 $name = $user->getName();
9191 $wgOut->addWikiText( '*' . wfMsg( 'username' ) . ' [[User:' . $name . '|' . $name . ']] (' .
@@ -97,9 +97,9 @@
9898 $wgLanguageNames[$user->mOptions['language']] . ' (' . $user->mOptions['language'] . ')'
9999 )
100100 );
101 - if ( IncubatorTest::NormalPrefix() == true ) {
 101+ if ( IncubatorTest::isNormalPrefix() == true ) {
102102 $testwiki = '[[W' . $user->mOptions[$wmincPref . '-project'] . '/' . $user->mOptions[$wmincPref . '-code'] . ']]';
103 - } elseif ( IncubatorTest::DisplayPrefix() == 'inc' ) {
 103+ } elseif ( IncubatorTest::displayPrefix() == 'inc' ) {
104104 $testwiki = 'Incubator';
105105 } else {
106106 $testwiki = wfMsg( 'wminc-testwiki-none' );
Index: trunk/extensions/WikimediaIncubator/CreateAccountTestWiki.php
@@ -4,7 +4,7 @@
55 * This can be used to work with my toolserver project (http://toolserver.org/~robin/?tool=proposewiki), so users don't *have* to change their preferences (automatically is always better :p)
66 */
77 class AutoTestWiki {
8 - function UserCreateForm( $template ) {
 8+ function onUserCreateForm( $template ) {
99 global $wgRequest;
1010 $projectvalue = strtolower( $wgRequest->getVal( 'testwikiproject', '' ) );
1111 $codevalue = strtolower( $wgRequest->getVal( 'testwikicode', '' ) );
@@ -16,7 +16,7 @@
1717 return true;
1818 }
1919
20 - function AddNewAccount( $user ) {
 20+ function onAddNewAccount( $user ) {
2121 global $wgRequest, $wmincPref;
2222 $getprojectvalue = $wgRequest->getVal( 'testwiki-project' );
2323 $getcodevalue = $wgRequest->getVal( 'testwiki-code' );
Index: trunk/extensions/WikimediaIncubator/IncubatorTest.php
@@ -5,7 +5,7 @@
66
77 class IncubatorTest
88 {
9 - static function AddPreferences( $user, &$preferences ) {
 9+ static function onGetPreferences( $user, &$preferences ) {
1010 global $wmincPref, $wmincPrefProject, $wmincPrefNone;
1111
1212 $preferences['language']['help-message'] = 'wminc-prefinfo-language';
@@ -35,7 +35,7 @@
3636 return true;
3737 }
3838
39 - function CodeValidation( $input, $alldata ) {
 39+ function codeValidation( $input, $alldata ) {
4040 global $wmincPref;
4141 // If the user selected a project that NEEDS a language code, but the user DID NOT enter a language code, give an error
4242 if ( !in_array( $alldata[$wmincPref . '-project'], array( '', 'none', 'inc' ) ) && !$input ) {
@@ -45,79 +45,85 @@
4646 }
4747 }
4848
49 - function NormalPrefix() {
 49+ function isNormalPrefix() {
5050 global $wgUser, $wmincPref;
51 - if ( in_array( $wgUser->mOptions[$wmincPref . '-project'], array( '', 'none', 'inc' ) ) ) {
 51+ if ( in_array( $wgUser->getOption($wmincPref . '-project'), array( '', 'none', 'inc' ) ) ) {
5252 return false; // false because this is NOT a normal prefix
5353 } else {
5454 return true; // true because this is a normal prefix
5555 }
5656 }
57 - function DisplayPrefix() {
 57+ function displayPrefix() {
 58+ // display the prefix of the user preference
5859 global $wgUser, $wmincPref;
59 - if ( self::NormalPrefix() == true ) {
60 - return 'W' . $wgUser->mOptions[$wmincPref . '-project'] . '/' . $wgUser->mOptions[$wmincPref . '-code']; // return the prefix
 60+ if ( self::isNormalPrefix() == true ) {
 61+ return 'W' . $wgUser->getOption($wmincPref . '-project') . '/' . $wgUser->getOption($wmincPref . '-code'); // return the prefix
6162 } else {
62 - return $wgUser->mOptions[$wmincPref . '-project']; // still provide the value
 63+ return $wgUser->getOption($wmincPref . '-project'); // still provide the value
6364 }
6465 }
6566
66 - function DisplayPrefixedTitle( $title, $namespace = '' ) {
 67+ function displayPrefixedTitle( $title, $namespace = '' ) {
6768 global $wgUser, $wmincPref;
6869 $out = '';
69 - if ( self::NormalPrefix() ) {
 70+ if ( self::isNormalPrefix() ) {
7071 if ( $namespace ) { $out .= $namespace . ':'; }
71 - $out .= self::DisplayPrefix() . '/' . $title;
 72+ $out .= self::displayPrefix() . '/' . $title;
7273 } else {
73 - $out .= self::DisplayPrefix();
 74+ $out .= self::displayPrefix();
7475 }
7576 return $out;
7677 }
7778
78 - function MagicWordVariable( &$magicWords ) {
 79+ function magicWordVariable( &$magicWords ) {
7980 $magicWords[] = 'usertestwiki';
8081 return true;
8182 }
8283
83 - function MagicWord( &$magicWords, $langCode ) {
 84+ function magicWord( &$magicWords, $langCode ) {
8485 $magicWords['usertestwiki'] = array( 0, 'USERTESTWIKI' );
8586 return true;
8687 }
8788
88 - function MagicWordValue( &$parser, &$cache, &$magicWordId, &$ret ) {
89 - $ret = self::DisplayPrefix();
 89+ function magicWordValue( &$parser, &$cache, &$magicWordId, &$ret ) {
 90+ if( !self::displayPrefix() ) {
 91+ $ret = 'none';
 92+ } else {
 93+ $ret = self::displayPrefix();
 94+ }
9095 return true;
9196 }
9297
93 - function EditPageCheckPrefix( $editpage ) {
 98+ function editPageCheckPrefix( $editpage ) {
9499 // If user has "project" as test wiki preference, it isn't needed to check
95 - if ( self::DisplayPrefix() != 'inc' ) {
96 - global $wgTitle;
97 - $namespaces = array( NS_MAIN, NS_TALK, NS_TEMPLATE, NS_TEMPLATE_TALK, NS_CATEGORY, NS_CATEGORY_TALK );
98 - // If it is in one of the above namespace, check if the page title has a prefix
99 - if ( in_array( $wgTitle->getNamespace(), $namespaces ) && !preg_match( '/W[bnpqt]\/[a-z][a-z][a-z]?/', $wgTitle->getText() ) ) {
100 - global $wgOut;
101 - wfLoadExtensionMessages( 'WikimediaIncubator' );
102 - $warning = '<div id="wminc-warning"><span id="wm-warning-unprefixed">'
103 - . wfMsg( 'wminc-warning-unprefixed' )
 100+ if ( self::displayPrefix() == 'inc' ) {
 101+ return true;
 102+ }
 103+ global $wgTitle;
 104+ $namespaces = array( NS_MAIN, NS_TALK, NS_TEMPLATE, NS_TEMPLATE_TALK, NS_CATEGORY, NS_CATEGORY_TALK );
 105+ // If it is in one of the above namespace, check if the page title has a prefix
 106+ if ( in_array( $wgTitle->getNamespace(), $namespaces ) && !preg_match( '/W[bnpqt]\/[a-z][a-z][a-z]?/', $wgTitle->getText() ) ) {
 107+ global $wgOut;
 108+ wfLoadExtensionMessages( 'WikimediaIncubator' );
 109+ $warning = '<div id="wminc-warning"><span id="wm-warning-unprefixed">'
 110+ . wfMsg( 'wminc-warning-unprefixed' )
 111+ . '</span>';
 112+ // If the user has a test wiki pref, suggest a page title with prefix
 113+ if ( self::isNormalPrefix() ) {
 114+ global $wgUser;
 115+ $suggest = self::displayPrefixedTitle( $wgTitle->getText(), $wgTitle->getNsText() );
 116+ if ( !$wgTitle->exists() ) { // Creating a page, so suggest to create a prefixed page
 117+ $warning .= ' <span id="wminc-warning-suggest">'
 118+ . wfMsg( 'wminc-warning-suggest', $suggest )
104119 . '</span>';
105 - // If the user has a test wiki pref, suggest a page title with prefix
106 - if ( self::NormalPrefix() ) {
107 - global $wgUser;
108 - $suggest = self::DisplayPrefixedTitle( $wgTitle->getText(), $wgTitle->getNsText() );
109 - if ( !$wgTitle->exists() ) { // Creating a page, so suggest to create a prefixed page
110 - $warning .= ' <span id="wminc-warning-suggest">'
111 - . wfMsg( 'wminc-warning-suggest', $suggest )
112 - . '</span>';
113 - } elseif ( $wgUser->isAllowed( 'move' ) ) { // Page exists, so suggest to move
114 - $warning .= ' <span id="wminc-warning-suggest-move" class="plainlinks">'
115 - . wfMsg( 'wminc-warning-suggest-move', $suggest, urlencode( $suggest ), urlencode( $wgTitle ) )
116 - . '</span>';
117 - }
 120+ } elseif ( $wgUser->isAllowed( 'move' ) ) { // Page exists, so suggest to move
 121+ $warning .= ' <span id="wminc-warning-suggest-move" class="plainlinks">'
 122+ . wfMsg( 'wminc-warning-suggest-move', $suggest, urlencode( $suggest ), urlencode( $wgTitle ) )
 123+ . '</span>';
118124 }
119 - $warning .= '</div>';
120 - $wgOut->addWikiText( $warning );
121 - }
 125+ }
 126+ $warning .= '</div>';
 127+ $wgOut->addWikiText( $warning );
122128 }
123129 return true;
124130 }
Index: trunk/extensions/WikimediaIncubator/TestWikiRC.php
@@ -4,10 +4,10 @@
55 */
66
77 class TestWikiRC {
8 - static function RcQuery( &$conds, &$tables, &$join_conds, $opts ) {
 8+ static function onRcQuery( &$conds, &$tables, &$join_conds, $opts ) {
99 global $wgUser, $wgRequest, $wmincPref;
10 - $projectvalue = strtolower( $wgRequest->getVal( 'rc-testwiki-project', $wgUser->mOptions[$wmincPref . '-project'] ) );
11 - $codevalue = strtolower( $wgRequest->getVal( 'rc-testwiki-code', $wgUser->mOptions[$wmincPref . '-code'] ) );
 10+ $projectvalue = strtolower( $wgRequest->getVal( 'rc-testwiki-project', $wgUser->getOption($wmincPref . '-project') ) );
 11+ $codevalue = strtolower( $wgRequest->getVal( 'rc-testwiki-code', $wgUser->getOption($wmincPref . '-code') ) );
1212 $fullprefix = 'W' . $projectvalue . '/' . $codevalue;
1313 $opts->add( 'rc-testwiki-project', false );
1414 $opts->setValue( 'rc-testwiki-project', $projectvalue );
@@ -25,16 +25,17 @@
2626 $dbr = wfGetDB( DB_SLAVE );
2727 $namespaces = array( NS_MAIN, NS_TALK, NS_TEMPLATE, NS_TEMPLATE_TALK, NS_CATEGORY, NS_CATEGORY_TALK );
2828 $conds[] = 'rc_namespace IN (' . $dbr->makeList( $namespaces ) . ')';
29 - $conds[] = 'rc_title like \'' . $fullprefix . '/%\' OR rc_title like \'' . $fullprefix . '\'';
 29+ $conds[] = 'rc_title like ' . $dbr->addQuotes( $dbr->escapeLike( $fullprefix ) . '/%' ) .
 30+ ' OR rc_title = ' . $dbr->addQuotes( $fullprefix );
3031 return true;
3132 }
3233 }
3334
34 - static function RcForm( &$items, $opts ) {
 35+ static function onRcForm( &$items, $opts ) {
3536 global $wgUser, $wgRequest, $wmincPref;
3637 wfLoadExtensionMessages( 'WikimediaIncubator' );
37 - $projectvalue = $wgRequest->getVal( 'rc-testwiki-project', $wgUser->mOptions[$wmincPref . '-project'] );
38 - $langcodevalue = $wgRequest->getVal( 'rc-testwiki-code', $wgUser->mOptions[$wmincPref . '-code'] );
 38+ $projectvalue = $wgRequest->getVal( 'rc-testwiki-project', $wgUser->getOption($wmincPref . '-project') );
 39+ $langcodevalue = $wgRequest->getVal( 'rc-testwiki-code', $wgUser->getOption($wmincPref . '-code') );
3940 $opts->consumeValue( 'rc-testwiki-project' );
4041 $opts->consumeValue( 'rc-testwiki-code' );
4142 $label = Xml::label( wfMsg( 'wminc-testwiki' ), 'rc-testwiki' );
Index: trunk/extensions/WikimediaIncubator/WikimediaIncubator.i18n.php
@@ -13,6 +13,8 @@
1414 $messages['en'] = array(
1515 'wminc-desc' => 'Test wiki system for Wikimedia Incubator',
1616 'wminc-viewuserlang' => 'Look up user language and test wiki',
 17+ 'wminc-viewuserlang-user' => 'Username:',
 18+ 'wminc-viewuserlang-go' => 'Go',
1719 'wminc-testwiki' => 'Test wiki:',
1820 'wminc-testwiki-none' => 'None/All',
1921 'wminc-prefinfo-language' => 'Your interface language - independent from your test wiki',

Follow-up revisions

RevisionCommit summaryAuthorDate
r83459actually use the messages (added in r51123)robin15:11, 7 March 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r50188add WikimediaIncubator extensionrobin17:56, 4 May 2009

Comments

#Comment by Krinkle (talk | contribs)   16:28, 5 March 2011
+		$ok = wfMsgHtml( 'go' );
+		$username = wfMsgHtml( 'username' );
+		$inputformtop = wfMsgHtml( 'wminc-viewuserlang' );
(...)
+	'wminc-viewuserlang-user' => 'Username:',
+	'wminc-viewuserlang-go' => 'Go',

Later in that file $ok is still used and the "wminc-viewuserlang-go" message isn't used anywhere. "go" and "username" are re-used instead of extension-specific ones.

If a message is similar to another one, to make it easier for translators tag it with {{Identical| centralnamehere }}</nowki> in the /qqq entry. Next, define the English-message. Translators will then translate it in other languages very quickly since they can base it from the other ones. A central 'identical message' page for 'Go' already exists at TranslateWiki: [[translatewiki:Template:Identical/Go]]. So the 'qqq' entry for 'wminc-viewuserlang-go' would be <code><nowiki>{{Identical|Go}}

#Comment by SPQRobin (talk | contribs)   15:14, 7 March 2011

I added the messages but forgot to replace them in $ok and $username. Fixed in r83459.

And btw, the /qqq entries already include the template "Identical" and some information.

#Comment by Krinkle (talk | contribs)   17:07, 7 March 2011

Cool. Marking ok.

Status & tagging log