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, DB_WRITE
, "SpecialAsksql::doSubmit" );
76 $this->logFinishedQuery();
79 @$n = wfNumFields( $res );
84 for ( $x = 0; $x < $n; ++
$x ) {
85 array_push( $k, wfFieldName( $res, $x ) );
88 if ( $n == 2 && in_array( "cur_title", $k ) && in_array( "cur_namespace", $k ) ) {
93 while ( $s = wfFetchObject( $res ) ) {
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";
110 $skin = $wgUser->getSkin();
111 $link = $skin->makeLink( $link );
112 $r .= "* [[$link]]<br>\n";
116 $r = "<table border=1 bordercolor=black cellspacing=0 " .
117 "cellpadding=2><tr>\n";
118 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
121 foreach ( $a as $y ) {
123 foreach ( $k as $x ) {
125 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
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>" ;
134 $o = htmlspecialchars( $o );
136 $r .= "<td>" . $o . "</td>\n";
143 $this->showForm( wfMsg( "querysuccessful" ) );
144 $wgOut->addHTML( "<hr>{$r}\n" );
147 function logQuery( $q ) {
148 global $wgSqlLogFile, $wgLogQueries, $wgUser;
149 if(!$wgLogQueries) return;
151 $f = fopen( $wgSqlLogFile, "a" );
152 fputs( $f, "\n\n" . wfTimestampNow() .
153 " query by " . $wgUser->getName() .
156 $this->starttime
= microtime();
159 function logFinishedQuery() {
160 global $wgSqlLogFile, $wgLogQueries;
161 if(!$wgLogQueries) return;
163 list($sec, $usec) = explode( " ", microtime() );
164 list($sec1, $usec1) = explode( " ", $this->starttime
);
165 $interval = ($sec +
$usec) - ($sec1 +
$usec1);
167 $f = fopen( $wgSqlLogFile, "a" );
168 fputs( $f, "finished at " . wfTimestampNow() . "; took $interval secs\n" );