2cc45bc91e57e31feb909ccc46c7ef9441d3867b
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
1 <?
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 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
40 "action=submit" );
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 $wgDBsqluser, $wgDBsqlpassword;
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 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
72 $this->logQuery( $wpSqlQuery );
73 $res = wfQuery( $wpSqlQuery, DB_WRITE, "SpecialAsksql::doSubmit" );
74 $this->logFinishedQuery();
75
76 $n = 0;
77 @$n = wfNumFields( $res );
78 $titleList = false;
79
80 if ( $n ) {
81 $k = array();
82 for ( $x = 0; $x < $n; ++$x ) {
83 array_push( $k, wfFieldName( $res, $x ) );
84 }
85
86 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
87 $titleList = true;
88 }
89
90 $a = array();
91 while ( $s = wfFetchObject( $res ) ) {
92 array_push( $a, $s );
93 }
94 wfFreeResult( $res );
95
96 if ( $titleList ) {
97 $r = "";
98 foreach ( $a as $y ) {
99 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
100 htmlspecialchars( $y->$x ) . "</a>" ;
101 $sTitle = htmlspecialchars( $y->cur_title );
102 if ( $y->cur_namespace ) {
103 $sNamespace = $wgLang->getNsText( $y->cur_namespace );
104 $link = "$sNamespace:$sTitle";
105 } else {
106 $link = "$sTitle";
107 }
108 $skin = $wgUser->getSkin();
109 $link = $skin->makeLink( $link );
110 $r .= "* [[$link]]<br>\n";
111 }
112 } else {
113
114 $r = "<table border=1 bordercolor=black cellspacing=0 " .
115 "cellpadding=2><tr>\n";
116 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
117 $r .= "</tr>\n";
118
119 foreach ( $a as $y ) {
120 $r .= "<tr>";
121 foreach ( $k as $x ) {
122 $o = $y->$x ;
123 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
124 $namespace = 0;
125 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
126 if( $x == "old_title" ) $namespace = $y->old_namespace;
127 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
128 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
129 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
130 htmlspecialchars( $y->$x ) . "</a>" ;
131 } else {
132 $o = htmlspecialchars( $o );
133 }
134 $r .= "<td>" . $o . "</td>\n";
135 }
136 $r .= "</tr>\n";
137 }
138 $r .= "</table>\n";
139 }
140 }
141 $this->showForm( wfMsg( "querysuccessful" ) );
142 $wgOut->addHTML( "<hr>{$r}\n" );
143 }
144
145 function logQuery( $q ) {
146 global $wgSqlLogFile, $wgLogQueries, $wgUser;
147 if(!$wgLogQueries) return;
148
149 $f = fopen( $wgSqlLogFile, "a" );
150 fputs( $f, "\n\n" . wfTimestampNow() .
151 " query by " . $wgUser->getName() .
152 ":\n$q\n" );
153 fclose( $f );
154 $this->starttime = microtime();
155 }
156
157 function logFinishedQuery() {
158 global $wgSqlLogFile, $wgLogQueries;
159 if(!$wgLogQueries) return;
160
161 list($sec, $usec) = explode( " ", microtime() );
162 list($sec1, $usec1) = explode( " ", $this->starttime );
163 $interval = ($sec + $usec) - ($sec1 + $usec1);
164
165 $f = fopen( $wgSqlLogFile, "a" );
166 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
167 fclose( $f );
168 }
169
170 }
171
172 ?>