Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
18 / 22 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
TagParser | |
81.82% |
18 / 22 |
|
66.67% |
2 / 3 |
8.38 | |
0.00% |
0 / 1 |
parseArgs | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
parseText | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
expandWikiTextTagArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 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 | |
20 | declare( strict_types=1 ); |
21 | |
22 | namespace MediaWiki\Extension\WikiSEO; |
23 | |
24 | use Parser; |
25 | use PPFrame; |
26 | |
27 | /** |
28 | * Parses tags and expands wikitext |
29 | * |
30 | * @package MediaWiki\Extension\WikiSEO |
31 | */ |
32 | class TagParser { |
33 | /** |
34 | * Parses key value pairs of format 'key=value' |
35 | * |
36 | * @param array $args Key value pairs to parse |
37 | * @param Parser $parser |
38 | * @param PPFrame $frame |
39 | * |
40 | * @return array |
41 | */ |
42 | public function parseArgs( array $args, Parser $parser, PPFrame $frame ): array { |
43 | $results = []; |
44 | |
45 | foreach ( $args as $arg ) { |
46 | $pair = explode( '=', $arg, 2 ); |
47 | $pair = array_map( 'trim', $pair ); |
48 | |
49 | if ( count( $pair ) === 2 ) { |
50 | [ $name, $value ] = $pair; |
51 | $results[$name] = $value; |
52 | } |
53 | } |
54 | |
55 | $results = array_filter( |
56 | $results, static function ( $value, $key ) { |
57 | return mb_strlen( $value ) > 0 && mb_strlen( $key ) > 0; |
58 | }, ARRAY_FILTER_USE_BOTH |
59 | ); |
60 | |
61 | return $this->expandWikiTextTagArray( $results, $parser, $frame ); |
62 | } |
63 | |
64 | /** |
65 | * Parses <seo> tag contents |
66 | * |
67 | * @param string|null $text Tag content |
68 | * @param Parser $parser |
69 | * @param PPFrame $frame |
70 | * |
71 | * @return array |
72 | */ |
73 | public function parseText( ?string $text, Parser $parser, PPFrame $frame ): array { |
74 | if ( $text === null ) { |
75 | return []; |
76 | } |
77 | |
78 | $lines = explode( '|', $text ); |
79 | |
80 | return $this->parseArgs( $lines, $parser, $frame ); |
81 | } |
82 | |
83 | /** |
84 | * Expands <seo> tag wiki text |
85 | * |
86 | * @param array $tags |
87 | * @param Parser $parser |
88 | * @param PPFrame $frame |
89 | * |
90 | * @return array Parsed wiki texts |
91 | */ |
92 | public function expandWikiTextTagArray( array $tags, Parser $parser, PPFrame $frame ): array { |
93 | foreach ( $tags as $key => $tag ) { |
94 | $tags[$key] = $parser->recursiveTagParseFully( $tag, $frame ); |
95 | } |
96 | |
97 | $tags = array_map( 'strip_tags', $tags ); |
98 | $tags = array_map( 'html_entity_decode', $tags ); |
99 | |
100 | return array_map( 'trim', $tags ); |
101 | } |
102 | } |