Forgot to add this file.
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
1 <?php
2
3 function wfSpecialAsksql()
4 {
5 global $wgUser, $wgOut, $wgRequest, $wgAllowSysopQueries;
6
7 if( !$wgAllowSysopQueries ) {
8 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
9 return;
10 }
11 if( !$wgUser->isSysop() ) {
12 $wgOut->sysopRequired();
13 return;
14 }
15
16 if( $wgRequest->wasPosted() ) {
17 $query = $wgRequest->getVal( 'wpSqlQuery' );
18 $action = $wgRequest->getVal( 'action' );
19 } else {
20 $query = "";
21 $action = "";
22 }
23 $f = new SqlQueryForm( $query);
24
25 if ( "submit" == $action ) {
26 $f->doSubmit();
27 } else {
28 $f->showForm( "" );
29 }
30 }
31
32 class SqlQueryForm {
33 var $query = "";
34
35 function SqlQueryForm( $query ) {
36 $this->query = $query;
37 }
38
39 function showForm( $err )
40 {
41 global $wgOut, $wgUser, $wgLang;
42 global $wgLogQueries;
43
44 $wgOut->setPagetitle( wfMsg( "asksql" ) );
45 $note = wfMsg( "asksqltext" );
46 if($wgLogQueries)
47 $note .= " " . wfMsg( "sqlislogged" );
48 $wgOut->addWikiText( $note );
49
50 if ( "" != $err ) {
51 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
52 }
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" );
58
59 $wgOut->addHTML( "<p>
60 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
61 <table border=0><tr>
62 <td align=right>{$q}:</td>
63 <td align=left>
64 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
65 . htmlspecialchars($this->query) ."
66 </textarea>
67 </td>
68 </tr><tr>
69 <td>&nbsp;</td><td align=\"left\">
70 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
71 </td></tr></table>
72 </form>\n" );
73
74 }
75
76 function doSubmit()
77 {
78 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
79 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
80
81 # Use a limit, folks!
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";
86 }
87 $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
88
89 $this->logQuery( $this->query );
90
91 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
92 $conn->startTimer( $wgSqlTimeout );
93 $res = $conn->query( $this->query, "SpecialAsksql::doSubmit" );
94 $conn->stopTimer();
95 $this->logFinishedQuery();
96
97 $n = 0;
98 @$n = $conn->numFields( $res );
99 $titleList = false;
100
101 if ( $n ) {
102 $k = array();
103 for ( $x = 0; $x < $n; ++$x ) {
104 array_push( $k, $conn->fieldName( $res, $x ) );
105 }
106
107 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
108 $titleList = true;
109 }
110
111 $a = array();
112 while ( $s = $conn->fetchObject( $res ) ) {
113 array_push( $a, $s );
114 }
115 $conn->freeResult( $res );
116
117 if ( $titleList ) {
118 $r = "";
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";
124 } else {
125 $link = "$sTitle";
126 }
127 $skin = $wgUser->getSkin();
128 $link = $skin->makeLink( $link );
129 $r .= "* [[$link]]<br>\n";
130 }
131 } else {
132
133 $r = "<table border=1 bordercolor=black cellspacing=0 " .
134 "cellpadding=2><tr>\n";
135 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
136 $r .= "</tr>\n";
137
138 foreach ( $a as $y ) {
139 $r .= "<tr>";
140 foreach ( $k as $x ) {
141 $o = $y->$x ;
142 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
143 $namespace = 0;
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>" ;
150 } else {
151 $o = htmlspecialchars( $o );
152 }
153 $r .= "<td>" . $o . "</td>\n";
154 }
155 $r .= "</tr>\n";
156 }
157 $r .= "</table>\n";
158 }
159 }
160 $this->showForm( wfMsg( "querysuccessful" ) );
161 $wgOut->addHTML( "<hr>{$r}\n" );
162 }
163
164 function logQuery( $q ) {
165 global $wgSqlLogFile, $wgLogQueries, $wgUser;
166 if(!$wgLogQueries) return;
167
168 $f = fopen( $wgSqlLogFile, "a" );
169 fputs( $f, "\n\n" . wfTimestampNow() .
170 " query by " . $wgUser->getName() .
171 ":\n$q\n" );
172 fclose( $f );
173 $this->starttime = wfTime();
174 }
175
176 function logFinishedQuery() {
177 global $wgSqlLogFile, $wgLogQueries;
178 if(!$wgLogQueries) return;
179
180 $interval = wfTime() - $this->starttime;
181
182 $f = fopen( $wgSqlLogFile, "a" );
183 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
184 fclose( $f );
185 }
186
187 }
188
189 ?>