New file form Christian List.
[mediawiki.git] / includes / SpecialAsksql.php
blob34403c389c150110a9400e62ed5604e5d23de947
1 <?
3 function wfSpecialAsksql()
5 global $wgUser, $wgOut, $action;
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
19 class SqlQueryForm {
21 function showForm( $err )
23 global $wgOut, $wgUser, $wgLang;
24 global $wpSqlQuery;
26 $wgOut->setPagetitle( wfMsg( "asksql" ) );
27 $wgOut->addWikiText( wfMsg( "asksqltext" ) );
29 if ( "" != $err ) {
30 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
32 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
33 $q = wfMsg( "sqlquery" );
34 $qb = wfMsg( "querybtn" );
35 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
36 "action=submit" );
38 $wgOut->addHTML( "<p>
39 <form id=\"asksql\" method=\"post\" action=\"{$action}\">
40 <table border=0><tr>
41 <td align=right>{$q}:</td>
42 <td align=left>
43 <textarea name=\"wpSqlQuery\" cols=80 rows=4 wrap=\"virtual\">"
44 . htmlspecialchars($wpSqlQuery) ."
45 </textarea>
46 </td>
47 </tr><tr>
48 <td>&nbsp;</td><td align=\"left\">
49 <input type=submit name=\"wpQueryBtn\" value=\"{$qb}\">
50 </td></tr></table>
51 </form>\n" );
55 function doSubmit()
57 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath, $wgLang;
58 global $wpSqlQuery;
59 global $wgDBsqluser, $wgDBsqlpassword;
61 # Use a limit, folks!
62 $wpSqlQuery = trim( $wpSqlQuery );
63 if( preg_match( "/^SELECT/i", $wpSqlQuery )
64 and !preg_match( "/LIMIT/i", $wpSqlQuery ) ) {
65 $wpSqlQuery .= " LIMIT 100";
67 if ( ! $wgUser->isDeveloper() ) {
68 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
70 $res = wfQuery( $wpSqlQuery, "SpecialAsksql::doSubmit" );
72 $n = 0;
73 @$n = wfNumFields( $res );
74 if ( $n ) {
75 $k = array();
76 for ( $x = 0; $x < $n; ++$x ) {
77 array_push( $k, wfFieldName( $res, $x ) );
79 $a = array();
80 while ( $s = wfFetchObject( $res ) ) {
81 array_push( $a, $s );
83 wfFreeResult( $res );
85 $r = "<table border=1 bordercolor=black cellspacing=0 " .
86 "cellpadding=2><tr>\n";
87 foreach ( $k as $x ) $r .= "<th>" . htmlspecialchars( $x ) . "</th>";
88 $r .= "</tr>\n";
90 foreach ( $a as $y ) {
91 $r .= "<tr>";
92 foreach ( $k as $x ) {
93 $o = $y->$x ;
94 if ( $x == "cur_title" or $x == "old_title" or $x == "rc_title") {
95 $namespace = 0;
96 if( $x == "cur_title" ) $namespace = $y->cur_namespace;
97 if( $x == "old_title" ) $namespace = $y->old_namespace;
98 if( $x == "rc_title" ) $namespace = $y->rc_namespace;
99 if( $namespace ) $o = $wgLang->getNsText( $namespace ) . ":" . $o;
100 $o = "<a href=\"" . wfLocalUrlE($o) . "\" class='internal'>" .
101 htmlspecialchars( $y->$x ) . "</a>" ;
102 } else {
103 $o = htmlspecialchars( $o );
105 $r .= "<td>" . $o . "</td>\n";
107 $r .= "</tr>\n";
109 $r .= "</table>\n";
111 $this->showForm( wfMsg( "querysuccessful" ) );
112 $wgOut->addHTML( "<hr>{$r}\n" );