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;
27 $wgOut->setPagetitle( wfMsg( "asksql" ) );
28 $note = wfMsg( "asksqltext" );
30 $note .= " " . wfMsg( "sqlislogged" );
31 $wgOut->addWikiText( $note );
34 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
36 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
37 $q = wfMsg( "sqlquery" );
38 $qb = wfMsg( "querybtn" );
39 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
43 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
45 <td align=right>{$q}:</td>
47 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
48 . htmlspecialchars($wpSqlQuery) ."
52 <td> </td><td align=\"left\">
53 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
61 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
63 global $wgDBsqluser, $wgDBsqlpassword;
66 $wpSqlQuery = trim( $wpSqlQuery );
67 if( preg_match( "/^SELECT/i", $wpSqlQuery )
68 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
69 $wpSqlQuery .= " LIMIT 100";
71 if ( ! $wgUser->isDeveloper() ) {
72 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
74 $this->logQuery( $wpSqlQuery );
75 $res = wfQuery( $wpSqlQuery, "SpecialAsksql::doSubmit" );
76 $this->logFinishedQuery();
79 @$n = wfNumFields( $res );
82 for ( $x = 0; $x < $n; ++
$x ) {
83 array_push( $k, wfFieldName( $res, $x ) );
86 while ( $s = wfFetchObject( $res ) ) {
91 $r = "<table border=1 bordercolor=black cellspacing=0 " .
92 "cellpadding=2><tr>\n";
93 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
96 foreach ( $a as $y ) {
98 foreach ( $k as $x ) {
100 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
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>" ;
109 $o = htmlspecialchars( $o );
111 $r .= "<td>" . $o . "</td>\n";
117 $this->showForm( wfMsg( "querysuccessful" ) );
118 $wgOut->addHTML( "<hr>{$r}\n" );
121 function logQuery( $q ) {
122 global $wgSqlLogFile, $wgLogQueries, $wgUser;
123 if(!$wgLogQueries) return;
125 $f = fopen( $wgSqlLogFile, "a" );
126 fputs( $f, "\n\n" . wfTimestampNow() .
127 " query by " . $wgUser->getName() .
130 $this->starttime
= microtime();
133 function logFinishedQuery() {
134 global $wgSqlLogFile, $wgLogQueries;
135 if(!$wgLogQueries) return;
137 list($sec, $usec) = explode( " ", microtime() );
138 list($sec1, $usec1) = explode( " ", $this->starttime
);
139 $interval = ($sec +
$usec) - ($sec1 +
$usec1);
141 $f = fopen( $wgSqlLogFile, "a" );
142 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );