3 * Content handler for wiki text pages.
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.
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.
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
27 * Content handler for wiki text pages.
31 class WikitextContentHandler
extends TextContentHandler
{
33 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT
) {
34 parent
::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT
] );
37 protected function getContentClass() {
38 return WikitextContent
::class;
42 * Returns a WikitextContent object representing a redirect to the given destination page.
44 * @param Title $destination The page to redirect to.
45 * @param string $text Text to include in the redirect, if possible.
49 * @see ContentHandler::makeRedirectContent
51 public function makeRedirectContent( Title
$destination, $text = '' ) {
54 if ( $destination->getNamespace() == NS_CATEGORY
) {
57 $iw = $destination->getInterwiki();
58 if ( $iw && Language
::fetchLanguageName( $iw, null, 'mw' ) ) {
63 $mwRedir = MagicWord
::get( 'redirect' );
64 $redirectText = $mwRedir->getSynonym( 0 ) .
65 ' [[' . $optionalColon . $destination->getFullText() . ']]';
68 $redirectText .= "\n" . $text;
71 $class = $this->getContentClass();
72 return new $class( $redirectText );
76 * Returns true because wikitext supports redirects.
78 * @return bool Always true.
80 * @see ContentHandler::supportsRedirects
82 public function supportsRedirects() {
87 * Returns true because wikitext supports sections.
89 * @return bool Always true.
91 * @see ContentHandler::supportsSections
93 public function supportsSections() {
98 * Returns true, because wikitext supports caching using the
99 * ParserCache mechanism.
103 * @return bool Always true.
105 * @see ContentHandler::isParserCacheSupported
107 public function isParserCacheSupported() {
111 public function getFieldsForSearchIndex( SearchEngine
$engine ) {
112 $fields = parent
::getFieldsForSearchIndex( $engine );
115 $engine->makeSearchFieldMapping( 'heading', SearchIndexField
::INDEX_TYPE_TEXT
);
116 $fields['heading']->setFlag( SearchIndexField
::FLAG_SCORING
);
118 $fields['auxiliary_text'] =
119 $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField
::INDEX_TYPE_TEXT
);
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
);
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
);
135 * Extract text of the file
136 * TODO: probably should go to file handler?
137 * @param Title $title
138 * @return string|null
140 protected function getFileText( Title
$title ) {
141 $file = wfLocalFile( $title );
142 if ( $file && $file->exists() ) {
143 $handler = $file->getHandler();
147 return $handler->getEntireText( $file );
153 public function getDataForSearchIndex( WikiPage
$page, ParserOutput
$parserOutput,
154 SearchEngine
$engine ) {
155 $fields = parent
::getDataForSearchIndex( $page, $parserOutput, $engine );
157 $structure = new WikiTextStructure( $parserOutput );
158 $fields['heading'] = $structure->headings();
160 $fields['opening_text'] = $structure->getOpeningText();
161 $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
162 $fields['auxiliary_text'] = $structure->getAuxiliaryText();
164 $title = $page->getTitle();
165 if ( NS_FILE
== $title->getNamespace() ) {
166 $fileText = $this->getFileText( $title );
168 $fields['file_text'] = $fileText;