Initial revision
[lhc/web/wiklou.git] / includes / SpecialAsksql.php
1 <?
2
3 function wfSpecialAsksql()
4 {
5 global $wgUser, $wgOut, $action;
6
7 if ( ! $wgUser->isSysop() ) {
8 $wgOut->sysopRequired();
9 return;
10 }
11 $fields = array( "wpSqlQuery" );
12 wfCleanFormFields( $fields );
13 $f = new SqlQueryForm();
14
15 if ( "submit" == $action ) { $f->doSubmit(); }
16 else { $f->showForm( "" ); }
17 }
18
19 class SqlQueryForm {
20
21 function showForm( $err )
22 {
23 global $wgOut, $wgUser, $wgLang;
24 global $wpSqlQuery;
25
26 $wgOut->setPagetitle( wfMsg( "asksql" ) );
27 $wgOut->addWikiText( wfMsg( "asksqltext" ) );
28
29 if ( "" != $err ) {
30 $wgOut->addHTML( "<p><font color='red' size='+1'>" . htmlspecialchars($err) . "</font>\n" );
31 }
32 if ( ! $wpSqlQuery ) { $wpSqlQuery = "SELECT ... FROM ... WHERE ..."; }
33 $q = wfMsg( "sqlquery" );
34 $qb = wfMsg( "querybtn" );
35 $action = wfLocalUrlE( $wgLang->specialPage( "Asksql" ),
36 "action=submit" );
37
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" );
52
53 }
54
55 function doSubmit()
56 {
57 global $wgOut, $wgUser, $wgServer, $wgScript, $wgArticlePath;
58 global $wpSqlQuery;
59 global $wgDBsqluser, $wgDBsqlpassword;
60
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";
66 }
67 if ( ! $wgUser->isDeveloper() ) {
68 $connection = wfGetDB( $wgDBsqluser, $wgDBsqlpassword );
69 }
70 $res = wfQuery( $wpSqlQuery, "SpecialAsksql::doSubmit" );
71
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 ) );
78 }
79 $a = array();
80 while ( $s = wfFetchObject( $res ) ) {
81 array_push( $a, $s );
82 }
83 wfFreeResult( $res );
84
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";
89
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" ) {
95 $o = str_replace ( "$1" , rawurlencode( $o ) , $wgArticlePath ) ;
96 $o = "<a href=\"{$o}\" class='internal'>" .
97 htmlspecialchars( $y->$x ) . "</a>" ;
98 } else {
99 $o = htmlspecialchars( $o );
100 }
101 $r .= "<td>" . $o . "</td>\n";
102 }
103 $r .= "</tr>\n";
104 }
105 $r .= "</table>\n";
106 }
107 $this->showForm( wfMsg( "querysuccessful" ) );
108 $wgOut->addHTML( "<hr>{$r}\n" );
109 }
110
111 }
112
113 ?>