Improve documentation of maintenance scripts.
[lhc/web/wiklou.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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 */
23
24 require_once( __DIR__ . '/Maintenance.php' );
25
26 /**
27 * Maintenance script that sends SQL queries from the specified file to the database.
28 *
29 * @ingroup Maintenance
30 */
31 class MwSql extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Send SQL queries to a MediaWiki database";
35 }
36
37 public function execute() {
38 $dbw = wfGetDB( DB_MASTER );
39 if ( $this->hasArg() ) {
40 $fileName = $this->getArg();
41 $file = fopen( $fileName, 'r' );
42 if ( !$file ) {
43 $this->error( "Unable to open input file", true );
44 }
45
46 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
47 if ( $error !== true ) {
48 $this->error( $error, true );
49 } else {
50 exit( 0 );
51 }
52 }
53
54 $useReadline = function_exists( 'readline_add_history' )
55 && Maintenance::posix_isatty( 0 /*STDIN*/ );
56
57 if ( $useReadline ) {
58 global $IP;
59 $historyFile = isset( $_ENV['HOME'] ) ?
60 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
61 readline_read_history( $historyFile );
62 }
63
64 $wholeLine = '';
65 while ( ( $line = Maintenance::readconsole() ) !== false ) {
66 $done = $dbw->streamStatementEnd( $wholeLine, $line );
67
68 $wholeLine .= $line;
69
70 if ( !$done ) {
71 continue;
72 }
73 if ( $useReadline ) {
74 readline_add_history( $wholeLine );
75 readline_write_history( $historyFile );
76 }
77 try{
78 $res = $dbw->query( $wholeLine );
79 $this->sqlPrintResult( $res, $dbw );
80 $wholeLine = '';
81 } catch (DBQueryError $e) {
82 $this->error( $e, true );
83 }
84 }
85 }
86
87 /**
88 * Print the results, callback for $db->sourceStream()
89 * @param $res ResultWrapper The results object
90 * @param $db DatabaseBase object
91 */
92 public function sqlPrintResult( $res, $db ) {
93 if ( !$res ) {
94 // Do nothing
95 } elseif ( is_object( $res ) && $res->numRows() ) {
96 foreach ( $res as $row ) {
97 $this->output( print_r( $row, true ) );
98 }
99 } else {
100 $affected = $db->affectedRows();
101 $this->output( "Query OK, $affected row(s) affected\n" );
102 }
103 }
104
105 /**
106 * @return int DB_TYPE constant
107 */
108 public function getDbType() {
109 return Maintenance::DB_ADMIN;
110 }
111 }
112
113 $maintClass = "MwSql";
114 require_once( RUN_MAINTENANCE_IF_MAIN );