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