Index: trunk/tools/WikipediaStatistics/Database.php |
— | — | @@ -0,0 +1,89 @@ |
| 2 | +<?php |
| 3 | +/* |
| 4 | + * Miniature and sort-of-similar version of the MediaWiki Database class |
| 5 | + */ |
| 6 | + |
| 7 | +class Database { |
| 8 | + |
| 9 | + /* Members */ |
| 10 | + |
| 11 | + private $connection; |
| 12 | + |
| 13 | + /* Functions */ |
| 14 | + |
| 15 | + public function __construct() { |
| 16 | + global $localSettings; |
| 17 | + $this->connection = mysql_connect( |
| 18 | + $localSettings['host'], |
| 19 | + $localSettings['username'], |
| 20 | + $localSettings['password'] |
| 21 | + ) or die ( "Unable to connect!" ); |
| 22 | + mysql_select_db( $localSettings['dbname'] ) |
| 23 | + or die ("Unable to select database!"); |
| 24 | + } |
| 25 | + |
| 26 | + public function __destruct() { |
| 27 | + mysql_close( $this->connection ); |
| 28 | + } |
| 29 | + |
| 30 | + public function select( |
| 31 | + $tables, |
| 32 | + $fields, |
| 33 | + $conditions = null |
| 34 | + ) { |
| 35 | + $query = sprintf( |
| 36 | + 'SELECT %s FROM %s', |
| 37 | + is_array( $fields ) ? implode( ',', $fields ) : $fields, |
| 38 | + is_array( $tables ) ? implode( ',', $tables ) : $tables |
| 39 | + ); |
| 40 | + if ( $conditions != null ) { |
| 41 | + $conditionList = array(); |
| 42 | + foreach ( $conditions as $key => $value ) { |
| 43 | + if ( is_int( $key ) ) { |
| 44 | + $conditionList[] = $value; |
| 45 | + } else { |
| 46 | + $conditionList[] = "{$key}=" . $this->addQuotes( $value ); |
| 47 | + } |
| 48 | + } |
| 49 | + if ( count( $conditionList > 0 ) ) { |
| 50 | + $query .= ' WHERE ' . implode( ' AND ', $conditionList ); |
| 51 | + } |
| 52 | + } |
| 53 | + $result = mysql_query( $query ) |
| 54 | + or die ("Error in query: $query. ".mysql_error() ); |
| 55 | + return new DatabaseResult( $result ); |
| 56 | + } |
| 57 | + |
| 58 | + public function addQuotes( |
| 59 | + $string |
| 60 | + ) { |
| 61 | + return "'" . mysql_real_escape_string( $string ) . "'"; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class DatabaseResult { |
| 66 | + |
| 67 | + /* Members */ |
| 68 | + |
| 69 | + private $result; |
| 70 | + |
| 71 | + /* Functions */ |
| 72 | + |
| 73 | + public function __construct( |
| 74 | + $result |
| 75 | + ) { |
| 76 | + $this->result = $result; |
| 77 | + } |
| 78 | + |
| 79 | + public function __destruct() { |
| 80 | + mysql_free_result( $this->result ); |
| 81 | + } |
| 82 | + |
| 83 | + public function numRows() { |
| 84 | + return mysql_num_rows( $this->result ); |
| 85 | + } |
| 86 | + |
| 87 | + public function fetchRow() { |
| 88 | + return mysql_fetch_array( $this->result ); |
| 89 | + } |
| 90 | +} |
Index: trunk/tools/WikipediaStatistics/index.php |
— | — | @@ -7,17 +7,67 @@ |
8 | 8 | <?php |
9 | 9 | |
10 | 10 | require_once 'LocalSettings.php'; |
| 11 | + require_once 'Database.php'; |
11 | 12 | |
12 | | - $dbh = new PDO( |
13 | | - 'mysql:host=' . $localSettings['host'] . ';' . |
14 | | - 'dbname=' . $localSettings['dbname'], |
15 | | - $localSettings['username'], |
16 | | - $localSettings['password'] |
| 13 | + // Set default parameters |
| 14 | + $parameters = array( |
| 15 | + 'from' => '2009-01-01', |
| 16 | + 'to' => '2009-02-01', |
17 | 17 | ); |
18 | | - foreach( $dbh->query( 'SELECT * FROM user_groups' ) as $row ){ |
19 | | - echo implode( ' / ', $row ) . '<br />'; |
| 18 | + // Detect custom parameters |
| 19 | + if ( isset( $_POST['from'] ) ) { |
| 20 | + $parameters['from'] = stripslashes( $_POST['from'] ); |
20 | 21 | } |
21 | | - $dbh = null; |
| 22 | + if ( isset( $_POST['to'] ) ) { |
| 23 | + $parameters['to'] = stripslashes( $_POST['to'] ); |
| 24 | + } |
22 | 25 | |
23 | 26 | ?> |
| 27 | + <h2>Number of Edits</h2> |
| 28 | + <fieldset> |
| 29 | + <form action="index.php" method="post"> |
| 30 | + <table> |
| 31 | + <tr> |
| 32 | + <td>From</td> |
| 33 | + <td> |
| 34 | + <input type="text" name="from" |
| 35 | + value="<?= $parameters['from'] ?>" /> |
| 36 | + </td> |
| 37 | + </tr> |
| 38 | + <tr> |
| 39 | + <td>To</td> |
| 40 | + <td> |
| 41 | + <input type="text" name="to" |
| 42 | + value="<?= $parameters['to'] ?>" /> |
| 43 | + </td> |
| 44 | + </tr> |
| 45 | + <tr> |
| 46 | + <td align="right" colspan="2"> |
| 47 | + <input type="submit" name="submit" value="Update" /> |
| 48 | + </td> |
| 49 | + </tr> |
| 50 | + </table> |
| 51 | + </form> |
| 52 | + <pre><?php |
| 53 | + |
| 54 | + $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 | + ) |
| 68 | + ); |
| 69 | + while( $row = $result->fetchRow() ) { |
| 70 | + var_dump( $row ); |
| 71 | + } |
| 72 | + ?></pre> |
| 73 | + </fieldset> |
24 | 74 | </body> |
\ No newline at end of file |