MediaWiki  1.23.1
sql.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
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  $this->addOption( 'wikidb', 'The database wiki ID to use if not the current one', false, true );
38  $this->addOption( 'slave', 'Use a slave server (either "any" or by name)', false, true );
39  }
40 
41  public function execute() {
42  $wiki = $this->getOption( 'wikidb' ) ?: false;
43  // Get the appropriate load balancer (for this wiki)
44  if ( $this->hasOption( 'cluster' ) ) {
45  $lb = wfGetLBFactory()->getExternalLB( $this->getOption( 'cluster' ), $wiki );
46  } else {
47  $lb = wfGetLB( $wiki );
48  }
49  // Figure out which server to use
50  if ( $this->hasOption( 'slave' ) ) {
51  $server = $this->getOption( 'slave' );
52  if ( $server === 'any' ) {
53  $index = DB_SLAVE;
54  } else {
55  $index = null;
56  for ( $i = 0; $i < $lb->getServerCount(); ++$i ) {
57  if ( $lb->getServerName( $i ) === $server ) {
58  $index = $i;
59  break;
60  }
61  }
62  if ( $index === null ) {
63  $this->error( "No slave server configured with the name '$server'.", 1 );
64  }
65  }
66  } else {
67  $index = DB_MASTER;
68  }
69  // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer
70  $db = $lb->getConnection( $index, array(), $wiki );
71  if ( $this->hasOption( 'slave' ) && $db->getLBInfo( 'master' ) !== null ) {
72  $this->error( "The server selected ({$db->getServer()}) is not a slave.", 1 );
73  }
74 
75  if ( $this->hasArg( 0 ) ) {
76  $file = fopen( $this->getArg( 0 ), 'r' );
77  if ( !$file ) {
78  $this->error( "Unable to open input file", true );
79  }
80 
81  $error = $db->sourceStream( $file, false, array( $this, 'sqlPrintResult' ) );
82  if ( $error !== true ) {
83  $this->error( $error, true );
84  } else {
85  exit( 0 );
86  }
87  }
88 
89  $useReadline = function_exists( 'readline_add_history' )
90  && Maintenance::posix_isatty( 0 /*STDIN*/ );
91 
92  if ( $useReadline ) {
93  global $IP;
94  $historyFile = isset( $_ENV['HOME'] ) ?
95  "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
96  readline_read_history( $historyFile );
97  }
98 
99  $wholeLine = '';
100  $newPrompt = '> ';
101  $prompt = $newPrompt;
102  while ( ( $line = Maintenance::readconsole( $prompt ) ) !== false ) {
103  if ( !$line ) {
104  # User simply pressed return key
105  continue;
106  }
107  $done = $db->streamStatementEnd( $wholeLine, $line );
108 
109  $wholeLine .= $line;
110 
111  if ( !$done ) {
112  $wholeLine .= ' ';
113  $prompt = ' -> ';
114  continue;
115  }
116  if ( $useReadline ) {
117  # Delimiter is eated by streamStatementEnd, we add it
118  # up in the history (bug 37020)
119  readline_add_history( $wholeLine . $db->getDelimiter() );
120  readline_write_history( $historyFile );
121  }
122  try {
123  $res = $db->query( $wholeLine );
124  $this->sqlPrintResult( $res, $db );
125  $prompt = $newPrompt;
126  $wholeLine = '';
127  } catch ( DBQueryError $e ) {
128  $doDie = ! Maintenance::posix_isatty( 0 );
129  $this->error( $e, $doDie );
130  }
131  }
132  wfWaitForSlaves();
133  }
134 
140  public function sqlPrintResult( $res, $db ) {
141  if ( !$res ) {
142  // Do nothing
143  return;
144  } elseif ( is_object( $res ) && $res->numRows() ) {
145  foreach ( $res as $row ) {
146  $this->output( print_r( $row, true ) );
147  }
148  } else {
149  $affected = $db->affectedRows();
150  $this->output( "Query OK, $affected row(s) affected\n" );
151  }
152  }
153 
157  public function getDbType() {
158  return Maintenance::DB_ADMIN;
159  }
160 }
161 
162 $maintClass = "MwSql";
163 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MwSql\__construct
__construct()
Default constructor.
Definition: sql.php:33
MwSql\sqlPrintResult
sqlPrintResult( $res, $db)
Print the results, callback for $db->sourceStream()
Definition: sql.php:140
wfGetLB
wfGetLB( $wiki=false)
Get a load balancer object.
Definition: GlobalFunctions.php:3660
Maintenance\readconsole
static readconsole( $prompt='> ')
Prompt the console for input.
Definition: Maintenance.php:1134
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:236
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
DBQueryError
Definition: DatabaseError.php:306
MwSql\execute
execute()
Do the actual work.
Definition: sql.php:41
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MwSql
Maintenance script that sends SQL queries from the specified file to the database.
Definition: sql.php:32
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3795
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:59
$line
$line
Definition: cdb.php:57
Maintenance\posix_isatty
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
Definition: Maintenance.php:1121
MwSql\getDbType
getDbType()
Definition: sql.php:157
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
wfGetLBFactory
& wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3669
$useReadline
if(isset( $options['d'])) $useReadline
Definition: eval.php:57
$maintClass
$maintClass
Definition: sql.php:162
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
$e
if( $useReadline) $e
Definition: eval.php:66
$IP
$IP
Definition: WebStart.php:88
$res
$res
Definition: database.txt:21