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)
commita4f99d65dcb0e5e4060f981015e57b97e2278e62
tree1292cd57efd621b464f12f46e08307903e820641
parent5b6127b8bfe099d3558c224335ff03a6e2ac52fa
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
}
}
}
includes/OutputPage.php
languages/classes/LanguageKk_cyrl.php
maintenance/addwiki.php