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