r55810 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55809‎ | r55810 | r55811 >
Date:08:02, 4 September 2009
Author:tstarling
Status:resolved
Tags:
Comment:
* Rewrote commandLine.inc to be a simple wrapper around Maintenance.php
* Don't show help if the first argument is "help", only show it if there is a "--help" option. That makes more sense for the scripts that accept filenames as parameters and is the backwards-compatible behaviour.
* Renamed spawnChild() to runChild(), spawn implies creating a new process to run simultaneously with the old one
* Removed error_reporting override, that's a matter for local policy
* s/private/protected
Modified paths:
  • /trunk/phase3/maintenance/Maintenance.php (modified) (history)
  • /trunk/phase3/maintenance/commandLine.inc (modified) (history)
  • /trunk/phase3/maintenance/doMaintenance.php (modified) (history)
  • /trunk/phase3/maintenance/nukeNS.php (modified) (history)
  • /trunk/phase3/maintenance/rebuildall.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/nukeNS.php
@@ -80,7 +80,7 @@
8181 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
8282 $dbw->commit();
8383 // Delete revisions as appropriate
84 - $child = $this->spawnChild( 'NukePage', 'NukePage.php' );
 84+ $child = $this->runChild( 'NukePage', 'NukePage.php' );
8585 $child->deleteRevisions( $revs );
8686 $this->purgeRedundantText( true );
8787 $n_deleted ++;
Index: trunk/phase3/maintenance/rebuildall.php
@@ -34,18 +34,18 @@
3535 // Rebuild the text index
3636 if ( $wgDBtype == 'mysql' ) {
3737 $this->output( "** Rebuilding fulltext search index (if you abort this will break searching; run this script again to fix):\n" );
38 - $rebuildText = $this->spawnChild( 'RebuildTextIndex', 'rebuildtextindex.php' );
 38+ $rebuildText = $this->runChild( 'RebuildTextIndex', 'rebuildtextindex.php' );
3939 $rebuildText->execute();
4040 }
4141
4242 // Rebuild RC
4343 $this->output( "\n\n** Rebuilding recentchanges table:\n" );
44 - $rebuildRC = $this->spawnChild( 'RebuildRecentchanges', 'rebuildrecentchanges.php' );
 44+ $rebuildRC = $this->runChild( 'RebuildRecentchanges', 'rebuildrecentchanges.php' );
4545 $rebuildRC->execute();
4646
4747 // Rebuild link tables
4848 $this->output( "\n\n** Rebuilding links tables -- this can take a long time. It should be safe to abort via ctrl+C if you get bored.\n" );
49 - $rebuildLinks = $this->spawnChild( 'RefreshLinks', 'refreshLinks.php' );
 49+ $rebuildLinks = $this->runChild( 'RefreshLinks', 'refreshLinks.php' );
