r47068 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47067‎ | r47068 | r47069 >
Date:00:48, 10 February 2009
Author:tparscal
Status:deferred
Tags:
Comment:
Added options to query
Modified paths:
  • /trunk/tools/WikipediaStatistics/Database.php (modified) (history)
  • /trunk/tools/WikipediaStatistics/index.php (modified) (history)

Diff [purge]

Index: trunk/tools/WikipediaStatistics/Database.php
@@ -29,7 +29,8 @@
3030 public function select(
3131 $tables,
3232 $fields,
33 - $conditions = null
 33+ $conditions = null,
 34+ $options = null
3435 ) {
3536 $query = sprintf(
3637 'SELECT %s FROM %s',
@@ -42,13 +43,32 @@
4344 if ( is_int( $key ) ) {
4445 $conditionList[] = $value;
4546 } else {
46 - $conditionList[] = "{$key}=" . $this->addQuotes( $value );
 47+ $conditionList[] = "{$key}=" . (
 48+ is_numeric( $value ) ?
 49+ $value : $this->addQuotes( $value )
 50+ );
4751 }
4852 }
4953 if ( count( $conditionList > 0 ) ) {
5054 $query .= ' WHERE ' . implode( ' AND ', $conditionList );
5155 }
5256 }
 57+ if ( $options != null ) {
 58+ $optionList = array();
 59+ foreach ( $options as $key => $value ) {
 60+ if ( is_int( $key ) ) {
 61+ $optionList[] = $value;
 62+ } else {
 63+ $optionList[] = "{$key} " . (
 64+ is_numeric( $value ) ?
 65+ $value : $this->addQuotes( $value )
 66+ );
 67+ }
 68+ }
 69+ if ( count( $optionList > 0 ) ) {
 70+ $query .= implode( ' ', $optionList );
 71+ }
 72+ }
5373 $result = mysql_query( $query )
5474 or die ("Error in query: $query. ".mysql_error() );
5575 return new DatabaseResult( $result );
Index: trunk/tools/WikipediaStatistics/index.php
@@ -1,6 +1,11 @@
22 <html>
33 <head>
44 <title>Wikipedia Statistics</title>
 5+ <style>
 6+ body {
 7+ font-family: sans-serif;
 8+ }
 9+ </style>
510 </head>
611 <body>
712 <h1>Wikipedia Statistics</h1>
@@ -9,10 +14,17 @@
1015 require_once 'LocalSettings.php';
1116 require_once 'Database.php';
1217
 18+ // Query types
 19+ $types = array(
 20+ 'edits' => 'Number of Edits',
 21+ 'editors' => 'Number of Editors',
 22+ );
 23+
1324 // Set default parameters
1425 $parameters = array(
1526 'from' => '2009-01-01',
1627 'to' => '2009-02-01',
 28+ 'type' => current( array_keys( $types ) ),
1729 );
1830 // Detect custom parameters
1931 if ( isset( $_POST['from'] ) ) {
@@ -21,13 +33,18 @@
2234 if ( isset( $_POST['to'] ) ) {
2335 $parameters['to'] = stripslashes( $_POST['to'] );
2436 }
 37+ if ( isset( $_POST['type'] ) ) {
 38+ $parameters['type'] = stripslashes( $_POST['type'] );
 39+ }
2540
2641 ?>
27 - <h2>Number of Edits</h2>
2842 <fieldset>
2943 <form action="index.php" method="post">
30 - <table>
 44+ <table cellpadding="10">
3145 <tr>
 46+ <th align="left" colspan="2">Date Range</th>
 47+ </tr>
 48+ <tr>
3249 <td>From</td>
3350 <td>
3451 <input type="text" name="from"
@@ -42,32 +59,65 @@
4360 </td>
4461 </tr>
4562 <tr>
 63+ <td>Type</td>
 64+ <td>
 65+ <?php foreach( $types as $type => $name ): ?>
 66+ <input type="radio" name="type" value="<?= $type ?>"
 67+ id="type_<?= $type ?>"
 68+ <?= $type == $parameters['type'] ? 'checked' : '' ?>
 69+ />
 70+ <label for="type_<?= $type ?>"><?= $name ?></label>
 71+ <br />
 72+ <?php endforeach; ?>
 73+ </td>
 74+ </tr>
 75+ <tr>
4676 <td align="right" colspan="2">
4777 <input type="submit" name="submit" value="Update" />
4878 </td>
4979 </tr>
5080 </table>
5181 </form>
 82+ <hr noshade="noshade" size="1" />
5283 <pre><?php
5384
5485 $dbr = new Database();
55 - $result = $dbr->select(
56 - 'revision',
57 - 'COUNT(*)',
58 - array(
59 - sprintf(
60 - 'rev_timestamp > %s',
61 - $dbr->addQuotes( date( 'Ymd', strtotime( $parameters['from'] ) ) )
62 - ),
63 - sprintf(
64 - 'rev_timestamp < %s',
65 - $dbr->addQuotes( date( 'Ymd', strtotime( $parameters['to'] ) ) )
66 - ),
67 - )
 86+ $dateRange = array(
 87+ sprintf(
 88+ 'rev_timestamp > %s',
 89+ $dbr->addQuotes( date( 'Ymd', strtotime( $parameters['from'] ) )
 90+ )
 91+ ),
 92+ sprintf(
 93+ 'rev_timestamp < %s',
 94+ $dbr->addQuotes( date( 'Ymd', strtotime( $parameters['to'] ) )
 95+ )
 96+ ),
6897 );
69 - while( $row = $result->fetchRow() ) {
70 - var_dump( $row );
 98+ switch ( $parameters['type'] ) {
 99+ case 'edits':
 100+ $result = $dbr->select(
 101+ 'revision',
 102+ 'COUNT( rev_id )',
 103+ $dateRange,
 104+ array( 'LIMIT' => 1 )
 105+ );
 106+ break;
 107+ case 'editors':
 108+ $result = $dbr->select(
 109+ 'revision',
 110+ 'DISTINCT COUNT( rev_user )',
 111+ $dateRange,
 112+ array( 'LIMIT' => 1 )
 113+ );
 114+ break;
71115 }
 116+ if ( isset( $result ) ) {
 117+ while( $row = $result->fetchRow() ) {
 118+ echo $row[0];
 119+ }
 120+ }
 121+
72122 ?></pre>
73123 </fieldset>
74124 </body>
\ No newline at end of file

Status & tagging log