1dbbe550acfdda47e246cf980ac3f11f7e5076f3
3 * Send SQL queries from the specified file to the database, performing
4 * variable replacement along the way.
7 * @ingroup Database Maintenance
10 require_once( "Maintenance.php" );
12 class MwSql
extends Maintenance
{
13 public function __construct() {
14 parent
::__construct();
15 $this->mDescription
= "Send SQL queries to a MediaWiki database";
18 public function execute() {
19 if ( $this->hasArg() ) {
20 $fileName = $this->getArg();
21 $file = fopen( $fileName, 'r' );
22 $promptCallback = false;
24 $file = $this->getStdin();
25 $promptObject = new SqlPromptPrinter( "> " );
26 $promptCallback = $promptObject->cb();
30 $this->error( "Unable to open input file\n", true );
32 $dbw = wfGetDB( DB_MASTER
);
33 $error = $dbw->sourceStream( $file, $promptCallback, array( $this, 'sqlPrintResult' ) );
34 if ( $error !== true ) {
35 $this->error( $error, true );
42 * Print the results, callback for $db->sourceStream()
43 * @param $res The results object
44 * @param $db Database object
46 public function sqlPrintResult( $res, $db ) {
49 } elseif ( is_object( $res ) && $res->numRows() ) {
50 while ( $row = $res->fetchObject() ) {
51 $this->output( print_r( $row, true ) );
54 $affected = $db->affectedRows();
55 $this->output( "Query OK, $affected row(s) affected\n" );
60 class SqlPromptPrinter
{
61 function __construct( $prompt ) {
62 $this->prompt
= $prompt;
66 return array( $this, 'printPrompt' );
69 function printPrompt() {
74 $maintClass = "MwSql";
75 require_once( DO_MAINTENANCE
);