phpcs: More require/include is not a function
[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 $this->addOption( 'cluster', 'Use an external cluster by name', false, true );
37 }
38
39 public function execute() {
40 // Get a DB handle (with this wiki's DB select) from the appropriate load balancer
41 if ( $this->hasOption( 'cluster' ) ) {
42 $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ) );
43 $dbw = $lb->getConnection( DB_MASTER ); // master for external LB
44 } else {
45 $dbw = wfGetDB( DB_MASTER ); // master for primary LB for this wiki
46 }
47 if ( $this->hasArg( 0 ) ) {
48 $file = fopen( $this->getArg( 0 ), 'r' );
49 if ( !$file ) {
50 $this->error( "Unable to open input file", true );
51 }
52
53 $error = $dbw->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
54 if ( $error !== true ) {
55 $this->error( $error, true );
56 } else {
57 exit( 0 );
58 }
59 }
60
61 $useReadline = function_exists( 'readline_add_history' )
62 && Maintenance::posix_isatty( 0 /*STDIN*/ );
63
64 if ( $useReadline ) {
65 global $IP;
66 $historyFile = isset( $_ENV['HOME'] ) ?
67 "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
68 readline_read_history( $historyFile );
69 }
70
71 $wholeLine = '';
72 $newPrompt = '> ';
73 $prompt = $newPrompt;
74 while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
75 if ( !$line ) {
76 # User simply pressed return key
77 continue;
78 }
79 $done = $dbw->streamStatementEnd( $wholeLine, $line );
80
81 $wholeLine .= $line;
82
83 if ( !$done ) {
84 $wholeLine .= ' ';
85 $prompt = ' -> ';
86 continue;
87 }
88 if ( $useReadline ) {
89 # Delimiter is eated by streamStatementEnd, we add it
90 # up in the history (bug 37020)
91 readline_add_history( $wholeLine . $dbw->getDelimiter() );
92 readline_write_history( $historyFile );
93 }
94 try {
95 $res = $dbw->query( $wholeLine );
96 $this->sqlPrintResult( $res, $dbw );
97 $prompt = $newPrompt;
98 $wholeLine = '';
99 } catch ( DBQueryError $e ) {
100 $doDie = ! Maintenance::posix_isatty( 0 );
101 $this->error( $e, $doDie );
102 }
103 }
104 wfWaitForSlaves();
105 }
106
107 /**
108 * Print the results, callback for $db->sourceStream()
109 * @param $res ResultWrapper The results object
110 * @param $db DatabaseBase object
111 */
112 public function sqlPrintResult( $res, $db ) {
113 if ( !$res ) {
114 // Do nothing
115 return;
116 } elseif ( is_object( $res ) && $res->numRows() ) {
117 foreach ( $res as $row ) {
118 $this->output( print_r( $row, true ) );
119 }
120 } else {
121 $affected = $db->affectedRows();
122 $this->output( "Query OK, $affected row(s) affected\n" );
123 }
124 }
125
126 /**
127 * @return int DB_TYPE constant
128 */
129 public function getDbType() {
130 return Maintenance::DB_ADMIN;
131 }
132 }
133
134 $maintClass = "MwSql";
135 require_once RUN_MAINTENANCE_IF_MAIN;