r108774 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108773‎ | r108774 | r108775 >
Date:22:57, 12 January 2012
Author:reedy
Status:resolved (Comments)
Tags:core 
Comment:
Give sql.php eval.php type scrollback
Modified paths:
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseMysql.php (modified) (history)
  • /trunk/phase3/maintenance/sql.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/sql.php
@@ -33,29 +33,52 @@
3434 if ( $this->hasArg() ) {
3535 $fileName = $this->getArg();
3636 $file = fopen( $fileName, 'r' );
37 - $promptCallback = false;
3837 } else {
3938 $file = $this->getStdin();
40 - $promptObject = new SqlPromptPrinter( "> " );
41 - $promptCallback = $promptObject->cb();
4239 }
4340
44 - if ( !$file )
 41+ if ( !$file ) {
4542 $this->error( "Unable to open input file", true );
 43+ }
4644
 45+ $useReadline = function_exists( 'readline_add_history' )
 46+ && Maintenance::posix_isatty( 0 /*STDIN*/ );
 47+
 48+ if ( $useReadline ) {
 49+ global $IP;
 50+ $historyFile = isset( $_ENV['HOME'] ) ?
 51+ "{$_ENV['HOME']}/.mwsql_history" : "$IP/maintenance/.mwsql_history";
 52+ readline_read_history( $historyFile );
 53+ }
 54+
4755 $dbw = wfGetDB( DB_MASTER );
48 - $error = $dbw->sourceStream( $file, $promptCallback, array( $this, 'sqlPrintResult' ) );
49 - if ( $error !== true ) {
50 - $this->error( $error, true );
51 - } else {
52 - exit( 0 );
 56+ $wholeLine = '';
 57+ while ( ( $line = Maintenance::readconsole() ) !== false ) {
 58+ $done = $dbw->streamStatementEnd( $wholeLine, $line );
 59+
 60+ $wholeLine .= $line;
 61+
 62+ if ( !$done ) {
 63+ continue;
 64+ }
 65+ if ( $useReadline ) {
 66+ readline_add_history( $wholeLine );
 67+ readline_write_history( $historyFile );
 68+ }
 69+ try{
 70+ $res = $dbw->query( $wholeLine );
 71+ $this->sqlPrintResult( $res, $dbw );
 72+ $wholeLine = '';
 73+ } catch (DBQueryError $e) {
 74+ $this->error( $e, true );
 75+ }
5376 }
5477 }
5578
5679 /**
5780 * Print the results, callback for $db->sourceStream()
58 - * @param $res The results object
59 - * @param $db Database object
 81+ * @param $res ResultWrapper The results object
 82+ * @param $db DatabaseBase object
6083 */
6184 public function sqlPrintResult( $res, $db ) {
6285 if ( !$res ) {
@@ -70,24 +93,13 @@
7194 }
7295 }
7396
 97+ /**
 98+ * @return int DB_TYPE constant
 99+ */
74100 public function getDbType() {
75101 return Maintenance::DB_ADMIN;
76102 }
77103 }
78104
79 -class SqlPromptPrinter {
80 - function __construct( $prompt ) {
81 - $this->prompt = $prompt;
82 - }
83 -
84 - function cb() {
85 - return array( $this, 'printPrompt' );
86 - }
87 -
88 - function printPrompt() {
89 - echo $this->prompt;
90 - }
91 -}
92 -
93105 $maintClass = "MwSql";
94106 require_once( RUN_MAINTENANCE_IF_MAIN );
Index: trunk/phase3/includes/db/DatabaseMysql.php
@@ -670,7 +670,7 @@
671671 }
672672 }
673673
674 - protected function streamStatementEnd( &$sql, &$newLine ) {
 674+ public function streamStatementEnd( &$sql, &$newLine ) {
675675 if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
676676 preg_match( '/^DELIMITER\s+(\S+)/' , $newLine, $m );
677677 $this->delimiter = $m[1];
Index: trunk/phase3/includes/db/Database.php
@@ -846,7 +846,6 @@
847847 $sqlx = substr( $commentedSql, 0, 500 );
848848 $sqlx = strtr( $sqlx, "\t\n", ' ' );
849849
850 -
851850 $master = $isMaster ? 'master' : 'slave';
852851 wfDebug( "Query {$this->mDBname} ($cnt) ($master): $sqlx\n" );
853852 }
@@ -3154,7 +3153,7 @@
31553154 * @param $inputCallback Callback: Optional function called for each complete line (ended with ;) sent
31563155 * @return bool|string
31573156 */
3158 - function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
 3157+ public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
31593158 $fname = 'DatabaseBase::sourceStream', $inputCallback = false )
31603159 {
31613160 $cmd = '';
@@ -3208,11 +3207,11 @@
32093208 /**
32103209 * Called by sourceStream() to check if we've reached a statement end
32113210 *
3212 - * @param $sql String: SQL assembled so far
3213 - * @param $newLine String: New line about to be added to $sql
3214 - * @returns Bool: Whether $newLine contains end of the statement
 3211+ * @param $sql String SQL assembled so far
 3212+ * @param $newLine String New line about to be added to $sql
 3213+ * @return Bool Whether $newLine contains end of the statement
32153214 */
3216 - protected function streamStatementEnd( &$sql, &$newLine ) {
 3215+ public function streamStatementEnd( &$sql, &$newLine ) {
32173216 if ( $this->delimiter ) {
32183217 $prev = $newLine;
32193218 $newLine = preg_replace( '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );

Follow-up revisions

RevisionCommit summaryAuthorDate
r108779Followup to r108744overlordq23:22, 12 January 2012
r109143Fixed reading from file from r108774reedy13:29, 17 January 2012

Comments

#Comment by Hashar (talk | contribs)   23:28, 12 January 2012

We said no new feature! This is breaking PostgreSQL http://integration.mediawiki.org/ci/job/MediaWiki-postgres-phpunit/33/

http://integration.mediawiki.org/ci/job/MediaWiki-postgres-phpunit/33/console :

The CLI installer says:

[exec] Setting up database
[exec] PHP Fatal error:  Access level to DatabasePostgres::streamStatementEnd() must be public (as in class DatabaseBase) in /var/lib/jenkins/jobs/MediaWiki-postgres-phpunit/workspace/mw-core/includes/db/DatabasePostgres.php on line 1061
#Comment by Hashar (talk | contribs)   23:31, 12 January 2012

Well was fixed by a follow up :b

#Comment by 😂 (talk | contribs)   10:06, 17 January 2012

You're opening $file and then never using it again in favor of the prompt.

#Comment by Aaron Schulz (talk | contribs)   20:04, 18 January 2012

Was this fixed?

#Comment by Reedy (talk | contribs)   20:06, 18 January 2012

Status & tagging log