Completed
Branch master (dc3656)
by
unknown
30:14
created

WikitextContentHandler::getFieldsForSearchIndex()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 17
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 26
rs 8.8571
1
<?php
2
/**
3
 * Content handler for wiki text pages.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @since 1.21
21
 *
22
 * @file
23
 * @ingroup Content
24
 */
25
26
/**
27
 * Content handler for wiki text pages.
28
 *
29
 * @ingroup Content
30
 */
31
class WikitextContentHandler extends TextContentHandler {
32
33
	public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
34
		parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
35
	}
36
37
	protected function getContentClass() {
38
		return WikitextContent::class;
39
	}
40
41
	/**
42
	 * Returns a WikitextContent object representing a redirect to the given destination page.
43
	 *
44
	 * @param Title $destination The page to redirect to.
45
	 * @param string $text Text to include in the redirect, if possible.
46
	 *
47
	 * @return Content
48
	 *
49
	 * @see ContentHandler::makeRedirectContent
50
	 */
51
	public function makeRedirectContent( Title $destination, $text = '' ) {
52
		$optionalColon = '';
53
54
		if ( $destination->getNamespace() == NS_CATEGORY ) {
55
			$optionalColon = ':';
56
		} else {
57
			$iw = $destination->getInterwiki();
58
			if ( $iw && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
59
				$optionalColon = ':';
60
			}
61
		}
62
63
		$mwRedir = MagicWord::get( 'redirect' );
64
		$redirectText = $mwRedir->getSynonym( 0 ) .
65
			' [[' . $optionalColon . $destination->getFullText() . ']]';
66
67
		if ( $text != '' ) {
68
			$redirectText .= "\n" . $text;
69
		}
70
71
		$class = $this->getContentClass();
72
		return new $class( $redirectText );
73
	}
74
75
	/**
76
	 * Returns true because wikitext supports redirects.
77
	 *
78
	 * @return bool Always true.
79
	 *
80
	 * @see ContentHandler::supportsRedirects
81
	 */
82
	public function supportsRedirects() {
83
		return true;
84
	}
85
86
	/**
87
	 * Returns true because wikitext supports sections.
88
	 *
89
	 * @return bool Always true.
90
	 *
91
	 * @see ContentHandler::supportsSections
92
	 */
93
	public function supportsSections() {
94
		return true;
95
	}
96
97
	/**
98
	 * Returns true, because wikitext supports caching using the
99
	 * ParserCache mechanism.
100
	 *
101
	 * @since 1.21
102
	 *
103
	 * @return bool Always true.
104
	 *
105
	 * @see ContentHandler::isParserCacheSupported
106
	 */
107
	public function isParserCacheSupported() {
108
		return true;
109
	}
110
111
	public function getFieldsForSearchIndex( SearchEngine $engine ) {
112
		$fields = parent::getFieldsForSearchIndex( $engine );
113
114
		$fields['heading'] =
115
			$engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
116
		$fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
117
118
		$fields['auxiliary_text'] =
119
			$engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
120
121
		$fields['opening_text'] =
122
			$engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
123
		$fields['opening_text']->setFlag( SearchIndexField::FLAG_SCORING |
124
		                                  SearchIndexField::FLAG_NO_HIGHLIGHT );
125
126
		// FIXME: this really belongs in separate file handler but files
127
		// do not have separate handler. Sadness.
128
		$fields['file_text'] =
129
			$engine->makeSearchFieldMapping( 'file_text', SearchIndexField::INDEX_TYPE_TEXT );
130
131
		$fields['defaultsort'] = $engine->makeSearchFieldMapping( 'defaultsort',
132
			SearchIndexField::INDEX_TYPE_TEXT );
133
		$fields['defaultsort']->setFlag( SearchIndexField::FLAG_SOURCE_DATA );
134
135
		return $fields;
136
	}
137
138
	/**
139
	 * Extract text of the file
140
	 * TODO: probably should go to file handler?
141
	 * @param Title $title
142
	 * @return string|null
143
	 */
144
	protected function getFileText( Title $title ) {
145
		$file = wfLocalFile( $title );
146
		if ( $file && $file->exists() ) {
147
			$handler = $file->getHandler();
148
			if ( !$handler ) {
149
				return null;
150
			}
151
			return $handler->getEntireText( $file );
152
		}
153
154
		return null;
155
	}
156
157
	public function getDataForSearchIndex( WikiPage $page, ParserOutput $parserOutput,
158
	                                       SearchEngine $engine ) {
159
		$fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
160
161
		$structure = new WikiTextStructure( $parserOutput );
162
		$fields['heading'] = $structure->headings();
163
		// text fields
164
		$fields['opening_text'] = $structure->getOpeningText();
165
		$fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
166
		$fields['auxiliary_text'] = $structure->getAuxiliaryText();
167
		$fields['defaultsort'] = $structure->getDefaultSort();
168
169
		$title = $page->getTitle();
170
		if ( NS_FILE == $title->getNamespace() ) {
171
			$fileText = $this->getFileText( $title );
172
			if ( $fileText ) {
173
				$fields['file_text'] = $fileText;
174
			}
175
		}
176
		return $fields;
177
	}
178
179
}
180