MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Content;
27
28use Content;
36use ReflectionMethod;
37use SearchEngine;
39use WikiPage;
40
47
48 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
49 parent::__construct( $modelId, $formats );
50 }
51
60 public function serializeContent( Content $content, $format = null ) {
61 $this->checkFormat( $format );
62
63 // @phan-suppress-next-line PhanUndeclaredMethod
64 return $content->getText();
65 }
66
82 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
83 // Nothing to do when the unsaved edit is already identical to the latest revision
84 if ( $myContent->equals( $yourContent ) ) {
85 return $yourContent;
86 }
87 // Impossible to have a conflict when the user just edited the latest revision. This can
88 // happen e.g. when $wgDiff3 is badly configured.
89 if ( $oldContent->equals( $yourContent ) ) {
90 return $myContent;
91 }
92
93 $this->checkModelID( $oldContent->getModel() );
94 $this->checkModelID( $myContent->getModel() );
95 $this->checkModelID( $yourContent->getModel() );
96
97 $format = $this->getDefaultFormat();
98
99 $old = $this->serializeContent( $oldContent, $format );
100 $mine = $this->serializeContent( $myContent, $format );
101 $yours = $this->serializeContent( $yourContent, $format );
102
103 $ok = wfMerge( $old, $mine, $yours, $result );
104
105 if ( !$ok ) {
106 return false;
107 }
108
109 if ( !$result ) {
110 return $this->makeEmptyContent();
111 }
112
113 $mergedContent = $this->unserializeContent( $result, $format );
114
115 return $mergedContent;
116 }
117
127 protected function getContentClass() {
128 return TextContent::class;
129 }
130
141 public function unserializeContent( $text, $format = null ) {
142 $this->checkFormat( $format );
143
144 $class = $this->getContentClass();
145 return new $class( $text );
146 }
147
155 public function makeEmptyContent() {
156 $class = $this->getContentClass();
157 return new $class( '' );
158 }
159
165 public function supportsDirectEditing() {
166 return true;
167 }
168
169 public function getFieldsForSearchIndex( SearchEngine $engine ) {
170 $fields = parent::getFieldsForSearchIndex( $engine );
171 $fields['language'] =
173
174 return $fields;
175 }
176
177 public function getDataForSearchIndex(
178 WikiPage $page,
179 ParserOutput $output,
180 SearchEngine $engine,
181 ?RevisionRecord $revision = null
182 ) {
183 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
184 $fields['language'] =
185 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
186 return $fields;
187 }
188
189 public function preSaveTransform(
190 Content $content,
191 PreSaveTransformParams $pstParams
192 ): Content {
193 '@phan-var TextContent $content';
194
195 $text = $content->getText();
196
197 $pst = TextContent::normalizeLineEndings( $text );
198
199 $contentClass = $this->getContentClass();
200 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
201 }
202
223 protected function fillParserOutput(
224 Content $content,
225 ContentParseParams $cpoParams,
226 ParserOutput &$output
227 ) {
228 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
230 '@phan-var TextContent $content';
231 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
232 // parse just to get links etc into the database, HTML is replaced below.
233 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
234 ->parse(
235 $content->getText(),
236 $cpoParams->getPage(),
237 $cpoParams->getParserOptions(),
238 true,
239 true,
240 $cpoParams->getRevId()
241 );
242 }
243
244 if ( $cpoParams->getGenerateHtml() ) {
245 // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
246 if ( method_exists( $content, 'getHtml' ) ) {
247 $method = new ReflectionMethod( $content, 'getHtml' );
248 $method->setAccessible( true );
249 $html = $method->invoke( $content );
250 $html = "<pre>$html</pre>";
251 } else {
252 // Return an HTML representation of the content
253 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
254 $html = "<pre>$html</pre>";
255 }
256 } else {
257 $html = null;
258 }
259
260 $output->clearWrapperDivClass();
261 $output->setRawText( $html );
262 }
263}
265class_alias( TextContentHandler::class, 'TextContentHandler' );
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition Defines.php:244
const CONTENT_MODEL_TEXT
Definition Defines.php:225
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.
Base content handler implementation for flat text contents.
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine, ?RevisionRecord $revision=null)
preSaveTransform(Content $content, PreSaveTransformParams $pstParams)
Returns a $content object with pre-save transformations applied (or the same object if no transformat...
getContentClass()
Returns the name of the associated Content class, to be used when creating new objects.
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$output)
Fills the provided ParserOutput object with information derived from the content.
unserializeContent( $text, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
makeEmptyContent()
Creates an empty TextContent object.
__construct( $modelId=CONTENT_MODEL_TEXT, $formats=[CONTENT_FORMAT_TEXT])
Constructor, initializing the ContentHandler instance with its model ID and a list of supported forma...
serializeContent(Content $content, $format=null)
Returns the content's text as-is.
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
A class containing constants representing the names of configuration variables.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
ParserOutput is a rendering of a Content object or a message.
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 representation for an editable wiki page.
Definition WikiPage.php:81
getContent( $audience=RevisionRecord::FOR_PUBLIC, Authority $performer=null)
Get the content of the current revision.
Definition WikiPage.php:767
getTitle()
Get the title object of the article.
Definition WikiPage.php:255
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.
Definition of a mapping for the search index field.
const INDEX_TYPE_KEYWORD
KEYWORD fields are indexed without any processing, so are appropriate for e.g.