d61424be3743a988fae501e38f4ef7eb513059ea
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
14 function wfSpecialAsksql() {
15 global $wgUser, $wgOut, $wgRequest, $wgAllowSysopQueries;
17 if( !$wgAllowSysopQueries ) {
18 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
21 if( !$wgUser->isSysop() ) {
22 $wgOut->sysopRequired();
26 if( $wgRequest->wasPosted() ) {
27 $query = $wgRequest->getVal( 'wpSqlQuery' );
28 $action = $wgRequest->getVal( 'action' );
33 $f = new SqlQueryForm( $query);
35 if ( "submit" == $action ) {
48 function SqlQueryForm( $query ) {
49 $this->query
= $query;
52 function showForm( $err ) {
53 global $wgOut, $wgUser, $wgLang;
56 $wgOut->setPagetitle( wfMsg( "asksql" ) );
57 $note = wfMsg( "asksqltext" );
59 $note .= " " . wfMsg( "sqlislogged" );
60 $wgOut->addWikiText( $note );
63 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
65 if ( ! $this->query
) { $this->query
= "SELECT ... FROM ... WHERE ..."; }
66 $q = wfMsg( "sqlquery" );
67 $qb = wfMsg( "querybtn" );
68 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Asksql" );
69 $action = $titleObj->escapeLocalURL( "action=submit" );
72 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
74 <td align=right>{$q}:</td>
76 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
77 . htmlspecialchars($this->query
) ."
81 <td> </td><td align=\"left\">
82 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
89 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
90 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
93 $this->query
= trim( $this->query
);
94 if( preg_match( "/^SELECT/i", $this->query
)
95 and !preg_match( "/LIMIT/i", $this->query
) ) {
96 $this->query
.= " LIMIT 100";
98 $conn = Database
::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
100 $this->logQuery( $this->query
);
102 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
103 $conn->startTimer( $wgSqlTimeout );
104 $res = $conn->query( $this->query
, "SpecialAsksql::doSubmit" );
106 $this->logFinishedQuery();
109 @$n = $conn->numFields( $res );
114 for ( $x = 0; $x < $n; ++
$x ) {
115 array_push( $k, $conn->fieldName( $res, $x ) );
118 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
123 while ( $s = $conn->fetchObject( $res ) ) {
124 array_push( $a, $s );
126 $conn->freeResult( $res );
130 foreach ( $a as $y ) {
131 $sTitle = htmlspecialchars( $y->cur_title
);
132 if ( $y->cur_namespace
) {
133 $sNamespace = $wgLang->getNsText( $y->cur_namespace
);
134 $link = "$sNamespace:$sTitle";
138 $skin = $wgUser->getSkin();
139 $link = $skin->makeLink( $link );
140 $r .= "* [[$link]]<br>\n";
144 $r = "<table border=1 bordercolor=black cellspacing=0 " .
145 "cellpadding=2><tr>\n";
146 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
149 foreach ( $a as $y ) {
151 foreach ( $k as $x ) {
153 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
155 if( $x == "cur_title" ) $namespace = $y->cur_namespace
;
156 if( $x == "old_title" ) $namespace = $y->old_namespace
;
157 if( $x == "rc_title" ) $namespace = $y->rc_namespace
;
158 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
159 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
160 htmlspecialchars( $y->$x ) . "</a>" ;
162 $o = htmlspecialchars( $o );
164 $r .= "<td>" . $o . "</td>\n";
171 $this->showForm( wfMsg( "querysuccessful" ) );
172 $wgOut->addHTML( "<hr>{$r}\n" );
175 function logQuery( $q ) {
176 global $wgSqlLogFile, $wgLogQueries, $wgUser;
177 if(!$wgLogQueries) return;
179 $f = fopen( $wgSqlLogFile, "a" );
180 fputs( $f, "\n\n" . wfTimestampNow() .
181 " query by " . $wgUser->getName() .
184 $this->starttime
= wfTime();
187 function logFinishedQuery() {
188 global $wgSqlLogFile, $wgLogQueries;
189 if(!$wgLogQueries) return;
191 $interval = wfTime() - $this->starttime
;
193 $f = fopen( $wgSqlLogFile, "a" );
194 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );