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