Changed the search default namespace code to use the new
[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, "SpecialAsksql::doSubmit" );
76 $this->logFinishedQuery();
77
78 $n = 0;
79 @$n = wfNumFields( $res );
80 if ( $n ) {
81 $k = array();
82 for ( $x = 0; $x < $n; ++$x ) {
83 array_push( $k, wfFieldName( $res, $x ) );
84 }
85 $a = array();
86 while ( $s = wfFetchObject( $res ) ) {
87 array_push( $a, $s );
88 }
89 wfFreeResult( $res );
90
91 $r = "<table border=1 bordercolor=black cellspacing=0 " .
92 "cellpadding=2><tr>\n";
93 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
94 $r .= "</tr>\n";
95
96 foreach ( $a as $y ) {
97 $r .= "<tr>";
98 foreach ( $k as $x ) {
99 $o = $y->$x ;
100 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
101 $namespace = 0;
102 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
103 if( $x == "old_title" ) $namespace = $y->old_namespace;
104 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
105 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
106 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
107 htmlspecialchars( $y->$x ) . "</a>" ;
108 } else {
109 $o = htmlspecialchars( $o );
110 }
111 $r .= "<td>" . $o . "</td>\n";
112 }
113 $r .= "</tr>\n";
114 }
115 $r .= "</table>\n";
116 }
117 $this->showForm( wfMsg( "querysuccessful" ) );
118 $wgOut->addHTML( "<hr>{$r}\n" );
119 }
120
121 function logQuery( $q ) {
122 global $wgSqlLogFile, $wgLogQueries, $wgUser;
123 if(!$wgLogQueries) return;
124
125 $f = fopen( $wgSqlLogFile, "a" );
126 fputs( $f, "\n\n" . wfTimestampNow() .
127 " query by " . $wgUser->getName() .
128 ":\n$q\n" );
129 fclose( $f );
130 $this->starttime = microtime();
131 }
132
133 function logFinishedQuery() {
134 global $wgSqlLogFile, $wgLogQueries;
135 if(!$wgLogQueries) return;
136
137 list($sec, $usec) = explode( " ", microtime() );
138 list($sec1, $usec1) = explode( " ", $this->starttime );
139 $interval = ($sec + $usec) - ($sec1 + $usec1);
140
141 $f = fopen( $wgSqlLogFile, "a" );
142 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );
143 fclose( $f );
144 }
145
146 }
147
148 ?>