Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / content / WikitextContentHandler.php
blob74b2f1aede0aeae8b8edebc05c0aaa37eaad0e40
1 <?php
2 /**
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
20 * @since 1.21
22 * @file
23 * @ingroup Content
26 /**
27 * Content handler for wiki text pages.
29 * @ingroup Content
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;
41 /**
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.
47 * @return Content
49 * @see ContentHandler::makeRedirectContent
51 public function makeRedirectContent( Title $destination, $text = '' ) {
52 $optionalColon = '';
54 if ( $destination->getNamespace() == NS_CATEGORY ) {
55 $optionalColon = ':';
56 } else {
57 $iw = $destination->getInterwiki();
58 if ( $iw && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
59 $optionalColon = ':';
63 $mwRedir = MagicWord::get( 'redirect' );
64 $redirectText = $mwRedir->getSynonym( 0 ) .
65 ' [[' . $optionalColon . $destination->getFullText() . ']]';
67 if ( $text != '' ) {
68 $redirectText .= "\n" . $text;
71 $class = $this->getContentClass();
72 return new $class( $redirectText );
75 /**
76 * Returns true because wikitext supports redirects.
78 * @return bool Always true.
80 * @see ContentHandler::supportsRedirects
82 public function supportsRedirects() {
83 return true;
86 /**
87 * Returns true because wikitext supports sections.
89 * @return bool Always true.
91 * @see ContentHandler::supportsSections
93 public function supportsSections() {
94 return true;
97 /**
98 * Returns true, because wikitext supports caching using the
99 * ParserCache mechanism.
101 * @since 1.21
103 * @return bool Always true.
105 * @see ContentHandler::isParserCacheSupported
107 public function isParserCacheSupported() {
108 return true;
112 * Get file handler
113 * @return FileContentHandler
115 protected function getFileHandler() {
116 return new FileContentHandler();
119 public function getFieldsForSearchIndex( SearchEngine $engine ) {
120 $fields = parent::getFieldsForSearchIndex( $engine );
122 $fields['heading'] =
123 $engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
124 $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
126 $fields['auxiliary_text'] =
127 $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
129 $fields['opening_text'] =
130 $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
131 $fields['opening_text']->setFlag( SearchIndexField::FLAG_SCORING |
132 SearchIndexField::FLAG_NO_HIGHLIGHT );
133 // Until we have full first-class content handler for files, we invoke it explicitly here
134 $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
136 return $fields;
139 public function getDataForSearchIndex( WikiPage $page, ParserOutput $parserOutput,
140 SearchEngine $engine ) {
141 $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
143 $structure = new WikiTextStructure( $parserOutput );
144 $fields['heading'] = $structure->headings();
145 // text fields
146 $fields['opening_text'] = $structure->getOpeningText();
147 $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
148 $fields['auxiliary_text'] = $structure->getAuxiliaryText();
149 $fields['defaultsort'] = $structure->getDefaultSort();
151 // Until we have full first-class content handler for files, we invoke it explicitly here
152 if ( NS_FILE == $page->getTitle()->getNamespace() ) {
153 $fields = array_merge( $fields,
154 $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
156 return $fields;