summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/specials/SpecialTranslationStats.php
blob: f84fa429490bdf255518f2ea43b634ab9ed0187f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
<?php
/**
 * Contains logic for special page Special:TranslationStats.
 *
 * @file
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @license GPL-2.0-or-later
 */

/**
 * @defgroup Stats Statistics
 * Collection of code to produce various kinds of statistics.
 */

/**
 * Includable special page for generating graphs on translations.
 *
 * @ingroup SpecialPage TranslateSpecialPage Stats
 */
class SpecialTranslationStats extends SpecialPage {
	/// @since 2012-03-05
	protected static $graphs = [
		'edits' => 'TranslatePerLanguageStats',
		'users' => 'TranslatePerLanguageStats',
		'registrations' => 'TranslateRegistrationStats',
		'reviews' => 'ReviewPerLanguageStats',
		'reviewers' => 'ReviewPerLanguageStats',
	];

	public function __construct() {
		parent::__construct( 'TranslationStats' );
	}

	public function isIncludable() {
		return true;
	}

	protected function getGroupName() {
		return 'wiki';
	}

	/**
	 * @since 2012-03-05
	 * @return array List of graph types
	 */
	public function getGraphTypes() {
		return array_keys( self::$graphs );
	}

	/**
	 * @since 2012-03-05
	 * @param string $type
	 * @return string
	 */
	public function getGraphClass( $type ) {
		return self::$graphs[$type];
	}

	public function execute( $par ) {
		$this->getOutput()->addModules( 'ext.translate.special.translationstats' );

		$opts = new FormOptions();
		$opts->add( 'graphit', false );
		$opts->add( 'preview', false );
		$opts->add( 'language', '' );
		$opts->add( 'count', 'edits' );
		$opts->add( 'scale', 'days' );
		$opts->add( 'days', 30 );
		$opts->add( 'width', 600 );
		$opts->add( 'height', 400 );
		$opts->add( 'group', '' );
		$opts->add( 'uselang', '' );
		$opts->add( 'start', '' );
		$opts->add( 'imagescale', 1.0 );
		$opts->fetchValuesFromRequest( $this->getRequest() );

		$pars = explode( ';', $par );

		foreach ( $pars as $item ) {
			if ( strpos( $item, '=' ) === false ) {
				continue;
			}

			list( $key, $value ) = array_map( 'trim', explode( '=', $item, 2 ) );
			if ( isset( $opts[$key] ) ) {
				$opts[$key] = $value;
			}
		}

		$opts->validateIntBounds( 'days', 1, 10000 );
		$opts->validateIntBounds( 'width', 200, 1000 );
		$opts->validateIntBounds( 'height', 200, 1000 );
		$opts->validateBounds( 'imagescale', 1.0, 4.0 );

		if ( $opts['start'] !== '' ) {
			$opts['start'] = rtrim( wfTimestamp( TS_ISO_8601, $opts['start'] ), 'Z' );
		}

		$validScales = [ 'months', 'weeks', 'days', 'hours' ];
		if ( !in_array( $opts['scale'], $validScales ) ) {
			$opts['scale'] = 'days';
		}

		if ( $opts['scale'] === 'hours' ) {
			$opts->validateIntBounds( 'days', 1, 4 );
		}

		$validCounts = $this->getGraphTypes();
		if ( !in_array( $opts['count'], $validCounts ) ) {
			$opts['count'] = 'edits';
		}

		foreach ( [ 'group', 'language' ] as $t ) {
			$values = array_map( 'trim', explode( ',', $opts[$t] ) );
			$values = array_splice( $values, 0, 4 );
			if ( $t === 'group' ) {
				// BC for old syntax which replaced _ to | which was not allowed
				$values = preg_replace( '~^page_~', 'page-', $values );
			}
			$opts[$t] = implode( ',', $values );
		}

		if ( $this->including() ) {
			$this->getOutput()->addHTML( $this->image( $opts ) );
		} elseif ( $opts['graphit'] ) {
			if ( !class_exists( PHPlot::class ) ) {
				header( 'HTTP/1.0 500 Multi fail' );
				echo 'PHPlot not found';
			}

			if ( !$this->getRequest()->getBool( 'debug' ) ) {
				$this->getOutput()->disable();
				header( 'Content-Type: image/png' );
				header( 'Cache-Control: private, max-age=3600' );
				header( 'Expires: ' . wfTimestamp( TS_RFC2822, time() + 3600 ) );
			}
			$this->draw( $opts );
		} else {
			$this->form( $opts );
		}
	}

	/**
	 * Constructs the form which can be used to generate custom graphs.
	 * @param FormOptions $opts
	 * @suppress SecurityCheck-DoubleEscaped Intentionally outputting what user should type
	 */
	protected function form( FormOptions $opts ) {
		global $wgScript;

		$this->setHeaders();
		$out = $this->getOutput();
		$out->addHelpLink( 'Help:Extension:Translate/Statistics_and_reporting' );
		$out->addWikiMsg( 'translate-statsf-intro' );

		$out->addHTML(
			Xml::fieldset( $this->msg( 'translate-statsf-options' )->text() ) .
				Html::openElement( 'form', [ 'action' => $wgScript ] ) .
				Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
				Html::hidden( 'preview', 1 ) .
				'<table>'
		);

		$submit = Xml::submitButton( $this->msg( 'translate-statsf-submit' )->text() );

		$out->addHTML(
			$this->eInput( 'width', $opts ) .
				$this->eInput( 'height', $opts ) .
				'<tr><td colspan="2"><hr /></td></tr>' .
				$this->eInput( 'start', $opts, 24 ) .
				$this->eInput( 'days', $opts ) .
				$this->eRadio( 'scale', $opts, [ 'months', 'weeks', 'days', 'hours' ] ) .
				$this->eRadio( 'count', $opts, $this->getGraphTypes() ) .
				'<tr><td colspan="2"><hr /></td></tr>' .
				$this->eLanguage( 'language', $opts ) .
				$this->eGroup( 'group', $opts ) .
				'<tr><td colspan="2"><hr /></td></tr>' .
				'<tr><td colspan="2">' . $submit . '</td></tr>'
		);

		$out->addHTML(
			'</table>' .
				'</form>' .
				'</fieldset>'
		);

		if ( !$opts['preview'] ) {
			return;
		}

		$spiParams = '';
		foreach ( $opts->getChangedValues() as $key => $v ) {
			if ( $key === 'preview' ) {
				continue;
			}

			if ( $spiParams !== '' ) {
				$spiParams .= ';';
			}

			$spiParams .= wfEscapeWikiText( "$key=$v" );
		}

		if ( $spiParams !== '' ) {
			$spiParams = '/' . $spiParams;
		}

		$titleText = $this->getPageTitle()->getPrefixedText();

		$out->addHTML(
			Html::element( 'hr' ) .
				Html::element( 'pre', [], "{{{$titleText}{$spiParams}}}" )
		);

		$out->addHTML(
			Html::element( 'hr' ) .
				Html::rawElement(
					'div',
					[ 'style' => 'margin: 1em auto; text-align: center;' ],
					$this->image( $opts )
				)
		);
	}

	/**
	 * Constructs a table row with label and input in two columns.
	 * @param string $name Option name.
	 * @param FormOptions $opts
	 * @param int $width
	 * @return string Html.
	 */
	protected function eInput( $name, FormOptions $opts, $width = 4 ) {
		$value = $opts[$name];

		return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
			Xml::input( $name, $width, $value, [ 'id' => $name ] ) .
			'</td></tr>' . "\n";
	}

	/**
	 * Constructs a label for option.
	 * @param string $name Option name.
	 * @return string Html.
	 */
	protected function eLabel( $name ) {
		// Give grep a chance to find the usages:
		// translate-statsf-width, translate-statsf-height, translate-statsf-start,
		// translate-statsf-days, translate-statsf-scale, translate-statsf-count,
		// translate-statsf-language, translate-statsf-group
		$label = 'translate-statsf-' . $name;
		$label = $this->msg( $label )->escaped();

		return Xml::tags( 'label', [ 'for' => $name ], $label );
	}

	/**
	 * Constructs a table row with label and radio input in two columns.
	 * @param string $name Option name.
	 * @param FormOptions $opts
	 * @param string[] $alts List of alternatives.
	 * @return string Html.
	 */
	protected function eRadio( $name, FormOptions $opts, array $alts ) {
		// Give grep a chance to find the usages:
		// translate-statsf-scale, translate-statsf-count
		$label = 'translate-statsf-' . $name;
		$label = $this->msg( $label )->escaped();
		$s = '<tr><td>' . $label . '</td><td>';

		$options = [];
		foreach ( $alts as $alt ) {
			$id = "$name-$alt";
			$radio = Xml::radio( $name, $alt, $alt === $opts[$name],
				[ 'id' => $id ] ) . ' ';
			$options[] = $radio . ' ' . $this->eLabel( $id );
		}

		$s .= implode( ' ', $options );
		$s .= '</td></tr>' . "\n";

		return $s;
	}

	/**
	 * Constructs a table row with label and language selector in two columns.
	 * @param string $name Option name.
	 * @param FormOptions $opts
	 * @return string Html.
	 */
	protected function eLanguage( $name, FormOptions $opts ) {
		$value = $opts[$name];

		$select = $this->languageSelector();
		$select->setTargetId( 'language' );

		return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
			$select->getHtmlAndPrepareJS() . '<br />' .
			Xml::input( $name, 20, $value, [ 'id' => $name ] ) .
			'</td></tr>' . "\n";
	}

	/**
	 * Constructs a JavaScript enhanced language selector.
	 * @return JsSelectToInput
	 */
	protected function languageSelector() {
		$languages = TranslateUtils::getLanguageNames( $this->getLanguage()->getCode() );

		ksort( $languages );

		$selector = new XmlSelect( 'mw-language-selector', 'mw-language-selector' );
		foreach ( $languages as $code => $name ) {
			$selector->addOption( "$code - $name", $code );
		}

		$jsSelect = new JsSelectToInput( $selector );

		return $jsSelect;
	}

	/**
	 * Constructs a table row with label and group selector in two columns.
	 * @param string $name Option name.
	 * @param FormOptions $opts
	 * @return string Html.
	 */
	protected function eGroup( $name, FormOptions $opts ) {
		$value = $opts[$name];

		$select = $this->groupSelector();
		$select->setTargetId( 'group' );

		return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
			$select->getHtmlAndPrepareJS() . '<br />' .
			Xml::input( $name, 20, $value, [ 'id' => $name ] ) .
			'</td></tr>' . "\n";
	}

	/**
	 * Constructs a JavaScript enhanced group selector.
	 * @return JsSelectToInput
	 */
	protected function groupSelector() {
		$groups = MessageGroups::singleton()->getGroups();
		/**
		 * @var MessageGroup $group
		 */
		foreach ( $groups as $key => $group ) {
			if ( !$group->exists() ) {
				unset( $groups[$key] );
				continue;
			}
		}

		ksort( $groups );

		$selector = new XmlSelect( 'mw-group-selector', 'mw-group-selector' );
		/**
		 * @var MessageGroup $name
		 */
		foreach ( $groups as $code => $name ) {
			$selector->addOption( $name->getLabel(), $code );
		}

		$jsSelect = new JsSelectToInput( $selector );

		return $jsSelect;
	}

	/**
	 * Returns an \<img> tag for graph.
	 * @param FormOptions $opts
	 * @return string Html.
	 */
	protected function image( FormOptions $opts ) {
		$title = $this->getPageTitle();

		$params = $opts->getChangedValues();
		$params[ 'graphit' ] = true;
		$src = $title->getLocalURL( $params );

		$srcsets = [];
		foreach ( [ 1.5, 2, 3 ] as $scale ) {
			$params[ 'imagescale' ] = $scale;
			$srcsets[] = "{$title->getLocalURL( $params )} {$scale}x";
		}

		return Xml::element( 'img',
			[
				'src' => $src,
				'srcset' => implode( ', ', $srcsets ),
				'width' => $opts['width'],
				'height' => $opts['height'],
			]
		);
	}

