Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
56 / 68
60.00% covered (warning)
60.00%
9 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndexContent
82.35% covered (warning)
82.35%
56 / 68
60.00% covered (warning)
60.00%
9 / 15
47.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFields
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategoriesText
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isEmpty
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 isValid
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 equals
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
9.02
 getWikitextForTransclusion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTextForSummary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSize
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 isCountable
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 matchMagicWord
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getPagelistTagContent
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
5.01
 getLinksToNamespace
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace ProofreadPage\Index;
4
5use Content;
6use MediaWiki\Parser\MagicWord;
7use MediaWiki\Parser\Sanitizer;
8use MediaWiki\Title\Title;
9use ProofreadPage\Link;
10use ProofreadPage\Pagination\PageList;
11use TextContent;
12use WikitextContent;
13
14/**
15 * @license GPL-2.0-or-later
16 *
17 * Content of a Index: page
18 */
19class IndexContent extends TextContent {
20
21    /**
22     * @var WikitextContent[]
23     */
24    private $fields;
25
26    /**
27     * @var Title[]
28     */
29    private $categories;
30
31    /**
32     * @param WikitextContent[] $fields
33     * @param Title[] $categories
34     */
35    public function __construct( array $fields, array $categories = [] ) {
36        $this->fields = $fields;
37        $this->categories = $categories;
38
39        parent::__construct( '', CONTENT_MODEL_PROOFREAD_INDEX );
40    }
41
42    /**
43     * Returns an associative array property name => value as WikitextContent
44     * @return WikitextContent[]
45     */
46    public function getFields() {
47        return $this->fields;
48    }
49
50    /**
51     * @return Title[]
52     */
53    public function getCategories() {
54        return $this->categories;
55    }
56
57    /**
58     * @return string[]
59     */
60    private function getCategoriesText() {
61        return array_map( static function ( Title $title ) {
62            return $title->getText();
63        }, $this->categories );
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function isEmpty() {
70        foreach ( $this->fields as $value ) {
71            if ( !$value->isEmpty() ) {
72                return false;
73            }
74        }
75
76        return !$this->categories;
77    }
78
79    /**
80     * @inheritDoc
81     */
82    public function isValid() {
83        foreach ( $this->categories as $category ) {
84            if ( !$category->isValid() || !$category->inNamespace( NS_CATEGORY ) ) {
85                return false;
86            }
87        }
88        return true;
89    }
90
91    /**
92     * @inheritDoc
93     */
94    public function equals( Content $that = null ) {
95        if ( !( $that instanceof IndexContent ) || $that->getModel() !== $this->getModel() ) {
96            return false;
97        }
98
99        foreach ( $this->fields as $key => $value ) {
100            if ( !array_key_exists( $key, $that->fields ) ||
101                !$value->equals( $that->fields[$key] )
102            ) {
103                return false;
104            }
105        }
106
107        foreach ( $that->fields as $key => $value ) {
108            if ( !array_key_exists( $key, $this->fields ) ||
109                !$value->equals( $this->fields[$key] )
110            ) {
111                return false;
112            }
113        }
114
115        $thisCategories = $this->getCategoriesText();
116        sort( $thisCategories );
117        $thatCategories = $that->getCategoriesText();
118        sort( $thatCategories );
119        return $thisCategories === $thatCategories;
120    }
121
122    /**
123     * @inheritDoc
124     */
125    public function getWikitextForTransclusion() {
126        return $this->serialize( CONTENT_FORMAT_WIKITEXT );
127    }
128
129    /**
130     * @inheritDoc
131     */
132    public function getText() {
133        return $this->serialize();
134    }
135
136    /**
137     * @inheritDoc
138     */
139    public function getTextForSummary( $maxlength = 250 ) {
140        return '';
141    }
142
143    /**
144     * @inheritDoc
145     */
146    public function getSize() {
147        $size = 0;
148
149        foreach ( $this->fields as $value ) {
150            $size += $value->getSize();
151        }
152
153        foreach ( $this->categories as $category ) {
154            $size += strlen( $category->getText() );
155        }
156
157        return $size;
158    }
159
160    /**
161     * @inheritDoc
162     */
163    public function isCountable( $hasLinks = null, Title $title = null ) {
164        foreach ( $this->fields as $value ) {
165            if ( $value->isCountable( $hasLinks, $title ) ) {
166                return true;
167            }
168        }
169
170        return false;
171    }
172
173    /**
174     * @inheritDoc
175     */
176    public function matchMagicWord( MagicWord $word ) {
177        foreach ( $this->fields as $value ) {
178            if ( $value->matchMagicWord( $word ) ) {
179                return true;
180            }
181        }
182
183        return false;
184    }
185
186    /**
187     * @return PageList|null
188     */
189    public function getPagelistTagContent() {
190        $tagParameters = null;
191        foreach ( $this->fields as $field ) {
192            preg_match_all( '/<pagelist([^<]*?)\/>/is',
193                $field->serialize( CONTENT_FORMAT_WIKITEXT ), $m, PREG_PATTERN_ORDER
194            );
195            if ( $m[1] ) {
196                if ( $tagParameters === null ) {
197                    $tagParameters = $m[1];
198                } else {
199                    $tagParameters = array_merge( $tagParameters, $m[1] );
200                }
201            }
202        }
203        if ( $tagParameters === null ) {
204            return $tagParameters;
205        }
206
207        return new PageList( Sanitizer::decodeTagAttributes( implode( $tagParameters ) ) );
208    }
209
210    /**
211     * Returns all links in a given namespace
212     *
213     * @param int $namespace the default namespace id
214     * @return Link[]
215     */
216    public function getLinksToNamespace( int $namespace ): array {
217        $linksExtractor = new WikitextLinksExtractor();
218        $links = [];
219        foreach ( $this->fields as $field ) {
220            $wikitext = $field->serialize( CONTENT_FORMAT_WIKITEXT );
221            $links = array_merge(
222                $links, $linksExtractor->getLinksToNamespace( $wikitext, $namespace )
223            );
224        }
225        return $links;
226    }
227}