5050 $rebuildLinks->execute();
5151
5252 $this->output( "Done.\n" );
Index: trunk/phase3/maintenance/doMaintenance.php
@@ -31,8 +31,6 @@
3232 exit( 1 );
3333 }
3434
35 -error_reporting( E_ALL | E_STRICT );
36 -
3735 if( !$maintClass || !class_exists( $maintClass ) ) {
3836 echo "\$maintClass is not set or is set to a non-existent class.";
3937 exit( 1 );
Index: trunk/phase3/maintenance/commandLine.inc
@@ -1,264 +1,30 @@
22 <?php
 3+
34 /**
4 - * @file
5 - * @todo document
6 - * @ingroup Maintenance
7 - * @defgroup Maintenance Maintenance
 5+ * Backwards-compatibility wrapper for old-style maintenance scripts
86 */
 7+require( dirname(__FILE__) . '/Maintenance.php' );
 8+$optionsWithArgs = array();
99
10 -$wgRequestTime = microtime(true);
11 -
12 -/** */
13 -# Abort if called from a web server
14 -if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
15 - print "This script must be run from the command line\n";
16 - exit();
17 -}
18 -
19 -if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) {
20 - print "Sorry! This version of MediaWiki requires PHP 5; you are running " .
21 - PHP_VERSION . ".\n\n" .
22 - "If you are sure you already have PHP 5 installed, it may be " .
23 - "installed\n" .
24 - "in a different path from PHP 4. Check with your system administrator.\n";
25 - die( -1 );
26 -}
27 -
28 -define('MEDIAWIKI',true);
29 -
30 -# Process command line arguments
31 -# $options becomes an array with keys set to the option names
32 -# $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
33 -# in the values of $options.
34 -# $args becomes a zero-based array containing the non-option arguments
35 -
36 -if ( !isset( $optionsWithArgs ) ) {
37 - $optionsWithArgs = array();
38 -}
39 -$optionsWithArgs[] = 'conf'; # For specifying the location of LocalSettings.php
40 -$optionsWithArgs[] = 'aconf'; # As above for AdminSettings.php
41 -$optionsWithArgs[] = 'wiki'; # For specifying the wiki ID
42 -
43 -$self = array_shift( $argv );
44 -$IP = strval( getenv('MW_INSTALL_PATH') ) !== ''
45 - ? getenv('MW_INSTALL_PATH')
46 - : realpath( dirname( __FILE__ ) . '/..' );
47 -#chdir( $IP );
48 -if ( file_exists( "$IP/StartProfiler.php" ) ) {
49 - require_once( "$IP/StartProfiler.php" );
50 -} else {
51 - require_once( "$IP/includes/ProfilerStub.php" );
52 -}
53 -
54 -$options = array();
55 -$args = array();
56 -
57 -
58 -# Parse arguments
59 -for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
60 - if ( $arg == '--' ) {
61 - # End of options, remainder should be considered arguments
62 - $arg = next( $argv );
63 - while( $arg !== false ) {
64 - $args[] = $arg;
65 - $arg = next( $argv );
 10+class CommandLineInc extends Maintenance {
 11+ public function __construct() {
 12+ global $optionsWithArgs;
 13+ parent::__construct();
 14+ foreach ( $optionsWithArgs as $name ) {
 15+ $this->addOption( $name, '', false, true );
6616 }
67 - break;
68 - } elseif ( substr( $arg, 0, 2 ) == '--' ) {
69 - # Long options
70 - $option = substr( $arg, 2 );
71 - if ( in_array( $option, $optionsWithArgs ) ) {
72 - $param = next( $argv );
73 - if ( $param === false ) {
74 - echo "$arg needs a value after it\n";
75 - die( -1 );
76 - }
77 - $options[$option] = $param;
78 - } else {
79 - $bits = explode( '=', $option, 2 );
80 - if( count( $bits ) > 1 ) {
81 - $option = $bits[0];
82 - $param = $bits[1];
83 - } else {
84 - $param = 1;
85 - }
86 - $options[$option] = $param;
87 - }
88 - } elseif ( substr( $arg, 0, 1 ) == '-' ) {
89 - # Short options
90 - for ( $p=1; $p<strlen( $arg ); $p++ ) {
91 - $option = $arg{$p};
92 - if ( in_array( $option, $optionsWithArgs ) ) {
93 - $param = next( $argv );
94 - if ( $param === false ) {
95 - echo "$arg needs a value after it\n";
96 - die( -1 );
97 - }
98 - $options[$option] = $param;
99 - } else {
100 - $options[$option] = 1;
101 - }
102 - }
103 - } else {
104 - $args[] = $arg;
105 - }
106 -}
10717
108 -
109 -# General initialisation
110 -
111 -$wgCommandLineMode = true;
112 -# Turn off output buffering if it's on
113 -@ob_end_flush();
114 -$sep = PATH_SEPARATOR;
115 -
116 -if (!isset( $wgUseNormalUser ) ) {
117 - $wgUseNormalUser = false;
118 -}
119 -
120 -if ( file_exists( dirname(__FILE__).'/wikimedia-mode' ) ) {
121 - $wgWikiFarm = true;
122 - $cluster = 'pmtpa';
123 - require_once( "$IP/includes/AutoLoader.php" );
124 - require_once( "$IP/includes/SiteConfiguration.php" );
125 -
126 - # Get $wgConf
127 - require( "$IP/wgConf.php" );
128 -
129 - if ( empty( $wgNoDBParam ) ) {
130 - # Check if we were passed a db name
131 - if ( isset( $options['wiki'] ) ) {
132 - $db = $options['wiki'];
133 - } else {
134 - $db = array_shift( $args );
135 - }
136 - list( $site, $lang ) = $wgConf->siteFromDB( $db );
137 -
138 - # If not, work out the language and site the old way
139 - if ( is_null( $site ) || is_null( $lang ) ) {
140 - if ( !$db ) {
141 - $lang = 'aa';
142 - } else {
143 - $lang = $db;
144 - }
145 - if ( isset( $args[0] ) ) {
146 - $site = array_shift( $args );
147 - } else {
148 - $site = 'wikipedia';
149 - }
150 - }
151 - } else {
152 - $lang = 'aa';
153 - $site = 'wikipedia';
 18+ # No help, it would just be misleading since it misses custom options
 19+ unset( $this->mParams['help'] );
15420 }
15521
156 - # This is for the IRC scripts, which now run as the apache user
157 - # The apache user doesn't have access to the wikiadmin_pass command
158 - if ( $_ENV['USER'] == 'apache' ) {
159 - #if ( posix_geteuid() == 48 ) {
160 - $wgUseNormalUser = true;
 22+ public function execute() {
 23+ global $args, $options;
 24+ $args = $this->mArgs;
 25+ $options = $this->mOptions;
16126 }
162 -
163 - putenv( 'wikilang='.$lang);
164 -
165 - $DP = $IP;
166 - ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
167 -
168 - if ( $lang == 'test' && $site == 'wikipedia' ) {
169 - define( 'TESTWIKI', 1 );
170 - }
171 -
172 - #require_once( $IP.'/includes/ProfilerStub.php' );
173 - require( $IP.'/includes/Defines.php' );
174 - require( $IP.'/CommonSettings.php' );
175 - if ( !$wgUseNormalUser && is_readable( "$IP/AdminSettings.php" ) ) {
176 - require( "$IP/AdminSettings.php" );
177 - }
178 -} else {
179 - $wgWikiFarm = false;
180 - if ( isset( $options['conf'] ) ) {
181 - $settingsFile = $options['conf'];
182 - } else {
183 - $settingsFile = "$IP/LocalSettings.php";
184 - }
185 - if ( isset( $options['wiki'] ) ) {
186 - $bits = explode( '-', $options['wiki'] );
187 - if ( count( $bits ) == 1 ) {
188 - $bits[] = '';
189 - }
190 - define( 'MW_DB', $bits[0] );
191 - define( 'MW_PREFIX', $bits[1] );
192 - }
193 -
194 - if ( ! is_readable( $settingsFile ) ) {
195 - print "A copy of your installation's LocalSettings.php\n" .
196 - "must exist and be readable in the source directory.\n";
197 - exit( 1 );
198 - }
199 - $wgCommandLineMode = true;
200 - $DP = $IP;
201 - require_once( "$IP/includes/AutoLoader.php" );
202 - require_once( "$IP/includes/Defines.php" );
203 - require_once( $settingsFile );
204 - /* ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" ); */
205 -
206 - $adminSettings = isset( $options['aconf'] )
207 - ? $options['aconf']
208 - : "{$IP}/AdminSettings.php";
209 - if( is_readable( $adminSettings ) )
210 - require_once( $adminSettings );
211 -
21227 }
21328
214 -# Turn off output buffering again, it might have been turned on in the settings files
215 -if( ob_get_level() ) {
216 - ob_end_flush();
217 -}
218 -# Same with these
219 -$wgCommandLineMode = true;
 29+$maintClass = 'CommandLineInc';
 30+require( DO_MAINTENANCE );
22031
221 -if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) {
222 - $wgDBuser = $wgDBadminuser;
223 - $wgDBpassword = $wgDBadminpassword;
224 -
225 - if( $wgDBservers ) {
226 - foreach ( $wgDBservers as $i => $server ) {
227 - $wgDBservers[$i]['user'] = $wgDBuser;
228 - $wgDBservers[$i]['password'] = $wgDBpassword;
229 - }
230 - }
231 - if( isset( $wgLBFactoryConf['serverTemplate'] ) ) {
232 - $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser;
233 - $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword;
234 - }
235 -}
236 -
237 -if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
238 - $fn = MW_CMDLINE_CALLBACK;
239 - $fn();
240 -}
241 -
242 -ini_set( 'memory_limit', -1 );
243 -
244 -if( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
245 - // Send PHP warnings and errors to stderr instead of stdout.
246 - // This aids in diagnosing problems, while keeping messages
247 - // out of redirected output.
248 - if( ini_get( 'display_errors' ) ) {
249 - ini_set( 'display_errors', 'stderr' );
250 - }
251 -
252 - // Don't touch the setting on earlier versions of PHP,
253 - // as setting it would disable output if you'd wanted it.
254 -
255 - // Note that exceptions are also sent to stderr when
256 - // command-line mode is on, regardless of PHP version.
257 -}
258 -$wgShowSQLErrors = true;
259 -
260 -require_once( "$IP/includes/Setup.php" );
261 -require_once( "$IP/maintenance/install-utils.inc" );
262 -$wgTitle = null; # Much much faster startup than creating a title object
263 -@set_time_limit(0);
264 -
265 -$wgProfiling = false; // only for Profiler.php mode; avoids OOM errors
Index: trunk/phase3/maintenance/Maintenance.php
@@ -51,13 +51,13 @@
5252 const STDIN_ALL = 'all';
5353
5454 // This is the desired params
55 - private $mParams = array();
 55+ protected $mParams = array();
5656
5757 // Array of desired args
58 - private $mArgList = array();
 58+ protected $mArgList = array();
5959
6060 // This is the list of options that were actually passed
61 - private $mOptions = array();
 61+ protected $mOptions = array();
6262
6363 // This is the list of arguments that were actually passed
6464 protected $mArgs = array();
@@ -66,14 +66,14 @@
6767 protected $mSelf;
6868
6969 // Special vars for params that are always used
70 - private $mQuiet = false;
71 - private $mDbUser, $mDbPass;
 70+ protected $mQuiet = false;
 71+ protected $mDbUser, $mDbPass;
7272
7373 // A description of the script, children should change this
7474 protected $mDescription = '';
7575
7676 // Have we already loaded our user input?
77 - private $mInputLoaded = false;
 77+ protected $mInputLoaded = false;
7878
7979 // Batch size. If a script supports this, they should set
8080 // a default with setBatchSize()
@@ -248,7 +248,7 @@
249249 /**
250250 * Add the default parameters to the scripts
251251 */
252 - private function addDefaultParams() {
 252+ protected function addDefaultParams() {
253253 $this->addOption( 'help', "Display this help message" );
254254 $this->addOption( 'quiet', "Whether to supress non-error output" );
255255 $this->addOption( 'conf', "Location of LocalSettings.php, if not default", false, true );
@@ -267,13 +267,13 @@
268268 }
269269
270270 /**
271 - * Spawn a child maintenance script. Pass all of the current arguments
 271+ * Run a child maintenance script. Pass all of the current arguments
272272 * to it.
273273 * @param $maintClass String A name of a child maintenance class
274274 * @param $classFile String Full path of where the child is
275275 * @return Maintenance child
276276 */
277 - protected function spawnChild( $maintClass, $classFile = null ) {
 277+ protected function runChild( $maintClass, $classFile = null ) {
278278 // If we haven't already specified, kill setup procedures
279279 // for child scripts, we've already got a sane environment
280280 self::disableSetup();
@@ -469,7 +469,7 @@
470470 /**
471471 * Run some validation checks on the params, etc
472472 */
473 - private function validateParamsAndArgs() {
 473+ protected function validateParamsAndArgs() {
474474 $die = false;
475475 # Check to make sure we've got all the required options
476476 foreach( $this->mParams as $opt => $info ) {
@@ -492,7 +492,7 @@
493493 /**
494494 * Handle the special variables that are global to all scripts
495495 */
496 - private function loadSpecialVars() {
 496+ protected function loadSpecialVars() {
497497 if( $this->hasOption( 'dbuser' ) )
498498 $this->mDbUser = $this->getOption( 'dbuser' );
499499 if( $this->hasOption( 'dbpass' ) )
@@ -507,9 +507,9 @@
508508 * Maybe show the help.
509509 * @param $force boolean Whether to force the help to show, default false
510510 */
511 - private function maybeHelp( $force = false ) {
 511+ protected function maybeHelp( $force = false ) {
512512 ksort( $this->mParams );
513 - if( $this->hasOption( 'help' ) || in_array( 'help', $this->mArgs ) || $force ) {
 513+ if( $this->hasOption( 'help' ) || $force ) {
514514 $this->mQuiet = false;
515515 if( $this->mDescription ) {
516516 $this->output( "\n" . $this->mDescription . "\n" );
@@ -754,7 +754,7 @@
755755 * Return all of the core maintenance scripts
756756 * @return array
757757 */
758 - private static function getCoreScripts() {
 758+ protected static function getCoreScripts() {
759759 if( !self::$mCoreScripts ) {
760760 self::disableSetup();
761761 $paths = array(

Follow-up revisions

RevisionCommit summaryAuthorDate
r55854Fix for r55810: oly set $optionsWithArgs if it's not defined, as in the old v...ialex19:40, 5 September 2009
r60007Readded definition for doxygen's Maintenance group that was removed in r55810ialex22:22, 12 December 2009

Status & tagging log