Apply patch from Karsten Düsterloh in Bug #28103.
[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 * @file
31 * @ingroup Maintenance
32 */
33
34 $wgUseNormalUser = (bool)getenv( 'MW_WIKIUSER' );
35
36 $optionsWithArgs = array( 'd' );
37
38 /** */
39 require_once( "commandLine.inc" );
40
41 if ( isset( $options['d'] ) ) {
42 $d = $options['d'];
43 if ( $d > 0 ) {
44 $wgDebugLogFile = '/dev/stdout';
45 }
46 if ( $d > 1 ) {
47 $lb = wfGetLB();
48 $serverCount = $lb->getServerCount();
49 for ( $i = 0; $i < $serverCount; $i++ ) {
50 $server = $lb->getServerInfo( $i );
51 $server['flags'] |= DBO_DEBUG;
52 $lb->setServerInfo( $i, $server );
53 }
54 }
55 if ( $d > 2 ) {
56 $wgDebugFunctionEntry = true;
57 }
58 }
59
60 if ( function_exists( 'readline_add_history' )
61 && posix_isatty( 0 /*STDIN*/ ) )
62 {
63 $useReadline = true;
64 } else {
65 $useReadline = false;
66 }
67
68 if ( $useReadline ) {
69 $historyFile = isset( $_ENV['HOME'] ) ?
70 "{$_ENV['HOME']}/.mweval_history" : "$IP/maintenance/.mweval_history";
71 readline_read_history( $historyFile );
72 }
73
74 while ( ( $line = Maintenance::readconsole() ) !== false ) {
75 if ( $useReadline ) {
76 readline_add_history( $line );
77 readline_write_history( $historyFile );
78 }
79 $val = eval( $line . ";" );
80 if ( wfIsHipHop() || is_null( $val ) ) {
81 echo "\n";
82 } elseif ( is_string( $val ) || is_numeric( $val ) ) {
83 echo "$val\n";
84 } else {
85 var_dump( $val );
86 }
87 }
88
89 print "\n";
90
91