3 function wfSpecialAsksql()
5 global $wgUser, $wgOut, $wgRequest;
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
12 if( $wgRequest->wasPosted() ) {
13 $query = $wgRequest->getVal( 'wpSqlQuery' );
14 $action = $wgRequest->getVal( 'action' );
19 $f = new SqlQueryForm( $query);
21 if ( "submit" == $action ) {
31 function SqlQueryForm( $query ) {
32 $this->query
= $query;
35 function showForm( $err )
37 global $wgOut, $wgUser, $wgLang;
40 $wgOut->setPagetitle( wfMsg( "asksql" ) );
41 $note = wfMsg( "asksqltext" );
43 $note .= " " . wfMsg( "sqlislogged" );
44 $wgOut->addWikiText( $note );
47 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
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" );
56 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
58 <td align=right>{$q}:</td>
60 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
61 . htmlspecialchars($this->query
) ."
65 <td> </td><td align=\"left\">
66 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
74 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
75 global $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname, $wgSqlTimeout;
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";
83 $conn = Database
::newFromParams( $wgDBserver, $wgDBsqluser, $wgDBsqlpassword, $wgDBname );
85 $this->logQuery( $this->query
);
87 # Start timer, will kill the DB thread in $wgSqlTimeout seconds
88 $conn->startTimer( $wgSqlTimeout );
89 $res = $conn->query( $this->query
, "SpecialAsksql::doSubmit" );
91 $this->logFinishedQuery();
94 @$n = $conn->numFields( $res );
99 for ( $x = 0; $x < $n; ++
$x ) {
100 array_push( $k, $conn->fieldName( $res, $x ) );
103 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
108 while ( $s = $conn->fetchObject( $res ) ) {
109 array_push( $a, $s );
111 $conn->freeResult( $res );
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";
123 $skin = $wgUser->getSkin();
124 $link = $skin->makeLink( $link );
125 $r .= "* [[$link]]<br>\n";
129 $r = "<table border=1 bordercolor=black cellspacing=0 " .
130 "cellpadding=2><tr>\n";
131 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
134 foreach ( $a as $y ) {
136 foreach ( $k as $x ) {
138 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
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>" ;
147 $o = htmlspecialchars( $o );
149 $r .= "<td>" . $o . "</td>\n";
156 $this->showForm( wfMsg( "querysuccessful" ) );
157 $wgOut->addHTML( "<hr>{$r}\n" );
160 function logQuery( $q ) {
161 global $wgSqlLogFile, $wgLogQueries, $wgUser;
162 if(!$wgLogQueries) return;
164 $f = fopen( $wgSqlLogFile, "a" );
165 fputs( $f, "\n\n" . wfTimestampNow() .
166 " query by " . $wgUser->getName() .
169 $this->starttime
= wfTime();
172 function logFinishedQuery() {
173 global $wgSqlLogFile, $wgLogQueries;
174 if(!$wgLogQueries) return;
176 $interval = wfTime() - $this->starttime
;
178 $f = fopen( $wgSqlLogFile, "a" );
179 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );