MediaWiki  master
TextContentHandler.php
Go to the documentation of this file.
1 <?php
29 
36 
37  public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
38  parent::__construct( $modelId, $formats );
39  }
40 
49  public function serializeContent( Content $content, $format = null ) {
50  $this->checkFormat( $format );
51 
52  // @phan-suppress-next-line PhanUndeclaredMethod
53  return $content->getText();
54  }
55 
71  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
72  $this->checkModelID( $oldContent->getModel() );
73  $this->checkModelID( $myContent->getModel() );
74  $this->checkModelID( $yourContent->getModel() );
75 
76  $format = $this->getDefaultFormat();
77 
78  $old = $this->serializeContent( $oldContent, $format );
79  $mine = $this->serializeContent( $myContent, $format );
80  $yours = $this->serializeContent( $yourContent, $format );
81 
82  $ok = wfMerge( $old, $mine, $yours, $result );
83 
84  if ( !$ok ) {
85  return false;
86  }
87 
88  if ( !$result ) {
89  return $this->makeEmptyContent();
90  }
91 
92  $mergedContent = $this->unserializeContent( $result, $format );
93 
94  return $mergedContent;
95  }
96 
106  protected function getContentClass() {
107  return TextContent::class;
108  }
109 
120  public function unserializeContent( $text, $format = null ) {
121  $this->checkFormat( $format );
122 
123  $class = $this->getContentClass();
124  return new $class( $text );
125  }
126 
134  public function makeEmptyContent() {
135  $class = $this->getContentClass();
136  return new $class( '' );
137  }
138 
144  public function supportsDirectEditing() {
145  return true;
146  }
147 
148  public function getFieldsForSearchIndex( SearchEngine $engine ) {
149  $fields = parent::getFieldsForSearchIndex( $engine );
150  $fields['language'] =
152 
153  return $fields;
154  }
155 
156  public function getDataForSearchIndex(
157  WikiPage $page,
158  ParserOutput $output,
159  SearchEngine $engine
160  ) {
161  $fields = parent::getDataForSearchIndex( $page, $output, $engine );
162  $fields['language'] =
163  $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
164  return $fields;
165  }
166 
167  public function preSaveTransform(
169  PreSaveTransformParams $pstParams
170  ): Content {
171  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
172  $content,
173  $pstParams
174  );
175 
176  if ( $shouldCallDeprecatedMethod ) {
177  return $this->callDeprecatedContentPST(
178  $content,
179  $pstParams
180  );
181  }
182 
183  '@phan-var TextContent $content';
184 
185  $text = $content->getText();
186 
187  $pst = TextContent::normalizeLineEndings( $text );
188 
189  $contentClass = $this->getContentClass();
190  return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
191  }
192 
211  protected function fillParserOutput(
213  ContentParseParams $cpoParams,
214  ParserOutput &$output
215  ) {
216  $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get( 'TextModelsToParse' );
217  '@phan-var TextContent $content';
218  if ( in_array( $content->getModel(), $textModelsToParse ) ) {
219  // parse just to get links etc into the database, HTML is replaced below.
220  $output = MediaWikiServices::getInstance()->getParser()
221  ->parse(
222  $content->getText(),
223  $cpoParams->getPage(),
224  $cpoParams->getParserOptions(),
225  true,
226  true,
227  $cpoParams->getRevId()
228  );
229  }
230 
231  if ( $cpoParams->getGenerateHtml() ) {
232  // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
233  if ( method_exists( $content, 'getHtml' ) ) {
234  $method = new ReflectionMethod( $content, 'getHtml' );
235  $method->setAccessible( true );
236  $html = $method->invoke( $content );
237  } else {
238  // Return an HTML representation of the content
239  $html = htmlspecialchars( $content->getText() );
240  }
241  } else {
242  $html = '';
243  }
244 
245  $output->clearWrapperDivClass();
246  $output->setText( $html );
247  }
248 }
SearchIndexField\INDEX_TYPE_KEYWORD
const INDEX_TYPE_KEYWORD
KEYWORD fields are indexed without any processing, so are appropriate for e.g.
Definition: SearchIndexField.php:24
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:62
MediaWiki\Content\Renderer\ContentParseParams\getRevId
getRevId()
Definition: ContentParseParams.php:48
MediaWiki\Content\Renderer\ContentParseParams\getPage
getPage()
Definition: ContentParseParams.php:40
TextContentHandler\__construct
__construct( $modelId=CONTENT_MODEL_TEXT, $formats=[CONTENT_FORMAT_TEXT])
Constructor, initializing the ContentHandler instance with its model ID and a list of supported forma...
Definition: TextContentHandler.php:37
ParserOutput
Definition: ParserOutput.php:35
MediaWiki\Content\Renderer\ContentParseParams\getGenerateHtml
getGenerateHtml()
Definition: ContentParseParams.php:64
TextContentHandler\unserializeContent
unserializeContent( $text, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
Definition: TextContentHandler.php:120
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:204
wfMerge
wfMerge( $old, $mine, $yours, &$result, &$mergeAttemptResult=null)
wfMerge attempts to merge differences between three texts.
Definition: GlobalFunctions.php:1992
ContentHandler\checkModelID
checkModelID( $model_id)
Definition: ContentHandler.php:488
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:63
ContentHandler\shouldCallDeprecatedContentTransformMethod
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
Definition: ContentHandler.php:1792
MediaWiki\Content\Renderer\ContentParseParams
Definition: ContentParseParams.php:11
TextContentHandler\supportsDirectEditing
supportsDirectEditing()
Definition: TextContentHandler.php:144
TextContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
Definition: TextContentHandler.php:49
MediaWiki\Content\Transform\PreSaveTransformParams
Definition: PreSaveTransformParams.php:12
MediaWiki\Content\Renderer\ContentParseParams\getParserOptions
getParserOptions()
Definition: ContentParseParams.php:56
TextContentHandler\fillParserOutput
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
Definition: TextContentHandler.php:211
TextContentHandler\preSaveTransform
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
Definition: TextContentHandler.php:167
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:314
TextContentHandler\merge3
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
Definition: TextContentHandler.php:71
ParserOutput\setText
setText( $text)
Definition: ParserOutput.php:810
TextContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: TextContentHandler.php:148
ContentHandler\getPageLanguage
getPageLanguage(Title $title, Content $content=null)
Get the language in which the content of the given page is written.
Definition: ContentHandler.php:724
ParserOutput\clearWrapperDivClass
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
Definition: ParserOutput.php:546
$content
$content
Definition: router.php:76
ContentHandler\getDefaultFormat
getDefaultFormat()
The format used for serialization/deserialization by default by this ContentHandler.
Definition: ContentHandler.php:522
WikiPage\getContent
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition: WikiPage.php:840
TextContentHandler\getDataForSearchIndex
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Definition: TextContentHandler.php:156
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:35
TextContentHandler\makeEmptyContent
makeEmptyContent()
Creates an empty TextContent object.
Definition: TextContentHandler.php:134
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
CONTENT_FORMAT_TEXT
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition: Defines.php:230
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:555
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:211
TextContentHandler\getContentClass
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
Definition: TextContentHandler.php:106
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
TextContent\normalizeLineEndings
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
Definition: TextContent.php:203
ContentHandler\callDeprecatedContentPST
callDeprecatedContentPST(Content $content, PreSaveTransformParams $params)
Provided content overrides deprecated Content::preSaveTransform, call it and return.
Definition: ContentHandler.php:1815