3 # If enabled through $wgAllowSysopQueries = true, this class
4 # let users with sysop right the possibility to make sql queries
5 # against the cur table.
6 # Heavy queries could slow down the database specially for the
9 function wfSpecialAsksql()
11 global $wgUser, $wgOut, $wgRequest, $wgAllowSysopQueries;
13 if( !$wgAllowSysopQueries ) {
14 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
17 if( !$wgUser->isSysop() ) {
18 $wgOut->sysopRequired();
22 if( $wgRequest->wasPosted() ) {
23 $query = $wgRequest->getVal( 'wpSqlQuery' );
24 $action = $wgRequest->getVal( 'action' );
29 $f = new SqlQueryForm( $query);
31 if ( "submit" == $action ) {
41 function SqlQueryForm( $query ) {
42 $this->query
= $query;
45 function showForm( $err )
47 global $wgOut, $wgUser, $wgLang;
50 $wgOut->setPagetitle( wfMsg( "asksql" ) );
51 $note = wfMsg( "asksqltext" );
53 $note .= " " . wfMsg( "sqlislogged" );
54 $wgOut->addWikiText( $note );
57 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
59 if ( ! $this->query
) { $this->query
= "SELECT ... FROM ... WHERE ..."; }
60 $q = wfMsg( "sqlquery" );
61 $qb = wfMsg( "querybtn" );
62 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Asksql" );
63 $action = $titleObj->escapeLocalURL( "action=submit" );
66 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
68 <td align=right>{$q}:</td>
70 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
71 . htmlspecialchars($this->query
) ."
75 <td> </td><td align=\"left\">
76 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
84 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
85 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
88 $this->query
= trim( $this->query
);
89 if( preg_match( "/^SELECT/i", $this->query
)
90 and !preg_match( "/LIMIT/i", $this->query
) ) {
91 $this->query
.= " LIMIT 100";
93 $conn = Database
::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
95 $this->logQuery( $this->query
);
97 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
98 $conn->startTimer( $wgSqlTimeout );
99 $res = $conn->query( $this->query
, "SpecialAsksql::doSubmit" );
101 $this->logFinishedQuery();
104 @$n = $conn->numFields( $res );
109 for ( $x = 0; $x < $n; ++
$x ) {
110 array_push( $k, $conn->fieldName( $res, $x ) );
113 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
118 while ( $s = $conn->fetchObject( $res ) ) {
119 array_push( $a, $s );
121 $conn->freeResult( $res );
125 foreach ( $a as $y ) {
126 $sTitle = htmlspecialchars( $y->cur_title
);
127 if ( $y->cur_namespace
) {
128 $sNamespace = $wgLang->getNsText( $y->cur_namespace
);
129 $link = "$sNamespace:$sTitle";
133 $skin = $wgUser->getSkin();
134 $link = $skin->makeLink( $link );
135 $r .= "* [[$link]]<br>\n";
139 $r = "<table border=1 bordercolor=black cellspacing=0 " .
140 "cellpadding=2><tr>\n";
141 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
144 foreach ( $a as $y ) {
146 foreach ( $k as $x ) {
148 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
150 if( $x == "cur_title" ) $namespace = $y->cur_namespace
;
151 if( $x == "old_title" ) $namespace = $y->old_namespace
;
152 if( $x == "rc_title" ) $namespace = $y->rc_namespace
;
153 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
154 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
155 htmlspecialchars( $y->$x ) . "</a>" ;
157 $o = htmlspecialchars( $o );
159 $r .= "<td>" . $o . "</td>\n";
166 $this->showForm( wfMsg( "querysuccessful" ) );
167 $wgOut->addHTML( "<hr>{$r}\n" );
170 function logQuery( $q ) {
171 global $wgSqlLogFile, $wgLogQueries, $wgUser;
172 if(!$wgLogQueries) return;
174 $f = fopen( $wgSqlLogFile, "a" );
175 fputs( $f, "\n\n" . wfTimestampNow() .
176 " query by " . $wgUser->getName() .
179 $this->starttime
= wfTime();
182 function logFinishedQuery() {
183 global $wgSqlLogFile, $wgLogQueries;
184 if(!$wgLogQueries) return;
186 $interval = wfTime() - $this->starttime
;
188 $f = fopen( $wgSqlLogFile, "a" );
189 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );