Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationStatsGraphOptions.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use FormOptions;
7
16 private $formOptions;
18 public const VALID_SCALES = [ 'years', 'months', 'weeks', 'days', 'hours' ];
19
20 public function __construct() {
21 $this->formOptions = new FormOptions();
22 $this->formOptions->add( 'preview', false );
23 $this->formOptions->add( 'language', [] );
24 $this->formOptions->add( 'count', 'edits' );
25 $this->formOptions->add( 'scale', 'days' );
26 $this->formOptions->add( 'days', 30 );
27 $this->formOptions->add( 'width', 800 );
28 $this->formOptions->add( 'height', 600 );
29 $this->formOptions->add( 'group', [] );
30 $this->formOptions->add( 'uselang', '' );
31 $this->formOptions->add( 'start', '' );
32 }
33
34 public function bindArray( array $inputs ): void {
35 foreach ( $inputs as $key => $value ) {
36 if ( $this->formOptions->validateName( $key ) ) {
37 $this->formOptions[$key] = $value;
38 }
39 }
40 }
41
42 public function hasValue( string $key ): bool {
43 return isset( $this->formOptions[$key] );
44 }
45
46 public function setValue( string $key, $value ): void {
47 $this->formOptions[$key] = $value;
48 }
49
50 public function getValue( string $key ) {
51 return $this->formOptions[$key];
52 }
53
54 public function normalize( array $validCounts ): void {
55 $this->formOptions->validateIntBounds( 'days', 1, 10000 );
56 $this->formOptions->validateIntBounds( 'width', 200, 1000 );
57 $this->formOptions->validateIntBounds( 'height', 200, 1000 );
58
59 if ( $this->formOptions['start'] !== '' ) {
60 $timestamp = wfTimestamp( TS_ISO_8601, $this->formOptions['start'] );
61 if ( $timestamp ) {
62 $this->formOptions['start'] = rtrim( $timestamp, 'Z' );
63 } else {
64 $this->formOptions['start'] = '';
65 }
66 }
67
68 if ( !in_array( $this->formOptions['scale'], self::VALID_SCALES ) ) {
69 $this->formOptions['scale'] = 'days';
70 }
71
72 if ( $this->formOptions['scale'] === 'hours' ) {
73 $this->formOptions->validateIntBounds( 'days', 1, 4 );
74 }
75
76 if ( !in_array( $this->formOptions['count'], $validCounts ) ) {
77 $this->formOptions['count'] = 'edits';
78 }
79
80 foreach ( [ 'group', 'language' ] as $t ) {
81 if ( is_string( $this->formOptions[$t] ) ) {
82 $this->formOptions[$t] = explode( ',', $this->formOptions[$t] );
83 }
84
85 $values = array_map( 'trim', $this->formOptions[$t] );
86 $values = array_splice( $values, 0, 4 );
87 if ( $t === 'group' ) {
88 // BC for old syntax which replaced _ to | which was not allowed
89 $values = preg_replace( '~^page_~', 'page-', $values );
90 }
91 $this->formOptions[$t] = $values;
92 }
93 }
94
95 public function getGroups(): array {
96 return $this->formOptions['group'];
97 }
98
99 public function getLanguages(): array {
100 return $this->formOptions['language'];
101 }
102
103 public function getFormOptions(): FormOptions {
104 return $this->formOptions;
105 }
106
107 public function boundValue( string $key, int $min, int $max ): void {
108 $this->formOptions->validateIntBounds( $key, $min, $max );
109 }
110}