* @ingroup Database Maintenance
*/
-require_once( dirname(__FILE__) . '/' . 'commandLine.inc' );
+require_once( "Maintenance.php" );
-if ( isset( $options['help'] ) ) {
- echo "Send SQL queries to a MediaWiki database.\nUsage: php sql.php [<file>]\n";
- exit( 1 );
-}
+class MwSql extends Maintenance {
+ public function __construct() {
+ parent::__construct();
+ $this->mDescription = "Send SQL queries to a MediaWiki database";
+ }
-if ( isset( $args[0] ) ) {
- $fileName = $args[0];
- $file = fopen( $fileName, 'r' );
- $promptCallback = false;
-} else {
- $file = STDIN;
- $promptObject = new SqlPromptPrinter( "> " );
- $promptCallback = $promptObject->cb();
-}
+ public function execute() {
+ if ( $this->hasArg() ) {
+ $fileName = $this->getArg();
+ $file = fopen( $fileName, 'r' );
+ $promptCallback = false;
+ } else {
+ $file = $this->getStdin();
+ $promptObject = new SqlPromptPrinter( "> " );
+ $promptCallback = $promptObject->cb();
+ }
+
+ if ( !$file )
+ $this->error( "Unable to open input file\n", true );
-if ( !$file ) {
- echo "Unable to open input file\n";
- exit( 1 );
-}
+ $dbw = wfGetDB( DB_MASTER );
+ $error = $dbw->sourceStream( $file, $promptCallback, array( $this, 'sqlPrintResult' ) );
+ if ( $error !== true ) {
+ $this->error( $error, true );
+ } else {
+ exit( 0 );
+ }
+ }
-$dbw =& wfGetDB( DB_MASTER );
-$error = $dbw->sourceStream( $file, $promptCallback, 'sqlPrintResult' );
-if ( $error !== true ) {
- echo $error;
- exit( 1 );
-} else {
- exit( 0 );
+ /**
+ * Print the results, callback for $db->sourceStream()
+ * @param $res The results object
+ * @param $db Database object
+ */
+ public function sqlPrintResult( $res, $db ) {
+ if ( !$res ) {
+ // Do nothing
+ } elseif ( is_object( $res ) && $res->numRows() ) {
+ while ( $row = $res->fetchObject() ) {
+ $this->output( print_r( $row, true ) );
+ }
+ } else {
+ $affected = $db->affectedRows();
+ $this->output( "Query OK, $affected row(s) affected\n" );
+ }
+ }
}
-//-----------------------------------------------------------------------------
class SqlPromptPrinter {
function __construct( $prompt ) {
$this->prompt = $prompt;
}
}
-function sqlPrintResult( $res, $db ) {
- if ( !$res ) {
- // Do nothing
- } elseif ( is_object( $res ) && $res->numRows() ) {
- while ( $row = $res->fetchObject() ) {
- print_r( $row );
- }
- } else {
- $affected = $db->affectedRows();
- echo "Query OK, $affected row(s) affected\n";
- }
-}
-
-
+$maintClass = "MwSql";
+require_once( DO_MAINTENANCE );