Find and fix places where globals were being used without declaring them.
authorThomas Bleher <tbleher@users.mediawiki.org>
Mon, 17 Dec 2007 12:24:16 +0000 (12:24 +0000)
committerThomas Bleher <tbleher@users.mediawiki.org>
Mon, 17 Dec 2007 12:24:16 +0000 (12:24 +0000)
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
}
}
}

includes/OutputPage.php
languages/classes/LanguageKk_cyrl.php
maintenance/addwiki.php

index de6bb5b..23effc3 100644 (file)
@@ -1345,7 +1345,7 @@ class OutputPage {
         * for when rate limiting has triggered.
         */
        public function rateLimited() {
-               global $wgOut;
+               global $wgOut, $wgTitle;
 
                $this->setPageTitle(wfMsg('actionthrottled'));
                $this->setRobotPolicy( 'noindex,follow' );
index 2444ea0..974326c 100644 (file)
@@ -23,7 +23,7 @@ class LanguageKk_cyrl extends Language {
        }
 
        function convertGrammarKk_cyrl( $word, $case ) {
-
+               global $wgGrammarForms;
                if ( isset( $wgGrammarForms['kk-kz'][$case][$word] ) ) {
                        return $wgGrammarForms['kk-kz'][$case][$word];
                }
index 8dcd2ec..a19b24c 100644 (file)
@@ -71,6 +71,7 @@ function addWiki( $lang, $site, $dbName )
                }
        }
 
+       global $wgTitle, $wgArticle;
        $wgTitle = Title::newMainPage();
        $wgArticle = new Article( $wgTitle );
        $ucsite = ucfirst( $site );