MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
32
39
40 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
41 parent::__construct( $modelId, $formats );
42 }
43
52 public function serializeContent( Content $content, $format = null ) {
53 $this->checkFormat( $format );
54
55 // @phan-suppress-next-line PhanUndeclaredMethod
56 return $content->getText();
57 }
58
74 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
75 // Nothing to do when the unsaved edit is already identical to the latest revision
76 if ( $myContent->equals( $yourContent ) ) {
77 return $yourContent;
78 }
79 // Impossible to have a conflict when the user just edited the latest revision. This can
80 // happen e.g. when $wgDiff3 is badly configured.
81 if ( $oldContent->equals( $yourContent ) ) {
82 return $myContent;
83 }
84
85 $this->checkModelID( $oldContent->getModel() );
86 $this->checkModelID( $myContent->getModel() );
87 $this->checkModelID( $yourContent->getModel() );
88
89 $format = $this->getDefaultFormat();
90
91 $old = $this->serializeContent( $oldContent, $format );
92 $mine = $this->serializeContent( $myContent, $format );
93 $yours = $this->serializeContent( $yourContent, $format );
94
95 $ok = wfMerge( $old, $mine, $yours, $result );
96
97 if ( !$ok ) {
98 return false;
99 }
100
101 if ( !$result ) {
102 return $this->makeEmptyContent();
103 }
104
105 $mergedContent = $this->unserializeContent( $result, $format );
106
107 return $mergedContent;
108 }
109
119 protected function getContentClass() {
120 return TextContent::class;
121 }
122
133 public function unserializeContent( $text, $format = null ) {
134 $this->checkFormat( $format );
135
136 $class = $this->getContentClass();
137 return new $class( $text );
138 }
139
147 public function makeEmptyContent() {
148 $class = $this->getContentClass();
149 return new $class( '' );
150 }
151
157 public function supportsDirectEditing() {
158 return true;
159 }
160
161 public function getFieldsForSearchIndex( SearchEngine $engine ) {
162 $fields = parent::getFieldsForSearchIndex( $engine );
163 $fields['language'] =
164 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
165
166 return $fields;
167 }
168
169 public function getDataForSearchIndex(
170 WikiPage $page,
171 ParserOutput $output,
172 SearchEngine $engine,
173 ?RevisionRecord $revision = null
174 ) {
175 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
176 $fields['language'] =
177 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
178 return $fields;
179 }
180
181 public function preSaveTransform(
182 Content $content,
183 PreSaveTransformParams $pstParams
184 ): Content {
185 '@phan-var TextContent $content';
186
187 $text = $content->getText();
188
189 $pst = TextContent::normalizeLineEndings( $text );
190
191 $contentClass = $this->getContentClass();
192 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
193 }
194
215 protected function fillParserOutput(
216 Content $content,
217 ContentParseParams $cpoParams,
218 ParserOutput &$output
219 ) {
220 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
221 MainConfigNames::TextModelsToParse );
222 '@phan-var TextContent $content';
223 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
224 // parse just to get links etc into the database, HTML is replaced below.
225 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
226 ->parse(
227 $content->getText(),
228 $cpoParams->getPage(),
229 $cpoParams->getParserOptions(),
230 true,
231 true,
232 $cpoParams->getRevId()
233 );
234 }
235
236 if ( $cpoParams->getGenerateHtml() ) {
237 // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
238 if ( method_exists( $content, 'getHtml' ) ) {
239 $method = new ReflectionMethod( $content, 'getHtml' );
240 $method->setAccessible( true );
241 $html = $method->invoke( $content );
242 $html = "<pre>$html</pre>";
243 } else {
244 // Return an HTML representation of the content
245 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
246 $html = "<pre>$html</pre>";
247 }
248 } else {
249 $html = null;
250 }
251
252 $output->clearWrapperDivClass();
253 $output->setRawText( $html );
254 }
255}
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition Defines.php:240
const CONTENT_MODEL_TEXT
Definition Defines.php:221
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.
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.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Rendered output of a wiki page, as parsed from wikitext.
clearWrapperDivClass()
Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div until addWrap...
setRawText(?string $text)
Set the raw text of the ParserOutput.
Page revision base class.
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)
Base representation for an editable wiki page.
Definition WikiPage.php:79
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition WikiPage.php:760
getTitle()
Get the title object of the article.
Definition WikiPage.php:260
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.
equals(Content $that=null)
Returns true if this Content objects is conceptually equivalent to the given Content object.