r28586 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r28585‎ | r28586 | r28587 >
Date:12:24, 17 December 2007
Author:tbleher
Status:old
Tags:
Comment:
Find and fix places where globals were being used without declaring them.

All the places were found using a script, reproduced below, in case
anyone wants to reuse it.
But beware: The script produces a lot of false positives, because it
doesn't fully parse PHP and is fooled e.g. by javascript functions
inside of <<<HERE documents.

#!/usr/bin/perl -0777 -n
#
# find functions where variables starting with $wg are used without declaring them as globals
# should be given a list of files on the command line
#
# first remove all comments
# Warning: this may remove too much, e.g. if /* is inside a string
s,/\*.*?\*/,,sg; # remove multiline comments
s,#.*,,g; # remove single line comments starting with #
s,//.*,,g; # and the ones starting with //
s,(?<!\\)'.*?(?<!\\)',,g; # remove 'single quoted single-line strings'; may again remove too much

# now process each function one by one
# does not deal correctly with javascript functions embedded in <<HERE documents, unfortunately
#
while (/(^\s*)(?:(?:private|public|protected|static)\s+)*function\s+(\w+)\s*\((.*?)\)\s*\{(.*?)\1\}/msg) {
$fname = $2; $farg = $3; $fbody = $4;
%globals = ();
while ($farg =~ /(\$\w+)/g) { # treat arguments to functions as globals here
$globals{$1} = 1;
}
while ($fbody =~ /^\s*global\s+([^;]+?)\s*;/msg) { # find all global vars
for (split /\s*,\s*/, $1) {
$globals{$_} = 1;
}
}
while ($fbody =~ /(?<!\\)(\$wg\w+)\b/g) { # search for all variables starting with $wg and see if they are declared as globals
if (not $globals{$1}) {
print "Global $1 not declared in function $fname, file $ARGV\n";
$globals{$1} = 1; # warn only once
}
}
}
Modified paths:
  • /trunk/extensions/Cache404/extensions/Cache404.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/FlaggedRevsPage.php (modified) (history)
  • /trunk/extensions/Makesysop/SpecialMakesysop_body.php (modified) (history)
  • /trunk/extensions/MultiUpload/SpecialMultipleUpload.php (modified) (history)
  • /trunk/extensions/Translate/fuzzy.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/DefinedMeaningModel.php (modified) (history)
  • /trunk/extensions/regexBlock/regexBlockCore.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/languages/classes/LanguageKk_cyrl.php (modified) (history)
  • /trunk/phase3/maintenance/addwiki.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/addwiki.php
@@ -71,6 +71,7 @@
7272 }
7373 }
7474
 75+ global $wgTitle, $wgArticle;
7576 $wgTitle = Title::newMainPage();
7677 $wgArticle = new Article( $wgTitle );
7778 $ucsite = ucfirst( $site );
Index: trunk/phase3/includes/OutputPage.php
@@ -1345,7 +1345,7 @@
13461346 * for when rate limiting has triggered.
13471347 */
13481348 public function rateLimited() {
1349 - global $wgOut;
 1349+ global $wgOut, $wgTitle;
13501350
13511351 $this->setPageTitle(wfMsg('actionthrottled'));
13521352 $this->setRobotPolicy( 'noindex,follow' );
Index: trunk/phase3/languages/classes/LanguageKk_cyrl.php
@@ -23,7 +23,7 @@
2424 }
2525
2626 function convertGrammarKk_cyrl( $word, $case ) {
27 -
 27+ global $wgGrammarForms;
2828 if ( isset( $wgGrammarForms['kk-kz'][$case][$word] ) ) {
2929 return $wgGrammarForms['kk-kz'][$case][$word];
3030 }
Index: trunk/extensions/MultiUpload/SpecialMultipleUpload.php
@@ -186,7 +186,6 @@
187187 */
188188 function uploadWarning( $warning ) {
189189 global $wgOut;
190 - global $wgUseCopyrightUpload;
191190
192191 if (!$this->mHasWarning) {
193192 $titleObj = Title::makeTitle( NS_SPECIAL, 'MultipleUpload' );
@@ -228,7 +227,7 @@
229228 }
230229
231230 function showWarningOptions() {
232 - global $wgOut, $wgMaxUploadFiles;
 231+ global $wgOut, $wgMaxUploadFiles, $wgUseCopyrightUpload;
233232 $save = wfMsgHtml( 'multipleupload-saveallfiles' );
234233 $reupload = wfMsgHtml( 'reupload' );
235234 $iw = wfMsgWikiHtml( 'multipleupload-ignoreallwarnings' );
Index: trunk/extensions/Makesysop/SpecialMakesysop_body.php
@@ -232,6 +232,7 @@
233233
234234 /** @see UserrightsForm::saveUserGroups in MediaWiki */
235235 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
 236+ global $wgOut;
236237 $split = $this->splitUsername( $username );
237238 if( WikiError::isError( $split ) ) {
238239 $wgOut->addWikiText( wfMsg( 'makesysop-nodatabase', $split->getMessage() ) );
Index: trunk/extensions/FlaggedRevs/FlaggedRevsPage.php
@@ -448,7 +448,7 @@
449449 * Removes flagged revision data for this page/id set
450450 */
451451 function unapproveRevision( $row=NULL ) {
452 - global $wgUser, $wgFlaggedRevsWatch;
 452+ global $wgUser, $wgParser, $wgFlaggedRevsWatch;
453453
454454 if( is_null($row) ) return false;
455455
Index: trunk/extensions/Cache404/extensions/Cache404.php
@@ -633,8 +633,8 @@
634634 global $wgUsePathInfo, $wgFilterCallback, $wgRequest, $wgUser, $wgOut, $_SERVER;
635635 global $wgAllowUserJs, $wgAllowUserCss, $wgUseSiteCss, $wgUseSiteJs, $wgCachePages;
636636 global $wgCache404SavedCallback;
637 - global $wgLang, $wgContLang, $wgMessageCache, $messageMemc, $wgUseDatabaseMessages,
638 - $wgMsgCacheExpiry, $wgDBname;
 637+ global $wgLang, $wgLanguageCode, $wgContLang, $wgContLanguageCode, $wgMessageCache,
 638+ $messageMemc, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgDBname;
639639
640640 # Prevent user options, user new talk
641641
Index: trunk/extensions/Translate/fuzzy.php
@@ -96,7 +96,7 @@
9797 }
9898
9999 private function updateMessage( $title, $text ) {
100 - global $wgTitle;
 100+ global $wgTitle, $wgArticle;
101101 $wgTitle = Title::newFromText( "Mediawiki:$title" );
102102
103103 echo "Updating {$wgTitle->getPrefixedText()}... ";
@@ -148,4 +148,4 @@
149149 exit( 1 );
150150 }
151151
152 -*/
\ No newline at end of file
 152+*/
Index: trunk/extensions/Wikidata/OmegaWiki/DefinedMeaningModel.php
@@ -240,7 +240,7 @@
241241 # $wgTitle, $wgUser, $wgRequest;
242242
243243 global
244 - $wgUser;
 244+ $wgUser, $wgOut;
245245
246246 if(!$wgUser->isAllowed('wikidata-copy')) {
247247 $wgOut->addWikiText(wfMsgSc("noedit",$dc->fetchName()));
Index: trunk/extensions/regexBlock/regexBlockCore.php
@@ -199,6 +199,7 @@
200200 @param $user_ip String
201201 */
202202 function wfGetRegexBlocked ($blocker, $user, $user_ip) {
 203+ global $wgContactLink;
203204 $names = wfGetRegexBlockedData ($blocker,$user,REGEXBLOCK_MODE_NAMES);
204205 $ips = wfGetRegexBlockedData ($blocker,$user,REGEXBLOCK_MODE_IPS);
205206 $username = $user->getName () ;

Status & tagging log