Merge maintenance-work branch (now with less errors!):
[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( "Maintenance.php" );
34
35 class EvalPrompt extends Maintenance {
36
37 public function __construct() {
38 parent::__construct();
39 $this->mDescription = "This script lets a command-line user start up the wiki engine and then poke\n" .
40 "about by issuing PHP commands directly.";
41 $this->addOption( 'd', "Enable MediaWiki debug output", false, true );
42 }
43
44 public function execute() {
45 global $wgUseNormalUser, $wgDebugFunctionEntry, $wgDebugLogFile;
46 $wgUseNormalUser = (bool)getenv('MW_WIKIUSER');
47 if ( $this->hasOption('d') ) {
48 $d = $this->getOption('d');
49 if ( $d > 0 ) {
50 $wgDebugLogFile = '/dev/stdout';
51 }
52 if ( $d > 1 ) {
53 $lb = wfGetLB();
54 foreach ( $lb->mServers as $i => $server ) {
55 $lb->mServers[$i]['flags'] |= DBO_DEBUG;
56 }
57 }
58 if ( $d > 2 ) {
59 $wgDebugFunctionEntry = true;
60 }
61 }
62
63 if ( function_exists( 'readline_add_history' )
64 && function_exists( 'posix_isatty' ) && posix_isatty( 0 /*STDIN*/ ) )
65 {
66 $useReadline = true;
67 } else {
68 $useReadline = false;
69 }
70
71 if ( $useReadline ) {
72 $historyFile = "{$_ENV['HOME']}/.mweval_history";
73 readline_read_history( $historyFile );
74 }
75
76 while ( ( $line = readconsole( '> ' ) ) !== false ) {
77 if ( $useReadline ) {
78 readline_add_history( $line );
79 readline_write_history( $historyFile );
80 }
81 $val = eval( $line . ";" );
82 if( is_null( $val ) ) {
83 echo "\n";
84 } elseif( is_string( $val ) || is_numeric( $val ) ) {
85 echo "$val\n";
86 } else {
87 var_dump( $val );
88 }
89 }
90 print "\n";
91 }
92 }
93
94 $maintClass = "EvalPrompt";
95 require_once( DO_MAINTENANCE );