Language:
PHP/PHP 3-4     Change language:
Pastebin: 115726
Author: Chad
Subject: Maintenance.php
Created: 2009-06-09 16:12:04
Download and save
Toggle line numbers
1<?php 
2 
3/** 
4 * Abstract maintenance class for quickly writing and churning out 
5 * maintenance scripts with minimal effort. All that _must_ be defined 
6 * is the execute() method. See docs/maintenance.txt for more info 
7 * and a quick demo of how to use it. 
8 * 
9 * @author Chad Horohoe <chad@anyonecanedit.org> 
10 * @since 1.16 
11 * @ingroup Maintenance 
12 */ 
13abstract class Maintenance { 
14 
15    // This is the desired params 
16    protected $mRequiredParams = array()
17 
18    // This is the list of options that were actually passed 
19    protected $mOptions = array()
20 
21    // This is the list of arguments that were actually passed 
22    protected $mArgs = array()
23 
24    // Name of the script currently running 
25    protected $mSelf
26 
27    // Special vars for params that are always used 
28    private $mQuiet = false
29    private $mDbUser, $mDbPass
30 
31    // A description of the script, children should change this 
32    protected $mDescription = ''; 
33 
34    /** 
35     * Default constructor. Children should call this if implementing 
36     * their own constructors 
37     */ 
38    public function __construct() { 
39        $this->addDefaultParams()
40    } 
41 
42    /** 
43     * Do the actual work. All child classes will need to implement this 
44     */ 
45    abstract protected function execute()
46 
47    /** 
48     * Our public interface for doing everything. We'll run some sanity 
49     * checks and then call the script's execute() method. 
50     */ 
51    public function go() { 
52        // Basic stuff to make sure we can go 
53        $this->sanityChecks()
54 
55        // Begin setting up the environment for script execution 
56        $this->setupEnvironment()
57 
58        // Get all of the arguments 
59        $this->loadArgs()
60 
61        // Show the help and die if we can 
62        $this->maybeHelp()
63 
64        // Load settings, using wikimedia-mode if needed 
65        if( file_exists( dirname(__FILE__).'/wikimedia-mode' ) ) { 
66            # TODO FIXME! Wikimedia-specific stuff needs to go away to an ext 
67            # Maybe a hook? 
68            $this->loadWikimediaSettings()
69        } else { 
70            $this->loadSettings()
71        } 
72 
73        // Final setup 
74        $this->finalSetup()
75 
76        // Execute the actual script 
77        $this->execute()
78    } 
79 
80    /** 
81     * Add a parameter to the script. Will be displayed on --help 
82     * with the associated description 
83     * 
84     * @param $name String The name of the param (help, version, etc) 
85     * @param $description String The description of the param to show on --help 
86     * @param $isFlag boolean Whether the param is a flag (like --help) or needs 
87     *                        a value (like --dbuser=someuser) 
88     */ 
89    protected function addParam( $name, $description, $isFlag = false ) { 
90        if( $isFlag ) 
91            $this->mFlags[ $name ] = $description
92        else 
93            $this->mRequiredParams[ $name ] = $description
94    } 
95 
96    /** 
97     * Return input from stdin. 
98     * @param $length int The number of bytes to read 
99     * @return mixed 
100     */ 
101    protected function getStdin( $len = 255 ) { 
102        $f = fopen( 'php://stdin', 'r' )
103        $input = fgets( $fr, $len )
104        fclose ( $fr )
105        return rtrim( $input )
106    } 
107 
108    /** 
109     * Throw some output to the user. Scripts can call this with no fears, 
110     * as we handle all --quiet stuff here 
111     * @param $out String The text to show to the user 
112     */ 
113    protected function output( $out ) { 
114        if( $this->mQuiet ) { 
115            return
116        } 
117        $f = fopen( 'php://stdout', 'w' )
118        fwrite( $f, $out )
119        fclose( $f )
120    } 
121 
122    /** 
123     * Throw an error to the user. Doesn't respect --quiet, so don't use 
124     * this for non-error output 
125     * @param $err String The error to display 
126     * @param $die boolean If true, go ahead and die out. 
127     */ 
128    protected function error( $err, $die = false ) { 
129        $f = fopen( 'php://stderr', 'w' )
130        fwrite( $f, $err )
131        fclose( $f )
132        if( $die ) die()
133    } 
134 
135    /** 
136     * Does the script need DB access? Override this and return true, 
137     * if needed 
138     * @return boolean 
139     */ 
140    protected function needsDB() { 
141        return false
142    } 
143 
144    /** 
145     * Add the default parameters to the scripts 
146     */ 
147    private function addDefaultParams() { 
148        $this->addParam( 'help', "Display this help message", true )
149        $this->addParam( 'quiet', "Whether to supress non-error output", true )
150        $this->addParam( 'conf', "Location of LocalSettings.php, if not default" )
151        $this->addParam( 'aconf', "Same, but for AdminSettings.php" )
152        $this->addParam( 'wiki', "For specifying the wiki ID" )
153        if( $this->needsDB() ) { 
154            $this->addParam( 'dbuser', "The DB user to use for this script" )
155            $this->addParam( 'dbpass', "The password to use for this script" )
156        } 
157    } 
158 
159    /** 
160     * Do some sanity checking 
161     */ 
162    private function sanityChecks() { 
163        # Abort if called from a web server 
164        if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) { 
165            $this->error( "This script must be run from the command line\n", true )
166        } 
167 
168        # Make sure we can handle script parameters 
169        if( !ini_get( 'register_argc_argv' ) ) { 
170            $this->error( "Cannot get command line arguments, register_argc_argv is set to false", true )
171        } 
172 
173        # Make sure we're on PHP5 or better 
174        if( version_compare( PHP_VERSION, '5.0.0' ) < 0 ) { 
175            $this->error( "Sorry! This version of MediaWiki requires PHP 5; you are running " . 
176                    PHP_VERSION . ".\n\n" . 
177                    "If you are sure you already have PHP 5 installed, it may be installed\n" . 
178                    "in a different path from PHP 4. Check with your system administrator.\n", true )
179        } 
180 
181        if( version_compare( phpversion(), '5.2.4' ) >= 0 ) { 
182            // Send PHP warnings and errors to stderr instead of stdout. 
183            // This aids in diagnosing problems, while keeping messages 
184            // out of redirected output. 
185            if( ini_get( 'display_errors' ) ) { 
186                ini_set( 'display_errors', 'stderr' )
187            } 
188 
189            // Don't touch the setting on earlier versions of PHP, 
190            // as setting it would disable output if you'd wanted it. 
191 
192            // Note that exceptions are also sent to stderr when 
193            // command-line mode is on, regardless of PHP version. 
194        } 
195 
196        # Set the memory limit 
197        ini_set( 'memory_limit', -1 )
198    } 
199 
200    /** 
201     * Setup the maintenance envinronment 
202     */ 
203    private function setupEnvironment() { 
204        global $IP, $wgCommandLineMode, $wgUseNormalUser, $wgRequestTime
205 
206        $wgRequestTime = microtime(true)
207 
208        # Define us as being in Mediawiki 
209        define( 'MEDIAWIKI', true )
210 
211        # Setup $IP, using MW_INSTALL_PATH if it exists 
212        $IP = strval( getenv('MW_INSTALL_PATH') ) !== '' 
213            ? getenv('MW_INSTALL_PATH') 
214            : realpath( dirname( __FILE__ ) . '/..' )
215 
216        # Setup the profiler 
217        if ( file_exists( "$IP/StartProfiler.php" ) ) { 
218            require_once( "$IP/StartProfiler.php" )
219        } else { 
220            require_once( "$IP/includes/ProfilerStub.php" )
221        } 
222 
223        $wgCommandLineMode = true
224        # Turn off output buffering if it's on 
225        @ob_end_flush()
226 
227        if (!isset( $wgUseNormalUser ) ) { 
228            $wgUseNormalUser = false
229        } 
230    } 
231 
232    /** 
233     * Process command line arguments 
234     * $mOptions becomes an array with keys set to the option names 
235     * $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned 
236     * in the values of $options. 
237     * $args becomes a zero-based array containing the non-option arguments 
238     */ 
239    private function loadArgs() { 
240        global $argv
241        $this->mSelf = array_shift( $argv )
242 
243        $options = array()
244        $args = array()
245 
246        # Parse arguments 
247        for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) { 
248            if ( $arg == '--' ) { 
249                # End of options, remainder should be considered arguments 
250                $arg = next( $argv )
251                while( $arg !== false ) { 
252                    $args[] = $arg
253                    $arg = next( $argv )
254                } 
255                break
256            } elseif ( substr( $arg, 0, 2 ) == '--' ) { 
257                # Long options 
258                $option = substr( $arg, 2 )
259                if ( in_array( $option, array_keys( $this->mRequiredParams ) ) ) { 
260                    $param = next( $argv )
261                    if ( $param === false ) { 
262                        $this->error( "$arg needs a value after it\n", true )
263                    } 
264                    $options[$option] = $param
265                } else { 
266                    $bits = explode( '=', $option, 2 )
267                    if( count( $bits ) > 1 ) { 
268                        $option = $bits[0]
269                        $param = $bits[1]
270                    } else { 
271                        $param = 1
272                    } 
273                    $options[$option] = $param
274                } 
275            } elseif ( substr( $arg, 0, 1 ) == '-' ) { 
276                # Short options 
277                for ( $p=1; $p<strlen( $arg ); $p++ ) { 
278                    $option = $arg{$p}
279                    if ( in_array( $option, array_keys( $this->mRequiredParams ) ) ) { 
280                        $param = next( $argv )
281                        if ( $param === false ) { 
282                            $this->error( "$arg needs a value after it\n", true )
283                        } 
284                        $options[$option] = $param
285                    } else { 
286                        $options[$option] = 1
287                    } 
288                } 
289            } else { 
290                $args[] = $arg
291            } 
292        } 
293 
294        # These vars get special treatment 
295        if( isset( $options['dbuser'] ) ) 
296            $this->mDbUser = $options['dbuser']
297        if( isset( $options['dbpass'] ) ) 
298            $this->mDbPass = $options['dbpass']
299        if( isset( $options['quiet'] ) ) 
300            $this->mQuiet = true
301 
302        $this->mOptions = $options
303        $this->mArgs = $args
304    } 
305 
306    /** 
307     * Maybe show the help. 
308     */ 
309    private function maybeHelp() { 
310        if( isset( $this->mOptions['help'] ) ) { 
311            $this->mQuiet = false
312            if( $this->mDescription ) { 
313                $this->output( $this->mDescription . "\n" )
314            } 
315            $params = array_merge( $this->mRequiredParams, $this->mFlags )
316            $this->output( "\nUsage: php " . $this->mSelf . " [--" . 
317                            implode( array_keys( $params ), "|--" ) . "]\n" )
318            foreach( $params as $par => $desc ) { 
319                $this->output( "\t$par : $desc\n" )
320            } 
321        } 
322    } 
323 
324    /** 
325     * Handle some last-minute setup here. 
326     */ 
327    private function finalSetup() { 
328        global $wgCommandLineMode, $wgUseNormalUser, $wgShowSQLErrors, $wgTitle, $wgProfiling, $IP
329        global $wgDBadminuser, $wgDBadminpassword, $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf
330 
331        # Turn off output buffering again, it might have been turned on in the settings files 
332        if( ob_get_level() ) { 
333            ob_end_flush()
334        } 
335        # Same with these 
336        $wgCommandLineMode = true
337 
338        if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) ) { 
339            $wgDBuser = $wgDBadminuser
340            $wgDBpassword = $wgDBadminpassword
341 
342            if( $wgDBservers ) { 
343                foreach ( $wgDBservers as $i => $server ) { 
344                    $wgDBservers[$i]['user'] = $wgDBuser
345                    $wgDBservers[$i]['password'] = $wgDBpassword
346                } 
347            } 
348            if( isset( $wgLBFactoryConf['serverTemplate'] ) ) { 
349                $wgLBFactoryConf['serverTemplate']['user'] = $wgDBuser
350                $wgLBFactoryConf['serverTemplate']['password'] = $wgDBpassword
351            } 
352        } 
353 
354        if ( defined( 'MW_CMDLINE_CALLBACK' ) ) { 
355            $fn = MW_CMDLINE_CALLBACK; 
356            $fn()
357        } 
358 
359        $wgShowSQLErrors = true
360 
361        require_once( "$IP/includes/Setup.php" )
362        require_once( "$IP/install-utils.inc" )
363        $wgTitle = null; # Much much faster startup than creating a title object 
364        @set_time_limit(0)
365 
366        $wgProfiling = false; // only for Profiler.php mode; avoids OOM errors 
367    } 
368 
369    private function loadWikimediaSettings() { 
370    } 
371    private function loadSettings() { 
372    } 
373} 
374 
Download and save
Toggle line numbers
Thread:
[115726] Maintenance.php by Chad at 2009-06-09 16:12:04
  [117881] zSoSpvJcoHJImTvWT by viagra at 2009-07-20 21:17:40 (diff)
  [118533] uuhKzoJMjiRcED by cialis vente at 2009-07-25 05:46:40 (diff)
  [118917] drWVvvFTpSerfst by compra viagra at 2009-07-31 00:11:02 (diff)
  [118964] yUKIcYOzLUFuMQ by compra cialis at 2009-07-31 01:02:20 (diff)
  [119005] npVEYddxnQ by comprare cialis at 2009-07-31 01:52:24 (diff)
  [119047] quHppPkubxRyZU by viagra ordonnance at 2009-07-31 02:44:54 (diff)
  [119092] mUvCTJTUAQr by viagra en france at 2009-07-31 03:35:41 (diff)
  [119134] laEwvNSYPq by cialis naturelle at 2009-07-31 04:25:54 (diff)
  [119181] DjTOGMBaTqnUGaTyvG by cialis at 2009-07-31 05:17:39 (diff)
  [119209] YWJXlalJwl by comprare cialis at 2009-07-31 06:08:30 (diff)
  [119237] cmcqLfMMMjZeGz by acquista cialis at 2009-07-31 06:58:26 (diff)
  [119264] CasCeOXTZQir by comprare viagra at 2009-07-31 07:48:42 (diff)
  [119329] XXZCVEAKmKaFrHEAf by acquistare levitra a san marino at 2009-07-31 19:28:10 (diff)
  [119367] LPxraLfGfrSq by cialis vs cialis vs levitra at 2009-07-31 20:49:00 (diff)
  [119408] iTjmeBhelLmOkCqNdj by cialis generic at 2009-07-31 22:09:00 (diff)
  [119440] hKKzoIYFrEFzF by viagra mg at 2009-07-31 23:30:18 (diff)
  [119472] OVregxQaBkTserDkOGH by comprare viagra in svizzera at 2009-08-01 00:51:08 (diff)
  [120567] tfiBzTNZLZvpPBF by viagra at 2009-08-09 08:55:50 (diff)
  [120601] EcUxDDuHzqZqMHR by achat viagra at 2009-08-09 10:21:44 (diff)
  [120635] yPagVgLQmxijjvuUfRB by kamagra at 2009-08-09 11:47:19 (diff)
  [120672] mOFZPdtUHBtZo by viagra at 2009-08-09 13:10:31 (diff)
  [120707] cMUEKeFSRIQobn by achat viagra at 2009-08-09 14:34:24 (diff)
  [121020] fPoTiWozJAoEmSXWZy by cialis generique at 2009-08-14 03:06:13 (diff)
  [121079] tePSPkfGJOSJtZg by viagra at 2009-08-14 04:30:00 (diff)
  [121138] EDGKqlEQDO by viagra sur le net at 2009-08-14 05:54:19 (diff)
  [121200] YPqxHFzaqH by prix cialis at 2009-08-14 07:16:58 (diff)
  [121257] ZVFUzCZEpJbmCQz by cialis at 2009-08-14 08:44:02 (diff)
  [121314] VyqlPdZbLxStJQdBX by cialis at 2009-08-14 10:11:45 (diff)
  [121349] IlnTOmDHZhiZJn by cialis at 2009-08-14 11:37:41 (diff)
  [121384] rsBVESCcOg by cialis vente at 2009-08-14 13:05:05 (diff)
  [121417] uzldDkaGQB by achat viagra at 2009-08-14 14:29:31 (diff)
  [121450] JOVqJlCoKvDlko by cialis at 2009-08-14 15:53:25 (diff)
  [121597] AldVkxaPwMBjDVB by viagra at 2009-08-17 23:52:44 (diff)
  [121629] YvZQARQNcblVcvZflvo by viagra at 2009-08-18 01:32:21 (diff)
  [121690] NwEhjgQTpMwUUTwO by cialis online at 2009-08-18 03:10:44 (diff)
  [121767] uzrFPKhJefrahqiq by cialis kaufen billig at 2009-08-18 04:52:00 (diff)
  [121940] TPvjVbnOLkDBKvNe by viagra prix at 2009-08-18 23:09:12 (diff)
  [121973] VJpeUSqFCckx by acheter cialis sans ordonnance at 2009-08-19 03:57:00 (diff)
  [122089] BrtanrfjvjodUGbGFP by viagra at 2009-08-20 19:20:09 (diff)
  [122131] HySfxGYeqviuF by comprare viagra at 2009-08-21 01:19:23 (diff)
  [122198] muXUHlvmRbxIsyR by cialis vendita at 2009-08-21 07:15:38 (diff)
  [122379] PbeYFIagdM by acquistare cialis at 2009-08-21 13:13:50 (diff)
  [122518] EWVAwyaffyhlE by viagra generico at 2009-08-21 17:40:00 (diff)
  [122684] VHWpiujdvAQMGn by acquisto cialis senza ricetta at 2009-08-21 23:39:51 (diff)
  [122887] nmiklghkBiUtut by achat cialis generique at 2009-08-22 12:08:10 (diff)
  [122952] XhiDqVwjBrOQMFv by cialis at 2009-08-22 13:56:57 (diff)
  [123023] NMZelkLryV by acheter cialis at 2009-08-22 15:40:30 (diff)
  [123751] BKMKtHpYrE by achat cialis sur internet at 2009-08-28 15:36:47 (diff)
  [123828] kZSMZHSZeMRDeVzvjsf by acheter cialis en ligne at 2009-08-28 17:16:40 (diff)
  [123896] emZHMcekPPY by viagra prix at 2009-08-28 18:53:09 (diff)
  [123965] sXeAlcZkZBOf by acheter viagra at 2009-08-28 20:32:09 (diff)
  [124036] sezcxaQyNvyqBhF by acquistare viagra online at 2009-08-28 22:12:51 (diff)
  [124080] SHLzopUgRFfHpOuHoyt by achat cialis france at 2009-08-28 23:53:43 (diff)
  [124115] YJXYNWceoAD by cialis at 2009-08-29 01:31:10 (diff)
  [124152] wWEaIumEkhuQOqnlM by cialis at 2009-08-29 03:07:45 (diff)
  [124185] nfjWVUxNUXSsvaAGk by acheter viagra en ligne at 2009-08-29 04:46:40 (diff)
  [124218] jSohQmtsRy by viagra at 2009-08-29 06:24:46 (diff)
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.