MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
16use SearchEngine;
18
26
28 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
29 parent::__construct( $modelId, $formats );
30 }
31
40 public function serializeContent( Content $content, $format = null ) {
41 $this->checkFormat( $format );
42
43 // @phan-suppress-next-line PhanUndeclaredMethod
44 return $content->getText();
45 }
46
61 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
62 // Nothing to do when the unsaved edit is already identical to the latest revision
63 if ( $myContent->equals( $yourContent ) ) {
64 return $yourContent;
65 }
66 // Impossible to have a conflict when the user just edited the latest revision. This can
67 // happen e.g. when $wgDiff3 is badly configured.
68 if ( $oldContent->equals( $yourContent ) ) {
69 return $myContent;
70 }
71
72 $this->checkModelID( $oldContent->getModel() );
73 $this->checkModelID( $myContent->getModel() );
74 $this->checkModelID( $yourContent->getModel() );
75
76 $format = $this->getDefaultFormat();
77
78 $old = $this->serializeContent( $oldContent, $format );
79 $mine = $this->serializeContent( $myContent, $format );
80 $yours = $this->serializeContent( $yourContent, $format );
81
82 $ok = wfMerge( $old, $mine, $yours, $result );
83
84 if ( !$ok ) {
85 return false;
86 }
87
88 if ( !$result ) {
89 return $this->makeEmptyContent();
90 }
91
92 $mergedContent = $this->unserializeContent( $result, $format );
93
94 return $mergedContent;
95 }
96
106 protected function getContentClass() {
107 return TextContent::class;
108 }
109
120 public function unserializeContent( $text, $format = null ) {
121 $this->checkFormat( $format );
122
123 $class = $this->getContentClass();
124 return new $class( $text );
125 }
126
134 public function makeEmptyContent() {
135 $class = $this->getContentClass();
136 return new $class( '' );
137 }
138
144 public function supportsDirectEditing() {
145 return true;
146 }
147
149 public function getFieldsForSearchIndex( SearchEngine $engine ) {
150 $fields = parent::getFieldsForSearchIndex( $engine );
151 $fields['language'] =
152 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
153
154 return $fields;
155 }
156
158 public function getDataForSearchIndex(
159 WikiPage $page,
160 ParserOutput $output,
161 SearchEngine $engine,
162 ?RevisionRecord $revision = null
163 ) {
164 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
165 $fields['language'] =
166 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
167 return $fields;
168 }
169
170 public function preSaveTransform(
171 Content $content,
172 PreSaveTransformParams $pstParams
173 ): Content {
174 '@phan-var TextContent $content';
175
176 $text = $content->getText();
177
178 $pst = TextContent::normalizeLineEndings( $text );
179
180 $contentClass = $this->getContentClass();
181 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
182 }
183
204 protected function fillParserOutput(
205 Content $content,
206 ContentParseParams $cpoParams,
207 ParserOutput &$output
208 ) {
209 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
211 '@phan-var TextContent $content';
212 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
213 // parse just to get links etc into the database, HTML is replaced below.
214 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
215 ->parse(
216 $content->getText(),
217 $cpoParams->getPage(),
218 $cpoParams->getParserOptions(),
219 true,
220 true,
221 $cpoParams->getRevId()
222 );
223 }
224
225 if ( $cpoParams->getGenerateHtml() ) {
226 // Return an HTML representation of the content
227 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
228 $html = "<pre>$html</pre>";
229 } else {
230 $html = null;
231 }
232
233 $output->clearWrapperDivClass();
234 $output->setRawText( $html );
235 }
236}
238class_alias( TextContentHandler::class, 'TextContentHandler' );
const CONTENT_FORMAT_TEXT
For future use, e.g.
Definition Defines.php:261
const CONTENT_MODEL_TEXT
Definition Defines.php:238
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.Expose title, redirect, namespace, text, source_text,...
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.Overriding class shou...
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:82
getContent( $audience=RevisionRecord::FOR_PUBLIC, ?Authority $performer=null)
Get the content of the current revision.
Definition WikiPage.php:761
getTitle()
Get the title object of the article.
Definition WikiPage.php:250
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:28
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.