Debian Codegraph @

includes/content/TextContentHandler.php

raw symbols SCIP
1 <?php
2 /**
3 * Base content handler class for flat text contents.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 */
25
26 namespace MediaWiki\Content;
27
28 use MediaWiki\Content\Renderer\ContentParseParams;
29 use MediaWiki\Content\Transform\PreSaveTransformParams;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\MediaWikiServices;
32 use MediaWiki\Parser\ParserOutput;
33 use MediaWiki\Revision\RevisionRecord;
34 use SearchEngine;
35 use SearchIndexField;
36 use WikiPage;
37
38 /**
39 * Base content handler implementation for flat text contents.
40 *
41 * @ingroup Content
42 */
43 class TextContentHandler extends ContentHandler {
44
45 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
46 parent::__construct( $modelId, $formats );
47 }
48
49 /**
50 * Returns the content's text as-is.
51 *
52 * @param Content $content
53 * @param string|null $format The serialization format to check
54 *
55 * @return mixed
56 */
57 public function serializeContent( Content $content, $format = null ) {
58 $this->checkFormat( $format );
59
60 // @phan-suppress-next-line PhanUndeclaredMethod
61 return $content->getText();
62 }
63
64 /**
65 * Attempts to merge differences between three versions. Returns a new
66 * Content object for a clean merge and false for failure or a conflict.
67 *
68 * All three Content objects passed as parameters must have the same
69 * content model.
70 *
71 * This text-based implementation uses wfMerge().
72 *
73 * @param Content $oldContent The page's previous content.
74 * @param Content $myContent One of the page's conflicting contents.
75 * @param Content $yourContent One of the page's conflicting contents.
76 *
77 * @return Content|false
78 */
79 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
80 // Nothing to do when the unsaved edit is already identical to the latest revision
81 if ( $myContent->equals( $yourContent ) ) {
82 return $yourContent;
83 }
84 // Impossible to have a conflict when the user just edited the latest revision. This can
85 // happen e.g. when $wgDiff3 is badly configured.
86 if ( $oldContent->equals( $yourContent ) ) {
87 return $myContent;
88 }
89
90 $this->checkModelID( $oldContent->getModel() );
91 $this->checkModelID( $myContent->getModel() );
92 $this->checkModelID( $yourContent->getModel() );
93
94 $format = $this->getDefaultFormat();
95
96 $old = $this->serializeContent( $oldContent, $format );
97 $mine = $this->serializeContent( $myContent, $format );
98 $yours = $this->serializeContent( $yourContent, $format );
99
100 $ok = wfMerge( $old, $mine, $yours, $result );
101
102 if ( !$ok ) {
103 return false;
104 }
105
106 if ( !$result ) {
107 return $this->makeEmptyContent();
108 }
109
110 $mergedContent = $this->unserializeContent( $result, $format );
111
112 return $mergedContent;
113 }
114
115 /**
116 * Returns the name of the associated Content class, to
117 * be used when creating new objects. Override expected
118 * by subclasses.
119 *
120 * @since 1.24
121 *
122 * @return class-string<TextContent>
123 */
124 protected function getContentClass() {
125 return TextContent::class;
126 }
127
128 /**
129 * Unserializes a Content object of the type supported by this ContentHandler.
130 *
131 * @since 1.21
132 *
133 * @param string $text Serialized form of the content
134 * @param string|null $format The format used for serialization
135 *
136 * @return Content The TextContent object wrapping $text
137 */
138 public function unserializeContent( $text, $format = null ) {
139 $this->checkFormat( $format );
140
141 $class = $this->getContentClass();
142 return new $class( $text );
143 }
144
145 /**
146 * Creates an empty TextContent object.
147 *
148 * @since 1.21
149 *
150 * @return Content A new TextContent object with empty text.
151 */
152 public function makeEmptyContent() {
153 $class = $this->getContentClass();
154 return new $class( '' );
155 }
156
157 /**
158 * @see ContentHandler::supportsDirectEditing
159 *
160 * @return bool Should return true for TextContent and derivatives.
161 */
162 public function supportsDirectEditing() {
163 return true;
164 }
165
166 public function getFieldsForSearchIndex( SearchEngine $engine ) {
167 $fields = parent::getFieldsForSearchIndex( $engine );
168 $fields['language'] =
169 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
170
171 return $fields;
172 }
173
174 public function getDataForSearchIndex(
175 WikiPage $page,
176 ParserOutput $output,
177 SearchEngine $engine,
178 ?RevisionRecord $revision = null
179 ) {
180 $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
181 $fields['language'] =
182 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
183 return $fields;
184 }
185
186 public function preSaveTransform(
187 Content $content,
188 PreSaveTransformParams $pstParams
189 ): Content {
190 '@phan-var TextContent $content';
191
192 $text = $content->getText();
193
194 $pst = TextContent::normalizeLineEndings( $text );
195
196 $contentClass = $this->getContentClass();
197 return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
198 }
199
200 /**
201 * Fills the provided ParserOutput object with information derived from the content.
202 * Unless $generateHtml was false, this includes an HTML representation of the content
203 * provided by getHtml().
204 *
205 * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
206 * wikitext parser on the text to extract any (wikitext) links, magic words, etc.,
207 * but note that the Table of Contents will *not* be generated
208 * (feature added by T307691, but should be refactored: T313455).
209 *
210 * Subclasses may override this to provide custom content processing.
211 * For custom HTML generation alone, it is sufficient to override getHtml().
212 *
213 * @stable to override
214 *
215 * @since 1.38
216 * @param Content $content
217 * @param ContentParseParams $cpoParams
218 * @param ParserOutput &$output The output object to fill (reference).
219 */
220 protected function fillParserOutput(
221 Content $content,
222 ContentParseParams $cpoParams,
223 ParserOutput &$output
224 ) {
225 $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
226 MainConfigNames::TextModelsToParse );
227 '@phan-var TextContent $content';
228 if ( in_array( $content->getModel(), $textModelsToParse ) ) {
229 // parse just to get links etc into the database, HTML is replaced below.
230 $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
231 ->parse(
232 $content->getText(),
233 $cpoParams->getPage(),
234 $cpoParams->getParserOptions(),
235 true,
236 true,
237 $cpoParams->getRevId()
238 );
239 }
240
241 if ( $cpoParams->getGenerateHtml() ) {
242 // Return an HTML representation of the content
243 $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
244 $html = "<pre>$html</pre>";
245 } else {
246 $html = null;
247 }
248
249 $output->clearWrapperDivClass();
250 $output->setRawText( $html );
251 }
252 }
253 /** @deprecated class alias since 1.43 */
254 class_alias( TextContentHandler::class, 'TextContentHandler' );