Fix Parse error: syntax error, unexpected T_LNUMBER
[lhc/web/wiklou.git] / maintenance / sql.php
1 <?php
2
3 /**
4 * Send SQL queries from the specified file to the database, performing
5 * variable replacement along the way.
6 */
7
8 require_once( dirname(__FILE__) . '/' . 'commandLine.inc' );
9
10 if ( isset( $options['help'] ) ) {
11 echo "Send SQL queries to a MediaWiki database.\nUsage: php sql.php [<file>]\n";
12 exit( 1 );
13 }
14
15 if ( isset( $args[0] ) ) {
16 $fileName = $args[0];
17 $file = fopen( $fileName, 'r' );
18 $promptCallback = false;
19 } else {
20 $file = STDIN;
21 $promptObject = new SqlPromptPrinter( "> " );
22 $promptCallback = $promptObject->cb();
23 }
24
25 if ( !$file ) {
26 echo "Unable to open input file\n";
27 exit( 1 );
28 }
29
30 $dbw =& wfGetDB( DB_MASTER );
31 $error = $dbw->sourceStream( $file, $promptCallback, 'sqlPrintResult' );
32 if ( $error !== true ) {
33 echo $error;
34 exit( 1 );
35 } else {
36 exit( 0 );
37 }
38
39 //-----------------------------------------------------------------------------
40 class SqlPromptPrinter {
41 function __construct( $prompt ) {
42 $this->prompt = $prompt;
43 }
44
45 function cb() {
46 return array( $this, 'printPrompt' );
47 }
48
49 function printPrompt() {
50 echo $this->prompt;
51 }
52 }
53
54 function sqlPrintResult( $res ) {
55 if ( !$res ) {
56 // Do nothing
57 } elseif ( $res->numRows() ) {
58 while ( $row = $res->fetchObject() ) {
59 print_r( $row );
60 }
61 } else {
62 $affected = $res->db->affectedRows();
63 echo "Query OK, $affected row(s) affected\n";
64 }
65 }
66
67 ?>