3 function wfSpecialAsksql()
5 global $wgUser, $wgOut, $action;
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
21 function showForm( $err )
23 global $wgOut, $wgUser, $wgLang;
26 $wpSqlQuery = $_REQUEST["wpSqlQuery"];
28 $wgOut->setPagetitle( wfMsg( "asksql" ) );
29 $note = wfMsg( "asksqltext" );
31 $note .= " " . wfMsg( "sqlislogged" );
32 $wgOut->addWikiText( $note );
35 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
37 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
38 $q = wfMsg( "sqlquery" );
39 $qb = wfMsg( "querybtn" );
40 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
44 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
46 <td align=right>{$q}:</td>
48 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
49 . htmlspecialchars($wpSqlQuery) ."
53 <td> </td><td align=\"left\">
54 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
62 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
64 global $wgDBsqluser, $wgDBsqlpassword;
65 $wpSqlQuery = $_REQUEST["wpSqlQuery"];
68 $wpSqlQuery = trim( $wpSqlQuery );
69 if( preg_match( "/^SELECT/i", $wpSqlQuery )
70 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
71 $wpSqlQuery .= " LIMIT 100";
73 if ( ! $wgUser->isDeveloper() ) {
74 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
76 $this->logQuery( $wpSqlQuery );
77 $res = wfQuery( $wpSqlQuery, DB_WRITE
, "SpecialAsksql::doSubmit" );
78 $this->logFinishedQuery();
81 @$n = wfNumFields( $res );
86 for ( $x = 0; $x < $n; ++
$x ) {
87 array_push( $k, wfFieldName( $res, $x ) );
90 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
95 while ( $s = wfFetchObject( $res ) ) {
102 foreach ( $a as $y ) {
103 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
104 htmlspecialchars( $y->$x ) . "</a>" ;
105 $sTitle = htmlspecialchars( $y->cur_title
);
106 if ( $y->cur_namespace
) {
107 $sNamespace = $wgLang->getNsText( $y->cur_namespace
);
108 $link = "$sNamespace:$sTitle";
112 $skin = $wgUser->getSkin();
113 $link = $skin->makeLink( $link );
114 $r .= "* [[$link]]<br>\n";
118 $r = "<table border=1 bordercolor=black cellspacing=0 " .
119 "cellpadding=2><tr>\n";
120 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
123 foreach ( $a as $y ) {
125 foreach ( $k as $x ) {
127 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
129 if( $x == "cur_title" ) $namespace = $y->cur_namespace
;
130 if( $x == "old_title" ) $namespace = $y->old_namespace
;
131 if( $x == "rc_title" ) $namespace = $y->rc_namespace
;
132 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
133 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
134 htmlspecialchars( $y->$x ) . "</a>" ;
136 $o = htmlspecialchars( $o );
138 $r .= "<td>" . $o . "</td>\n";
145 $this->showForm( wfMsg( "querysuccessful" ) );
146 $wgOut->addHTML( "<hr>{$r}\n" );
149 function logQuery( $q ) {
150 global $wgSqlLogFile, $wgLogQueries, $wgUser;
151 if(!$wgLogQueries) return;
153 $f = fopen( $wgSqlLogFile, "a" );
154 fputs( $f, "\n\n" . wfTimestampNow() .
155 " query by " . $wgUser->getName() .
158 $this->starttime
= microtime();
161 function logFinishedQuery() {
162 global $wgSqlLogFile, $wgLogQueries;
163 if(!$wgLogQueries) return;
165 list($sec, $usec) = explode( " ", microtime() );
166 list($sec1, $usec1) = explode( " ", $this->starttime
);
167 $interval = ($sec +
$usec) - ($sec1 +
$usec1);
169 $f = fopen( $wgSqlLogFile, "a" );
170 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );