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