MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Content;
27
34use ReflectionMethod;
35use SearchEngine;
37use WikiPage;
38
45
46 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
47 parent::__construct( $modelId, $formats );
48 }
49
58 public function serializeContent( Content $content, $format = null ) {
59 $this->checkFormat( $format );
60
61 // @phan-suppress-next-line PhanUndeclaredMethod
62 return $content->getText();
63 }
64
80 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
81 // Nothing to do when the unsaved edit is already identical to the latest revision
82 if ( $myContent->equals( $yourContent ) ) {
83 return $yourContent;
84 }
85 // Impossible to have a conflict when the user just edited the latest revision. This can
86 // happen e.g. when $wgDiff3 is badly configured.
87 if ( $oldContent->equals( $yourContent ) ) {
88 return $myContent;
89 }
90
91 $this->checkModelID( $oldContent->getModel() );
92 $this->checkModelID( $myContent->getModel() );
93 $this->checkModelID( $yourContent->getModel() );
94
95 $format = $this->getDefaultFormat();
96
97 $old = $this->serializeContent( $oldContent, $format );
98 $mine = $this->serializeContent( $myContent, $format );
99 $yours = $this->serializeContent( $yourContent, $format );
100
101 $ok = wfMerge( $old, $mine, $yours, $result );
102
103 if ( !$ok ) {
104 return false;
105 }
106
107 if ( !$result ) {
108 return $this->makeEmptyContent();
109 }
110
111 $mergedContent = $this->unserializeContent( $result, $format );
112
113 return $mergedContent;
114 }
115
125 protected function getContentClass() {
126 return TextContent::class;
127 }
128
139 public function unserializeContent( $text, $format = null ) {
140 $this->checkFormat( $format );
141
142 $class = $this->getContentClass();
143 return new $class( $text );
144 }
145
153 public function makeEmptyContent() {
154 $class = $this->getContentClass();
155 return new $class( '' );
156 }
157
163 public function supportsDirectEditing() {
164 return true;
165 }
166
167 public function getFieldsForSearchIndex( SearchEngine $engine ) {
168 $fields = parent::getFieldsForSearchIndex( $engine );
169 $fields['language'] =
170 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
171
172 return $fields;
173 }
174
175 public function getDataForSearchIndex(
176 WikiPage $page,
177 ParserOutput $output,
178 SearchEngine $engine,
179 ?RevisionRecord $revision = null
180 ) {
181 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
182 $fields['language'] =
183 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
184 return $fields;
185 }
186
187 public function preSaveTransform(
188 Content $content,
189 PreSaveTransformParams $pstParams
190 ): Content {
191 '@phan-var TextContent $content';
192
193 $text = $content->getText();
194
195 $pst = TextContent::normalizeLineEndings( $text );
196
197 $contentClass = $this->getContentClass();
198 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
199 }
200
221 protected function fillParserOutput(
222 Content $content,
223 ContentParseParams $cpoParams,
224 ParserOutput &$output
225 ) {
226 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
228 '@phan-var TextContent $content';
229 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
230 // parse just to get links etc into the database, HTML is replaced below.
231 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
232 ->parse(
233 $content->getText(),
234 $cpoParams->getPage(),
235 $cpoParams->getParserOptions(),
236 true,
237 true,
238 $cpoParams->getRevId()
239 );
240 }
241
242 if ( $cpoParams->getGenerateHtml() ) {
243 // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
244 if ( method_exists( $content, 'getHtml' ) ) {
245 $method = new ReflectionMethod( $content, 'getHtml' );
246 $method->setAccessible( true );
247 $html = $method->invoke( $content );
248 $html = "<pre>$html</pre>";
249 } else {
250 // Return an HTML representation of the content
251 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
252 $html = "<pre>$html</pre>";
253 }
254 } else {
255 $html = null;
256 }
257
258 $output->clearWrapperDivClass();
259 $output->setRawText( $html );
260 }
261}
263class_alias( TextContentHandler::class, 'TextContentHandler' );
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition Defines.php:250
const CONTENT_MODEL_TEXT
Definition Defines.php:231
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.
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)
Return fields to be indexed by search engine as representation of this document.
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:84
getTitle()
Get the title object of the article.
Definition WikiPage.php:252
getContent( $audience=RevisionRecord::FOR_PUBLIC, ?Authority $performer=null)
Get the content of the current revision.
Definition WikiPage.php:763
Base interface for representing page content.
Definition Content.php:39
equals(?Content $that=null)
Returns true if this Content objects is conceptually equivalent to the given Content object.
getModel()
Returns the ID of the content model used by this Content object.
Definition of a mapping for the search index field.