	/**
	 * Fetches and preprocesses graph data that can be fed to graph drawer.
	 * @param FormOptions $opts
	 * @return array ( string => array ) Data indexed by their date labels.
	 */
	protected function getData( FormOptions $opts ) {
		$dbr = wfGetDB( DB_REPLICA );

		$class = $this->getGraphClass( $opts['count'] );
		$so = new $class( $opts );

		$fixedStart = $opts->getValue( 'start' ) !== '';

		$now = time();
		$period = 3600 * 24 * $opts->getValue( 'days' );

		if ( $fixedStart ) {
			$cutoff = wfTimestamp( TS_UNIX, $opts->getValue( 'start' ) );
		} else {
			$cutoff = $now - $period;
		}
		$cutoff = self::roundTimestampToCutoff( $opts['scale'], $cutoff, 'earlier' );

		$start = $cutoff;

		if ( $fixedStart ) {
			$end = self::roundTimestampToCutoff( $opts['scale'], $start + $period, 'later' ) - 1;
		} else {
			$end = null;
		}

		$tables = [];
		$fields = [];
		$conds = [];
		$type = __METHOD__;
		$options = [];
		$joins = [];

		$so->preQuery( $tables, $fields, $conds, $type, $options, $joins, $start, $end );
		$res = $dbr->select( $tables, $fields, $conds, $type, $options, $joins );
		wfDebug( __METHOD__ . "-queryend\n" );

		// Start processing the data
		$dateFormat = $so->getDateFormat();
		$increment = self::getIncrement( $opts['scale'] );

		$labels = $so->labels();
		$keys = array_keys( $labels );
		$values = array_pad( [], count( $labels ), 0 );
		$defaults = array_combine( $keys, $values );

		$data = [];
		// Allow 10 seconds in the future for processing time
		$lastValue = $end !== null ? $end : $now + 10;
		$lang = $this->getLanguage();
		while ( $cutoff <= $lastValue ) {
			$date = $lang->sprintfDate( $dateFormat, wfTimestamp( TS_MW, $cutoff ) );
			$cutoff += $increment;
			$data[$date] = $defaults;
		}

		// Processing
		$labelToIndex = array_flip( $labels );

		foreach ( $res as $row ) {
			$indexLabels = $so->indexOf( $row );
			if ( $indexLabels === false ) {
				continue;
			}

			foreach ( (array)$indexLabels as $i ) {
				if ( !isset( $labelToIndex[$i] ) ) {
					continue;
				}
				$date = $lang->sprintfDate( $dateFormat, $so->getTimestamp( $row ) );
				// Ignore values outside range
				if ( !isset( $data[$date] ) ) {
					continue;
				}

				$data[$date][$labelToIndex[$i]]++;
			}
		}

		// Don't display dummy label
		if ( count( $labels ) === 1 && $labels[0] === 'all' ) {
			$labels = [];
		}

		foreach ( $labels as &$label ) {
			if ( strpos( $label, '@' ) === false ) {
				continue;
			}
			list( $groupId, $code ) = explode( '@', $label, 2 );
			if ( $code && $groupId ) {
				$code = TranslateUtils::getLanguageName( $code, $lang->getCode() ) . " ($code)";
				$group = MessageGroups::getGroup( $groupId );
				$group = $group ? $group->getLabel() : $groupId;
				$label = "$group @ $code";
			} elseif ( $code ) {
				$label = TranslateUtils::getLanguageName( $code, $lang->getCode() ) . " ($code)";
			} elseif ( $groupId ) {
				$group = MessageGroups::getGroup( $groupId );
				$label = $group ? $group->getLabel() : $groupId;
			}
		}

		if ( $end === null ) {
			$last = array_splice( $data, -1, 1 );
			// Indicator that the last value is not full
			$data[key( $last ) . '*'] = current( $last );
		}

		return [ $labels, $data ];
	}

	/**
	 * Gets the closest earlieast timestamp that corresponds to start of a
	 * period in given scale, like, midnight, monday or first day of the month.
	 * @param string $scale One of hours, days, weeks, months
	 * @param int $cutoff Timestamp in unix format.
	 * @param string $direction One of earlier, later
	 * @return int
	 */
	protected static function roundTimestampToCutoff( $scale, $cutoff, $direction = 'earlier' ) {
		$dir = $direction === 'earlier' ? -1 : 1;

		/* Ensure that the first item in the graph has full data even
		* if it doesn't align with the given 'days' boundary */
		if ( $scale === 'hours' ) {
			$cutoff += self::roundingAddition( $cutoff, 3600, $dir );
		} elseif ( $scale === 'days' ) {
			$cutoff += self::roundingAddition( $cutoff, 86400, $dir );
		} elseif ( $scale === 'weeks' ) {
			/* Here we assume that week starts on monday, which does not
			* always hold true. Go Xwards day by day until we are on monday */
			while ( date( 'D', $cutoff ) !== 'Mon' ) {
				$cutoff += $dir * 86400;
			}
			// Round to nearest day
			$cutoff -= ( $cutoff % 86400 );
		} elseif ( $scale === 'months' ) {
			// Go Xwards/ day by day until we are on the first day of the month
			while ( date( 'j', $cutoff ) !== '1' ) {
				$cutoff += $dir * 86400;
			}
			// Round to nearest day
			$cutoff -= ( $cutoff % 86400 );
		}

		return $cutoff;
	}

