Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
ParserOutputSearchDataExtractor | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
getCategories | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getExternalLinks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOutgoingLinks | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getTemplates | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Search; |
4 | |
5 | use MediaWiki\Category\Category; |
6 | use MediaWiki\Parser\ParserOutput; |
7 | use MediaWiki\Title\Title; |
8 | |
9 | /** |
10 | * Extracts data from ParserOutput for indexing in the search engine. |
11 | * |
12 | * This program is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * |
17 | * This program is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * You should have received a copy of the GNU General Public License along |
23 | * with this program; if not, write to the Free Software Foundation, Inc., |
24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 | * http://www.gnu.org/copyleft/gpl.html |
26 | * |
27 | * @since 1.28 |
28 | */ |
29 | class ParserOutputSearchDataExtractor { |
30 | |
31 | /** |
32 | * Get a list of categories, as an array with title text strings. |
33 | * |
34 | * @param ParserOutput $parserOutput |
35 | * @return string[] |
36 | */ |
37 | public function getCategories( ParserOutput $parserOutput ) { |
38 | $categories = []; |
39 | |
40 | foreach ( $parserOutput->getCategoryNames() as $key ) { |
41 | $name = Category::newFromName( $key )->getName(); |
42 | $categories[] = str_replace( '_', ' ', $name ); |
43 | } |
44 | |
45 | return $categories; |
46 | } |
47 | |
48 | /** |
49 | * Get a list of external links from ParserOutput, as an array of strings. |
50 | * |
51 | * @param ParserOutput $parserOutput |
52 | * @return string[] |
53 | */ |
54 | public function getExternalLinks( ParserOutput $parserOutput ) { |
55 | return array_keys( $parserOutput->getExternalLinks() ); |
56 | } |
57 | |
58 | /** |
59 | * Get a list of outgoing wiki links (including interwiki links), as |
60 | * an array of prefixed title strings. |
61 | * |
62 | * @param ParserOutput $parserOutput |
63 | * @return string[] |
64 | */ |
65 | public function getOutgoingLinks( ParserOutput $parserOutput ) { |
66 | $outgoingLinks = []; |
67 | |
68 | foreach ( $parserOutput->getLinks() as $linkedNamespace => $namespaceLinks ) { |
69 | foreach ( $namespaceLinks as $linkedDbKey => $_ ) { |
70 | $outgoingLinks[] = |
71 | Title::makeTitle( $linkedNamespace, $linkedDbKey )->getPrefixedDBkey(); |
72 | } |
73 | } |
74 | |
75 | return $outgoingLinks; |
76 | } |
77 | |
78 | /** |
79 | * Get a list of templates used in the ParserOutput content, as prefixed title strings |
80 | * |
81 | * @param ParserOutput $parserOutput |
82 | * @return string[] |
83 | */ |
84 | public function getTemplates( ParserOutput $parserOutput ) { |
85 | $templates = []; |
86 | |
87 | foreach ( $parserOutput->getTemplates() as $tNS => $templatesInNS ) { |
88 | foreach ( $templatesInNS as $tDbKey => $_ ) { |
89 | $templateTitle = Title::makeTitle( $tNS, $tDbKey ); |
90 | $templates[] = $templateTitle->getPrefixedText(); |
91 | } |
92 | } |
93 | |
94 | return $templates; |
95 | } |
96 | |
97 | } |