3 function wfSpecialAsksql()
5 global $wgUser, $wgOut, $wgRequest, $wgAllowSysopQueries;
7 if( !$wgAllowSysopQueries ) {
8 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
11 if( !$wgUser->isSysop() ) {
12 $wgOut->sysopRequired();
16 if( $wgRequest->wasPosted() ) {
17 $query = $wgRequest->getVal( 'wpSqlQuery' );
18 $action = $wgRequest->getVal( 'action' );
23 $f = new SqlQueryForm( $query);
25 if ( "submit" == $action ) {
35 function SqlQueryForm( $query ) {
36 $this->query
= $query;
39 function showForm( $err )
41 global $wgOut, $wgUser, $wgLang;
44 $wgOut->setPagetitle( wfMsg( "asksql" ) );
45 $note = wfMsg( "asksqltext" );
47 $note .= " " . wfMsg( "sqlislogged" );
48 $wgOut->addWikiText( $note );
51 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
53 if ( ! $this->query
) { $this->query
= "SELECT ... FROM ... WHERE ..."; }
54 $q = wfMsg( "sqlquery" );
55 $qb = wfMsg( "querybtn" );
56 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Asksql" );
57 $action = $titleObj->escapeLocalURL( "action=submit" );
60 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
62 <td align=right>{$q}:</td>
64 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
65 . htmlspecialchars($this->query
) ."
69 <td> </td><td align=\"left\">
70 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
78 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
79 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
82 $this->query
= trim( $this->query
);
83 if( preg_match( "/^SELECT/i", $this->query
)
84 and !preg_match( "/LIMIT/i", $this->query
) ) {
85 $this->query
.= " LIMIT 100";
87 $conn = Database
::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
89 $this->logQuery( $this->query
);
91 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
92 $conn->startTimer( $wgSqlTimeout );
93 $res = $conn->query( $this->query
, "SpecialAsksql::doSubmit" );
95 $this->logFinishedQuery();
98 @$n = $conn->numFields( $res );
103 for ( $x = 0; $x < $n; ++
$x ) {
104 array_push( $k, $conn->fieldName( $res, $x ) );
107 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
112 while ( $s = $conn->fetchObject( $res ) ) {
113 array_push( $a, $s );
115 $conn->freeResult( $res );
119 foreach ( $a as $y ) {
120 $sTitle = htmlspecialchars( $y->cur_title
);
121 if ( $y->cur_namespace
) {
122 $sNamespace = $wgLang->getNsText( $y->cur_namespace
);
123 $link = "$sNamespace:$sTitle";
127 $skin = $wgUser->getSkin();
128 $link = $skin->makeLink( $link );
129 $r .= "* [[$link]]<br>\n";
133 $r = "<table border=1 bordercolor=black cellspacing=0 " .
134 "cellpadding=2><tr>\n";
135 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
138 foreach ( $a as $y ) {
140 foreach ( $k as $x ) {
142 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
144 if( $x == "cur_title" ) $namespace = $y->cur_namespace
;
145 if( $x == "old_title" ) $namespace = $y->old_namespace
;
146 if( $x == "rc_title" ) $namespace = $y->rc_namespace
;
147 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
148 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
149 htmlspecialchars( $y->$x ) . "</a>" ;
151 $o = htmlspecialchars( $o );
153 $r .= "<td>" . $o . "</td>\n";
160 $this->showForm( wfMsg( "querysuccessful" ) );
161 $wgOut->addHTML( "<hr>{$r}\n" );
164 function logQuery( $q ) {
165 global $wgSqlLogFile, $wgLogQueries, $wgUser;
166 if(!$wgLogQueries) return;
168 $f = fopen( $wgSqlLogFile, "a" );
169 fputs( $f, "\n\n" . wfTimestampNow() .
170 " query by " . $wgUser->getName() .
173 $this->starttime
= wfTime();
176 function logFinishedQuery() {
177 global $wgSqlLogFile, $wgLogQueries;
178 if(!$wgLogQueries) return;
180 $interval = wfTime() - $this->starttime
;
182 $f = fopen( $wgSqlLogFile, "a" );
183 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );