Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / maintenance / eval.php
blobd98e5cd01c391c9dc3ab80624d69c043c4fcb338
1 <?php
2 /**
3 * This script lets a command-line user start up the wiki engine and then poke
4 * about by issuing PHP commands directly.
6 * Unlike eg Python, you need to use a 'return' statement explicitly for the
7 * interactive shell to print out the value of the expression. Multiple lines
8 * are evaluated separately, so blocks need to be input without a line break.
9 * Fatal errors such as use of undeclared functions can kill the shell.
11 * To get decent line editing behavior, you should compile PHP with support
12 * for GNU readline (pass --with-readline to configure).
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
29 * @file
30 * @ingroup Maintenance
33 $optionsWithArgs = [ 'd' ];
35 require_once __DIR__ . "/commandLine.inc";
37 if ( isset( $options['d'] ) ) {
38 $d = $options['d'];
39 if ( $d > 0 ) {
40 $wgDebugLogFile = '/dev/stdout';
42 if ( $d > 1 ) {
43 $lb = wfGetLB();
44 $serverCount = $lb->getServerCount();
45 for ( $i = 0; $i < $serverCount; $i++ ) {
46 $server = $lb->getServerInfo( $i );
47 $server['flags'] |= DBO_DEBUG;
48 $lb->setServerInfo( $i, $server );
53 $__useReadline = function_exists( 'readline_add_history' )
54 && Maintenance::posix_isatty( 0 /*STDIN*/ );
56 if ( $__useReadline ) {
57 $__historyFile = isset( $_ENV['HOME'] ) ?
58 "{$_ENV['HOME']}/.mweval_history" : "$IP/maintenance/.mweval_history";
59 readline_read_history( $__historyFile );
62 $__e = null; // PHP exception
63 while ( ( $__line = Maintenance::readconsole() ) !== false ) {
64 if ( $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
65 // Internal state may be corrupted or fatals may occur later due
66 // to some object not being set. Don't drop out of eval in case
67 // lines were being pasted in (which would then get dumped to the shell).
68 // Instead, just absorb the remaning commands. Let "exit" through per DWIM.
69 echo "Exception was thrown before; please restart eval.php\n";
70 continue;
72 if ( $__useReadline ) {
73 readline_add_history( $__line );
74 readline_write_history( $__historyFile );
76 try {
77 $__val = eval( $__line . ";" );
78 } catch ( Exception $__e ) {
79 echo "Caught exception " . get_class( $__e ) .
80 ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n";
81 continue;
83 if ( wfIsHHVM() || is_null( $__val ) ) {
84 echo "\n";
85 } elseif ( is_string( $__val ) || is_numeric( $__val ) ) {
86 echo "$__val\n";
87 } else {
88 var_dump( $__val );
92 print "\n";