MediaWiki REL1_37
WikitextContentHandler.php
Go to the documentation of this file.
1<?php
30
37
38 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
39 parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
40 }
41
42 protected function getContentClass() {
43 return WikitextContent::class;
44 }
45
56 public function makeRedirectContent( Title $destination, $text = '' ) {
57 $optionalColon = '';
58
59 $services = MediaWikiServices::getInstance();
60 if ( $destination->getNamespace() === NS_CATEGORY ) {
61 $optionalColon = ':';
62 } else {
63 $iw = $destination->getInterwiki();
64 if ( $iw && $services
65 ->getLanguageNameUtils()
66 ->getLanguageName( $iw,
67 LanguageNameUtils::AUTONYMS,
68 LanguageNameUtils::DEFINED )
69 ) {
70 $optionalColon = ':';
71 }
72 }
73
74 $mwRedir = $services->getMagicWordFactory()->get( 'redirect' );
75 $redirectText = $mwRedir->getSynonym( 0 ) .
76 ' [[' . $optionalColon . $destination->getFullText() . ']]';
77
78 if ( $text != '' ) {
79 $redirectText .= "\n" . $text;
80 }
81
82 $class = $this->getContentClass();
83 return new $class( $redirectText );
84 }
85
93 public function supportsRedirects() {
94 return true;
95 }
96
104 public function supportsSections() {
105 return true;
106 }
107
118 public function isParserCacheSupported() {
119 return true;
120 }
121
125 protected function getFileHandler() {
126 return new FileContentHandler();
127 }
128
129 public function getFieldsForSearchIndex( SearchEngine $engine ) {
130 $fields = parent::getFieldsForSearchIndex( $engine );
131
132 $fields['heading'] =
133 $engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
134 $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
135
136 $fields['auxiliary_text'] =
137 $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
138
139 $fields['opening_text'] =
140 $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
141 $fields['opening_text']->setFlag(
142 SearchIndexField::FLAG_SCORING | SearchIndexField::FLAG_NO_HIGHLIGHT
143 );
144 // Until we have full first-class content handler for files, we invoke it explicitly here
145 $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
146
147 return $fields;
148 }
149
150 public function getDataForSearchIndex(
151 WikiPage $page,
152 ParserOutput $parserOutput,
153 SearchEngine $engine
154 ) {
155 $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
156
157 $structure = new WikiTextStructure( $parserOutput );
158 $fields['heading'] = $structure->headings();
159 // text fields
160 $fields['opening_text'] = $structure->getOpeningText();
161 $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
162 $fields['auxiliary_text'] = $structure->getAuxiliaryText();
163 $fields['defaultsort'] = $structure->getDefaultSort();
164
165 // Until we have full first-class content handler for files, we invoke it explicitly here
166 if ( $page->getTitle()->getNamespace() === NS_FILE ) {
167 $fields = array_merge( $fields,
168 $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
169 }
170 return $fields;
171 }
172
181 public function serializeContent( Content $content, $format = null ) {
182 $this->checkFormat( $format );
183
184 // NOTE: MessageContent also uses CONTENT_MODEL_WIKITEXT, but it's not a TextContent!
185 // Perhaps MessageContent should use a separate ContentHandler instead.
186 if ( $content instanceof MessageContent ) {
187 return $content->getMessage()->plain();
188 }
189
190 return parent::serializeContent( $content, $format );
191 }
192
193 public function preSaveTransform(
195 PreSaveTransformParams $pstParams
196 ): Content {
197 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
198 $content,
199 $pstParams
200 );
201
202 if ( $shouldCallDeprecatedMethod ) {
203 return $this->callDeprecatedContentPST(
204 $content,
205 $pstParams
206 );
207 }
208
209 '@phan-var WikitextContent $content';
210
211 $text = $content->getText();
212
213 $parser = MediaWikiServices::getInstance()->getParser();
214 $pst = $parser->preSaveTransform(
215 $text,
216 $pstParams->getPage(),
217 $pstParams->getUser(),
218 $pstParams->getParserOptions()
219 );
220
221 if ( $text === $pst ) {
222 return $content;
223 }
224
225 $contentClass = $this->getContentClass();
226 $ret = new $contentClass( $pst );
227 $ret->setPreSaveTransformFlags( $parser->getOutput()->getAllFlags() );
228 return $ret;
229 }
230
240 public function preloadTransform(
242 PreloadTransformParams $pltParams
243 ): Content {
244 $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
245 $content,
246 $pltParams
247 );
248
249 if ( $shouldCallDeprecatedMethod ) {
250 return $this->callDeprecatedContentPLT(
251 $content,
252 $pltParams
253 );
254 }
255
256 '@phan-var WikitextContent $content';
257
258 $text = $content->getText();
259 $plt = MediaWikiServices::getInstance()->getParser()->getPreloadText(
260 $text,
261 $pltParams->getPage(),
262 $pltParams->getParserOptions(),
263 $pltParams->getParams()
264 );
265
266 $contentClass = $this->getContentClass();
267 return new $contentClass( $plt );
268 }
269}
const NS_FILE
Definition Defines.php:70
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:208
const CONTENT_FORMAT_WIKITEXT
Wikitext.
Definition Defines.php:224
const NS_CATEGORY
Definition Defines.php:78
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Content handler for File: files TODO: this handler s not used directly now, but instead manually call...
A service that provides utilities to do with language names and codes.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Wrapper allowing us to handle a system message as a Content object.
Contain a class for special pages.
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Base content handler implementation for flat text contents.
Represents a title within MediaWiki.
Definition Title.php:48
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1078
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition Title.php:1945
getInterwiki()
Get the interwiki prefix.
Definition Title.php:979
Class representing a MediaWiki article and history.
Definition WikiPage.php:60
getTitle()
Get the title object of the article.
Definition WikiPage.php:311
Class allowing to explore structure of parsed wikitext.
Content handler for wiki text pages.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
supportsRedirects()
Returns true because wikitext supports redirects.
preloadTransform(Content $content, PreloadTransformParams $pltParams)
Returns a Content object with preload transformations applied (or this object if no transformations a...
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
makeRedirectContent(Title $destination, $text='')
Returns a WikitextContent object representing a redirect to the given destination page.
isParserCacheSupported()
Returns true, because wikitext supports caching using the ParserCache mechanism.
__construct( $modelId=CONTENT_MODEL_WIKITEXT)
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
supportsSections()
Returns true because wikitext supports sections.
getDataForSearchIndex(WikiPage $page, ParserOutput $parserOutput, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Base interface for content objects.
Definition Content.php:35
$content
Definition router.php:76