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