isSysop() ) { $wgOut->sysopRequired(); return; } $fields = array( "wpSqlQuery" ); wfCleanFormFields( $fields ); $f = new SqlQueryForm(); if ( "submit" == $action ) { $f->doSubmit(); } else { $f->showForm( "" ); } } class SqlQueryForm { function showForm( $err ) { global $wgOut, $wgUser, $wgLang; global $wpSqlQuery; global $wgLogQueries; $wgOut->setPagetitle( wfMsg( "asksql" ) ); $note = wfMsg( "asksqltext" ); if($wgLogQueries) $note .= " " . wfMsg( "sqlislogged" ); $wgOut->addWikiText( $note ); if ( "" != $err ) { $wgOut->addHTML( "

" . htmlspecialchars($err) . "\n" ); } if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; } $q = wfMsg( "sqlquery" ); $qb = wfMsg( "querybtn" ); $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ), "action=submit" ); $wgOut->addHTML( "

{$q}:
 
\n" ); } function doSubmit() { global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang; global $wpSqlQuery; global $wgDBsqluser, $wgDBsqlpassword; # Use a limit, folks! $wpSqlQuery = trim( $wpSqlQuery ); if( preg_match( "/^SELECT/i", $wpSqlQuery ) and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) { $wpSqlQuery .= " LIMIT 100"; } if ( ! $wgUser->isDeveloper() ) { $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword ); } $this->logQuery( $wpSqlQuery ); $res = wfQuery( $wpSqlQuery, DB_WRITE, "SpecialAsksql::doSubmit" ); $this->logFinishedQuery(); $n = 0; @$n = wfNumFields( $res ); $titleList = false; if ( $n ) { $k = array(); for ( $x = 0; $x < $n; ++$x ) { array_push( $k, wfFieldName( $res, $x ) ); } if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) { $titleList = true; } $a = array(); while ( $s = wfFetchObject( $res ) ) { array_push( $a, $s ); } wfFreeResult( $res ); if ( $titleList ) { $r = ""; foreach ( $a as $y ) { $o = "" . htmlspecialchars( $y->$x ) . "" ; $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 = "\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"; } } $this->showForm( wfMsg( "querysuccessful" ) ); $wgOut->addHTML( "
{$r}\n" ); } function logQuery( $q ) { global $wgSqlLogFile, $wgLogQueries, $wgUser; if(!$wgLogQueries) return; $f = fopen( $wgSqlLogFile, "a" ); fputs( $f, "\n\n" . wfTimestampNow() . " query by " . $wgUser->getName() . ":\n$q\n" ); fclose( $f ); $this->starttime = microtime(); } function logFinishedQuery() { global $wgSqlLogFile, $wgLogQueries; if(!$wgLogQueries) return; list($sec, $usec) = explode( " ", microtime() ); list($sec1, $usec1) = explode( " ", $this->starttime ); $interval = ($sec + $usec) - ($sec1 + $usec1); $f = fopen( $wgSqlLogFile, "a" ); fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" ); fclose( $f ); } } ?>