r2469 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r2468‎ | r2469 | r2470 >
Date:13:03, 11 February 2004
Author:timstarling
Status:old
Tags:
Comment:
Specia:Asksql security fix, Special:Asksql query timeout feature
Modified paths:
  • /trunk/phase3/includes/Database.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/SpecialAsksql.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialAsksql.php
@@ -60,7 +60,7 @@
6161 {
6262 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
6363 global $wpSqlQuery;
64 - global $wgDBsqluser, $wgDBsqlpassword;
 64+ global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
6565
6666 # Use a limit, folks!
6767 $wpSqlQuery = trim( $wpSqlQuery );
@@ -68,19 +68,24 @@
6969 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
7070 $wpSqlQuery .= " LIMIT 100";
7171 }
72 - $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
 72+ $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
 73+
7374 $this->logQuery( $wpSqlQuery );
74 - $res = wfQuery( $wpSqlQuery, DB_WRITE, "SpecialAsksql::doSubmit" );
 75+
 76+ # Start timer, will kill the DB thread in $wgSqlTimeout seconds
 77+ $conn->startTimer( $wgSqlTimeout );
 78+ $res = $conn->query( $wpSqlQuery, "SpecialAsksql::doSubmit" );
 79+ $conn->stopTimer();
7580 $this->logFinishedQuery();
7681
7782 $n = 0;
78 - @$n = wfNumFields( $res );
 83+ @$n = $conn->numFields( $res );
7984 $titleList = false;
8085
8186 if ( $n ) {
8287 $k = array();
8388 for ( $x = 0; $x < $n; ++$x ) {
84 - array_push( $k, wfFieldName( $res, $x ) );
 89+ array_push( $k, $conn->fieldName( $res, $x ) );
8590 }
8691
8792 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
@@ -88,10 +93,10 @@
8994 }
9095
9196 $a = array();
92 - while ( $s = wfFetchObject( $res ) ) {
 97+ while ( $s = $conn->fetchObject( $res ) ) {
9398 array_push( $a, $s );
9499 }
95 - wfFreeResult( $res );
 100+ $conn->freeResult( $res );
96101
97102 if ( $titleList ) {
98103 $r = "";
Index: trunk/phase3/includes/DefaultSettings.php
@@ -40,6 +40,7 @@
4141 $wgDBminWordLen = 4;
4242 $wgDBtransactions = false; # Set to true if using InnoDB tables
4343 $wgDBmysql4 = false; # Set to true to use enhanced fulltext search
 44+$wgSqlTimeout = 30;
4445
4546 # Database load balancer
4647 $wgDBservers = false; # e.g. array("larousse", "pliny")
Index: trunk/phase3/includes/OutputPage.php
@@ -465,7 +465,7 @@
466466 $this->returnToMain();
467467 }
468468
469 - function databaseError( $fname )
 469+ function databaseError( $fname, &$conn )
470470 {
471471 global $wgUser, $wgCommandLineMode;
472472
@@ -479,10 +479,10 @@
480480 $msg = wfMsgNoDB( "dberrortext" );
481481 }
482482
483 - $msg = str_replace( "$1", htmlspecialchars( wfLastDBquery() ), $msg );
 483+ $msg = str_replace( "$1", htmlspecialchars( $conn->lastQuery() ), $msg );
484484 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
485 - $msg = str_replace( "$3", wfLastErrno(), $msg );
486 - $msg = str_replace( "$4", htmlspecialchars( wfLastError() ), $msg );
 485+ $msg = str_replace( "$3", $conn->lastErrno(), $msg );
 486+ $msg = str_replace( "$4", htmlspecialchars( $conn->lastError() ), $msg );
487487
488488 if ( $wgCommandLineMode || !is_object( $wgUser )) {
489489 print "$msg\n";
Index: trunk/phase3/includes/Database.php
@@ -173,7 +173,8 @@
174174 } else {
175175 wfDebug("SQL ERROR: " . mysql_error( $this->mConn ) . "\n");
176176 if ( $this->mOut ) {
177 - $this->mOut->databaseError( $fname ); // calls wfAbruptExit()
 177+ // this calls wfAbruptExit()
 178+ $this->mOut->databaseError( $fname, $this );
178179 }
179180 }
180181 }
@@ -385,6 +386,21 @@
386387 $this->mDatabase = $db;
387388 mysql_select_db( $db, $this->mConn );
388389 }
 390+
 391+ function startTimer( $timeout )
 392+ {
 393+ $thisdir = dirname( getenv( "SCRIPT_FILENAME" ) );
 394+ $tid = mysql_thread_id( $this->mConn );
 395+ $this->mTimerProc = popen( "php $thisdir/killthread.php $timeout $tid &", "w" );
 396+ }
 397+
 398+ function stopTimer()
 399+ {
 400+ if ( $this->mTimerProc ) {
 401+ pclose( $this->mTimerProc );
 402+ }
 403+ }
 404+
389405 }
390406
391407 #------------------------------------------------------------------------------

Status & tagging log