	/**
	 * @param int $ts
	 * @param int $amount
	 * @param int $dir
	 * @return int
	 */
	protected static function roundingAddition( $ts, $amount, $dir ) {
		if ( $dir === -1 ) {
			return -1 * ( $ts % $amount );
		} else {
			return $amount - ( $ts % $amount );
		}
	}

	/**
	 * Adds raw image data of the graph to the output.
	 * @param FormOptions $opts
	 */
	public function draw( FormOptions $opts ) {
		global $wgTranslatePHPlotFont;

		$imageScale = $opts->getValue( 'imagescale' );
		$width = $opts->getValue( 'width' );
		$height = $opts->getValue( 'height' );
		// Define the object
		$plot = new PHPlot( $width * $imageScale, $height * $imageScale );

		list( $legend, $resData ) = $this->getData( $opts );
		$count = count( $resData );
		$skip = (int)( $count / ( $width / 60 ) - 1 );
		$i = $count;
		$data = [];

		foreach ( $resData as $date => $edits ) {
			if ( $skip > 0 &&
				( $count - $i ) % $skip !== 0
			) {
				$date = '';
			}

			if ( strpos( $date, ';' ) !== false ) {
				list( , $date ) = explode( ';', $date, 2 );
			}

			array_unshift( $edits, $date );
			$data[] = $edits;
			$i--;
		}

		$font = FCFontFinder::findFile( $this->getLanguage()->getCode() );
		if ( !$font ) {
			$font = $wgTranslatePHPlotFont;
		}
		$numberFont = FCFontFinder::findFile( 'en' );
		$plot->SetDefaultTTFont( $font );
		$plot->SetFontTTF( 'generic', $font, 12 * $imageScale );
		$plot->SetFontTTF( 'legend', $font, 12 * $imageScale );
		$plot->SetFontTTF( 'x_title', $font, 10 * $imageScale );
		$plot->SetFontTTF( 'y_title', $font, 10 * $imageScale );
		$plot->SetFontTTF( 'x_label', $numberFont, 8 * $imageScale );
		$plot->SetFontTTF( 'y_label', $numberFont, 8 * $imageScale );

		$plot->SetDataValues( $data );

		if ( $legend !== null ) {
			$plot->SetLegend( $legend );
		}

		// Give grep a chance to find the usages:
		// translate-stats-edits, translate-stats-users, translate-stats-registrations,
		// translate-stats-reviews, translate-stats-reviewers
		$yTitle = $this->msg( 'translate-stats-' . $opts['count'] )->escaped();

		// Turn off X axis ticks and labels because they get in the way:
		$plot->SetYTitle( $yTitle );
		$plot->SetXTickLabelPos( 'none' );
		$plot->SetXTickPos( 'none' );
		$plot->SetXLabelAngle( 45 );

		$max = max( array_map( 'max', $resData ) );
		$max = self::roundToSignificant( $max, 1 );
		$max = round( $max, (int)( -log( $max, 10 ) ) );

		$yTick = 10;
		while ( $max / $yTick > $height / 20 ) {
			$yTick *= 2;
		}

		// If we have very small case, ensure that there is at least one tick
		$yTick = min( $max, $yTick );
		$yTick = self::roundToSignificant( $yTick );
		$plot->SetYTickIncrement( $yTick );
		$plot->SetPlotAreaWorld( null, 0, null, max( $max, 10 ) );

		$plot->SetTransparentColor( 'white' );
		$plot->SetBackgroundColor( 'white' );

		// Draw it
		$plot->DrawGraph();
	}

	/**
	 * Enhanced version of round that supports rounding up to a given scale
	 * relative to the number itself. Examples:
	 * - roundToSignificant( 1234, 0 ) = 10000
	 * - roundToSignificant( 1234, 1 ) = 2000
	 * - roundToSignificant( 1234, 2 ) = 1300
	 * - roundToSignificant( 1234, 3 ) = 1240
	 *
	 * @param int $number Number to round.
	 * @param int $significant How many signficant numbers to keep.
	 * @return int Rounded number.
	 */
	public static function roundToSignificant( $number, $significant = 1 ) {
		$log = (int)log( $number, 10 );
		$nonSignificant = max( 0, $log - $significant + 1 );
		$factor = pow( 10, $nonSignificant );

		return (int)( ceil( $number / $factor ) * $factor );
	}

