MediaWiki master
TextContentHandler.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Content;
8
9use MediaWiki\Content\Renderer\ContentParseParams;
10use MediaWiki\Content\Transform\PreSaveTransformParams;
11use MediaWiki\MainConfigNames;
12use MediaWiki\MediaWikiServices;
13use MediaWiki\Page\WikiPage;
14use MediaWiki\Parser\ParserFactory;
15use MediaWiki\Parser\ParserOptions;
16use MediaWiki\Parser\ParserOutput;
17use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
18use MediaWiki\Revision\RevisionRecord;
19use MediaWiki\Search\SearchEngine;
20use MediaWiki\Search\SearchIndexField;
21
29
31 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
32 parent::__construct( $modelId, $formats );
33 }
34
43 public function serializeContent( Content $content, $format = null ) {
44 $this->checkFormat( $format );
45
46 // @phan-suppress-next-line PhanUndeclaredMethod
47 return $content->getText();
48 }
49
64 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
65 // Nothing to do when the unsaved edit is already identical to the latest revision
66 if ( $myContent->equals( $yourContent ) ) {
67 return $yourContent;
68 }
69 // Impossible to have a conflict when the user just edited the latest revision. This can
70 // happen e.g. when $wgDiff3 is badly configured.
71 if ( $oldContent->equals( $yourContent ) ) {
72 return $myContent;
73 }
74
75 $this->checkModelID( $oldContent->getModel() );
76 $this->checkModelID( $myContent->getModel() );
77 $this->checkModelID( $yourContent->getModel() );
78
79 $format = $this->getDefaultFormat();
80
81 $old = $this->serializeContent( $oldContent, $format );
82 $mine = $this->serializeContent( $myContent, $format );
83 $yours = $this->serializeContent( $yourContent, $format );
84
85 $ok = wfMerge( $old, $mine, $yours, $result );
86
87 if ( !$ok ) {
88 return false;
89 }
90
91 if ( !$result ) {
92 return $this->makeEmptyContent();
93 }
94
95 $mergedContent = $this->unserializeContent( $result, $format );
96
97 return $mergedContent;
98 }
99
109 protected function getContentClass() {
110 return TextContent::class;
111 }
112
123 public function unserializeContent( $text, $format = null ) {
124 $this->checkFormat( $format );
125
126 $class = $this->getContentClass();
127 return new $class( $text );
128 }
129
137 public function makeEmptyContent() {
138 $class = $this->getContentClass();
139 return new $class( '' );
140 }
141
147 public function supportsDirectEditing() {
148 return true;
149 }
150
152 public function getFieldsForSearchIndex( SearchEngine $engine ) {
153 $fields = parent::getFieldsForSearchIndex( $engine );
154 $fields['language'] =
155 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
156
157 return $fields;
158 }
159
161 public function getDataForSearchIndex(
162 WikiPage $page,
163 ParserOutput $output,
164 SearchEngine $engine,
165 ?RevisionRecord $revision = null
166 ) {
167 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
168 $fields['language'] =
169 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
170 return $fields;
171 }
172
173 public function preSaveTransform(
174 Content $content,
175 PreSaveTransformParams $pstParams
176 ): Content {
177 '@phan-var TextContent $content';
178
179 $text = $content->getText();
180
181 $pst = TextContent::normalizeLineEndings( $text );
182
183 $contentClass = $this->getContentClass();
184 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
185 }
186
207 protected function fillParserOutput(
208 Content $content,
209 ContentParseParams $cpoParams,
210 ParserOutput &$output
211 ) {
212 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
214 $parserFactory = MediaWikiServices::getInstance()->getParserFactory();
215 $parsoidParserFactory = MediaWikiServices::getInstance()->getParsoidParserFactory();
216
217 '@phan-var TextContent $content';
218 $this->collectMetadata(
219 $content, $cpoParams, $output, $textModelsToParse, $parserFactory,
220 $parsoidParserFactory, $cpoParams->getParserOptions()
221 );
222
223 if ( $cpoParams->getGenerateHtml() ) {
224 // Return an HTML representation of the content
225 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
226 $html = "<pre>$html</pre>";
227 } else {
228 $html = null;
229 }
230
231 $output->clearWrapperDivClass();
232 $output->setContentHolderText( $html );
233 }
234
235 protected function collectMetadata(
236 TextContent $content,
237 ContentParseParams $cpoParams,
238 ParserOutput $output,
239 array $textModelsToParse,
240 ParserFactory $parserFactory,
241 ParsoidParserFactory $parsoidParserFactory,
242 ParserOptions $parserOptions
243 ) {
244 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
245 // Parse just to get links etc into the database, no HTML is added;
246 // HTML is generated by the caller.
247
248 if ( $parserOptions->getUseParsoid() ) {
249 $parser = $parsoidParserFactory->create();
250 } else {
251 $parser = $parserFactory->getInstance();
252 }
253
254 $parser->parse(
255 $content->getText(),
256 $cpoParams->getPage(),
257 $parserOptions,
258 true,
259 true,
260 $cpoParams->getRevId()
261 )->collectMetadata( $output );
262 }
263 }
264
265}
266
267class_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...
collectMetadata(TextContent $content, ContentParseParams $cpoParams, ParserOutput $output, array $textModelsToParse, ParserFactory $parserFactory, ParsoidParserFactory $parsoidParserFactory, ParserOptions $parserOptions)
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.
Content object implementation for representing flat text.
static normalizeLineEndings( $text)
Do a "\\r\\n" -> "\\n" and "\\r" -> "\\n" transformation as well as trim trailing whitespace.
getText()
Returns the text represented by this Content object, as a string.
const TextModelsToParse
Name constant for the TextModelsToParse setting, for use with Config\get().
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 latest revision.
Definition WikiPage.php:775
getTitle()
Get the title object of the article.
Definition WikiPage.php:250
getInstance()
Get the main shared instance, or if it is locked, get a new instance.
Set options of the Parser.
getUseParsoid()
Parsoid-format HTML output, or legacy wikitext parser HTML?
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...
setContentHolderText(?string $text)
Sets the body fragment text of the ParserOutput.
ParserFactory which uses a ParsoidParser.
Page revision base class.
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.