MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
30use ReflectionMethod;
31use SearchEngine;
33
41
42 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
43 parent::__construct( $modelId, $formats );
44 }
45
54 public function serializeContent( Content $content, $format = null ) {
55 $this->checkFormat( $format );
56
57 // @phan-suppress-next-line PhanUndeclaredMethod
58 return $content->getText();
59 }
60
75 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
76 // Nothing to do when the unsaved edit is already identical to the latest revision
77 if ( $myContent->equals( $yourContent ) ) {
78 return $yourContent;
79 }
80 // Impossible to have a conflict when the user just edited the latest revision. This can
81 // happen e.g. when $wgDiff3 is badly configured.
82 if ( $oldContent->equals( $yourContent ) ) {
83 return $myContent;
84 }
85
86 $this->checkModelID( $oldContent->getModel() );
87 $this->checkModelID( $myContent->getModel() );
88 $this->checkModelID( $yourContent->getModel() );
89
90 $format = $this->getDefaultFormat();
91
92 $old = $this->serializeContent( $oldContent, $format );
93 $mine = $this->serializeContent( $myContent, $format );
94 $yours = $this->serializeContent( $yourContent, $format );
95
96 $ok = wfMerge( $old, $mine, $yours, $result );
97
98 if ( !$ok ) {
99 return false;
100 }
101
102 if ( !$result ) {
103 return $this->makeEmptyContent();
104 }
105
106 $mergedContent = $this->unserializeContent( $result, $format );
107
108 return $mergedContent;
109 }
110
120 protected function getContentClass() {
121 return TextContent::class;
122 }
123
134 public function unserializeContent( $text, $format = null ) {
135 $this->checkFormat( $format );
136
137 $class = $this->getContentClass();
138 return new $class( $text );
139 }
140
148 public function makeEmptyContent() {
149 $class = $this->getContentClass();
150 return new $class( '' );
151 }
152
158 public function supportsDirectEditing() {
159 return true;
160 }
161
162 public function getFieldsForSearchIndex( SearchEngine $engine ) {
163 $fields = parent::getFieldsForSearchIndex( $engine );
164 $fields['language'] =
165 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
166
167 return $fields;
168 }
169
170 public function getDataForSearchIndex(
171 WikiPage $page,
172 ParserOutput $output,
173 SearchEngine $engine,
174 ?RevisionRecord $revision = null
175 ) {
176 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
177 $fields['language'] =
178 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
179 return $fields;
180 }
181
182 public function preSaveTransform(
183 Content $content,
184 PreSaveTransformParams $pstParams
185 ): Content {
186 '@phan-var TextContent $content';
187
188 $text = $content->getText();
189
190 $pst = TextContent::normalizeLineEndings( $text );
191
192 $contentClass = $this->getContentClass();
193 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
194 }
195
216 protected function fillParserOutput(
217 Content $content,
218 ContentParseParams $cpoParams,
219 ParserOutput &$output
220 ) {
221 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
223 '@phan-var TextContent $content';
224 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
225 // parse just to get links etc into the database, HTML is replaced below.
226 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
227 ->parse(
228 $content->getText(),
229 $cpoParams->getPage(),
230 $cpoParams->getParserOptions(),
231 true,
232 true,
233 $cpoParams->getRevId()
234 );
235 }
236
237 if ( $cpoParams->getGenerateHtml() ) {
238 // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
239 if ( method_exists( $content, 'getHtml' ) ) {
240 $method = new ReflectionMethod( $content, 'getHtml' );
241 $method->setAccessible( true );
242 $html = $method->invoke( $content );
243 $html = "<pre>$html</pre>";
244 } else {
245 // Return an HTML representation of the content
246 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
247 $html = "<pre>$html</pre>";
248 }
249 } else {
250 $html = null;
251 }
252
253 $output->clearWrapperDivClass();
254 $output->setRawText( $html );
255 }
256}
258class_alias( TextContentHandler::class, 'TextContentHandler' );
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition Defines.php:271
const CONTENT_MODEL_TEXT
Definition Defines.php:252
wfMerge(string $old, string $mine, string $yours, ?string &$simplisticMergeAttempt, ?string &$mergeLeftovers=null)
wfMerge attempts to merge differences between three texts.
Base class for content handling.
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.
Base representation for an editable wiki page.
Definition WikiPage.php:93
getContent( $audience=RevisionRecord::FOR_PUBLIC, ?Authority $performer=null)
Get the content of the current revision.
Definition WikiPage.php:772
getTitle()
Get the title object of the article.
Definition WikiPage.php:261
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.
Content objects represent page content, e.g.
Definition Content.php:42
equals(?Content $that=null)
Whether this Content object is conceptually equivalent to another one.
getModel()
Get the content model ID.
Definition of a mapping for the search index field.