	/**
	 * Returns an increment in seconds for a given scale.
	 * The increment must be small enough that we will hit every item in the
	 * scale when using different multiples of the increment. It should be
	 * large enough to avoid hitting the same item multiple times.
	 * @param string $scale Either months, weeks, days or hours.
	 * @return int Number of seconds in the increment.
	 */
	public static function getIncrement( $scale ) {
		$increment = 3600 * 24;
		if ( $scale === 'months' ) {
			/* We use increment to fill up the values. Use number small enough
			 * to ensure we hit each month */
			$increment = 3600 * 24 * 15;
		} elseif ( $scale === 'weeks' ) {
			$increment = 3600 * 24 * 7;
		} elseif ( $scale === 'hours' ) {
			$increment = 3600;
		}

		return $increment;
	}
}

/**
 * Interface for producing different kinds of graphs.
 * The graphs are based on data queried from the database.
 * @ingroup Stats
 */
interface TranslationStatsInterface {
	/**
	 * Constructor. The implementation can access the graph options, but not
	 * define new ones.
	 * @param FormOptions $opts
	 */
	public function __construct( FormOptions $opts );

	/**
	 * Query details that the graph must fill.
	 * @param array &$tables Empty list. Append table names.
	 * @param array &$fields Empty list. Append field names.
	 * @param array &$conds Empty array. Append select conditions.
	 * @param string &$type Append graph type (used to identify queries).
	 * @param array &$options Empty array. Append extra query options.
	 * @param array &$joins Empty array. Append extra join conditions.
	 * @param string $start Precalculated start cutoff timestamp
	 * @param string $end Precalculated end cutoff timestamp
	 */
	public function preQuery( &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end );

	/**
	 * Return the indexes which this result contributes to.
	 * Return 'all' if only one variable is measured. Return false if none.
	 * @param array $row Database Result Row
	 */
	public function indexOf( $row );

	/**
	 * Return the names of the variables being measured.
	 * Return 'all' if only one variable is measured. Must match indexes
	 * returned by indexOf() and contain them all.
	 * @return string[]
	 */
	public function labels();

	/**
	 * Return the timestamp associated with this result row.
	 * @param array $row Database Result Row
	 * @return string Timestamp.
	 */
	public function getTimestamp( $row );

	/**
	 * Return time formatting string.
	 * @see Language::sprintfDate()
	 * @return string
	 */
	public function getDateFormat();
}

/**
 * Provides some hand default implementations for TranslationStatsInterface.
 * @ingroup Stats
 */
abstract class TranslationStatsBase implements TranslationStatsInterface {
	/**
	 * @var FormOptions Graph options.
	 */
	protected $opts;

	public function __construct( FormOptions $opts ) {
		$this->opts = $opts;
	}

	public function indexOf( $row ) {
		return [ 'all' ];
	}

	public function labels() {
		return [ 'all' ];
	}

	public function getDateFormat() {
		$dateFormat = 'Y-m-d';
		if ( $this->opts['scale'] === 'months' ) {
			$dateFormat = 'Y-m';
		} elseif ( $this->opts['scale'] === 'weeks' ) {
			$dateFormat = 'Y-\WW';
		} elseif ( $this->opts['scale'] === 'hours' ) {
			$dateFormat .= ';H';
		}

		return $dateFormat;
	}

	protected static function makeTimeCondition( $field, $start, $end ) {
		$db = wfGetDB( DB_REPLICA );

		$conds = [];
		if ( $start !== null ) {
			$conds[] = "$field >= '{$db->timestamp( $start )}'";
		}
		if ( $end !== null ) {
			$conds[] = "$field <= '{$db->timestamp( $end )}'";
		}

		return $conds;
	}

	/**
	 * @since 2012-03-05
	 * @param array $groupIds
	 * @return array
	 */
	protected static function namespacesFromGroups( $groupIds ) {
		$namespaces = [];
		foreach ( $groupIds as $id ) {
			$group = MessageGroups::getGroup( $id );
			if ( $group ) {
				$namespace = $group->getNamespace();
				$namespaces[$namespace] = true;
			}
		}

		return array_keys( $namespaces );
	}
}

/**
 * Graph which provides statistics on active users and number of translations.
 * @ingroup Stats
 */
class TranslatePerLanguageStats extends TranslationStatsBase {
	/** @var bool[] array( string => bool ) Cache used to count active users only once per day. */
	protected $usercache;

	protected $codes, $groups;

	public function __construct( FormOptions $opts ) {
		parent::__construct( $opts );
		// This query is slow... ensure a lower limit.
		$opts->validateIntBounds( 'days', 1, 200 );
	}

