r37611 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37610‎ | r37611 | r37612 >
Date:13:14, 13 July 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* 2008-07-13:2 group filter for graphs
Modified paths:
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Stats.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -11,7 +11,7 @@
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1313 */
1414
15 -define( 'TRANSLATE_VERSION', '9 (2008-07-13:1)' );
 15+define( 'TRANSLATE_VERSION', '9 (2008-07-13:2)' );
1616
1717 $wgExtensionCredits['specialpage'][] = array(
1818 'name' => 'Translate',
Index: trunk/extensions/Translate/Stats.php
@@ -13,11 +13,11 @@
1414
1515
1616 $opts = new FormOptions();
17 - $opts->add( 'language', 'en' );
 17+ $opts->add( 'language', '' );
1818 $opts->add( 'days', 30 );
1919 $opts->add( 'width', 600 );
2020 $opts->add( 'height', 400 );
21 - $opts->add( 'ts', 0 );
 21+ $opts->add( 'group', '' );
2222 $opts->fetchValuesFromRequest( $wgRequest );
2323
2424 $pars = explode( ';', $par );
@@ -33,7 +33,7 @@
3434 $opts->validateIntBounds( 'height', 200, 1000 );
3535
3636 $title = $this->getTitle();
37 - $cgiparams = wfArrayToCgi( array( 'ts' => time() ), $opts->getAllValues() );
 37+ $cgiparams = wfArrayToCgi( $opts->getAllValues() );
3838 $href = $title->getLocalUrl( $cgiparams );
3939
4040
@@ -48,16 +48,10 @@
4949 )
5050 );
5151 } else {
52 - if ( $opts['ts'] === 0 ) {
53 - $wgOut->redirect( $href );
54 - return;
55 - }
 52+ // Cache for two hours
 53+ $lastMod = $wgOut->checkLastModified( wfTimestamp( TS_MW, time() - 2*3600 ) );
 54+ if ( $lastMod ) return;
5655
57 - if ( time() - $opts['ts'] < 3600*2 ) {
58 - $lastMod = $wgOut->checkLastModified( wfTimestamp( TS_MW, $opts['ts'] ) );
59 - if ( $lastMod ) return;
60 - }
61 -
6256 $wgOut->disable();
6357
6458 if ( !class_exists('PHPlot') ) {
@@ -78,36 +72,55 @@
7973 $cutoff = $now - ( 3600 * 24 * $opts->getValue('days') -1 );
8074 $cutoff -= ($cutoff % 86400);
8175 $cutoffDb = $dbr->timestamp( $cutoff );
82 - $code = $dbr->escapeLike( $opts->getValue('language') );
8376
84 - $res = $dbr->select(
85 - 'recentchanges',
86 - 'rc_timestamp',
87 - array(
88 - "rc_timestamp >= '$cutoffDb'",
89 - 'rc_namespace' => $wgTranslateMessageNamespaces,
90 - "rc_title like '%%/$code'",
91 - 'rc_bot' => 0
92 - ),
93 - __METHOD__,
94 - array( 'ORDER BY' => 'rc_timestamp' )
 77+ $so = new TranslatePerLanguageStats( $opts );
 78+
 79+
 80+ $tables = array( 'recentchanges' );
 81+ $fields = array( 'rc_timestamp' );
 82+
 83+ $conds = array(
 84+ "rc_timestamp >= '$cutoffDb'",
 85+ 'rc_namespace' => $wgTranslateMessageNamespaces,
 86+ 'rc_bot' => 0
9587 );
9688
 89+ $type = __METHOD__;
 90+ $options = array( 'ORDER BY' => 'rc_timestamp' );
 91+
 92+ $so->preQuery( $tables, $fields, $conds, $type, $options );
 93+ $res = $dbr->select( $tables, $fields, $conds, $type, $options );
 94+
 95+
 96+ // Initialisations
 97+ $so->postQuery( $res );
 98+
9799 $data = array();
98100 while ( $cutoff < $now ) {
99101 $date = $wgLang->sprintfDate( 'Y-m-d', wfTimestamp( TS_MW, $cutoff ) );
100 - $data[$date] = 0;
 102+ $so->preProcess( $data[$date] );
101103 $cutoff += 24 * 3600;
102104 }
103105
 106+ // Processing
104107 foreach ( $res as $row ) {
105108 $date = $wgLang->sprintfDate( 'Y-m-d', $row->rc_timestamp );
106 - if ( !isset($data[$date]) ) $data[$date] = 0;
107 - $data[$date]++;
 109+ $index = $so->indexOf( $row );
 110+ if ( $index < 0 ) continue;
 111+
 112+ if ( !isset($data[$date][$index]) ) $data[$date][$index] = 0;
 113+ $data[$date][$index]++;
108114 }
109115
110 - return $data;
 116+ $labels = null;
 117+ if ( $opts['type'] === 'userlang' ) {
 118+ $labels = @array_keys($usercache);
 119+ }
 120+ $so->labels( $labels );
111121
 122+ //var_dump( $data );
 123+ return array($labels, $data);
 124+
112125 }
113126
114127 public function draw( FormOptions $opts ) {
@@ -118,9 +131,8 @@
119132 $height = $opts->getValue( 'height' );
120133 //Define the object
121134 $plot = new PHPlot($width, $height);
122 - $code = 'nl';
123135
124 - $resData = $this->getData($opts);
 136+ list( $legend, $resData ) = $this->getData($opts);
125137 $count = count($resData);
126138 $skip = intval($count / ($width/60) -1);
127139 $i = $count;
@@ -128,13 +140,18 @@
129141 if ( $skip > 0 ) {
130142 if ( ($count-$i)%$skip !== 0 ) { $date = ''; }
131143 }
132 - $data[] = array( $date, $edits );
 144+ array_unshift( $edits, $date );
 145+ $data[] = $edits;
133146 $i--;
134147 }
135148
136149 $plot->SetDefaultTTFont($wgTranslatePHPlotFont);
137150
138151 $plot->SetDataValues( $data );
 152+
 153+ if ( $legend !== null )
 154+ $plot->SetLegend($legend);
 155+
139156 $plot->setFont( 'x_label', null, 8 );
140157 $plot->setFont( 'y_label', null, 8 );
141158
@@ -144,7 +161,7 @@
145162 $plot->SetXTickPos('none');
146163 $plot->SetXLabelAngle(45);
147164
148 - $max = max( $resData );
 165+ $max = max( array_map( 'max', $resData ) );
149166 $yTick = 5;
150167 while ( $max / $yTick > $height/20 ) $yTick *= 2;
151168
@@ -159,4 +176,90 @@
160177
161178 }
162179
 180+}
 181+
 182+
 183+class TranslatePerLanguageStats {
 184+ protected $opts;
 185+ protected $cache;
 186+ protected $index;
 187+ protected $filters;
 188+
 189+ public function __construct( FormOptions $opts ) {
 190+ $this->opts = $opts;
 191+ }
 192+
 193+ public function preQuery( &$tables, &$fields, &$conds, &$type, &$options ) {
 194+ $db = wfGetDb();
 195+
 196+ $groups = array_map( 'trim', explode(',', $this->opts['group']) );
 197+ $codes = array_map( 'trim', explode(',', $this->opts['language']) );
 198+
 199+ $filters['language'] = trim($this->opts['language']) !== '';
 200+ $filters['group'] = trim($this->opts['group']) !== '';
 201+
 202+ foreach ( $groups as $group )
 203+ foreach ( $codes as $code )
 204+ $this->cache[$group . $code] = count($this->cache);
 205+
 206+ if ( $filters['language'] ) {
 207+ $myconds = array();
 208+ foreach( $codes as $code ) {
 209+ $myconds[] = 'rc_title like \'%%/' . $db->escapeLike( $code ) . "'";
 210+ }
 211+
 212+ $conds[] = $db->makeList( $myconds, LIST_OR );
 213+ }
 214+
 215+ if ( max($filters) ) $fields[] = 'rc_title';
 216+ if ( $filters['group'] ) $fields[] = 'rc_namespace';
 217+
 218+ $type .= '-perlang';
 219+
 220+ $this->filters = $filters;
 221+
 222+ }
 223+
 224+ public function postQuery( $rows ) {}
 225+
 226+ public function preProcess( &$initial ) {
 227+ $initial = array_pad( array(), max(1, count($this->cache)), 0 );
 228+ }
 229+
 230+ public function indexOf( $row ) {
 231+ global $wgContLang;
 232+
 233+ if ( max($this->filters) === 0 ) return 0;
 234+ if ( strpos( $row->rc_title, '/' ) === false ) return -1;
 235+
 236+ list( $key, $code ) = explode('/', $wgContLang->lcfirst($row->rc_title), 2);
 237+ $indexKey = '';
 238+
 239+ if ( $this->filters['group'] ) {
 240+ if ( $this->index === null ) $this->index = TranslateUtils::messageIndex();
 241+
 242+ $key = strtolower($row->rc_namespace. ':' . $key);
 243+ $group = @$this->index[$key];
 244+ if ( is_null($group) ) return -1;
 245+ $indexKey .= $group;
 246+ }
 247+
 248+ if ( $this->filters['language'] ) {
 249+ $indexKey .= $code;
 250+ }
 251+
 252+
 253+ if ( count($this->cache) > 1 ) {
 254+ return isset($this->cache[$indexKey]) ? $this->cache[$indexKey] : -1;
 255+ } else {
 256+ return 0;
 257+ }
 258+ }
 259+
 260+ public function labels( &$labels ) {
 261+ if ( count($this->cache) > 1 ) {
 262+ $labels = array_keys($this->cache);
 263+ }
 264+ }
 265+
163266 }
\ No newline at end of file
Index: trunk/extensions/Translate/README
@@ -34,6 +34,7 @@
3535
3636 == Changes in version 10 ==
3737
 38+* 2008-07-13:2 group filter for graphs
3839 * 2008-07-13:1 experimental alias-export for extensions
3940 * 2008-07-08:1 simple edit stats with phplot
4041 * 2008-07-05:1 fuzzy.php was ignoring the namespace

Status & tagging log