Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
6 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Validator
50.00% covered (danger)
50.00%
6 / 12
50.00% covered (danger)
50.00%
1 / 2
6.00
0.00% covered (danger)
0.00%
0 / 1
 validateParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getValidParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20declare( strict_types=1 );
21
22namespace MediaWiki\Extension\WikiSEO;
23
24/**
25 * Class Validator
26 *
27 * @package MediaWiki\Extension\WikiSEO
28 */
29class Validator {
30    /**
31     * Valid parameter names across all generators
32     * @see WikiSEO::mergeValidTags()
33     *
34     * @var string[]
35     */
36    public static $validParameterNames = [
37        // This needs to be the first entry see getValidParams()
38        'hreflang_%',
39
40        'title',
41        'title_mode',
42        'title_separator',
43
44        'keywords',
45        'description',
46        'canonical',
47
48        'robots',
49        'googlebot',
50
51        'image', 'image_width', 'image_height', 'image_alt',
52
53        'type',
54        'site_name',
55        'locale',
56        'section',
57        'author',
58
59        'published_time',
60
61        'twitter_site',
62    ];
63
64    /**
65     * https://stackoverflow.com/a/3191729
66     *
67     * @var array Language codes
68     */
69    public static $isoLanguageCodes = [
70        'af-za',
71        'am-et',
72        'ar-ae',
73        'ar-bh',
74        'ar-dz',
75        'ar-eg',
76        'ar-iq',
77        'ar-jo',
78        'ar-kw',
79        'ar-lb',
80        'ar-ly',
81        'ar-ma',
82        'arn-cl',
83        'ar-om',
84        'ar-qa',
85        'ar-sa',
86        'ar-sy',
87        'ar-tn',
88        'ar-ye',
89        'as-in',
90        'az-cyrl-az',
91        'az-latn-az',
92        'ba-ru',
93        'be-by',
94        'bg-bg',
95        'bn-bd',
96        'bn-in',
97        'bo-cn',
98        'br-fr',
99        'bs-cyrl-ba',
100        'bs-latn-ba',
101        'ca-es',
102        'co-fr',
103        'cs-cz',
104        'cy-gb',
105        'da-dk',
106        'de-at',
107        'de-ch',
108        'de-de',
109        'de-li',
110        'de-lu',
111        'dsb-de',
112        'dv-mv',
113        'el-gr',
114        'en-029',
115        'en-au',
116        'en-bz',
117        'en-ca',
118        'en-gb',
119        'en-ie',
120        'en-in',
121        'en-jm',
122        'en-my',
123        'en-nz',
124        'en-ph',
125        'en-sg',
126        'en-tt',
127        'en-us',
128        'en-za',
129        'en-zw',
130        'es-ar',
131        'es-bo',
132        'es-cl',
133        'es-co',
134        'es-cr',
135        'es-do',
136        'es-ec',
137        'es-es',
138        'es-gt',
139        'es-hn',
140        'es-mx',
141        'es-ni',
142        'es-pa',
143        'es-pe',
144        'es-pr',
145        'es-py',
146        'es-sv',
147        'es-us',
148        'es-uy',
149        'es-ve',
150        'et-ee',
151        'eu-es',
152        'fa-ir',
153        'fi-fi',
154        'fil-ph',
155        'fo-fo',
156        'fr-be',
157        'fr-ca',
158        'fr-ch',
159        'fr-fr',
160        'fr-lu',
161        'fr-mc',
162        'fy-nl',
163        'ga-ie',
164        'gd-gb',
165        'gl-es',
166        'gsw-fr',
167        'gu-in',
168        'ha-latn-ng',
169        'he-il',
170        'hi-in',
171        'hr-ba',
172        'hr-hr',
173        'hsb-de',
174        'hu-hu',
175        'hy-am',
176        'id-id',
177        'ig-ng',
178        'ii-cn',
179        'is-is',
180        'it-ch',
181        'it-it',
182        'iu-cans-ca',
183        'iu-latn-ca',
184        'ja-jp',
185        'ka-ge',
186        'kk-kz',
187        'kl-gl',
188        'km-kh',
189        'kn-in',
190        'kok-in',
191        'ko-kr',
192        'ky-kg',
193        'lb-lu',
194        'lo-la',
195        'lt-lt',
196        'lv-lv',
197        'mi-nz',
198        'mk-mk',
199        'ml-in',
200        'mn-mn',
201        'mn-mong-cn',
202        'moh-ca',
203        'mr-in',
204        'ms-bn',
205        'ms-my',
206        'mt-mt',
207        'nb-no',
208        'ne-np',
209        'nl-be',
210        'nl-nl',
211        'nn-no',
212        'nso-za',
213        'oc-fr',
214        'or-in',
215        'pa-in',
216        'pl-pl',
217        'prs-af',
218        'ps-af',
219        'pt-br',
220        'pt-pt',
221        'qut-gt',
222        'quz-bo',
223        'quz-ec',
224        'quz-pe',
225        'rm-ch',
226        'ro-ro',
227        'ru-ru',
228        'rw-rw',
229        'sah-ru',
230        'sa-in',
231        'se-fi',
232        'se-no',
233        'se-se',
234        'si-lk',
235        'sk-sk',
236        'sl-si',
237        'sma-no',
238        'sma-se',
239        'smj-no',
240        'smj-se',
241        'smn-fi',
242        'sms-fi',
243        'sq-al',
244        'sr-cyrl-ba',
245        'sr-cyrl-cs',
246        'sr-cyrl-me',
247        'sr-cyrl-rs',
248        'sr-latn-ba',
249        'sr-latn-cs',
250        'sr-latn-me',
251        'sr-latn-rs',
252        'sv-fi',
253        'sv-se',
254        'sw-ke',
255        'syr-sy',
256        'ta-in',
257        'te-in',
258        'tg-cyrl-tj',
259        'th-th',
260        'tk-tm',
261        'tn-za',
262        'tr-tr',
263        'tt-ru',
264        'tzm-latn-dz',
265        'ug-cn',
266        'uk-ua',
267        'ur-pk',
268        'uz-cyrl-uz',
269        'uz-latn-uz',
270        'vi-vn',
271        'wo-sn',
272        'xh-za',
273        'yo-ng',
274        'zh-cn',
275        'zh-hk',
276        'zh-mo',
277        'zh-sg',
278        'zh-tw',
279        'zu-za',
280    ];
281
282    /**
283     * Removes all params that are not in $validParameterNames
284     *
285     * @param array $params Raw params
286     * @return array Validated params
287     */
288    public static function validateParams( array $params ): array {
289        $validatedParams = [];
290        $validParams = static::getValidParams();
291
292        foreach ( $params as $paramKey => $paramData ) {
293            if ( in_array( $paramKey, $validParams, true ) ) {
294                $validatedParams[$paramKey] = $paramData;
295            }
296        }
297
298        return $validatedParams;
299    }
300
301    /**
302     * Returns an array containing valid parameter names for use in WikiSEO
303     *
304     * @return string[]
305     */
306    public static function getValidParams(): array {
307        $params = self::$validParameterNames;
308
309        $hreflang = array_shift( $params );
310        $params += array_map( static function ( $langCode ) use ( $hreflang ) {
311            return sprintf( $hreflang . 's', $langCode );
312        }, self::$isoLanguageCodes );
313
314        return $params;
315    }
316}