	public function preQuery( &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end ) {
		global $wgTranslateMessageNamespaces;

		$db = wfGetDB( DB_REPLICA );

		$tables = [ 'recentchanges' ];
		$fields = [ 'rc_timestamp' ];
		$joins = [];

		$conds = [
			'rc_namespace' => $wgTranslateMessageNamespaces,
			'rc_bot' => 0,
			'rc_type != ' . RC_LOG,
		];

		$timeConds = self::makeTimeCondition( 'rc_timestamp', $start, $end );
		$conds = array_merge( $conds, $timeConds );

		$options = [ 'ORDER BY' => 'rc_timestamp' ];

		$this->groups = array_filter( array_map( 'trim', explode( ',', $this->opts['group'] ) ) );
		$this->groups = array_map( 'MessageGroups::normalizeId', $this->groups );
		$this->codes = array_filter( array_map( 'trim', explode( ',', $this->opts['language'] ) ) );

		$namespaces = self::namespacesFromGroups( $this->groups );
		if ( count( $namespaces ) ) {
			$conds['rc_namespace'] = $namespaces;
		}

		$languages = [];
		foreach ( $this->codes as $code ) {
			$languages[] = 'rc_title ' . $db->buildLike( $db->anyString(), "/$code" );
		}
		if ( count( $languages ) ) {
			$conds[] = $db->makeList( $languages, LIST_OR );
		}

		$fields[] = 'rc_title';

		if ( $this->groups ) {
			$fields[] = 'rc_namespace';
		}

		if ( $this->opts['count'] === 'users' ) {
			if ( class_exists( ActorMigration::class ) ) {
				$actorQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
				$tables += $actorQuery['tables'];
				$fields['rc_user_text'] = $actorQuery['fields']['rc_user_text'];
				$joins += $actorQuery['joins'];
			} else {
				$fields[] = 'rc_user_text';
			}
		}

		$type .= '-perlang';
	}

	public function indexOf( $row ) {
		// We need to check that there is only one user per day.
		if ( $this->opts['count'] === 'users' ) {
			$date = $this->formatTimestamp( $row->rc_timestamp );

			if ( isset( $this->usercache[$date][$row->rc_user_text] ) ) {
				return -1;
			} else {
				$this->usercache[$date][$row->rc_user_text] = 1;
			}
		}

		// Do not consider language-less pages.
		if ( strpos( $row->rc_title, '/' ) === false ) {
			return false;
		}

		// No filters, just one key to track.
		if ( !$this->groups && !$this->codes ) {
			return 'all';
		}

		// The key-building needs to be in sync with ::labels().
		list( $key, $code ) = TranslateUtils::figureMessage( $row->rc_title );

		$groups = [];
		$codes = [];

		if ( $this->groups ) {
			/*
			 * Get list of keys that the message belongs to, and filter
			 * out those which are not requested.
			 */
			$groups = TranslateUtils::messageKeyToGroups( $row->rc_namespace, $key );
			$groups = array_intersect( $this->groups, $groups );
		}

		if ( $this->codes ) {
			$codes = [ $code ];
		}

		return $this->combineTwoArrays( $groups, $codes );
	}

	public function labels() {
		return $this->combineTwoArrays( $this->groups, $this->codes );
	}

	public function getTimestamp( $row ) {
		return $row->rc_timestamp;
	}

	/**
	 * Makes a label for variable. If group or language code filters, or both
	 * are used, combine those in a pretty way.
	 * @param string $group Group name.
	 * @param string $code Language code.
	 * @return string Label.
	 */
	protected function makeLabel( $group, $code ) {
		if ( $group || $code ) {
			return "$group@$code";
		} else {
			return 'all';
		}
	}

	/**
	 * Cross-product of two lists with string results, where either
	 * list can be empty.
	 * @param string[] $groups Group names.
	 * @param string[] $codes Language codes.
	 * @return string[] Labels.
	 */
	protected function combineTwoArrays( $groups, $codes ) {
		if ( !count( $groups ) ) {
			$groups[] = false;
		}

		if ( !count( $codes ) ) {
			$codes[] = false;
		}

		$items = [];
		foreach ( $groups as $group ) {
			foreach ( $codes as $code ) {
				$items[] = $this->makeLabel( $group, $code );
			}
		}

		return $items;
	}

