MediaWiki  master
TextContentHandler.php
Go to the documentation of this file.
1 <?php
31 
38 
39  public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
40  parent::__construct( $modelId, $formats );
41  }
42 
51  public function serializeContent( Content $content, $format = null ) {
52  $this->checkFormat( $format );
53 
54  // @phan-suppress-next-line PhanUndeclaredMethod
55  return $content->getText();
56  }
57 
73  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
74  $this->checkModelID( $oldContent->getModel() );
75  $this->checkModelID( $myContent->getModel() );
76  $this->checkModelID( $yourContent->getModel() );
77 
78  $format = $this->getDefaultFormat();
79 
80  $old = $this->serializeContent( $oldContent, $format );
81  $mine = $this->serializeContent( $myContent, $format );
82  $yours = $this->serializeContent( $yourContent, $format );
83 
84  $ok = wfMerge( $old, $mine, $yours, $result );
85 
86  if ( !$ok ) {
87  return false;
88  }
89 
90  if ( !$result ) {
91  return $this->makeEmptyContent();
92  }
93 
94  $mergedContent = $this->unserializeContent( $result, $format );
95 
96  return $mergedContent;
97  }
98 
108  protected function getContentClass() {
109  return TextContent::class;
110  }
111 
122  public function unserializeContent( $text, $format = null ) {
123  $this->checkFormat( $format );
124 
125  $class = $this->getContentClass();
126  return new $class( $text );
127  }
128 
136  public function makeEmptyContent() {
137  $class = $this->getContentClass();
138  return new $class( '' );
139  }
140 
146  public function supportsDirectEditing() {
147  return true;
148  }
149 
150  public function getFieldsForSearchIndex( SearchEngine $engine ) {
151  $fields = parent::getFieldsForSearchIndex( $engine );
152  $fields['language'] =
154 
155  return $fields;
156  }
157 
158  public function getDataForSearchIndex(
159  WikiPage $page,
160  ParserOutput $output,
161  SearchEngine $engine,
162  ?RevisionRecord $revision = null
163  ) {
164  $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
165  $fields['language'] =
166  $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
167  return $fields;
168  }
169 
170  public function preSaveTransform(
172  PreSaveTransformParams $pstParams
173  ): Content {
174  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
175  $content,
176  $pstParams
177  );
178 
179  if ( $shouldCallDeprecatedMethod ) {
180  return $this->callDeprecatedContentPST(
181  $content,
182  $pstParams
183  );
184  }
185 
186  '@phan-var TextContent $content';
187 
188  $text = $content->getText();
189 
190  $pst = TextContent::normalizeLineEndings( $text );
191 
192  $contentClass = $this->getContentClass();
193  return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
194  }
195 
216  protected function fillParserOutput(
218  ContentParseParams $cpoParams,
219  ParserOutput &$output
220  ) {
221  $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
222  MainConfigNames::TextModelsToParse );
223  '@phan-var TextContent $content';
224  if ( in_array( $content->getModel(), $textModelsToParse ) ) {
225  // parse just to get links etc into the database, HTML is replaced below.
226  $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
227  ->parse(
228  $content->getText(),
229  $cpoParams->getPage(),
230  $cpoParams->getParserOptions(),
231  true,
232  true,
233  $cpoParams->getRevId()
234  );
235  }
236 
237  if ( $cpoParams->getGenerateHtml() ) {
238  // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
239  if ( method_exists( $content, 'getHtml' ) ) {
240  $method = new ReflectionMethod( $content, 'getHtml' );
241  $method->setAccessible( true );
242  $html = $method->invoke( $content );
243  } else {
244  // Return an HTML representation of the content
245  $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
246  }
247  } else {
248  $html = null;
249  }
250 
251  $output->clearWrapperDivClass();
252  $output->setText( $html );
253  }
254 }
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition: Defines.php:231
const CONTENT_MODEL_TEXT
Definition: Defines.php:212
wfMerge(string $old, string $mine, string $yours, ?string &$simplisticMergeAttempt, string &$mergeLeftovers=null)
wfMerge attempts to merge differences between three texts.
A content handler knows how do deal with a specific type of content on a wiki page.
checkModelID( $model_id)
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.
getPageLanguage(Title $title, Content $content=null)
Get the language in which the content of the given page is written.
getDefaultFormat()
The format used for serialization/deserialization by default by this ContentHandler.
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Page revision base class.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setText( $text)
Set the text of the ParserOutput.
Contain a class for special pages.
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Base content handler implementation for flat text contents.
unserializeContent( $text, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
__construct( $modelId=CONTENT_MODEL_TEXT, $formats=[CONTENT_FORMAT_TEXT])
Constructor, initializing the ContentHandler instance with its model ID and a list of supported forma...
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
makeEmptyContent()
Creates an empty TextContent object.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine, ?RevisionRecord $revision=null)
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Base representation for an editable wiki page.
Definition: WikiPage.php:77
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition: WikiPage.php:776
getTitle()
Get the title object of the article.
Definition: WikiPage.php:258
Base interface for representing page content.
Definition: Content.php:39
getModel()
Returns the ID of the content model used by this Content object.
const INDEX_TYPE_KEYWORD
KEYWORD fields are indexed without any processing, so are appropriate for e.g.
$content
Definition: router.php:76