Fix early finish problem introduced by dupe message fix
[mediawiki.git] / includes / SpecialAsksql.php
blobb5116efc30d0e2b220f3b27f0a81dbd801a701c2
1 <?php
3 function wfSpecialAsksql()
5 global $wgUser, $wgOut, $action;
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
19 class SqlQueryForm {
21 function showForm( $err )
23 global $wgOut, $wgUser, $wgLang;
24 global $wpSqlQuery;
25 global $wgLogQueries;
27 $wgOut->setPagetitle( wfMsg( "asksql" ) );
28 $note = wfMsg( "asksqltext" );
29 if($wgLogQueries)
30 $note .= " " . wfMsg( "sqlislogged" );
31 $wgOut->addWikiText( $note );
33 if ( "" != $err ) {
34 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
36 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
37 $q = wfMsg( "sqlquery" );
38 $qb = wfMsg( "querybtn" );
39 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
40 "action=submit" );
42 $wgOut->addHTML( "<p>
43 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
44 <table border=0><tr>
45 <td align=right>{$q}:</td>
46 <td align=left>
47 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
48 . htmlspecialchars($wpSqlQuery) ."
49 </textarea>
50 </td>
51 </tr><tr>
52 <td>&nbsp;</td><td align=\"left\">
53 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
54 </td></tr></table>
55 </form>\n" );
59 function doSubmit()
61 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
62 global $wpSqlQuery;
63 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
65 # Use a limit, folks!
66 $wpSqlQuery = trim( $wpSqlQuery );
67 if( preg_match( "/^SELECT/i", $wpSqlQuery )
68 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
69 $wpSqlQuery .= " LIMIT 100";
71 $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
73 $this->logQuery( $wpSqlQuery );
75 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
76 $conn->startTimer( $wgSqlTimeout );
77 $res = $conn->query( $wpSqlQuery, "SpecialAsksql::doSubmit" );
78 $conn->stopTimer();
79 $this->logFinishedQuery();
81 $n = 0;
82 @$n = $conn->numFields( $res );
83 $titleList = false;
85 if ( $n ) {
86 $k = array();
87 for ( $x = 0; $x < $n; ++$x ) {
88 array_push( $k, $conn->fieldName( $res, $x ) );
91 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
92 $titleList = true;
95 $a = array();
96 while ( $s = $conn->fetchObject( $res ) ) {
97 array_push( $a, $s );
99 $conn->freeResult( $res );
101 if ( $titleList ) {
102 $r = "";
103 foreach ( $a as $y ) {
104 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
105 htmlspecialchars( $y->$x ) . "</a>" ;
106 $sTitle = htmlspecialchars( $y->cur_title );
107 if ( $y->cur_namespace ) {
108 $sNamespace = $wgLang->getNsText( $y->cur_namespace );
109 $link = "$sNamespace:$sTitle";
110 } else {
111 $link = "$sTitle";
113 $skin = $wgUser->getSkin();
114 $link = $skin->makeLink( $link );
115 $r .= "* [[$link]]<br>\n";
117 } else {
119 $r = "<table border=1 bordercolor=black cellspacing=0 " .
120 "cellpadding=2><tr>\n";
121 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
122 $r .= "</tr>\n";
124 foreach ( $a as $y ) {
125 $r .= "<tr>";
126 foreach ( $k as $x ) {
127 $o = $y->$x ;
128 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
129 $namespace = 0;
130 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
131 if( $x == "old_title" ) $namespace = $y->old_namespace;
132 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
133 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
134 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
135 htmlspecialchars( $y->$x ) . "</a>" ;
136 } else {
137 $o = htmlspecialchars( $o );
139 $r .= "<td>" . $o . "</td>\n";
141 $r .= "</tr>\n";
143 $r .= "</table>\n";
146 $this->showForm( wfMsg( "querysuccessful" ) );
147 $wgOut->addHTML( "<hr>{$r}\n" );
150 function logQuery( $q ) {
151 global $wgSqlLogFile, $wgLogQueries, $wgUser;
152 if(!$wgLogQueries) return;
154 $f = fopen( $wgSqlLogFile, "a" );
155 fputs( $f, "\n\n" . wfTimestampNow() .
156 " query by " . $wgUser->getName() .
157 ":\n$q\n" );
158 fclose( $f );
159 $this->starttime = wfTime();
162 function logFinishedQuery() {
163 global $wgSqlLogFile, $wgLogQueries;
164 if(!$wgLogQueries) return;
166 $interval = wfTime() - $this->starttime;
168 $f = fopen( $wgSqlLogFile, "a" );
169 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
170 fclose( $f );