MediaWiki  master
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'] =
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(
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 }
ContentHandler\callDeprecatedContentPLT
callDeprecatedContentPLT(Content $content, PreloadTransformParams $params)
If provided content overrides deprecated Content::preloadTransform, call it and return.
Definition: ContentHandler.php:1663
ParserOutput
Definition: ParserOutput.php:31
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
SearchIndexField\FLAG_SCORING
const FLAG_SCORING
This field contains secondary information, which is already present in other fields,...
Definition: SearchIndexField.php:52
WikitextContentHandler\getFileHandler
getFileHandler()
Definition: WikitextContentHandler.php:125
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
WikitextContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: WikitextContentHandler.php:129
MediaWiki\Content\Transform\PreloadTransformParams
Definition: PreloadTransformParams.php:11
MediaWiki\Content\Transform\PreSaveTransformParams\getUser
getUser()
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Wikitext.
Definition: Defines.php:224
ContentHandler\shouldCallDeprecatedContentTransformMethod
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
Definition: ContentHandler.php:1617
MediaWiki\Content\Transform\PreloadTransformParams\getPage
getPage()
MediaWiki\Languages\LanguageNameUtils
A service that provides utilities to do with language names and codes.
Definition: LanguageNameUtils.php:42
WikitextContentHandler\getDataForSearchIndex
getDataForSearchIndex(WikiPage $page, ParserOutput $parserOutput, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Definition: WikitextContentHandler.php:150
MediaWiki\Content\Transform\PreSaveTransformParams
Definition: PreSaveTransformParams.php:12
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1069
Title\getInterwiki
getInterwiki()
Get the interwiki prefix.
Definition: Title.php:970
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:311
Title\getFullText
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1936
SearchIndexField\FLAG_NO_HIGHLIGHT
const FLAG_NO_HIGHLIGHT
This field does not need highlight handling.
Definition: SearchIndexField.php:57
$content
$content
Definition: router.php:76
WikitextContentHandler
Content handler for wiki text pages.
Definition: WikitextContentHandler.php:36
WikitextContentHandler\isParserCacheSupported
isParserCacheSupported()
Returns true, because wikitext supports caching using the ParserCache mechanism.
Definition: WikitextContentHandler.php:118
WikitextContentHandler\__construct
__construct( $modelId=CONTENT_MODEL_WIKITEXT)
Definition: WikitextContentHandler.php:38
FileContentHandler
Content handler for File: files TODO: this handler s not used directly now, but instead manually call...
Definition: FileContentHandler.php:11
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:33
WikitextContentHandler\preloadTransform
preloadTransform(Content $content, PreloadTransformParams $pltParams)
Returns a Content object with preload transformations applied (or this object if no transformations a...
Definition: WikitextContentHandler.php:240
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:208
MediaWiki\Content\Transform\PreloadTransformParams\getParams
getParams()
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:37
Content
Base interface for content objects.
Definition: Content.php:35
SearchEngine\makeSearchFieldMapping
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Definition: SearchEngine.php:776
WikitextContentHandler\preSaveTransform
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
Definition: WikitextContentHandler.php:193
Title
Represents a title within MediaWiki.
Definition: Title.php:48
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:552
MessageContent
Wrapper allowing us to handle a system message as a Content object.
Definition: MessageContent.php:36
WikitextContentHandler\supportsRedirects
supportsRedirects()
Returns true because wikitext supports redirects.
Definition: WikitextContentHandler.php:93
MediaWiki\Content\Transform\PreloadTransformParams\getParserOptions
getParserOptions()
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
MediaWiki\Content\Transform\PreSaveTransformParams\getParserOptions
getParserOptions()
WikiTextStructure
Class allowing to explore structure of parsed wikitext.
Definition: WikiTextStructure.php:8
ContentHandler\callDeprecatedContentPST
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Definition: ContentHandler.php:1640
NS_FILE
const NS_FILE
Definition: Defines.php:70
WikitextContentHandler\supportsSections
supportsSections()
Returns true because wikitext supports sections.
Definition: WikitextContentHandler.php:104
WikitextContentHandler\makeRedirectContent
makeRedirectContent(Title $destination, $text='')
Returns a WikitextContent object representing a redirect to the given destination page.
Definition: WikitextContentHandler.php:56
WikitextContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
Definition: WikitextContentHandler.php:181
WikitextContentHandler\getContentClass
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
Definition: WikitextContentHandler.php:42
MediaWiki\Content\Transform\PreSaveTransformParams\getPage
getPage()
SearchIndexField\INDEX_TYPE_TEXT
const INDEX_TYPE_TEXT
TEXT fields are suitable for natural language and may be subject to analysis such as stemming.
Definition: SearchIndexField.php:19