MediaWiki  master
TextContentHandler.php
Go to the documentation of this file.
1 <?php
27 
34 
35  public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
36  parent::__construct( $modelId, $formats );
37  }
38 
47  public function serializeContent( Content $content, $format = null ) {
48  $this->checkFormat( $format );
49 
50  // @phan-suppress-next-line PhanUndeclaredMethod
51  return $content->getText();
52  }
53 
69  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
70  $this->checkModelID( $oldContent->getModel() );
71  $this->checkModelID( $myContent->getModel() );
72  $this->checkModelID( $yourContent->getModel() );
73 
74  $format = $this->getDefaultFormat();
75 
76  $old = $this->serializeContent( $oldContent, $format );
77  $mine = $this->serializeContent( $myContent, $format );
78  $yours = $this->serializeContent( $yourContent, $format );
79 
80  $ok = wfMerge( $old, $mine, $yours, $result );
81 
82  if ( !$ok ) {
83  return false;
84  }
85 
86  if ( !$result ) {
87  return $this->makeEmptyContent();
88  }
89 
90  $mergedContent = $this->unserializeContent( $result, $format );
91 
92  return $mergedContent;
93  }
94 
104  protected function getContentClass() {
105  return TextContent::class;
106  }
107 
118  public function unserializeContent( $text, $format = null ) {
119  $this->checkFormat( $format );
120 
121  $class = $this->getContentClass();
122  return new $class( $text );
123  }
124 
132  public function makeEmptyContent() {
133  $class = $this->getContentClass();
134  return new $class( '' );
135  }
136 
142  public function supportsDirectEditing() {
143  return true;
144  }
145 
146  public function getFieldsForSearchIndex( SearchEngine $engine ) {
147  $fields = parent::getFieldsForSearchIndex( $engine );
148  $fields['language'] =
150 
151  return $fields;
152  }
153 
154  public function getDataForSearchIndex(
155  WikiPage $page,
156  ParserOutput $output,
157  SearchEngine $engine
158  ) {
159  $fields = parent::getDataForSearchIndex( $page, $output, $engine );
160  $fields['language'] =
161  $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
162  return $fields;
163  }
164 
165  public function preSaveTransform(
167  PreSaveTransformParams $pstParams
168  ): Content {
169  $shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
170  $content,
171  $pstParams
172  );
173 
174  if ( $shouldCallDeprecatedMethod ) {
175  return $this->callDeprecatedContentPST(
176  $content,
177  $pstParams
178  );
179  }
180 
181  '@phan-var TextContent $content';
182 
183  $text = $content->getText();
184 
185  $pst = TextContent::normalizeLineEndings( $text );
186 
187  $contentClass = $this->getContentClass();
188  return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
189  }
190 }
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:60
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:35
ParserOutput
Definition: ParserOutput.php:31
TextContentHandler\unserializeContent
unserializeContent( $text, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
Definition: TextContentHandler.php:118
wfMerge
wfMerge( $old, $mine, $yours, &$result, &$mergeAttemptResult=null)
wfMerge attempts to merge differences between three texts.
Definition: GlobalFunctions.php:2006
ContentHandler\checkModelID
checkModelID( $model_id)
Definition: ContentHandler.php:485
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:60
ContentHandler\shouldCallDeprecatedContentTransformMethod
shouldCallDeprecatedContentTransformMethod(Content $content, $params)
Check if we need to provide content overrides deprecated Content method.
Definition: ContentHandler.php:1617
TextContentHandler\supportsDirectEditing
supportsDirectEditing()
Definition: TextContentHandler.php:142
TextContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
Definition: TextContentHandler.php:47
MediaWiki\Content\Transform\PreSaveTransformParams
Definition: PreSaveTransformParams.php:12
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:165
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:311
TextContentHandler\merge3
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
Definition: TextContentHandler.php:69
TextContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: TextContentHandler.php:146
ContentHandler\getPageLanguage
getPageLanguage(Title $title, Content $content=null)
Get the language in which the content of the given page is written.
Definition: ContentHandler.php:721
$content
$content
Definition: router.php:76
ContentHandler\getDefaultFormat
getDefaultFormat()
The format used for serialization/deserialization by default by this ContentHandler.
Definition: ContentHandler.php:519
WikiPage\getContent
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition: WikiPage.php:837
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:154
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:33
TextContentHandler\makeEmptyContent
makeEmptyContent()
Creates an empty TextContent object.
Definition: TextContentHandler.php:132
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:552
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:104
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:1640