moving define(mediawiki, true) from index.php to includes/Defines.php
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
index 8b8d4ac..63fefb1 100644 (file)
@@ -1,27 +1,50 @@
-<?
+<?php
+#
+# If enabled through $wgAllowSysopQueries = true, this class
+# let users with sysop right the possibility to make sql queries
+# against the cur table.
+# Heavy queries could slow down the database specially for the
+# biggest wikis.
 
 function wfSpecialAsksql()
 {
-       global $wgUser, $wgOut, $action;
+       global $wgUser, $wgOut, $wgRequest, $wgAllowSysopQueries;
 
-       if ( ! $wgUser->isSysop() ) {
+       if( !$wgAllowSysopQueries ) {
+               $wgOut->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( "" );
+       }
 }
 
 class SqlQueryForm {
-
+       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 +56,11 @@ class SqlQueryForm {
                if ( "" != $err ) {
                        $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\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( "<p>
 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
@@ -45,7 +68,7 @@ class SqlQueryForm {
 <td align=right>{$q}:</td>
 <td align=left>
 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
-. htmlspecialchars($wpSqlQuery) ."
+. htmlspecialchars($this->query) ."
 </textarea>
 </td>
 </tr><tr>
@@ -59,22 +82,21 @@ class SqlQueryForm {
        function doSubmit()
        {
                global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
-               global $wpSqlQuery;
                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";
                }
                $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
 
-               $this->logQuery( $wpSqlQuery );
+               $this->logQuery( $this->query );
 
                # Start timer, will kill the DB thread in $wgSqlTimeout seconds
                $conn->startTimer( $wgSqlTimeout );
-               $res = $conn->query( $wpSqlQuery, "SpecialAsksql::doSubmit" );
+               $res = $conn->query( $this->query, "SpecialAsksql::doSubmit" );
                $conn->stopTimer();
                $this->logFinishedQuery();
 
@@ -101,8 +123,6 @@ class SqlQueryForm {
                        if ( $titleList ) {
                                $r = "";
                                foreach ( $a as $y ) {
-                                       $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
-                                         htmlspecialchars( $y->$x ) . "</a>" ;
                                        $sTitle = htmlspecialchars( $y->cur_title );
                                        if ( $y->cur_namespace ) {
                                                $sNamespace = $wgLang->getNsText( $y->cur_namespace );
@@ -156,16 +176,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" );