Move $wgUseNormalUser setting to constructors.
[lhc/web/wiklou.git] / maintenance / eval.php
1 <?php
2 /**
3 * PHP lacks an interactive mode, but this can be very helpful when debugging.
4 * This script lets a command-line user start up the wiki engine and then poke
5 * about by issuing PHP commands directly.
6 *
7 * Unlike eg Python, you need to use a 'return' statement explicitly for the
8 * interactive shell to print out the value of the expression. Multiple lines
9 * are evaluated separately, so blocks need to be input without a line break.
10 * Fatal errors such as use of undeclared functions can kill the shell.
11 *
12 * To get decent line editing behavior, you should compile PHP with support
13 * for GNU readline (pass --with-readline to configure).
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
29 *
30 * @ingroup Maintenance
31 */
32
33 require_once( dirname(__FILE__) . '/Maintenance.php' );
34
35 class EvalPrompt extends Maintenance {
36
37 public function __construct() {
38 global $wgUseNormalUser;
39 parent::__construct();
40 $this->mDescription = "This script lets a command-line user start up the wiki engine and then poke\n" .
41 "about by issuing PHP commands directly.";
42 $this->addOption( 'd', "Enable MediaWiki debug output", false, true );
43 $wgUseNormalUser = (bool)getenv('MW_WIKIUSER');
44 }
45
46 public function execute() {
47 global $wgDebugFunctionEntry, $wgDebugLogFile;
48 if ( $this->hasOption('d') ) {
49 $d = $this->getOption('d');
50 if ( $d > 0 ) {
51 $wgDebugLogFile = '/dev/stdout';
52 }
53 if ( $d > 1 ) {
54 $lb = wfGetLB();
55 foreach ( $lb->mServers as $i => $server ) {
56 $lb->mServers[$i]['flags'] |= DBO_DEBUG;
57 }
58 }
59 if ( $d > 2 ) {
60 $wgDebugFunctionEntry = true;
61 }
62 }
63
64 if ( function_exists( 'readline_add_history' )
65 && function_exists( 'posix_isatty' ) && posix_isatty( 0 /*STDIN*/ ) )
66 {
67 $useReadline = true;
68 } else {
69 $useReadline = false;
70 }
71
72 if ( $useReadline ) {
73 $historyFile = "{$_ENV['HOME']}/.mweval_history";
74 readline_read_history( $historyFile );
75 }
76
77 while ( ( $line = readconsole( '> ' ) ) !== false ) {
78 if ( $useReadline ) {
79 readline_add_history( $line );
80 readline_write_history( $historyFile );
81 }
82 $val = eval( $line . ";" );
83 if( is_null( $val ) ) {
84 echo "\n";
85 } elseif( is_string( $val ) || is_numeric( $val ) ) {
86 echo "$val\n";
87 } else {
88 var_dump( $val );
89 }
90 }
91 print "\n";
92 }
93 }
94
95 $maintClass = "EvalPrompt";
96 require_once( DO_MAINTENANCE );