MediaWiki  master
TextContentHandler.php
Go to the documentation of this file.
1 <?php
30 
37 
38  public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
39  parent::__construct( $modelId, $formats );
40  }
41 
50  public function serializeContent( Content $content, $format = null ) {
51  $this->checkFormat( $format );
52 
53  // @phan-suppress-next-line PhanUndeclaredMethod
54  return $content->getText();
55  }
56 
72  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
73  $this->checkModelID( $oldContent->getModel() );
74  $this->checkModelID( $myContent->getModel() );
75  $this->checkModelID( $yourContent->getModel() );
76 
77  $format = $this->getDefaultFormat();
78 
79  $old = $this->serializeContent( $oldContent, $format );
80  $mine = $this->serializeContent( $myContent, $format );
81  $yours = $this->serializeContent( $yourContent, $format );
82 
83  $ok = wfMerge( $old, $mine, $yours, $result );
84 
85  if ( !$ok ) {
86  return false;
87  }
88 
89  if ( !$result ) {
90  return $this->makeEmptyContent();
91  }
92 
93  $mergedContent = $this->unserializeContent( $result, $format );
94 
95  return $mergedContent;
96  }
97 
107  protected function getContentClass() {
108  return TextContent::class;
109  }
110 
121  public function unserializeContent( $text, $format = null ) {
122  $this->checkFormat( $format );
123 
124  $class = $this->getContentClass();
125  return new $class( $text );
126  }
127 
135  public function makeEmptyContent() {
136  $class = $this->getContentClass();
137  return new $class( '' );
138  }
139 
145  public function supportsDirectEditing() {
146  return true;
147  }
148 
149  public function getFieldsForSearchIndex( SearchEngine $engine ) {
150  $fields = parent::getFieldsForSearchIndex( $engine );
151  $fields['language'] =
153 
154  return $fields;
155  }
156 
157  public function getDataForSearchIndex(
158  WikiPage $page,
159  ParserOutput $output,
160  SearchEngine $engine
161  ) {
162  $fields = parent::getDataForSearchIndex( $page, $output, $engine );
163  $fields['language'] =
164  $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
165  return $fields;
166  }
167 
168  public function preSaveTransform(
170  PreSaveTransformParams $pstParams
171  ): Content {
172  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
173  $content,
174  $pstParams
175  );
176 
177  if ( $shouldCallDeprecatedMethod ) {
178  return $this->callDeprecatedContentPST(
179  $content,
180  $pstParams
181  );
182  }
183 
184  '@phan-var TextContent $content';
185 
186  $text = $content->getText();
187 
188  $pst = TextContent::normalizeLineEndings( $text );
189 
190  $contentClass = $this->getContentClass();
191  return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
192  }
193 
212  protected function fillParserOutput(
214  ContentParseParams $cpoParams,
215  ParserOutput &$output
216  ) {
217  $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
218  MainConfigNames::TextModelsToParse );
219  '@phan-var TextContent $content';
220  if ( in_array( $content->getModel(), $textModelsToParse ) ) {
221  // parse just to get links etc into the database, HTML is replaced below.
222  $output = MediaWikiServices::getInstance()->getParser()
223  ->parse(
224  $content->getText(),
225  $cpoParams->getPage(),
226  $cpoParams->getParserOptions(),
227  true,
228  true,
229  $cpoParams->getRevId()
230  );
231  }
232 
233  if ( $cpoParams->getGenerateHtml() ) {
234  // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
235  if ( method_exists( $content, 'getHtml' ) ) {
236  $method = new ReflectionMethod( $content, 'getHtml' );
237  $method->setAccessible( true );
238  $html = $method->invoke( $content );
239  } else {
240  // Return an HTML representation of the content
241  $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
242  }
243  } else {
244  $html = null;
245  }
246 
247  $output->clearWrapperDivClass();
248  $output->setText( $html );
249  }
250 }
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition: Defines.php:232
const CONTENT_MODEL_TEXT
Definition: Defines.php:213
wfMerge( $old, $mine, $yours, &$result, &$mergeAttemptResult=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.
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.
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
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...
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:62
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition: WikiPage.php:825
getTitle()
Get the title object of the article.
Definition: WikiPage.php:303
Base interface for content objects.
Definition: Content.php:35
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