]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/search/ParserOutputSearchDataExtractor.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / search / ParserOutputSearchDataExtractor.php
1 <?php
2
3 namespace MediaWiki\Search;
4
5 use Category;
6 use ParserOutput;
7 use 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->getCategoryLinks() as $key ) {
41                         $categories[] = Category::newFromName( $key )->getTitle()->getText();
42                 }
43
44                 return $categories;
45         }
46
47         /**
48          * Get a list of external links from ParserOutput, as an array of strings.
49          *
50          * @param ParserOutput $parserOutput
51          * @return string[]
52          */
53         public function getExternalLinks( ParserOutput $parserOutput ) {
54                 return array_keys( $parserOutput->getExternalLinks() );
55         }
56
57         /**
58          * Get a list of outgoing wiki links (including interwiki links), as
59          * an array of prefixed title strings.
60          *
61          * @param ParserOutput $parserOutput
62          * @return string[]
63          */
64         public function getOutgoingLinks( ParserOutput $parserOutput ) {
65                 $outgoingLinks = [];
66
67                 foreach ( $parserOutput->getLinks() as $linkedNamespace => $namespaceLinks ) {
68                         foreach ( array_keys( $namespaceLinks ) as $linkedDbKey ) {
69                                 $outgoingLinks[] =
70                                         Title::makeTitle( $linkedNamespace, $linkedDbKey )->getPrefixedDBkey();
71                         }
72                 }
73
74                 return $outgoingLinks;
75         }
76
77         /**
78          * Get a list of templates used in the ParserOutput content, as prefixed title strings
79          *
80          * @param ParserOutput $parserOutput
81          * @return string[]
82          */
83         public function getTemplates( ParserOutput $parserOutput ) {
84                 $templates = [];
85
86                 foreach ( $parserOutput->getTemplates() as $tNS => $templatesInNS ) {
87                         foreach ( array_keys( $templatesInNS ) as $tDbKey ) {
88                                 $templateTitle = Title::makeTitle( $tNS, $tDbKey );
89                                 $templates[] = $templateTitle->getPrefixedText();
90                         }
91                 }
92
93                 return $templates;
94         }
95
96 }