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