* Fixed magic quotes in $_REQUEST, in Setup.php
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
1 <?php
2
3 function wfSpecialAsksql()
4 {
5 global $wgUser, $wgOut, $action;
6
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
10 }
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
14
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
17 }
18
19 class SqlQueryForm {
20
21 function showForm( $err )
22 {
23 global $wgOut, $wgUser, $wgLang;
24 global $wpSqlQuery;
25 global $wgLogQueries;
26
27 $wgOut->setPagetitle( wfMsg( "asksql" ) );
28 $note = wfMsg( "asksqltext" );
29 if($wgLogQueries)
30 $note .= " " . wfMsg( "sqlislogged" );
31 $wgOut->addWikiText( $note );
32
33 if ( "" != $err ) {
34 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
35 }
36 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
37 $q = wfMsg( "sqlquery" );
38 $qb = wfMsg( "querybtn" );
39 $titleObj = Title::makeTitle( NS_SPECIAL, "Asksql" );
40 $action = $titleObj->getURL( "action=submit", true );
41
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" );
56
57 }
58
59 function doSubmit()
60 {
61 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
62 global $wpSqlQuery;
63 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
64
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";
70 }
71 $conn = Database::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
72
73 $this->logQuery( $wpSqlQuery );
74
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();
80
81 $n = 0;
82 @$n = $conn->numFields( $res );
83 $titleList = false;
84
85 if ( $n ) {
86 $k = array();
87 for ( $x = 0; $x < $n; ++$x ) {
88 array_push( $k, $conn->fieldName( $res, $x ) );
89 }
90
91 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
92 $titleList = true;
93 }
94
95 $a = array();
96 while ( $s = $conn->fetchObject( $res ) ) {
97 array_push( $a, $s );
98 }
99 $conn->freeResult( $res );
100
101 if ( $titleList ) {
102 $r = "";
103 foreach ( $a as $y ) {
104 $sTitle = htmlspecialchars( $y->cur_title );
105 if ( $y->cur_namespace ) {
106 $sNamespace = $wgLang->getNsText( $y->cur_namespace );
107 $link = "$sNamespace:$sTitle";
108 } else {
109 $link = "$sTitle";
110 }
111 $skin = $wgUser->getSkin();
112 $link = $skin->makeLink( $link );
113 $r .= "* [[$link]]<br>\n";
114 }
115 } else {
116
117 $r = "<table border=1 bordercolor=black cellspacing=0 " .
118 "cellpadding=2><tr>\n";
119 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
120 $r .= "</tr>\n";
121
122 foreach ( $a as $y ) {
123 $r .= "<tr>";
124 foreach ( $k as $x ) {
125 $o = $y->$x ;
126 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
127 $namespace = 0;
128 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
129 if( $x == "old_title" ) $namespace = $y->old_namespace;
130 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
131 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
132 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
133 htmlspecialchars( $y->$x ) . "</a>" ;
134 } else {
135 $o = htmlspecialchars( $o );
136 }
137 $r .= "<td>" . $o . "</td>\n";
138 }
139 $r .= "</tr>\n";
140 }
141 $r .= "</table>\n";
142 }
143 }
144 $this->showForm( wfMsg( "querysuccessful" ) );
145 $wgOut->addHTML( "<hr>{$r}\n" );
146 }
147
148 function logQuery( $q ) {
149 global $wgSqlLogFile, $wgLogQueries, $wgUser;
150 if(!$wgLogQueries) return;
151
152 $f = fopen( $wgSqlLogFile, "a" );
153 fputs( $f, "\n\n" . wfTimestampNow() .
154 " query by " . $wgUser->getName() .
155 ":\n$q\n" );
156 fclose( $f );
157 $this->starttime = wfTime();
158 }
159
160 function logFinishedQuery() {
161 global $wgSqlLogFile, $wgLogQueries;
162 if(!$wgLogQueries) return;
163
164 $interval = wfTime() - $this->starttime;
165
166 $f = fopen( $wgSqlLogFile, "a" );
167 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
168 fclose( $f );
169 }
170
171 }
172
173 ?>