	/**
	 * Returns unique index for given item in the scale being used.
	 * Called a lot, so performance intensive.
	 * @param string $timestamp Timestamp in mediawiki format.
	 * @return string
	 */
	protected function formatTimestamp( $timestamp ) {
		global $wgContLang;

		switch ( $this->opts['scale'] ) {
			case 'hours' :
				$cut = 4;
				break;
			case 'days' :
				$cut = 6;
				break;
			case 'months':
				$cut = 8;
				break;
			default :
				return $wgContLang->sprintfDate( $this->getDateFormat(), $timestamp );
		}

		return substr( $timestamp, 0, -$cut );
	}
}

/**
 * Graph which provides statistics about amount of registered users in a given time.
 * @ingroup Stats
 */
class TranslateRegistrationStats extends TranslationStatsBase {
	public function preQuery( &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end ) {
		$tables = 'user';
		$fields = 'user_registration';
		$conds = self::makeTimeCondition( 'user_registration', $start, $end );
		$type .= '-registration';
		$options = [];
		$joins = [];
	}

	public function getTimestamp( $row ) {
		return $row->user_registration;
	}
}

/**
 * Graph which provides statistics on number of reviews and reviewers.
 * @since 2012-03-05
 * @ingroup Stats
 */
class ReviewPerLanguageStats extends TranslatePerLanguageStats {
	public function preQuery( &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end ) {
		global $wgTranslateMessageNamespaces;

		$db = wfGetDB( DB_REPLICA );

		$tables = [ 'logging' ];
		$fields = [ 'log_timestamp' ];
		$joins = [];

		$conds = [
			'log_namespace' => $wgTranslateMessageNamespaces,
			'log_action' => 'message',
		];

		$timeConds = self::makeTimeCondition( 'log_timestamp', $start, $end );
		$conds = array_merge( $conds, $timeConds );

		$options = [ 'ORDER BY' => 'log_timestamp' ];

		$this->groups = array_filter( array_map( 'trim', explode( ',', $this->opts['group'] ) ) );
		$this->codes = array_filter( array_map( 'trim', explode( ',', $this->opts['language'] ) ) );

		$namespaces = self::namespacesFromGroups( $this->groups );
		if ( count( $namespaces ) ) {
			$conds['log_namespace'] = $namespaces;
		}

		$languages = [];
		foreach ( $this->codes as $code ) {
			$languages[] = 'log_title ' . $db->buildLike( $db->anyString(), "/$code" );
		}
		if ( count( $languages ) ) {
			$conds[] = $db->makeList( $languages, LIST_OR );
		}

		$fields[] = 'log_title';

		if ( $this->groups ) {
			$fields[] = 'log_namespace';
		}

		if ( $this->opts['count'] === 'reviewers' ) {
			if ( class_exists( ActorMigration::class ) ) {
				$actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
				$tables += $actorQuery['tables'];
				$fields['log_user_text'] = $actorQuery['fields']['log_user_text'];
				$joins += $actorQuery['joins'];
			} else {
				$fields[] = 'log_user_text';
			}
		}

		$type .= '-reviews';
	}

	public function indexOf( $row ) {
		// We need to check that there is only one user per day.
		if ( $this->opts['count'] === 'reviewers' ) {
			$date = $this->formatTimestamp( $row->log_timestamp );

			if ( isset( $this->usercache[$date][$row->log_user_text] ) ) {
				return -1;
			} else {
				$this->usercache[$date][$row->log_user_text] = 1;
			}
		}

		// Do not consider language-less pages.
		if ( strpos( $row->log_title, '/' ) === false ) {
			return false;
		}

		// No filters, just one key to track.
		if ( !$this->groups && !$this->codes ) {
			return 'all';
		}

		// The key-building needs to be in sync with ::labels().
		list( $key, $code ) = TranslateUtils::figureMessage( $row->log_title );

		$groups = [];
		$codes = [];

		if ( $this->groups ) {
			/* Get list of keys that the message belongs to, and filter
			 * out those which are not requested. */
			$groups = TranslateUtils::messageKeyToGroups( $row->log_namespace, $key );
			$groups = array_intersect( $this->groups, $groups );
		}

		if ( $this->codes ) {
			$codes = [ $code ];
		}

		return $this->combineTwoArrays( $groups, $codes );
	}

	public function labels() {
		return $this->combineTwoArrays( $this->groups, $this->codes );
	}

	public function getTimestamp( $row ) {
		return $row->log_timestamp;
	}
}