Merge "tests: rm duplicate code in language classes"
[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 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that sends SQL queries from the specified file to the database.
29 *
30 * @ingroup Maintenance
31 */
32 class MwSql extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Send SQL queries to a MediaWiki database";
36 }
37
38 public function execute() {
39 $dbw = wfGetDB( DB_MASTER );
40 if ( $this->hasArg() ) {
41 $fileName = $this->getArg();
42 $file = fopen( $fileName, 'r' );
43 if ( !$file ) {
44 $this->error( "Unable to open input file", true );
45 }
46
47 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
48 if ( $error !== true ) {
49 $this->error( $error, true );
50 } else {
51 exit( 0 );
52 }
53 }
54
55 $useReadline = function_exists( 'readline_add_history' )
56 && Maintenance::posix_isatty( 0 /*STDIN*/ );
57
58 if ( $useReadline ) {
59 global $IP;
60 $historyFile = isset( $_ENV['HOME'] ) ?
61 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
62 readline_read_history( $historyFile );
63 }
64
65 $wholeLine = '';
66 $newPrompt = '> ';
67 $prompt = $newPrompt;
68 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
69 if( !$line ) {
70 # User simply pressed return key
71 continue;
72 }
73 $done = $dbw->streamStatementEnd( $wholeLine, $line );
74
75 $wholeLine .= $line;
76
77 if ( !$done ) {
78 $wholeLine .= ' ';
79 $prompt = ' -> ';
80 continue;
81 }
82 if ( $useReadline ) {
83 # Delimiter is eated by streamStatementEnd, we add it
84 # up in the history (bug 37020)
85 readline_add_history( $wholeLine . $dbw->getDelimiter() );
86 readline_write_history( $historyFile );
87 }
88 try{
89 $res = $dbw->query( $wholeLine );
90 $this->sqlPrintResult( $res, $dbw );
91 $prompt = $newPrompt;
92 $wholeLine = '';
93 } catch (DBQueryError $e) {
94 $doDie = ! Maintenance::posix_isatty( 0 );
95 $this->error( $e, $doDie );
96 }
97 }
98 }
99
100 /**
101 * Print the results, callback for $db->sourceStream()
102 * @param $res ResultWrapper The results object
103 * @param $db DatabaseBase object
104 */
105 public function sqlPrintResult( $res, $db ) {
106 if ( !$res ) {
107 // Do nothing
108 } elseif ( is_object( $res ) && $res->numRows() ) {
109 foreach ( $res as $row ) {
110 $this->output( print_r( $row, true ) );
111 }
112 } else {
113 $affected = $db->affectedRows();
114 $this->output( "Query OK, $affected row(s) affected\n" );
115 }
116 }
117
118 /**
119 * @return int DB_TYPE constant
120 */
121 public function getDbType() {
122 return Maintenance::DB_ADMIN;
123 }
124 }
125
126 $maintClass = "MwSql";
127 require_once( RUN_MAINTENANCE_IF_MAIN );