MediaWiki  master
WikitextContentHandler.php
Go to the documentation of this file.
1 <?php
32 
39 
40  public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
41  parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
42  }
43 
44  protected function getContentClass() {
45  return WikitextContent::class;
46  }
47 
58  public function makeRedirectContent( Title $destination, $text = '' ) {
59  $optionalColon = '';
60 
61  $services = MediaWikiServices::getInstance();
62  if ( $destination->getNamespace() === NS_CATEGORY ) {
63  $optionalColon = ':';
64  } else {
65  $iw = $destination->getInterwiki();
66  if ( $iw && $services
67  ->getLanguageNameUtils()
68  ->getLanguageName( $iw,
69  LanguageNameUtils::AUTONYMS,
70  LanguageNameUtils::DEFINED )
71  ) {
72  $optionalColon = ':';
73  }
74  }
75 
76  $mwRedir = $services->getMagicWordFactory()->get( 'redirect' );
77  $redirectText = $mwRedir->getSynonym( 0 ) .
78  ' [[' . $optionalColon . $destination->getFullText() . ']]';
79 
80  if ( $text != '' ) {
81  $redirectText .= "\n" . $text;
82  }
83 
84  $class = $this->getContentClass();
85  return new $class( $redirectText );
86  }
87 
95  public function supportsRedirects() {
96  return true;
97  }
98 
106  public function supportsSections() {
107  return true;
108  }
109 
120  public function isParserCacheSupported() {
121  return true;
122  }
123 
127  protected function getFileHandler() {
128  return new FileContentHandler();
129  }
130 
131  public function getFieldsForSearchIndex( SearchEngine $engine ) {
132  $fields = parent::getFieldsForSearchIndex( $engine );
133 
134  $fields['heading'] =
136  $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
137 
138  $fields['auxiliary_text'] =
139  $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
140 
141  $fields['opening_text'] =
142  $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
143  $fields['opening_text']->setFlag(
145  );
146  // Until we have full first-class content handler for files, we invoke it explicitly here
147  $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
148 
149  return $fields;
150  }
151 
152  public function getDataForSearchIndex(
153  WikiPage $page,
154  ParserOutput $parserOutput,
155  SearchEngine $engine
156  ) {
157  $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
158 
159  $structure = new WikiTextStructure( $parserOutput );
160  $fields['heading'] = $structure->headings();
161  // text fields
162  $fields['opening_text'] = $structure->getOpeningText();
163  $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
164  $fields['auxiliary_text'] = $structure->getAuxiliaryText();
165  $fields['defaultsort'] = $structure->getDefaultSort();
166 
167  // Until we have full first-class content handler for files, we invoke it explicitly here
168  if ( $page->getTitle()->getNamespace() === NS_FILE ) {
169  $fields = array_merge( $fields,
170  $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
171  }
172  return $fields;
173  }
174 
183  public function serializeContent( Content $content, $format = null ) {
184  $this->checkFormat( $format );
185 
186  // NOTE: MessageContent also uses CONTENT_MODEL_WIKITEXT, but it's not a TextContent!
187  // Perhaps MessageContent should use a separate ContentHandler instead.
188  if ( $content instanceof MessageContent ) {
189  return $content->getMessage()->plain();
190  }
191 
192  return parent::serializeContent( $content, $format );
193  }
194 
195  public function preSaveTransform(
197  PreSaveTransformParams $pstParams
198  ): Content {
199  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
200  $content,
201  $pstParams
202  );
203 
204  if ( $shouldCallDeprecatedMethod ) {
205  return $this->callDeprecatedContentPST(
206  $content,
207  $pstParams
208  );
209  }
210 
211  '@phan-var WikitextContent $content';
212 
213  $text = $content->getText();
214 
215  $parser = MediaWikiServices::getInstance()->getParser();
216  $pst = $parser->preSaveTransform(
217  $text,
218  $pstParams->getPage(),
219  $pstParams->getUser(),
220  $pstParams->getParserOptions()
221  );
222 
223  if ( $text === $pst ) {
224  return $content;
225  }
226 
227  $contentClass = $this->getContentClass();
228  $ret = new $contentClass( $pst );
229  $ret->setPreSaveTransformFlags( $parser->getOutput()->getAllFlags() );
230  return $ret;
231  }
232 
242  public function preloadTransform(
244  PreloadTransformParams $pltParams
245  ): Content {
246  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
247  $content,
248  $pltParams
249  );
250 
251  if ( $shouldCallDeprecatedMethod ) {
252  return $this->callDeprecatedContentPLT(
253  $content,
254  $pltParams
255  );
256  }
257 
258  '@phan-var WikitextContent $content';
259 
260  $text = $content->getText();
261  $plt = MediaWikiServices::getInstance()->getParser()->getPreloadText(
262  $text,
263  $pltParams->getPage(),
264  $pltParams->getParserOptions(),
265  $pltParams->getParams()
266  );
267 
268  $contentClass = $this->getContentClass();
269  return new $contentClass( $plt );
270  }
271 
281  protected function fillParserOutput(
283  ContentParseParams $cpoParams,
284  ParserOutput &$parserOutput
285  ) {
286  '@phan-var WikitextContent $content';
287  $services = MediaWikiServices::getInstance();
288  $title = $services->getTitleFactory()->castFromPageReference( $cpoParams->getPage() );
289  $parserOptions = $cpoParams->getParserOptions();
290  $revId = $cpoParams->getRevId();
291 
292  list( $redir, $text ) = $content->getRedirectTargetAndText();
293  $parserOutput = $services->getParser()->getFreshParser()
294  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable castFrom does not return null here
295  ->parse( $text, $title, $parserOptions, true, true, $revId );
296 
297  // Add redirect indicator at the top
298  if ( $redir ) {
299  // Make sure to include the redirect link in pagelinks
300  $parserOutput->addLink( $redir );
301  if ( $cpoParams->getGenerateHtml() ) {
302  $redirTarget = $content->getRedirectTarget();
303  $parserOutput->setText(
304  Article::getRedirectHeaderHtml( $title->getPageLanguage(), $redirTarget, false ) .
305  $parserOutput->getRawText()
306  );
307  $parserOutput->addModuleStyles( [ 'mediawiki.action.view.redirectPage' ] );
308  } else {
309  $parserOutput->setText( null );
310  }
311  }
312 
313  // Pass along user-signature flag
314  if ( in_array( 'user-signature', $content->getPreSaveTransformFlags() ) ) {
315  $parserOutput->setOutputFlag( ParserOutputFlags::USER_SIGNATURE );
316  }
317  }
318 }
const NS_FILE
Definition: Defines.php:70
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:210
const CONTENT_FORMAT_WIKITEXT
Wikitext.
Definition: Defines.php:226
const NS_CATEGORY
Definition: Defines.php:78
static getRedirectHeaderHtml(Language $lang, $target, $forceKnown=false)
Return the HTML for the top of a redirect page.
Definition: Article.php:1740
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.
addLink(LinkTarget $link, $id=null)
Record a local or interwiki inline link for saving in future link tables.
getRawText()
Get the cacheable text with <mw:editsection> markers still in it.
setText( $text)
Set the text of the ParserOutput.
setOutputFlag(string $name, bool $val=true)
Provides a uniform interface to various boolean flags stored in the ParserOutput.
addModuleStyles( $modules)
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:1065
getFullText()
Get the prefixed title with spaces, plus any fragment (part beginning with '#')
Definition: Title.php:1910
getInterwiki()
Get the interwiki prefix.
Definition: Title.php:966
Base representation for an editable wiki page.
Definition: WikiPage.php:62
getTitle()
Get the title object of the article.
Definition: WikiPage.php:307
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.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput)
Returns a ParserOutput object resulting from parsing the content's text using the global Parser servi...
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
const FLAG_SCORING
This field contains secondary information, which is already present in other fields,...
const INDEX_TYPE_TEXT
TEXT fields are suitable for natural language and may be subject to analysis such as stemming.
const FLAG_NO_HIGHLIGHT
This field does not need highlight handling.
$content
Definition: router.php:76