Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
65.39% |
257 / 393 |
|
46.43% |
26 / 56 |
CRAP | |
0.00% |
0 / 1 |
ContentHandler | |
65.56% |
257 / 392 |
|
46.43% |
26 / 56 |
843.69 | |
0.00% |
0 / 1 |
getContentText | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
makeContent | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getDefaultModelFor | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getForContent | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getForModelID | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getLocalizedName | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
getContentModels | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getAllContentFormats | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
serializeContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
exportTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
unserializeContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
importTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeEmptyContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
makeRedirectContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getModelID | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkModelID | |
25.00% |
1 / 4 |
|
0.00% |
0 / 1 |
3.69 | |||
getSupportedFormats | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSupportedFormat | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
checkFormat | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getActionOverrides | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createDifferenceEngine | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getSlotDiffRenderer | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
getSlotDiffRendererInternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSlotDiffRendererWithOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
createTextSlotDiffRenderer | |
84.62% |
22 / 26 |
|
0.00% |
0 / 1 |
5.09 | |||
getPageLanguage | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
3.01 | |||
getPageViewLanguage | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
canBeUsedOn | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getDiffEngineClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
merge3 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getLanguageConverter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getChangeType | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
21 | |||
getAutosummary | |
56.86% |
29 / 51 |
|
0.00% |
0 / 1 |
18.03 | |||
getChangeTag | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getAutoDeleteReason | |
0.00% |
0 / 49 |
|
0.00% |
0 / 1 |
210 | |||
getUndoContent | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
isParserCacheSupported | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
supportsSections | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
supportsCategories | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsRedirects | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
supportsDirectEditing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsPreloadContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
generateHTMLOnEdit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supportsDirectApiEditing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFieldsForSearchIndex | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
addSearchField | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDataForSearchIndex | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |||
getParserOutputForIndexing | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
latestRevision | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
getSecondaryDataUpdates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDeletionUpdates | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
preSaveTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
preloadTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validateSave | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getParserOutput | |
81.58% |
31 / 38 |
|
0.00% |
0 / 1 |
5.16 | |||
fillParserOutputInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
fillParserOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Base class for content handling. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @since 1.21 |
22 | * |
23 | * @file |
24 | * @ingroup Content |
25 | * |
26 | * @author Daniel Kinzler |
27 | */ |
28 | |
29 | namespace MediaWiki\Content; |
30 | |
31 | use Action; |
32 | use DifferenceEngine; |
33 | use DifferenceEngineSlotDiffRenderer; |
34 | use InvalidArgumentException; |
35 | use LogicException; |
36 | use MediaWiki\CommentStore\CommentStore; |
37 | use MediaWiki\Content\Renderer\ContentParseParams; |
38 | use MediaWiki\Content\Transform\PreloadTransformParams; |
39 | use MediaWiki\Content\Transform\PreSaveTransformParams; |
40 | use MediaWiki\Context\IContextSource; |
41 | use MediaWiki\Context\RequestContext; |
42 | use MediaWiki\Deferred\DeferrableUpdate; |
43 | use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer; |
44 | use MediaWiki\HookContainer\HookRunner; |
45 | use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
46 | use MediaWiki\Language\ILanguageConverter; |
47 | use MediaWiki\Language\Language; |
48 | use MediaWiki\Logger\LoggerFactory; |
49 | use MediaWiki\MainConfigNames; |
50 | use MediaWiki\MediaWikiServices; |
51 | use MediaWiki\Page\ParserOutputAccess; |
52 | use MediaWiki\Parser\ParserCache; |
53 | use MediaWiki\Parser\ParserOutput; |
54 | use MediaWiki\Revision\RevisionRecord; |
55 | use MediaWiki\Revision\SlotRecord; |
56 | use MediaWiki\Revision\SlotRenderingProvider; |
57 | use MediaWiki\Search\ParserOutputSearchDataExtractor; |
58 | use MediaWiki\Title\Title; |
59 | use MWContentSerializationException; |
60 | use MWException; |
61 | use MWUnknownContentModelException; |
62 | use SearchEngine; |
63 | use SearchIndexField; |
64 | use SlotDiffRenderer; |
65 | use StatusValue; |
66 | use TextSlotDiffRenderer; |
67 | use UnexpectedValueException; |
68 | use Wikimedia\Assert\Assert; |
69 | use Wikimedia\Rdbms\IDBAccessObject; |
70 | use Wikimedia\ScopedCallback; |
71 | use WikiPage; |
72 | |
73 | /** |
74 | * A content handler knows how do deal with a specific type of content on a wiki |
75 | * page. Content is stored in the database in a serialized form (using a |
76 | * serialization format a.k.a. MIME type) and is unserialized into its native |
77 | * PHP representation (the content model), which is wrapped in an instance of |
78 | * the appropriate subclass of Content. |
79 | * |
80 | * ContentHandler instances are stateless singletons that serve, among other |
81 | * things, as a factory for Content objects. Generally, there is one subclass |
82 | * of ContentHandler and one subclass of Content for every type of content model. |
83 | * |
84 | * Some content types have a flat model, that is, their native representation |
85 | * is the same as their serialized form. Examples would be JavaScript and CSS |
86 | * code. As of now, this also applies to wikitext (MediaWiki's default content |
87 | * type), but wikitext content may be represented by a DOM or AST structure in |
88 | * the future. |
89 | * |
90 | * @stable to extend |
91 | * |
92 | * @ingroup Content |
93 | */ |
94 | abstract class ContentHandler { |
95 | use ProtectedHookAccessorTrait; |
96 | |
97 | /** |
98 | * Convenience function for getting flat text from a Content object. This |
99 | * should only be used in the context of backwards compatibility with code |
100 | * that is not yet able to handle Content objects! |
101 | * |
102 | * If $content is null, this method returns the empty string. |
103 | * |
104 | * If $content is an instance of TextContent, this method returns the flat |
105 | * text as returned by $content->getText(). |
106 | * |
107 | * If $content is not a TextContent object, this method returns null. |
108 | * |
109 | * @since 1.21 |
110 | * |
111 | * @deprecated since 1.37, use Content::getText() for TextContent instances |
112 | * instead. Hard deprecated since 1.43. |
113 | * |
114 | * @param Content|null $content |
115 | * @return string|null Textual form of the content, if available. |
116 | */ |
117 | public static function getContentText( ?Content $content = null ) { |
118 | wfDeprecated( __METHOD__, '1.37' ); |
119 | if ( $content === null ) { |
120 | return ''; |
121 | } |
122 | |
123 | if ( $content instanceof TextContent ) { |
124 | return $content->getText(); |
125 | } |
126 | |
127 | wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' ); |
128 | return null; |
129 | } |
130 | |
131 | /** |
132 | * Convenience function for creating a Content object from a given textual |
133 | * representation. |
134 | * |
135 | * $text will be deserialized into a Content object of the model specified |
136 | * by $modelId (or, if that is not given, $title->getContentModel()) using |
137 | * the given format. |
138 | * |
139 | * @since 1.21 |
140 | * |
141 | * @param string $text The textual representation, will be |
142 | * unserialized to create the Content object |
143 | * @param Title|null $title The title of the page this text belongs to. |
144 | * Required if $modelId is not provided. |
145 | * @param string|null $modelId The model to deserialize to. If not provided, |
146 | * $title->getContentModel() is used. |
147 | * @param string|null $format The format to use for deserialization. If not |
148 | * given, the model's default format is used. |
149 | * |
150 | * @throws MWContentSerializationException |
151 | * @throws MWUnknownContentModelException |
152 | * @return Content A Content object representing the text. |
153 | */ |
154 | public static function makeContent( $text, ?Title $title = null, |
155 | $modelId = null, $format = null ) { |
156 | if ( !$title && !$modelId ) { |
157 | throw new InvalidArgumentException( "Must provide a Title object or a content model ID." ); |
158 | } |
159 | |
160 | return MediaWikiServices::getInstance() |
161 | ->getContentHandlerFactory() |
162 | ->getContentHandler( $modelId ?? $title->getContentModel() ) |
163 | ->unserializeContent( $text, $format ); |
164 | } |
165 | |
166 | /** |
167 | * Returns the name of the default content model to be used for the page |
168 | * with the given title. |
169 | * |
170 | * Note: There should rarely be need to call this method directly. |
171 | * To determine the actual content model for a given page, use |
172 | * Title::getContentModel(). |
173 | * |
174 | * Which model is to be used by default for the page is determined based |
175 | * on several factors: |
176 | * - The global setting $wgNamespaceContentModels specifies a content model |
177 | * per namespace. |
178 | * - The hook ContentHandlerDefaultModelFor may be used to override the page's default |
179 | * model. |
180 | * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript |
181 | * model if they end in .js or .css, respectively. |
182 | * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise. |
183 | * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS |
184 | * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor |
185 | * hook should be used instead if possible. |
186 | * - The hook TitleIsWikitextPage may be used to force a page to use the |
187 | * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor |
188 | * hook should be used instead if possible. |
189 | * |
190 | * If none of the above applies, the wikitext model is used. |
191 | * |
192 | * @since 1.21 |
193 | * @deprecated since 1.33, use SlotRoleHandler::getDefaultModel() together with |
194 | * SlotRoleRegistry::getRoleHandler(). Hard deprecated since 1.43. |
195 | * |
196 | * @param Title $title |
197 | * |
198 | * @return string Default model name for the page given by $title |
199 | */ |
200 | public static function getDefaultModelFor( Title $title ) { |
201 | wfDeprecated( __METHOD__, '1.33' ); |
202 | $slotRoleregistry = MediaWikiServices::getInstance()->getSlotRoleRegistry(); |
203 | $mainSlotHandler = $slotRoleregistry->getRoleHandler( 'main' ); |
204 | return $mainSlotHandler->getDefaultModel( $title ); |
205 | } |
206 | |
207 | /** |
208 | * Returns the appropriate ContentHandler singleton for the given Content |
209 | * object. |
210 | * |
211 | * @deprecated since 1.35, instead use |
212 | * ContentHandlerFactory::getContentHandler( $content->getModel() ). |
213 | * Hard deprecated since 1.43. |
214 | * |
215 | * @since 1.21 |
216 | * |
217 | * @param Content $content |
218 | * |
219 | * @return ContentHandler |
220 | * @throws MWUnknownContentModelException |
221 | */ |
222 | public static function getForContent( Content $content ) { |
223 | wfDeprecated( __METHOD__, '1.35' ); |
224 | return MediaWikiServices::getInstance() |
225 | ->getContentHandlerFactory() |
226 | ->getContentHandler( $content->getModel() ); |
227 | } |
228 | |
229 | /** |
230 | * Returns the ContentHandler singleton for the given model ID. Use the |
231 | * CONTENT_MODEL_XXX constants to identify the desired content model. |
232 | * |
233 | * ContentHandler singletons are taken from the global $wgContentHandlers |
234 | * array. Keys in that array are model names, the values are either |
235 | * ContentHandler singleton objects, or strings specifying the appropriate |
236 | * subclass of ContentHandler. |
237 | * |
238 | * If a class name is encountered when looking up the singleton for a given |
239 | * model name, the class is instantiated and the class name is replaced by |
240 | * the resulting singleton in $wgContentHandlers. |
241 | * |
242 | * If no ContentHandler is defined for the desired $modelId, the |
243 | * ContentHandler may be provided by the ContentHandlerForModelID hook. |
244 | * If no ContentHandler can be determined, an MWUnknownContentModelException is raised. |
245 | * |
246 | * @since 1.21 |
247 | * |
248 | * @deprecated since 1.35, use ContentHandlerFactory::getContentHandler |
249 | * Hard deprecated since 1.43. |
250 | * @see ContentHandlerFactory::getContentHandler() |
251 | * |
252 | * @param string $modelId The ID of the content model for which to get a |
253 | * handler. Use CONTENT_MODEL_XXX constants. |
254 | * |
255 | * @throws MWUnknownContentModelException If no handler is known for the model ID. |
256 | * @return ContentHandler The ContentHandler singleton for handling the model given by the ID. |
257 | */ |
258 | public static function getForModelID( $modelId ) { |
259 | wfDeprecated( __METHOD__, '1.35' ); |
260 | return MediaWikiServices::getInstance() |
261 | ->getContentHandlerFactory() |
262 | ->getContentHandler( $modelId ); |
263 | } |
264 | |
265 | /** |
266 | * Returns the localized name for a given content model. |
267 | * |
268 | * Model names are localized using system messages. Message keys |
269 | * have the form content-model-$name, where $name is getContentModelName( $id ). |
270 | * |
271 | * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX |
272 | * constant or returned by Content::getModel() or SlotRecord::getModel(). |
273 | * @param Language|null $lang The language to parse the message in (since 1.26) |
274 | * |
275 | * @return string The content model's localized name. |
276 | */ |
277 | public static function getLocalizedName( $name, ?Language $lang = null ) { |
278 | // Messages: content-model-wikitext, content-model-text, |
279 | // content-model-javascript, content-model-css |
280 | // Lowercase the name as message keys need to be in lowercase, T358341 |
281 | $key = "content-model-" . strtolower( $name ?? '' ); |
282 | |
283 | $msg = wfMessage( $key ); |
284 | if ( $lang ) { |
285 | $msg->inLanguage( $lang ); |
286 | } |
287 | |
288 | return $msg->exists() ? $msg->plain() : $name; |
289 | } |
290 | |
291 | /** |
292 | * @deprecated since 1.35, use ContentHandlerFactory::getContentModels |
293 | * Hard deprecated since 1.43. |
294 | * @see ContentHandlerFactory::getContentModels |
295 | * |
296 | * @return string[] |
297 | */ |
298 | public static function getContentModels() { |
299 | wfDeprecated( __METHOD__, '1.35' ); |
300 | return MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentModels(); |
301 | } |
302 | |
303 | /** |
304 | * @return string[] |
305 | * |
306 | * @deprecated since 1.35, use ContentHandlerFactory::getAllContentFormats |
307 | * Hard deprecated since 1.43. |
308 | * @see ContentHandlerFactory::getAllContentFormats |
309 | */ |
310 | public static function getAllContentFormats() { |
311 | wfDeprecated( __METHOD__, '1.35' ); |
312 | return MediaWikiServices::getInstance()->getContentHandlerFactory()->getAllContentFormats(); |
313 | } |
314 | |
315 | // ------------------------------------------------------------------------ |
316 | |
317 | /** |
318 | * @var string |
319 | */ |
320 | protected $mModelID; |
321 | |
322 | /** |
323 | * @var string[] |
324 | */ |
325 | protected $mSupportedFormats; |
326 | |
327 | /** |
328 | * Constructor, initializing the ContentHandler instance with its model ID |
329 | * and a list of supported formats. Values for the parameters are typically |
330 | * provided as literals by subclass's constructors. |
331 | * |
332 | * @stable to call |
333 | * |
334 | * @param string $modelId (use CONTENT_MODEL_XXX constants). |
335 | * @param string[] $formats List for supported serialization formats |
336 | * (typically as MIME types) |
337 | */ |
338 | public function __construct( $modelId, $formats ) { |
339 | $this->mModelID = $modelId; |
340 | $this->mSupportedFormats = $formats; |
341 | } |
342 | |
343 | /** |
344 | * Serializes a Content object of the type supported by this ContentHandler. |
345 | * |
346 | * @stable to override |
347 | * |
348 | * @since 1.21 |
349 | * |
350 | * @param Content $content The Content object to serialize |
351 | * @param string|null $format The desired serialization format |
352 | * |
353 | * @return string Serialized form of the content |
354 | */ |
355 | abstract public function serializeContent( Content $content, $format = null ); |
356 | |
357 | /** |
358 | * Applies transformations on export (returns the blob unchanged by default). |
359 | * Subclasses may override this to perform transformations such as conversion |
360 | * of legacy formats or filtering of internal meta-data. |
361 | * |
362 | * @stable to override |
363 | * |
364 | * @param string $blob The blob to be exported |
365 | * @param string|null $format The blob's serialization format |
366 | * |
367 | * @return string |
368 | */ |
369 | public function exportTransform( $blob, $format = null ) { |
370 | return $blob; |
371 | } |
372 | |
373 | /** |
374 | * Unserializes a Content object of the type supported by this ContentHandler. |
375 | * |
376 | * @stable to override |
377 | * @since 1.21 |
378 | * |
379 | * @param string $blob Serialized form of the content |
380 | * @param string|null $format The format used for serialization |
381 | * |
382 | * @return Content The Content object created by deserializing $blob |
383 | * @throws MWContentSerializationException |
384 | */ |
385 | abstract public function unserializeContent( $blob, $format = null ); |
386 | |
387 | /** |
388 | * Apply import transformation (by default, returns $blob unchanged). |
389 | * This gives subclasses an opportunity to transform data blobs on import. |
390 | * |
391 | * @stable to override |
392 | * @since 1.24 |
393 | * |
394 | * @param string $blob |
395 | * @param string|null $format |
396 | * |
397 | * @return string |
398 | */ |
399 | public function importTransform( $blob, $format = null ) { |
400 | return $blob; |
401 | } |
402 | |
403 | /** |
404 | * Creates an empty Content object of the type supported by this |
405 | * ContentHandler. |
406 | * |
407 | * @stable to override |
408 | * @since 1.21 |
409 | * |
410 | * @return Content |
411 | */ |
412 | abstract public function makeEmptyContent(); |
413 | |
414 | /** |
415 | * Creates a new Content object that acts as a redirect to the given page, |
416 | * or null if redirects are not supported by this content model. |
417 | * |
418 | * This default implementation always returns null. Subclasses supporting redirects |
419 | * must override this method. |
420 | * |
421 | * Note that subclasses that override this method to return a Content object |
422 | * should also override supportsRedirects() to return true. |
423 | * |
424 | * @stable to override |
425 | * @since 1.21 |
426 | * |
427 | * @param Title $destination The page to redirect to. |
428 | * @param string $text Text to include in the redirect, if possible. |
429 | * |
430 | * @return Content|null Always null. |
431 | */ |
432 | public function makeRedirectContent( Title $destination, $text = '' ) { |
433 | return null; |
434 | } |
435 | |
436 | /** |
437 | * Returns the model id that identifies the content model this |
438 | * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants. |
439 | * |
440 | * @since 1.21 |
441 | * |
442 | * @return string The model ID |
443 | */ |
444 | public function getModelID() { |
445 | return $this->mModelID; |
446 | } |
447 | |
448 | /** |
449 | * @since 1.21 |
450 | * |
451 | * @param string $model_id The model to check |
452 | * |
453 | * @throws MWException If the model ID is not the ID of the content model supported by this |
454 | * ContentHandler. |
455 | */ |
456 | protected function checkModelID( $model_id ) { |
457 | if ( $model_id !== $this->mModelID ) { |
458 | throw new MWException( "Bad content model: " . |
459 | "expected {$this->mModelID} " . |
460 | "but got $model_id." ); |
461 | } |
462 | } |
463 | |
464 | /** |
465 | * Returns a list of serialization formats supported by the |
466 | * serializeContent() and unserializeContent() methods of this |
467 | * ContentHandler. |
468 | * |
469 | * @stable to override |
470 | * @since 1.21 |
471 | * |
472 | * @return string[] List of serialization formats as MIME type like strings |
473 | */ |
474 | public function getSupportedFormats() { |
475 | return $this->mSupportedFormats; |
476 | } |
477 | |
478 | /** |
479 | * The format used for serialization/deserialization by default by this |
480 | * ContentHandler. |
481 | * |
482 | * This default implementation will return the first element of the array |
483 | * of formats that was passed to the constructor. |
484 | * |
485 | * @stable to override |
486 | * @since 1.21 |
487 | * |
488 | * @return string The name of the default serialization format as a MIME type |
489 | */ |
490 | public function getDefaultFormat() { |
491 | return $this->mSupportedFormats[0]; |
492 | } |
493 | |
494 | /** |
495 | * Returns true if $format is a serialization format supported by this |
496 | * ContentHandler, and false otherwise. |
497 | * |
498 | * Note that if $format is null, this method always returns true, because |
499 | * null means "use the default format". |
500 | * |
501 | * @stable to override |
502 | * @since 1.21 |
503 | * |
504 | * @param string $format The serialization format to check |
505 | * |
506 | * @return bool |
507 | */ |
508 | public function isSupportedFormat( $format ) { |
509 | if ( !$format ) { |
510 | return true; // this means "use the default" |
511 | } |
512 | |
513 | return in_array( $format, $this->mSupportedFormats ); |
514 | } |
515 | |
516 | /** |
517 | * Convenient for checking whether a format provided as a parameter is actually supported. |
518 | * |
519 | * @param string $format The serialization format to check |
520 | * |
521 | * @throws MWException If the format is not supported by this content handler. |
522 | */ |
523 | protected function checkFormat( $format ) { |
524 | if ( !$this->isSupportedFormat( $format ) ) { |
525 | throw new MWException( |
526 | "Format $format is not supported for content model " |
527 | . $this->getModelID() |
528 | ); |
529 | } |
530 | } |
531 | |
532 | /** |
533 | * Returns overrides for action handlers. |
534 | * Classes listed here will be used instead of the default one when |
535 | * (and only when) $wgActions[$action] === true. This allows subclasses |
536 | * to override the default action handlers. |
537 | * |
538 | * @stable to override |
539 | * @since 1.21 |
540 | * |
541 | * @return array<string,class-string|callable|false|Action|array> An array mapping action names |
542 | * (typically "view", "edit", "history" etc.) to a specification according to |
543 | * {@see ActionFactory::getActionSpec}. Can be the full qualified class name of an Action |
544 | * class, a callable taking ( Article $article, IContextSource $context ) as parameters and |
545 | * returning an Action object, false to disable an action, an actual Action object, |
546 | * or an ObjectFactory specification array (can have 'class', 'services', etc.). |
547 | * An empty array in this default implementation. |
548 | * |
549 | * @see Action::factory |
550 | */ |
551 | public function getActionOverrides() { |
552 | return []; |
553 | } |
554 | |
555 | /** |
556 | * Factory for creating an appropriate DifferenceEngine for this content model. |
557 | * Since 1.32, this is only used for page-level diffs; to diff two content objects, |
558 | * use getSlotDiffRenderer. |
559 | * |
560 | * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The |
561 | * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or |
562 | * wrap it. |
563 | * (Note that in older versions of MediaWiki the hook documentation instructed extensions |
564 | * to return false from the hook; you should not rely on always being able to decorate |
565 | * the DifferenceEngine instance from the hook. If the owner of the content type wants to |
566 | * decorate the instance, overriding this method is a safer approach.) |
567 | * |
568 | * @todo This is page-level functionality so it should not belong to ContentHandler. |
569 | * Move it to a better place once one exists (e.g. PageTypeHandler). |
570 | * |
571 | * @since 1.21 |
572 | * |
573 | * @param IContextSource $context Context to use, anything else will be ignored. |
574 | * @param int $old Revision ID we want to show and diff with. |
575 | * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'. |
576 | * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0. |
577 | * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false. |
578 | * @param bool $unhide If set, allow viewing deleted revs. Defaults to false. |
579 | * |
580 | * @return DifferenceEngine |
581 | */ |
582 | public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, |
583 | $rcid = 0, // FIXME: Deprecated, no longer used |
584 | $refreshCache = false, $unhide = false |
585 | ) { |
586 | $diffEngineClass = $this->getDiffEngineClass(); |
587 | $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide ); |
588 | $this->getHookRunner()->onGetDifferenceEngine( |
589 | $context, $old, $new, $refreshCache, $unhide, $differenceEngine ); |
590 | return $differenceEngine; |
591 | } |
592 | |
593 | /** |
594 | * Get an appropriate SlotDiffRenderer for this content model. |
595 | * |
596 | * @stable to override |
597 | * @since 1.32 |
598 | * |
599 | * @param IContextSource $context |
600 | * @param array $options An associative array of options passed to the SlotDiffRenderer: |
601 | * - diff-type: (string) The text diff format |
602 | * - contentLanguage: (string) The language code of the content language, |
603 | * to be passed to the TextDiffer constructor. This is ignored if a |
604 | * TextDiffer object is provided. |
605 | * - textDiffer: (TextDiffer) A TextDiffer object to use for text |
606 | * comparison. |
607 | * @return SlotDiffRenderer |
608 | */ |
609 | final public function getSlotDiffRenderer( IContextSource $context, array $options = [] ) { |
610 | $slotDiffRenderer = $this->getSlotDiffRendererWithOptions( $context, $options ); |
611 | if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer::class ) { |
612 | // To keep B/C, when SlotDiffRenderer is not overridden for a given content type |
613 | // but DifferenceEngine is, use that instead. |
614 | $differenceEngine = $this->createDifferenceEngine( $context ); |
615 | if ( get_class( $differenceEngine ) !== DifferenceEngine::class ) { |
616 | // TODO turn this into a deprecation warning in a later release |
617 | LoggerFactory::getInstance( 'diff' )->info( |
618 | 'Falling back to DifferenceEngineSlotDiffRenderer', [ |
619 | 'modelID' => $this->getModelID(), |
620 | 'DifferenceEngine' => get_class( $differenceEngine ), |
621 | ] ); |
622 | $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine ); |
623 | } |
624 | } |
625 | $this->getHookRunner()->onGetSlotDiffRenderer( $this, $slotDiffRenderer, $context ); |
626 | return $slotDiffRenderer; |
627 | } |
628 | |
629 | /** |
630 | * Return the SlotDiffRenderer appropriate for this content handler. |
631 | * @deprecated since 1.35; use getSlotDiffRendererWithOptions instead |
632 | * Emitting deprecation warnings since 1.41. |
633 | * @param IContextSource $context |
634 | * @return SlotDiffRenderer|null |
635 | */ |
636 | protected function getSlotDiffRendererInternal( IContextSource $context ) { |
637 | return null; |
638 | } |
639 | |
640 | /** |
641 | * Return the SlotDiffRenderer appropriate for this content handler. |
642 | * @stable to override |
643 | * |
644 | * @param IContextSource $context |
645 | * @param array $options See getSlotDiffRenderer() |
646 | * |
647 | * @return SlotDiffRenderer |
648 | */ |
649 | protected function getSlotDiffRendererWithOptions( IContextSource $context, $options = [] ) { |
650 | $internalRenderer = $this->getSlotDiffRendererInternal( $context ); |
651 | // `getSlotDiffRendererInternal` has been overridden by a class using the deprecated method. |
652 | // Options will not work so exit early! |
653 | if ( $internalRenderer !== null ) { |
654 | wfDeprecated( 'ContentHandler::getSlotDiffRendererInternal', '1.35' ); |
655 | return $internalRenderer; |
656 | } |
657 | return $this->createTextSlotDiffRenderer( $options ); |
658 | } |
659 | |
660 | /** |
661 | * Create a TextSlotDiffRenderer and inject dependencies |
662 | * |
663 | * @since 1.41 |
664 | * @param array $options See getSlotDiffRenderer() |
665 | * @return TextSlotDiffRenderer |
666 | */ |
667 | final protected function createTextSlotDiffRenderer( array $options = [] ): TextSlotDiffRenderer { |
668 | $slotDiffRenderer = new TextSlotDiffRenderer(); |
669 | |
670 | $services = MediaWikiServices::getInstance(); |
671 | $slotDiffRenderer->setStatsFactory( $services->getStatsFactory() ); |
672 | $slotDiffRenderer->setHookContainer( $services->getHookContainer() ); |
673 | $slotDiffRenderer->setContentModel( $this->getModelID() ); |
674 | |
675 | if ( isset( $options['textDiffer'] ) ) { |
676 | $textDiffer = $options['textDiffer']; |
677 | } else { |
678 | if ( isset( $options['contentLanguage'] ) ) { |
679 | $language = $services->getLanguageFactory()->getLanguage( $options['contentLanguage'] ); |
680 | } else { |
681 | $language = $services->getContentLanguage(); |
682 | } |
683 | $config = $services->getMainConfig(); |
684 | $textDiffer = new ManifoldTextDiffer( |
685 | RequestContext::getMain(), |
686 | $language, |
687 | $config->get( MainConfigNames::DiffEngine ), |
688 | $config->get( MainConfigNames::ExternalDiffEngine ), |
689 | $config->get( MainConfigNames::Wikidiff2Options ) |
690 | ); |
691 | } |
692 | $format = $options['diff-type'] ?? 'table'; |
693 | if ( !$textDiffer->hasFormat( $format ) ) { |
694 | // Maybe it would be better to throw an exception here, but at |
695 | // present, the value comes straight from user input without |
696 | // validation, so we have to fall back. |
697 | $format = 'table'; |
698 | } |
699 | $slotDiffRenderer->setFormat( $format ); |
700 | $slotDiffRenderer->setTextDiffer( $textDiffer ); |
701 | if ( $options['inline-toggle'] ?? false ) { |
702 | $slotDiffRenderer->setInlineToggleEnabled(); |
703 | } |
704 | |
705 | return $slotDiffRenderer; |
706 | } |
707 | |
708 | /** |
709 | * Get the language in which the content of the given page is written. |
710 | * |
711 | * This default implementation just returns the content language (except for pages |
712 | * in the MediaWiki namespace) |
713 | * |
714 | * Note that the page's language is not cacheable, since it may in some |
715 | * cases depend on user settings. |
716 | * |
717 | * Also note that the page language may or may not depend on the actual content of the page, |
718 | * that is, this method may load the content in order to determine the language. |
719 | * |
720 | * @stable to override |
721 | * @since 1.21 |
722 | * |
723 | * @param Title $title The page to determine the language for. |
724 | * @param Content|null $content The page's content, if you have it handy, to avoid reloading it. |
725 | * |
726 | * @return Language |
727 | */ |
728 | public function getPageLanguage( Title $title, ?Content $content = null ) { |
729 | $services = MediaWikiServices::getInstance(); |
730 | $pageLang = $services->getContentLanguage(); |
731 | |
732 | if ( $title->inNamespace( NS_MEDIAWIKI ) ) { |
733 | // Parse mediawiki messages with correct target language |
734 | [ /* $unused */, $lang ] = $services->getMessageCache()->figureMessage( $title->getText() ); |
735 | $pageLang = $services->getLanguageFactory()->getLanguage( $lang ); |
736 | } |
737 | |
738 | // Unused, T299369 |
739 | $userLang = null; |
740 | $this->getHookRunner()->onPageContentLanguage( $title, $pageLang, $userLang ); |
741 | |
742 | if ( !$pageLang instanceof Language ) { |
743 | throw new UnexpectedValueException( 'onPageContentLanguage() hook provided an invalid $pageLang object.' ); |
744 | } |
745 | |
746 | return $pageLang; |
747 | } |
748 | |
749 | /** |
750 | * Get the language in which the content of this page is written when |
751 | * viewed by user. Defaults to $this->getPageLanguage(), but if the user |
752 | * specified a preferred variant, the variant will be used. |
753 | * |
754 | * This default implementation just returns $this->getPageLanguage( $title, $content ) unless |
755 | * the user specified a preferred variant. |
756 | * |
757 | * Note that the pages view language is not cacheable, since it depends on user settings. |
758 | * |
759 | * Also note that the page language may or may not depend on the actual content of the page, |
760 | * that is, this method may load the content in order to determine the language. |
761 | * |
762 | * @stable to override |
763 | * @deprecated since 1.42 Use ParserOutput::getLanguage instead. See also OutputPage::getContLangForJS. |
764 | * @since 1.21 |
765 | * @param Title $title The page to determine the language for. |
766 | * @param Content|null $content The page's content, if you have it handy, to avoid reloading it. |
767 | * @return Language The page's language for viewing |
768 | */ |
769 | public function getPageViewLanguage( Title $title, ?Content $content = null ) { |
770 | $pageLang = $this->getPageLanguage( $title, $content ); |
771 | |
772 | if ( $title->getNamespace() !== NS_MEDIAWIKI ) { |
773 | // If the user chooses a variant, the content is actually |
774 | // in a language whose code is the variant code. |
775 | $variant = $this->getLanguageConverter( $pageLang )->getPreferredVariant(); |
776 | if ( $pageLang->getCode() !== $variant ) { |
777 | $pageLang = MediaWikiServices::getInstance()->getLanguageFactory() |
778 | ->getLanguage( $variant ); |
779 | } |
780 | } |
781 | |
782 | return $pageLang; |
783 | } |
784 | |
785 | /** |
786 | * Determines whether the content type handled by this ContentHandler |
787 | * can be used for the main slot of the given page. |
788 | * |
789 | * This default implementation always returns true. |
790 | * Subclasses may override this to restrict the use of this content model to specific locations, |
791 | * typically based on the namespace or some other aspect of the title, such as a special suffix |
792 | * (e.g. ".svg" for SVG content). |
793 | * |
794 | * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which |
795 | * content model can be used where. |
796 | * |
797 | * @stable to override |
798 | * |
799 | * @see SlotRoleHandler::isAllowedModel |
800 | * |
801 | * @param Title $title The page's title. |
802 | * |
803 | * @return bool True if content of this kind can be used on the given page, false otherwise. |
804 | */ |
805 | public function canBeUsedOn( Title $title ) { |
806 | $ok = true; |
807 | |
808 | $this->getHookRunner()->onContentModelCanBeUsedOn( $this->getModelID(), $title, $ok ); |
809 | |
810 | return $ok; |
811 | } |
812 | |
813 | /** |
814 | * Returns the name of the diff engine to use. |
815 | * |
816 | * @stable to override |
817 | * @since 1.21 |
818 | * |
819 | * @return class-string<DifferenceEngine> |
820 | */ |
821 | protected function getDiffEngineClass() { |
822 | return DifferenceEngine::class; |
823 | } |
824 | |
825 | /** |
826 | * Attempts to merge differences between three versions. Returns a new |
827 | * Content object for a clean merge and false for failure or a conflict. |
828 | * |
829 | * This default implementation always returns false. |
830 | * |
831 | * @stable to override |
832 | * @since 1.21 |
833 | * |
834 | * @param Content $oldContent The page's previous content. |
835 | * @param Content $myContent One of the page's conflicting contents. |
836 | * @param Content $yourContent One of the page's conflicting contents. |
837 | * |
838 | * @return Content|false Always false. |
839 | */ |
840 | public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { |
841 | return false; |
842 | } |
843 | |
844 | /** |
845 | * Shorthand for getting a Language Converter for specific language |
846 | * @param Language $language Language of converter |
847 | * @return ILanguageConverter |
848 | */ |
849 | private function getLanguageConverter( $language ): ILanguageConverter { |
850 | return MediaWikiServices::getInstance()->getLanguageConverterFactory() |
851 | ->getLanguageConverter( $language ); |
852 | } |
853 | |
854 | /** |
855 | * Return type of change if one exists for the given edit. |
856 | * |
857 | * @stable to override |
858 | * @since 1.31 |
859 | * |
860 | * @param Content|null $oldContent The previous text of the page. |
861 | * @param Content|null $newContent The submitted text of the page. |
862 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
863 | * |
864 | * @return string|null String key representing type of change, or null. |
865 | */ |
866 | private function getChangeType( |
867 | ?Content $oldContent = null, |
868 | ?Content $newContent = null, |
869 | $flags = 0 |
870 | ) { |
871 | $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null; |
872 | $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null; |
873 | |
874 | // We check for the type of change in the given edit, and return string key accordingly |
875 | |
876 | // Blanking of a page |
877 | if ( $oldContent && $oldContent->getSize() > 0 && |
878 | $newContent && $newContent->getSize() === 0 |
879 | ) { |
880 | return 'blank'; |
881 | } |
882 | |
883 | // Redirects |
884 | if ( $newTarget ) { |
885 | if ( !$oldTarget ) { |
886 | // New redirect page (by creating new page or by changing content page) |
887 | return 'new-redirect'; |
888 | } elseif ( !$newTarget->equals( $oldTarget ) || |
889 | $oldTarget->getFragment() !== $newTarget->getFragment() |
890 | ) { |
891 | // Redirect target changed |
892 | return 'changed-redirect-target'; |
893 | } |
894 | } elseif ( $oldTarget ) { |
895 | // Changing an existing redirect into a non-redirect |
896 | return 'removed-redirect'; |
897 | } |
898 | |
899 | // New page created |
900 | if ( $flags & EDIT_NEW && $newContent ) { |
901 | if ( $newContent->getSize() === 0 ) { |
902 | // New blank page |
903 | return 'newblank'; |
904 | } else { |
905 | return 'newpage'; |
906 | } |
907 | } |
908 | |
909 | // Removing more than 90% of the page |
910 | if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) { |
911 | return 'replace'; |
912 | } |
913 | |
914 | // Content model changed |
915 | if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) { |
916 | return 'contentmodelchange'; |
917 | } |
918 | |
919 | return null; |
920 | } |
921 | |
922 | /** |
923 | * Return an applicable auto-summary if one exists for the given edit. |
924 | * |
925 | * @stable to override |
926 | * @since 1.21 |
927 | * |
928 | * @param Content|null $oldContent The previous text of the page. |
929 | * @param Content|null $newContent The submitted text of the page. |
930 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
931 | * |
932 | * @return string An appropriate auto-summary, or an empty string. |
933 | */ |
934 | public function getAutosummary( |
935 | ?Content $oldContent = null, |
936 | ?Content $newContent = null, |
937 | $flags = 0 |
938 | ) { |
939 | $changeType = $this->getChangeType( $oldContent, $newContent, $flags ); |
940 | |
941 | // There's no applicable auto-summary for our case, so our auto-summary is empty. |
942 | if ( !$changeType ) { |
943 | return ''; |
944 | } |
945 | |
946 | // Set the maximum auto-summary length to the general maximum summary length |
947 | // T221617 |
948 | $summaryLimit = CommentStore::COMMENT_CHARACTER_LIMIT; |
949 | |
950 | // Decide what kind of auto-summary is needed. |
951 | switch ( $changeType ) { |
952 | case 'new-redirect': |
953 | $newTarget = $newContent->getRedirectTarget(); |
954 | $truncatedtext = $newContent->getTextForSummary( |
955 | $summaryLimit |
956 | - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() ) |
957 | - strlen( $newTarget->getFullText() ) |
958 | ); |
959 | |
960 | return wfMessage( 'autoredircomment', $newTarget->getFullText() ) |
961 | ->plaintextParams( $truncatedtext )->inContentLanguage()->text(); |
962 | case 'changed-redirect-target': |
963 | $oldTarget = $oldContent->getRedirectTarget(); |
964 | $newTarget = $newContent->getRedirectTarget(); |
965 | |
966 | $truncatedtext = $newContent->getTextForSummary( |
967 | $summaryLimit |
968 | - strlen( wfMessage( 'autosumm-changed-redirect-target' ) |
969 | ->inContentLanguage()->text() ) |
970 | - strlen( $oldTarget->getFullText() ) |
971 | - strlen( $newTarget->getFullText() ) |
972 | ); |
973 | |
974 | return wfMessage( 'autosumm-changed-redirect-target', |
975 | $oldTarget->getFullText(), |
976 | $newTarget->getFullText() ) |
977 | ->rawParams( $truncatedtext )->inContentLanguage()->text(); |
978 | case 'removed-redirect': |
979 | $oldTarget = $oldContent->getRedirectTarget(); |
980 | $truncatedtext = $newContent->getTextForSummary( |
981 | $summaryLimit |
982 | - strlen( wfMessage( 'autosumm-removed-redirect' ) |
983 | ->inContentLanguage()->text() ) |
984 | - strlen( $oldTarget->getFullText() ) ); |
985 | |
986 | return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() ) |
987 | ->rawParams( $truncatedtext )->inContentLanguage()->text(); |
988 | case 'newpage': |
989 | // If they're making a new article, give its text, truncated, in the summary. |
990 | $truncatedtext = $newContent->getTextForSummary( |
991 | $summaryLimit - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) ); |
992 | |
993 | return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext ) |
994 | ->inContentLanguage()->text(); |
995 | case 'blank': |
996 | return wfMessage( 'autosumm-blank' )->inContentLanguage()->text(); |
997 | case 'replace': |
998 | $truncatedtext = $newContent->getTextForSummary( |
999 | $summaryLimit - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) ); |
1000 | |
1001 | return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext ) |
1002 | ->inContentLanguage()->text(); |
1003 | case 'newblank': |
1004 | return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text(); |
1005 | default: |
1006 | return ''; |
1007 | } |
1008 | } |
1009 | |
1010 | /** |
1011 | * Return an applicable tag if one exists for the given edit or return null. |
1012 | * |
1013 | * @stable to override |
1014 | * @since 1.31 |
1015 | * |
1016 | * @param Content|null $oldContent The previous text of the page. |
1017 | * @param Content|null $newContent The submitted text of the page. |
1018 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
1019 | * |
1020 | * @return string|null An appropriate tag, or null. |
1021 | */ |
1022 | public function getChangeTag( |
1023 | ?Content $oldContent = null, |
1024 | ?Content $newContent = null, |
1025 | $flags = 0 |
1026 | ) { |
1027 | $changeType = $this->getChangeType( $oldContent, $newContent, $flags ); |
1028 | |
1029 | // There's no applicable tag for this change. |
1030 | if ( !$changeType ) { |
1031 | return null; |
1032 | } |
1033 | |
1034 | // Core tags use the same keys as ones returned from $this->getChangeType() |
1035 | // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking |
1036 | // if this type of change should be tagged |
1037 | $tag = 'mw-' . $changeType; |
1038 | |
1039 | // Not all change types are tagged, so we check against the list of defined tags. |
1040 | if ( in_array( $tag, MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags() ) ) { |
1041 | return $tag; |
1042 | } |
1043 | |
1044 | return null; |
1045 | } |
1046 | |
1047 | /** |
1048 | * Auto-generates a deletion reason |
1049 | * |
1050 | * @stable to override |
1051 | * @since 1.21 |
1052 | * |
1053 | * @param Title $title The page's title |
1054 | * @param bool &$hasHistory Whether the page has a history |
1055 | * |
1056 | * @return string|false String containing deletion reason or empty string, or |
1057 | * boolean false if no revision occurred |
1058 | */ |
1059 | public function getAutoDeleteReason( Title $title, &$hasHistory = false ) { |
1060 | if ( func_num_args() === 2 ) { |
1061 | wfDeprecated( __METHOD__ . ': $hasHistory parameter', '1.38' ); |
1062 | } |
1063 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
1064 | $revStore = MediaWikiServices::getInstance()->getRevisionStore(); |
1065 | |
1066 | // Get the last revision |
1067 | $revRecord = $revStore->getRevisionByTitle( $title ); |
1068 | |
1069 | if ( $revRecord === null ) { |
1070 | return false; |
1071 | } |
1072 | |
1073 | // Get the article's contents |
1074 | $content = $revRecord->getContent( SlotRecord::MAIN ); |
1075 | $blank = false; |
1076 | |
1077 | // If the page is blank, use the text from the previous revision, |
1078 | // which can only be blank if there's a move/import/protect dummy |
1079 | // revision involved |
1080 | if ( !$content || $content->isEmpty() ) { |
1081 | $prev = $revStore->getPreviousRevision( $revRecord ); |
1082 | |
1083 | if ( $prev ) { |
1084 | $revRecord = $prev; |
1085 | $content = $prev->getContent( SlotRecord::MAIN ); |
1086 | $blank = true; |
1087 | } |
1088 | } |
1089 | |
1090 | $this->checkModelID( $revRecord->getSlot( SlotRecord::MAIN )->getModel() ); |
1091 | |
1092 | // Find out if there was only one contributor |
1093 | // Only scan the last 20 revisions |
1094 | $queryBuilder = $revStore->newSelectQueryBuilder( $dbr ) |
1095 | ->where( [ |
1096 | 'rev_page' => $title->getArticleID(), |
1097 | $dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' |
1098 | ] ) |
1099 | ->limit( 20 ); |
1100 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
1101 | |
1102 | if ( !$res->numRows() ) { |
1103 | // This page has no revisions, which is very weird |
1104 | return false; |
1105 | } |
1106 | |
1107 | $hasHistory = ( $res->numRows() > 1 ); |
1108 | $row = $res->fetchObject(); |
1109 | |
1110 | if ( $row ) { // $row is false if the only contributor is hidden |
1111 | $onlyAuthor = $row->rev_user_text; |
1112 | // Try to find a second contributor |
1113 | foreach ( $res as $row ) { |
1114 | if ( $row->rev_user_text != $onlyAuthor ) { // T24999 |
1115 | $onlyAuthor = false; |
1116 | break; |
1117 | } |
1118 | } |
1119 | } else { |
1120 | $onlyAuthor = false; |
1121 | } |
1122 | |
1123 | // Generate the summary with a '$1' placeholder |
1124 | if ( $blank ) { |
1125 | // The current revision is blank and the one before is also |
1126 | // blank. It's just not our lucky day |
1127 | $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text(); |
1128 | } else { |
1129 | if ( $onlyAuthor ) { |
1130 | $reason = wfMessage( |
1131 | 'excontentauthor', |
1132 | '$1', |
1133 | $onlyAuthor |
1134 | )->inContentLanguage()->text(); |
1135 | } else { |
1136 | $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text(); |
1137 | } |
1138 | } |
1139 | |
1140 | if ( $reason == '-' ) { |
1141 | // Allow these UI messages to be blanked out cleanly |
1142 | return ''; |
1143 | } |
1144 | |
1145 | // Max content length = max comment length - length of the comment (excl. $1) |
1146 | $maxLength = CommentStore::COMMENT_CHARACTER_LIMIT - ( strlen( $reason ) - 2 ); |
1147 | $text = $content ? $content->getTextForSummary( $maxLength ) : ''; |
1148 | |
1149 | // Now replace the '$1' placeholder |
1150 | $reason = str_replace( '$1', $text, $reason ); |
1151 | |
1152 | return $reason; |
1153 | } |
1154 | |
1155 | /** |
1156 | * Get the Content object that needs to be saved in order to undo all changes |
1157 | * between $undo and $undoafter. |
1158 | * |
1159 | * @stable to override |
1160 | * @since 1.21 |
1161 | * @since 1.32 accepts Content objects for all parameters instead of Revision objects. |
1162 | * Passing Revision objects is deprecated. |
1163 | * @since 1.37 only accepts Content objects |
1164 | * |
1165 | * @param Content $currentContent The current text |
1166 | * @param Content $undoContent The content of the revision to undo |
1167 | * @param Content $undoAfterContent Must be from an earlier revision than $undo |
1168 | * @param bool $undoIsLatest Set true if $undo is from the current revision (since 1.32) |
1169 | * |
1170 | * @return Content|false Content on success, false on failure |
1171 | */ |
1172 | public function getUndoContent( |
1173 | Content $currentContent, |
1174 | Content $undoContent, |
1175 | Content $undoAfterContent, |
1176 | $undoIsLatest = false |
1177 | ) { |
1178 | try { |
1179 | $this->checkModelID( $currentContent->getModel() ); |
1180 | $this->checkModelID( $undoContent->getModel() ); |
1181 | if ( !$undoIsLatest ) { |
1182 | // If we are undoing the most recent revision, |
1183 | // its ok to revert content model changes. However |
1184 | // if we are undoing a revision in the middle, then |
1185 | // doing that will be confusing. |
1186 | $this->checkModelID( $undoAfterContent->getModel() ); |
1187 | } |
1188 | } catch ( MWException $e ) { |
1189 | // If the revisions have different content models |
1190 | // just return false |
1191 | return false; |
1192 | } |
1193 | |
1194 | if ( $currentContent->equals( $undoContent ) ) { |
1195 | // No use doing a merge if it's just a straight revert. |
1196 | return $undoAfterContent; |
1197 | } |
1198 | |
1199 | $undone_content = $this->merge3( $undoContent, $undoAfterContent, $currentContent ); |
1200 | |
1201 | return $undone_content; |
1202 | } |
1203 | |
1204 | /** |
1205 | * Returns true for content models that support caching using the |
1206 | * ParserCache mechanism. See WikiPage::shouldCheckParserCache(). |
1207 | * |
1208 | * @stable to override |
1209 | * @since 1.21 |
1210 | * |
1211 | * @return bool Always false. |
1212 | */ |
1213 | public function isParserCacheSupported() { |
1214 | return false; |
1215 | } |
1216 | |
1217 | /** |
1218 | * Returns true if this content model supports sections. |
1219 | * This default implementation returns false. |
1220 | * |
1221 | * Content models that return true here should also implement |
1222 | * Content::getSection, Content::replaceSection, etc. to handle sections. |
1223 | * |
1224 | * @stable to override |
1225 | * |
1226 | * @return bool Always false. |
1227 | */ |
1228 | public function supportsSections() { |
1229 | return false; |
1230 | } |
1231 | |
1232 | /** |
1233 | * Returns true if this content model supports categories. |
1234 | * The default implementation returns true. |
1235 | * |
1236 | * @stable to override |
1237 | * |
1238 | * @return bool Always true. |
1239 | */ |
1240 | public function supportsCategories() { |
1241 | return true; |
1242 | } |
1243 | |
1244 | /** |
1245 | * Returns true if this content model supports redirects. |
1246 | * This default implementation returns false. |
1247 | * |
1248 | * Content models that return true here should also implement |
1249 | * ContentHandler::makeRedirectContent to return a Content object. |
1250 | * |
1251 | * @stable to override |
1252 | * |
1253 | * @return bool Always false. |
1254 | */ |
1255 | public function supportsRedirects() { |
1256 | return false; |
1257 | } |
1258 | |
1259 | /** |
1260 | * Return true if this content model supports direct editing, such as via EditPage. |
1261 | * This should return true for TextContent and its derivatives, and return false |
1262 | * for structured data content. |
1263 | * |
1264 | * @stable to override |
1265 | * |
1266 | * @return bool Default is false. |
1267 | */ |
1268 | public function supportsDirectEditing() { |
1269 | return false; |
1270 | } |
1271 | |
1272 | /** |
1273 | * If a non-existing page can be created with the contents from another (arbitrary) page being |
1274 | * preloaded in the editor, see {@see EditPage::getContentObject}. Only makes sense together |
1275 | * with {@see supportsDirectEditing}. |
1276 | * |
1277 | * @stable to override |
1278 | * @since 1.39 |
1279 | * |
1280 | * @return bool |
1281 | */ |
1282 | public function supportsPreloadContent(): bool { |
1283 | return false; |
1284 | } |
1285 | |
1286 | /** |
1287 | * Whether an edit on the content should trigger an HTML render and ParserCache entry. |
1288 | * |
1289 | * @stable to override |
1290 | * @since 1.37 |
1291 | * |
1292 | * @return bool true if edit should trigger an HTML render false otherwise |
1293 | */ |
1294 | public function generateHTMLOnEdit(): bool { |
1295 | return true; |
1296 | } |
1297 | |
1298 | /** |
1299 | * Whether or not this content model supports direct editing via ApiEditPage |
1300 | * |
1301 | * @stable to override |
1302 | * |
1303 | * @return bool Default is false, and true for TextContent and derivatives. |
1304 | */ |
1305 | public function supportsDirectApiEditing() { |
1306 | return $this->supportsDirectEditing(); |
1307 | } |
1308 | |
1309 | /** |
1310 | * Get fields definition for search index |
1311 | * |
1312 | * @todo Expose title, redirect, namespace, text, source_text, text_bytes |
1313 | * field mappings here. (see T142670 and T143409) |
1314 | * |
1315 | * @stable to override |
1316 | * |
1317 | * @param SearchEngine $engine |
1318 | * @return SearchIndexField[] List of fields this content handler can provide. |
1319 | * @since 1.28 |
1320 | */ |
1321 | public function getFieldsForSearchIndex( SearchEngine $engine ) { |
1322 | $fields = []; |
1323 | $fields['category'] = $engine->makeSearchFieldMapping( |
1324 | 'category', |
1325 | SearchIndexField::INDEX_TYPE_TEXT |
1326 | ); |
1327 | $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD ); |
1328 | |
1329 | $fields['external_link'] = $engine->makeSearchFieldMapping( |
1330 | 'external_link', |
1331 | SearchIndexField::INDEX_TYPE_KEYWORD |
1332 | ); |
1333 | |
1334 | $fields['outgoing_link'] = $engine->makeSearchFieldMapping( |
1335 | 'outgoing_link', |
1336 | SearchIndexField::INDEX_TYPE_KEYWORD |
1337 | ); |
1338 | |
1339 | $fields['template'] = $engine->makeSearchFieldMapping( |
1340 | 'template', |
1341 | SearchIndexField::INDEX_TYPE_KEYWORD |
1342 | ); |
1343 | $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD ); |
1344 | |
1345 | $fields['content_model'] = $engine->makeSearchFieldMapping( |
1346 | 'content_model', |
1347 | SearchIndexField::INDEX_TYPE_KEYWORD |
1348 | ); |
1349 | |
1350 | return $fields; |
1351 | } |
1352 | |
1353 | /** |
1354 | * Add new field definition to array. |
1355 | * @param SearchIndexField[] &$fields |
1356 | * @param SearchEngine $engine |
1357 | * @param string $name |
1358 | * @param string $type |
1359 | * @return SearchIndexField[] new field defs |
1360 | * @since 1.28 |
1361 | */ |
1362 | protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) { |
1363 | $fields[$name] = $engine->makeSearchFieldMapping( $name, $type ); |
1364 | return $fields; |
1365 | } |
1366 | |
1367 | /** |
1368 | * Return fields to be indexed by search engine |
1369 | * as representation of this document. |
1370 | * Overriding class should call parent function or take care of calling |
1371 | * the SearchDataForIndex hook. |
1372 | * |
1373 | * The $output must be the result of a call to {@link getParserOutputForIndexing()} |
1374 | * on the same content handler. That method may return ParserOutput |
1375 | * {@link ParserOutput::hasText() without HTML}; this base implementation |
1376 | * does not rely on the HTML being present, so it is safe to call |
1377 | * even by subclasses that override {@link getParserOutputForIndexing()} |
1378 | * to skip HTML generation. On the other hand, |
1379 | * since the default implementation of {@link getParserOutputForIndexing()} |
1380 | * does generate HTML, subclasses are free to rely on the HTML here |
1381 | * if they do not override {@link getParserOutputForIndexing()}. |
1382 | * |
1383 | * @stable to override |
1384 | * @param WikiPage $page Page to index |
1385 | * @param ParserOutput $output |
1386 | * @param SearchEngine $engine Search engine for which we are indexing |
1387 | * @param RevisionRecord|null $revision Revision content to fetch if provided or use the latest revision |
1388 | * from WikiPage::getRevisionRecord() if not |
1389 | * @return array Map of name=>value for fields, an empty array is returned if the latest |
1390 | * revision cannot be retrieved. |
1391 | * @since 1.28 |
1392 | */ |
1393 | public function getDataForSearchIndex( |
1394 | WikiPage $page, |
1395 | ParserOutput $output, |
1396 | SearchEngine $engine, |
1397 | ?RevisionRecord $revision = null |
1398 | ) { |
1399 | $revision ??= $page->getRevisionRecord(); |
1400 | if ( $revision === null ) { |
1401 | LoggerFactory::getInstance( 'search' )->warning( |
1402 | "Called getDataForSearchIndex on the page {page_id} for which the " . |
1403 | "latest revision cannot be loaded.", |
1404 | [ "page_id" => $page->getId() ] |
1405 | ); |
1406 | return []; |
1407 | } |
1408 | Assert::invariant( $revision->getPageId() === $page->getId(), |
1409 | '$revision and $page must target the same page_id' ); |
1410 | |
1411 | $fieldData = []; |
1412 | $content = $revision->getContent( SlotRecord::MAIN ); |
1413 | |
1414 | if ( $content ) { |
1415 | $searchDataExtractor = new ParserOutputSearchDataExtractor(); |
1416 | |
1417 | $fieldData['category'] = $searchDataExtractor->getCategories( $output ); |
1418 | $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output ); |
1419 | $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output ); |
1420 | $fieldData['template'] = $searchDataExtractor->getTemplates( $output ); |
1421 | |
1422 | $text = $content->getTextForSearchIndex(); |
1423 | |
1424 | $fieldData['text'] = $text; |
1425 | $fieldData['source_text'] = $text; |
1426 | $fieldData['text_bytes'] = $content->getSize(); |
1427 | $fieldData['content_model'] = $content->getModel(); |
1428 | } |
1429 | |
1430 | $this->getHookRunner()->onSearchDataForIndex( $fieldData, $this, $page, $output, $engine ); |
1431 | $this->getHookRunner()->onSearchDataForIndex2( $fieldData, $this, $page, $output, $engine, $revision ); |
1432 | |
1433 | return $fieldData; |
1434 | } |
1435 | |
1436 | /** |
1437 | * Produce page output suitable for indexing. |
1438 | * Typically used with {@link getDataForSearchIndex()}. |
1439 | * |
1440 | * Specific content handlers may override it if they need different content handling. |
1441 | * |
1442 | * The default implementation returns output {@link ParserOutput::hasText() with HTML}, |
1443 | * but callers should not rely on this, and subclasses may override this method |
1444 | * and skip HTML generation if it is not needed for indexing. |
1445 | * (In that case, they should not attempt to store the output in the $cache.) |
1446 | * |
1447 | * @stable to override |
1448 | * |
1449 | * @param WikiPage $page |
1450 | * @param ParserCache|null $cache deprecated since 1.38 and won't have any effect |
1451 | * @param RevisionRecord|null $revision |
1452 | * @return ParserOutput|null null when the ParserOutput cannot be obtained |
1453 | * @see ParserOutputAccess::getParserOutput() for failure modes |
1454 | */ |
1455 | public function getParserOutputForIndexing( |
1456 | WikiPage $page, |
1457 | ?ParserCache $cache = null, |
1458 | ?RevisionRecord $revision = null |
1459 | ) { |
1460 | // TODO: MCR: ContentHandler should be called per slot, not for the whole page. |
1461 | // See T190066. |
1462 | $parserOptions = $page->makeParserOptions( 'canonical' ); |
1463 | $parserOptions->setRenderReason( 'ParserOutputForIndexing' ); |
1464 | $parserOutputAccess = MediaWikiServices::getInstance()->getParserOutputAccess(); |
1465 | return $parserOutputAccess->getParserOutput( |
1466 | $page, |
1467 | $parserOptions, |
1468 | $revision, |
1469 | ParserOutputAccess::OPT_NO_UPDATE_CACHE |
1470 | )->getValue(); |
1471 | } |
1472 | |
1473 | /** |
1474 | * Get the latest revision of the given $page, |
1475 | * fetching it from the primary if necessary. |
1476 | * |
1477 | * @param WikiPage $page |
1478 | * @return RevisionRecord |
1479 | * @since 1.36 (previously private) |
1480 | */ |
1481 | protected function latestRevision( WikiPage $page ): RevisionRecord { |
1482 | $revRecord = $page->getRevisionRecord(); |
1483 | if ( $revRecord == null ) { |
1484 | // If the content represents a brand new page it's possible |
1485 | // we need to fetch it from the primary. |
1486 | $page->loadPageData( IDBAccessObject::READ_LATEST ); |
1487 | $revRecord = $page->getRevisionRecord(); |
1488 | if ( $revRecord == null ) { |
1489 | $text = $page->getTitle()->getPrefixedText(); |
1490 | throw new MWException( |
1491 | "No revision could be loaded for page: $text" ); |
1492 | } |
1493 | } |
1494 | |
1495 | return $revRecord; |
1496 | } |
1497 | |
1498 | /** |
1499 | * Returns a list of DeferrableUpdate objects for recording information about the |
1500 | * given Content in some secondary data store. |
1501 | * |
1502 | * Application logic should not call this method directly. Instead, it should call |
1503 | * DerivedPageDataUpdater::getSecondaryDataUpdates(). |
1504 | * |
1505 | * @note Implementations must not return a LinksUpdate instance. Instead, a LinksUpdate |
1506 | * is created by the calling code in DerivedPageDataUpdater, on the combined ParserOutput |
1507 | * of all slots, not for each slot individually. This is in contrast to the old |
1508 | * getSecondaryDataUpdates method defined by AbstractContent, which returned a LinksUpdate. |
1509 | * |
1510 | * @note Implementations should not call $content->getParserOutput, they should call |
1511 | * $slotOutput->getSlotRendering( $role, false ) instead if they need to access a ParserOutput |
1512 | * of $content. This allows existing ParserOutput objects to be re-used, while avoiding |
1513 | * creating a ParserOutput when none is needed. |
1514 | * |
1515 | * @stable to override |
1516 | * |
1517 | * @param Title $title The title of the page to supply the updates for |
1518 | * @param Content $content The content to generate data updates for. |
1519 | * @param string $role The role (slot) in which the content is being used. Which updates |
1520 | * are performed should generally not depend on the role the content has, but the |
1521 | * DeferrableUpdates themselves may need to know the role, to track to which slot the |
1522 | * data refers, and to avoid overwriting data of the same kind from another slot. |
1523 | * @param SlotRenderingProvider $slotOutput A provider that can be used to gain access to |
1524 | * a ParserOutput of $content by calling $slotOutput->getSlotParserOutput( $role, false ). |
1525 | * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information |
1526 | * about this content object somewhere. The default implementation returns an empty |
1527 | * array. |
1528 | * @since 1.32 |
1529 | */ |
1530 | public function getSecondaryDataUpdates( |
1531 | Title $title, |
1532 | Content $content, |
1533 | $role, |
1534 | SlotRenderingProvider $slotOutput |
1535 | ) { |
1536 | return []; |
1537 | } |
1538 | |
1539 | /** |
1540 | * Returns a list of DeferrableUpdate objects for removing information about content |
1541 | * in some secondary data store. This is used when a page is deleted, and also when |
1542 | * a slot is removed from a page. |
1543 | * |
1544 | * Application logic should not call this method directly. Instead, it should call |
1545 | * WikiPage::getSecondaryDataUpdates(). |
1546 | * |
1547 | * @note Implementations must not return a LinksDeletionUpdate instance. Instead, a |
1548 | * LinksDeletionUpdate is created by the calling code in WikiPage. |
1549 | * This is in contrast to the old getDeletionUpdates method defined by AbstractContent, |
1550 | * which returned a LinksUpdate. |
1551 | * |
1552 | * @note Implementations should not rely on the page's current content, but rather the current |
1553 | * state of the secondary data store. |
1554 | * |
1555 | * @stable to override |
1556 | * |
1557 | * @param Title $title The title of the page to supply the updates for |
1558 | * @param string $role The role (slot) in which the content is being used. Which updates |
1559 | * are performed should generally not depend on the role the content has, but the |
1560 | * DeferrableUpdates themselves may need to know the role, to track to which slot the |
1561 | * data refers, and to avoid overwriting data of the same kind from another slot. |
1562 | * |
1563 | * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information |
1564 | * about this content object somewhere. The default implementation returns an empty |
1565 | * array. |
1566 | * |
1567 | * @since 1.32 |
1568 | */ |
1569 | public function getDeletionUpdates( Title $title, $role ) { |
1570 | return []; |
1571 | } |
1572 | |
1573 | /** |
1574 | * Returns a $content object with pre-save transformations applied (or the same |
1575 | * object if no transformations apply). |
1576 | * |
1577 | * @note Not stable to call other then from ContentHandler hierarchy. |
1578 | * Callers need to use ContentTransformer::preSaveTransform. |
1579 | * @stable to override |
1580 | * @since 1.37 |
1581 | * |
1582 | * @param Content $content |
1583 | * @param PreSaveTransformParams $pstParams |
1584 | * |
1585 | * @return Content |
1586 | */ |
1587 | public function preSaveTransform( |
1588 | Content $content, |
1589 | PreSaveTransformParams $pstParams |
1590 | ): Content { |
1591 | return $content; |
1592 | } |
1593 | |
1594 | /** |
1595 | * Returns a $content object with preload transformations applied (or the same |
1596 | * object if no transformations apply). |
1597 | * |
1598 | * @note Not stable to call other then from ContentHandler hierarchy. |
1599 | * Callers need to use ContentTransformer::preLoadTransform. |
1600 | * @stable to override |
1601 | * @since 1.37 |
1602 | * |
1603 | * @param Content $content |
1604 | * @param PreloadTransformParams $pltParams |
1605 | * |
1606 | * @return Content |
1607 | */ |
1608 | public function preloadTransform( |
1609 | Content $content, |
1610 | PreloadTransformParams $pltParams |
1611 | ): Content { |
1612 | return $content; |
1613 | } |
1614 | |
1615 | /** |
1616 | * Validate content for saving it. |
1617 | * |
1618 | * This may be used to check the content's consistency with global state. This function should |
1619 | * NOT write any information to the database. |
1620 | * |
1621 | * Note that this method will usually be called inside the same transaction |
1622 | * bracket that will be used to save the new revision, so the revision passed |
1623 | * in is probably unsaved (has no id) and might belong to unsaved page. |
1624 | * |
1625 | * @since 1.38 |
1626 | * @stable to override |
1627 | * |
1628 | * @param Content $content |
1629 | * @param ValidationParams $validationParams |
1630 | * |
1631 | * @return StatusValue A status object indicating if content can be saved in the given revision. |
1632 | */ |
1633 | public function validateSave( |
1634 | Content $content, |
1635 | ValidationParams $validationParams |
1636 | ) { |
1637 | if ( $content->isValid() ) { |
1638 | return StatusValue::newGood(); |
1639 | } else { |
1640 | return StatusValue::newFatal( "invalid-content-data" ); |
1641 | } |
1642 | } |
1643 | |
1644 | /** |
1645 | * Returns a ParserOutput object containing information derived from this content. |
1646 | * Most importantly, unless $cpoParams->getGenerateHtml was false, the return value contains an |
1647 | * HTML representation of the content. |
1648 | * |
1649 | * Subclasses that want to control the parser output may override |
1650 | * fillParserOutput() instead. |
1651 | * |
1652 | * |
1653 | * |
1654 | * @since 1.38 |
1655 | * |
1656 | * @param Content $content |
1657 | * @param ContentParseParams $cpoParams |
1658 | * @return ParserOutput Containing information derived from this content. |
1659 | */ |
1660 | public function getParserOutput( |
1661 | Content $content, |
1662 | ContentParseParams $cpoParams |
1663 | ) { |
1664 | $services = MediaWikiServices::getInstance(); |
1665 | $title = $services->getTitleFactory()->newFromPageReference( $cpoParams->getPage() ); |
1666 | $parserOptions = $cpoParams->getParserOptions(); |
1667 | |
1668 | if ( $parserOptions->getIsPreview() ) { |
1669 | $scopedCallback = $parserOptions->setupFakeRevision( |
1670 | $title, |
1671 | $content, |
1672 | $parserOptions->getUserIdentity(), |
1673 | $cpoParams->getRevId() ?: 0 |
1674 | ); |
1675 | } |
1676 | |
1677 | $hookRunner = new HookRunner( $services->getHookContainer() ); |
1678 | |
1679 | $po = new ParserOutput(); |
1680 | |
1681 | // Initialize to the page language |
1682 | $po->setLanguage( $title->getPageLanguage() ); |
1683 | |
1684 | $parserOptions->registerWatcher( [ &$po, 'recordOption' ] ); |
1685 | if ( $hookRunner->onContentGetParserOutput( |
1686 | // FIXME $cpoParams->getRevId() may be null here? |
1687 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
1688 | $content, $title, $cpoParams->getRevId(), $parserOptions, $cpoParams->getGenerateHtml(), $po ) |
1689 | ) { |
1690 | // Save and restore the old value, just in case something is reusing |
1691 | // the ParserOptions object in some weird way. |
1692 | $oldRedir = $parserOptions->getRedirectTarget(); |
1693 | $parserOptions->setRedirectTarget( $content->getRedirectTarget() ); |
1694 | |
1695 | $po->resetParseStartTime(); |
1696 | $this->fillParserOutput( |
1697 | $content, |
1698 | $cpoParams, |
1699 | $po |
1700 | ); |
1701 | $po->recordTimeProfile(); |
1702 | |
1703 | MediaWikiServices::getInstance()->get( '_ParserObserver' )->notifyParse( |
1704 | $title, |
1705 | $cpoParams->getRevId(), |
1706 | $parserOptions, |
1707 | $content, |
1708 | $po |
1709 | ); |
1710 | $parserOptions->setRedirectTarget( $oldRedir ); |
1711 | } |
1712 | |
1713 | $hookRunner->onContentAlterParserOutput( $content, $title, $po ); |
1714 | $parserOptions->registerWatcher( null ); |
1715 | if ( isset( $scopedCallback ) ) { |
1716 | ScopedCallback::consume( $scopedCallback ); |
1717 | } |
1718 | |
1719 | return $po; |
1720 | } |
1721 | |
1722 | /** |
1723 | * A temporary layer to move AbstractContent::fillParserOutput to ContentHandler::fillParserOutput |
1724 | * |
1725 | * @internal only core AbstractContent::fillParserOutput implementations need to call this. |
1726 | * @since 1.38 |
1727 | * @param Content $content |
1728 | * @param ContentParseParams $cpoParams |
1729 | * @param ParserOutput &$output The output object to fill (reference). |
1730 | */ |
1731 | public function fillParserOutputInternal( |
1732 | Content $content, |
1733 | ContentParseParams $cpoParams, |
1734 | ParserOutput &$output |
1735 | ) { |
1736 | $this->fillParserOutput( $content, $cpoParams, $output ); |
1737 | } |
1738 | |
1739 | /** |
1740 | * Fills the provided ParserOutput with information derived from the content. |
1741 | * Unless $cpoParams->getGenerateHtml() was false, |
1742 | * this includes an HTML representation of the content. |
1743 | * |
1744 | * If $cpoParams->getGenerateHtml() is false, and you chose not to generate |
1745 | * html, the ParserOutput must have a text of null. If the |
1746 | * text of the ParserOutput object is anything other than null (even if ''), |
1747 | * it is assumed that you don't support not generating html, and that it is |
1748 | * safe to reuse the parser output for calls expecting that html was generated. |
1749 | * |
1750 | * Subclasses are expected to override this method. |
1751 | * |
1752 | * This placeholder implementation always throws an exception. |
1753 | * |
1754 | * @stable to override |
1755 | * |
1756 | * @since 1.38 |
1757 | * @param Content $content |
1758 | * @param ContentParseParams $cpoParams |
1759 | * @param ParserOutput &$output The output object to fill (reference). |
1760 | * Most implementations should modify the output object passed in here; |
1761 | * if you choose to replace it with a fresh object instead, |
1762 | * make sure you call {@link ParserOutput::resetParseStartTime()} on it. |
1763 | */ |
1764 | protected function fillParserOutput( |
1765 | Content $content, |
1766 | ContentParseParams $cpoParams, |
1767 | ParserOutput &$output |
1768 | ) { |
1769 | // Subclasses must override fillParserOutput() to directly don't fail. |
1770 | throw new LogicException( 'Subclasses of ContentHandler must override fillParserOutput!' ); |
1771 | } |
1772 | |
1773 | } |
1774 | |
1775 | /** @deprecated class alias since 1.43 */ |
1776 | class_alias( ContentHandler::class, 'ContentHandler' ); |