Index: trunk/tools/WikipediaStatistics/Database.php |
— | — | @@ -29,7 +29,8 @@ |
30 | 30 | public function select( |
31 | 31 | $tables, |
32 | 32 | $fields, |
33 | | - $conditions = null |
| 33 | + $conditions = null, |
| 34 | + $options = null |
34 | 35 | ) { |
35 | 36 | $query = sprintf( |
36 | 37 | 'SELECT %s FROM %s', |
— | — | @@ -42,13 +43,32 @@ |
43 | 44 | if ( is_int( $key ) ) { |
44 | 45 | $conditionList[] = $value; |
45 | 46 | } else { |
46 | | - $conditionList[] = "{$key}=" . $this->addQuotes( $value ); |
| 47 | + $conditionList[] = "{$key}=" . ( |
| 48 | + is_numeric( $value ) ? |
| 49 | + $value : $this->addQuotes( $value ) |
| 50 | + ); |
47 | 51 | } |
48 | 52 | } |
49 | 53 | if ( count( $conditionList > 0 ) ) { |
50 | 54 | $query .= ' WHERE ' . implode( ' AND ', $conditionList ); |
51 | 55 | } |
52 | 56 | } |
| 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 | + } |
53 | 73 | $result = mysql_query( $query ) |
54 | 74 | or die ("Error in query: $query. ".mysql_error() ); |
55 | 75 | return new DatabaseResult( $result ); |
Index: trunk/tools/WikipediaStatistics/index.php |
— | — | @@ -1,6 +1,11 @@ |
2 | 2 | <html> |
3 | 3 | <head> |
4 | 4 | <title>Wikipedia Statistics</title> |
| 5 | + <style> |
| 6 | + body { |
| 7 | + font-family: sans-serif; |
| 8 | + } |
| 9 | + </style> |
5 | 10 | </head> |
6 | 11 | <body> |
7 | 12 | <h1>Wikipedia Statistics</h1> |
— | — | @@ -9,10 +14,17 @@ |
10 | 15 | require_once 'LocalSettings.php'; |
11 | 16 | require_once 'Database.php'; |
12 | 17 | |
| 18 | + // Query types |
| 19 | + $types = array( |
| 20 | + 'edits' => 'Number of Edits', |
| 21 | + 'editors' => 'Number of Editors', |
| 22 | + ); |
| 23 | + |
13 | 24 | // Set default parameters |
14 | 25 | $parameters = array( |
15 | 26 | 'from' => '2009-01-01', |
16 | 27 | 'to' => '2009-02-01', |
| 28 | + 'type' => current( array_keys( $types ) ), |
17 | 29 | ); |
18 | 30 | // Detect custom parameters |
19 | 31 | if ( isset( $_POST['from'] ) ) { |
— | — | @@ -21,13 +33,18 @@ |
22 | 34 | if ( isset( $_POST['to'] ) ) { |
23 | 35 | $parameters['to'] = stripslashes( $_POST['to'] ); |
24 | 36 | } |
| 37 | + if ( isset( $_POST['type'] ) ) { |
| 38 | + $parameters['type'] = stripslashes( $_POST['type'] ); |
| 39 | + } |
25 | 40 | |
26 | 41 | ?> |
27 | | - <h2>Number of Edits</h2> |
28 | 42 | <fieldset> |
29 | 43 | <form action="index.php" method="post"> |
30 | | - <table> |
| 44 | + <table cellpadding="10"> |
31 | 45 | <tr> |
| 46 | + <th align="left" colspan="2">Date Range</th> |
| 47 | + </tr> |
| 48 | + <tr> |
32 | 49 | <td>From</td> |
33 | 50 | <td> |
34 | 51 | <input type="text" name="from" |
— | — | @@ -42,32 +59,65 @@ |
43 | 60 | </td> |
44 | 61 | </tr> |
45 | 62 | <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> |
46 | 76 | <td align="right" colspan="2"> |
47 | 77 | <input type="submit" name="submit" value="Update" /> |
48 | 78 | </td> |
49 | 79 | </tr> |
50 | 80 | </table> |
51 | 81 | </form> |
| 82 | + <hr noshade="noshade" size="1" /> |
52 | 83 | <pre><?php |
53 | 84 | |
54 | 85 | $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 | + ), |
68 | 97 | ); |
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; |
71 | 115 | } |
| 116 | + if ( isset( $result ) ) { |
| 117 | + while( $row = $result->fetchRow() ) { |
| 118 | + echo $row[0]; |
| 119 | + } |
| 120 | + } |
| 121 | + |
72 | 122 | ?></pre> |
73 | 123 | </fieldset> |
74 | 124 | </body> |
\ No newline at end of file |