X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FSpecialAsksql.php;h=6096623e27ca74ec39897984fc530da1772c91c6;hb=60c94987aab3141a19d735fbc003d5cb646a8502;hp=062e1c1a28168a7e73c3be19de1e995c0f23fda5;hpb=ba780a4674c0d6f7d1bdcc6fbe7ff3dc91298248;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/SpecialAsksql.php b/includes/SpecialAsksql.php index 062e1c1a28..6096623e27 100644 --- a/includes/SpecialAsksql.php +++ b/includes/SpecialAsksql.php @@ -1,27 +1,60 @@ -isSysop() ) { +errorpage( "nosuchspecialpage", "nospecialpagetext" ); + return; + } + if( !$wgUser->isSysop() ) { $wgOut->sysopRequired(); return; } - $fields = array( "wpSqlQuery" ); - wfCleanFormFields( $fields ); - $f = new SqlQueryForm(); + + if( $wgRequest->wasPosted() ) { + $query = $wgRequest->getVal( 'wpSqlQuery' ); + $action = $wgRequest->getVal( 'action' ); + } else { + $query = ""; + $action = ""; + } + $f = new SqlQueryForm( $query); - if ( "submit" == $action ) { $f->doSubmit(); } - else { $f->showForm( "" ); } + if ( "submit" == $action ) { + $f->doSubmit(); + } else { + $f->showForm( '' ); + } } +/** + * @access private + * @package MediaWiki + * @subpackage SpecialPage + */ class SqlQueryForm { - - function showForm( $err ) - { + var $query = ''; + + function SqlQueryForm( $query ) { + $this->query = $query; + } + + function showForm( $err ) { global $wgOut, $wgUser, $wgLang; - global $wpSqlQuery; global $wgLogQueries; $wgOut->setPagetitle( wfMsg( "asksql" ) ); @@ -33,11 +66,11 @@ class SqlQueryForm { if ( "" != $err ) { $wgOut->addHTML( "

" . htmlspecialchars($err) . "\n" ); } - if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; } + if ( ! $this->query ) { $this->query = "SELECT ... FROM ... WHERE ..."; } $q = wfMsg( "sqlquery" ); $qb = wfMsg( "querybtn" ); - $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ), - "action=submit" ); + $titleObj = Title::makeTitle( NS_SPECIAL, "Asksql" ); + $action = $titleObj->escapeLocalURL( "action=submit" ); $wgOut->addHTML( "

@@ -45,7 +78,7 @@ class SqlQueryForm { {$q}: @@ -56,63 +89,88 @@ class SqlQueryForm { } - function doSubmit() - { + function doSubmit() { global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang; - global $wpSqlQuery; - global $wgDBsqluser, $wgDBsqlpassword; + global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout; # Use a limit, folks! - $wpSqlQuery = trim( $wpSqlQuery ); - if( preg_match( "/^SELECT/i", $wpSqlQuery ) - and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) { - $wpSqlQuery .= " LIMIT 100"; + $this->query = trim( $this->query ); + if( preg_match( "/^SELECT/i", $this->query ) + and !preg_match( "/LIMIT/i", $this->query ) ) { + $this->query .= " LIMIT 100"; } - if ( ! $wgUser->isDeveloper() ) { - $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword ); - } - $this->logQuery( $wpSqlQuery ); - $res = wfQuery( $wpSqlQuery, "SpecialAsksql::doSubmit" ); + $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname ); + + $this->logQuery( $this->query ); + + # Start timer, will kill the DB thread in $wgSqlTimeout seconds + $conn->startTimer( $wgSqlTimeout ); + $res = $conn->query( $this->query, "SpecialAsksql::doSubmit" ); + $conn->stopTimer(); $this->logFinishedQuery(); $n = 0; - @$n = wfNumFields( $res ); + @$n = $conn->numFields( $res ); + $titleList = false; + if ( $n ) { $k = array(); for ( $x = 0; $x < $n; ++$x ) { - array_push( $k, wfFieldName( $res, $x ) ); + array_push( $k, $conn->fieldName( $res, $x ) ); + } + + if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) { + $titleList = true; } + $a = array(); - while ( $s = wfFetchObject( $res ) ) { + while ( $s = $conn->fetchObject( $res ) ) { array_push( $a, $s ); } - wfFreeResult( $res ); - - $r = "\n"; - foreach ( $k as $x ) $r .= ""; - $r .= "\n"; - - foreach ( $a as $y ) { - $r .= ""; - foreach ( $k as $x ) { - $o = $y->$x ; - if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") { - $namespace = 0; - if( $x == "cur_title" ) $namespace = $y->cur_namespace; - if( $x == "old_title" ) $namespace = $y->old_namespace; - if( $x == "rc_title" ) $namespace = $y->rc_namespace; - if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o; - $o = "" . - htmlspecialchars( $y->$x ) . "" ; - } else { - $o = htmlspecialchars( $o ); - } - $r .= "\n"; + $conn->freeResult( $res ); + + if ( $titleList ) { + $r = ""; + foreach ( $a as $y ) { + $sTitle = htmlspecialchars( $y->cur_title ); + if ( $y->cur_namespace ) { + $sNamespace = $wgLang->getNsText( $y->cur_namespace ); + $link = "$sNamespace:$sTitle"; + } else { + $link = "$sTitle"; + } + $skin = $wgUser->getSkin(); + $link = $skin->makeLink( $link ); + $r .= "* [[$link]]
\n"; } + } else { + + $r = "
" . htmlspecialchars( $x ) . "
" . $o . "
\n"; + foreach ( $k as $x ) $r .= ""; $r .= "\n"; + + foreach ( $a as $y ) { + $r .= ""; + foreach ( $k as $x ) { + $o = $y->$x ; + if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") { + $namespace = 0; + if( $x == "cur_title" ) $namespace = $y->cur_namespace; + if( $x == "old_title" ) $namespace = $y->old_namespace; + if( $x == "rc_title" ) $namespace = $y->rc_namespace; + if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o; + $o = "" . + htmlspecialchars( $y->$x ) . "" ; + } else { + $o = htmlspecialchars( $o ); + } + $r .= "\n"; + } + $r .= "\n"; + } + $r .= "
" . htmlspecialchars( $x ) . "
" . $o . "
\n"; } - $r .= "\n"; } $this->showForm( wfMsg( "querysuccessful" ) ); $wgOut->addHTML( "
{$r}\n" ); @@ -127,16 +185,14 @@ class SqlQueryForm { " query by " . $wgUser->getName() . ":\n$q\n" ); fclose( $f ); - $this->starttime = microtime(); + $this->starttime = wfTime(); } function logFinishedQuery() { global $wgSqlLogFile, $wgLogQueries; if(!$wgLogQueries) return; - list($sec, $usec) = explode( " ", microtime() ); - list($sec1, $usec1) = explode( " ", $this->starttime ); - $interval = ($sec + $usec) - ($sec1 + $usec1); + $interval = wfTime() - $this->starttime; $f = fopen( $wgSqlLogFile, "a" ); fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );