Completed
Branch master (0c9f05)
by
unknown
29:21
created

getExternalLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
	 * @return string[]
35
	 */
36
	public function getCategories( ParserOutput $parserOutput ) {
37
		$categories = [];
38
39
		foreach ( $parserOutput->getCategoryLinks() as $key ) {
40
			$categories[] = Category::newFromName( $key )->getTitle()->getText();
41
		}
42
43
		return $categories;
44
	}
45
46
	/**
47
	 * Get a list of external links from ParserOutput, as an array of strings.
48
	 *
49
	 * @return string[]
50
	 */
51
	public function getExternalLinks( ParserOutput $parserOutput ) {
52
		return array_keys( $parserOutput->getExternalLinks() );
53
	}
54
55
	/**
56
	 * Get a list of outgoing wiki links (including interwiki links), as
57
	 * an array of prefixed title strings.
58
	 *
59
	 * @return string[]
60
	 */
61 View Code Duplication
	public function getOutgoingLinks( ParserOutput $parserOutput ) {
62
		$outgoingLinks = [];
63
64
		foreach ( $parserOutput->getLinks() as $linkedNamespace => $namespaceLinks ) {
65
			foreach ( array_keys( $namespaceLinks ) as $linkedDbKey ) {
66
				$outgoingLinks[] =
67
					Title::makeTitle( $linkedNamespace, $linkedDbKey )->getPrefixedDBkey();
68
			}
69
		}
70
71
		return $outgoingLinks;
72
	}
73
74
	/**
75
	 * Get a list of templates used in the ParserOutput content, as prefixed title strings
76
	 *
77
	 * @return string[]
78
	 */
79 View Code Duplication
	public function getTemplates( ParserOutput $parserOutput ) {
80
		$templates = [];
81
82
		foreach ( $parserOutput->getTemplates() as $tNS => $templatesInNS ) {
83
			foreach ( array_keys( $templatesInNS ) as $tDbKey ) {
84
				$templateTitle = Title::makeTitle( $tNS, $tDbKey );
85
				$templates[] = $templateTitle->getPrefixedText();
86
			}
87
		}
88
89
		return $templates;
90
	}
91
92
}
93