Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / ContentHandler.php
blob1c3e9330e869e9fa1011b781642db3119660f9cb
1 <?php
3 /**
4 * Base class for content handling.
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.
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.
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
21 * @since 1.21
23 * @file
24 * @ingroup Content
26 * @author Daniel Kinzler
29 namespace MediaWiki\Content;
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;
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.
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.
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.
90 * @stable to extend
92 * @ingroup Content
94 abstract class ContentHandler {
95 use ProtectedHookAccessorTrait;
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!
102 * If $content is null, this method returns the empty string.
104 * If $content is an instance of TextContent, this method returns the flat
105 * text as returned by $content->getText().
107 * If $content is not a TextContent object, this method returns null.
109 * @since 1.21
111 * @deprecated since 1.37, use Content::getText() for TextContent instances
112 * instead. Hard deprecated since 1.43.
114 * @param Content|null $content
115 * @return string|null Textual form of the content, if available.
117 public static function getContentText( ?Content $content = null ) {
118 wfDeprecated( __METHOD__, '1.37' );
119 if ( $content === null ) {
120 return '';
123 if ( $content instanceof TextContent ) {
124 return $content->getText();
127 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
128 return null;
132 * Convenience function for creating a Content object from a given textual
133 * representation.
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.
139 * @since 1.21
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.
150 * @throws MWContentSerializationException
151 * @throws MWUnknownContentModelException
152 * @return Content A Content object representing the text.
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." );
160 return MediaWikiServices::getInstance()
161 ->getContentHandlerFactory()
162 ->getContentHandler( $modelId ?? $title->getContentModel() )
163 ->unserializeContent( $text, $format );
167 * Returns the name of the default content model to be used for the page
168 * with the given title.
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().
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.
190 * If none of the above applies, the wikitext model is used.
192 * @since 1.21
193 * @deprecated since 1.33, use SlotRoleHandler::getDefaultModel() together with
194 * SlotRoleRegistry::getRoleHandler(). Hard deprecated since 1.43.
196 * @param Title $title
198 * @return string Default model name for the page given by $title
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 );
208 * Returns the appropriate ContentHandler singleton for the given Content
209 * object.
211 * @deprecated since 1.35, instead use
212 * ContentHandlerFactory::getContentHandler( $content->getModel() ).
213 * Hard deprecated since 1.43.
215 * @since 1.21
217 * @param Content $content
219 * @return ContentHandler
220 * @throws MWUnknownContentModelException
222 public static function getForContent( Content $content ) {
223 wfDeprecated( __METHOD__, '1.35' );
224 return MediaWikiServices::getInstance()
225 ->getContentHandlerFactory()
226 ->getContentHandler( $content->getModel() );
230 * Returns the ContentHandler singleton for the given model ID. Use the
231 * CONTENT_MODEL_XXX constants to identify the desired content model.
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.
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.
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.
246 * @since 1.21
248 * @deprecated since 1.35, use ContentHandlerFactory::getContentHandler
249 * Hard deprecated since 1.43.
250 * @see ContentHandlerFactory::getContentHandler()
252 * @param string $modelId The ID of the content model for which to get a
253 * handler. Use CONTENT_MODEL_XXX constants.
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.
258 public static function getForModelID( $modelId ) {
259 wfDeprecated( __METHOD__, '1.35' );
260 return MediaWikiServices::getInstance()
261 ->getContentHandlerFactory()
262 ->getContentHandler( $modelId );
266 * Returns the localized name for a given content model.
268 * Model names are localized using system messages. Message keys
269 * have the form content-model-$name, where $name is getContentModelName( $id ).
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)
275 * @return string The content model's localized name.
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 ?? '' );
283 $msg = wfMessage( $key );
284 if ( $lang ) {
285 $msg->inLanguage( $lang );
288 return $msg->exists() ? $msg->plain() : $name;
292 * @deprecated since 1.35, use ContentHandlerFactory::getContentModels
293 * Hard deprecated since 1.43.
294 * @see ContentHandlerFactory::getContentModels
296 * @return string[]
298 public static function getContentModels() {
299 wfDeprecated( __METHOD__, '1.35' );
300 return MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentModels();
304 * @return string[]
306 * @deprecated since 1.35, use ContentHandlerFactory::getAllContentFormats
307 * Hard deprecated since 1.43.
308 * @see ContentHandlerFactory::getAllContentFormats
310 public static function getAllContentFormats() {
311 wfDeprecated( __METHOD__, '1.35' );
312 return MediaWikiServices::getInstance()->getContentHandlerFactory()->getAllContentFormats();
315 // ------------------------------------------------------------------------
318 * @var string
320 protected $mModelID;
323 * @var string[]
325 protected $mSupportedFormats;
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.
332 * @stable to call
334 * @param string $modelId (use CONTENT_MODEL_XXX constants).
335 * @param string[] $formats List for supported serialization formats
336 * (typically as MIME types)
338 public function __construct( $modelId, $formats ) {
339 $this->mModelID = $modelId;
340 $this->mSupportedFormats = $formats;
344 * Serializes a Content object of the type supported by this ContentHandler.
346 * @stable to override
348 * @since 1.21
350 * @param Content $content The Content object to serialize
351 * @param string|null $format The desired serialization format
353 * @return string Serialized form of the content
355 abstract public function serializeContent( Content $content, $format = null );
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.
362 * @stable to override
364 * @param string $blob The blob to be exported
365 * @param string|null $format The blob's serialization format
367 * @return string
369 public function exportTransform( $blob, $format = null ) {
370 return $blob;
374 * Unserializes a Content object of the type supported by this ContentHandler.
376 * @stable to override
377 * @since 1.21
379 * @param string $blob Serialized form of the content
380 * @param string|null $format The format used for serialization
382 * @return Content The Content object created by deserializing $blob
383 * @throws MWContentSerializationException
385 abstract public function unserializeContent( $blob, $format = null );
388 * Apply import transformation (by default, returns $blob unchanged).
389 * This gives subclasses an opportunity to transform data blobs on import.
391 * @stable to override
392 * @since 1.24
394 * @param string $blob
395 * @param string|null $format
397 * @return string
399 public function importTransform( $blob, $format = null ) {
400 return $blob;
404 * Creates an empty Content object of the type supported by this
405 * ContentHandler.
407 * @stable to override
408 * @since 1.21
410 * @return Content
412 abstract public function makeEmptyContent();
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.
418 * This default implementation always returns null. Subclasses supporting redirects
419 * must override this method.
421 * Note that subclasses that override this method to return a Content object
422 * should also override supportsRedirects() to return true.
424 * @stable to override
425 * @since 1.21
427 * @param Title $destination The page to redirect to.
428 * @param string $text Text to include in the redirect, if possible.
430 * @return Content|null Always null.
432 public function makeRedirectContent( Title $destination, $text = '' ) {
433 return null;
437 * Returns the model id that identifies the content model this
438 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
440 * @since 1.21
442 * @return string The model ID
444 public function getModelID() {
445 return $this->mModelID;
449 * @since 1.21
451 * @param string $model_id The model to check
453 * @throws MWException If the model ID is not the ID of the content model supported by this
454 * ContentHandler.
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." );
465 * Returns a list of serialization formats supported by the
466 * serializeContent() and unserializeContent() methods of this
467 * ContentHandler.
469 * @stable to override
470 * @since 1.21
472 * @return string[] List of serialization formats as MIME type like strings
474 public function getSupportedFormats() {
475 return $this->mSupportedFormats;
479 * The format used for serialization/deserialization by default by this
480 * ContentHandler.
482 * This default implementation will return the first element of the array
483 * of formats that was passed to the constructor.
485 * @stable to override
486 * @since 1.21
488 * @return string The name of the default serialization format as a MIME type
490 public function getDefaultFormat() {
491 return $this->mSupportedFormats[0];
495 * Returns true if $format is a serialization format supported by this
496 * ContentHandler, and false otherwise.
498 * Note that if $format is null, this method always returns true, because
499 * null means "use the default format".
501 * @stable to override
502 * @since 1.21
504 * @param string $format The serialization format to check
506 * @return bool
508 public function isSupportedFormat( $format ) {
509 if ( !$format ) {
510 return true; // this means "use the default"
513 return in_array( $format, $this->mSupportedFormats );
517 * Convenient for checking whether a format provided as a parameter is actually supported.
519 * @param string $format The serialization format to check
521 * @throws MWException If the format is not supported by this content handler.
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()
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.
538 * @stable to override
539 * @since 1.21
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.
549 * @see Action::factory
551 public function getActionOverrides() {
552 return [];
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.
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.)
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).
571 * @since 1.21
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.
580 * @return DifferenceEngine
582 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
583 $rcid = 0, // FIXME: Deprecated, no longer used
584 $refreshCache = false, $unhide = false
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;
594 * Get an appropriate SlotDiffRenderer for this content model.
596 * @stable to override
597 * @since 1.32
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
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 );
625 $this->getHookRunner()->onGetSlotDiffRenderer( $this, $slotDiffRenderer, $context );
626 return $slotDiffRenderer;
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
636 protected function getSlotDiffRendererInternal( IContextSource $context ) {
637 return null;
641 * Return the SlotDiffRenderer appropriate for this content handler.
642 * @stable to override
644 * @param IContextSource $context
645 * @param array $options See getSlotDiffRenderer()
647 * @return SlotDiffRenderer
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;
657 return $this->createTextSlotDiffRenderer( $options );
661 * Create a TextSlotDiffRenderer and inject dependencies
663 * @since 1.41
664 * @param array $options See getSlotDiffRenderer()
665 * @return TextSlotDiffRenderer
667 final protected function createTextSlotDiffRenderer( array $options = [] ): TextSlotDiffRenderer {
668 $slotDiffRenderer = new TextSlotDiffRenderer();
670 $services = MediaWikiServices::getInstance();
671 $slotDiffRenderer->setStatsFactory( $services->getStatsFactory() );
672 $slotDiffRenderer->setHookContainer( $services->getHookContainer() );
673 $slotDiffRenderer->setContentModel( $this->getModelID() );
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();
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 )
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';
699 $slotDiffRenderer->setFormat( $format );
700 $slotDiffRenderer->setTextDiffer( $textDiffer );
701 if ( $options['inline-toggle'] ?? false ) {
702 $slotDiffRenderer->setInlineToggleEnabled();
705 return $slotDiffRenderer;
709 * Get the language in which the content of the given page is written.
711 * This default implementation just returns the content language (except for pages
712 * in the MediaWiki namespace)
714 * Note that the page's language is not cacheable, since it may in some
715 * cases depend on user settings.
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.
720 * @stable to override
721 * @since 1.21
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.
726 * @return Language
728 public function getPageLanguage( Title $title, ?Content $content = null ) {
729 $services = MediaWikiServices::getInstance();
730 $pageLang = $services->getContentLanguage();
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 );
738 // Unused, T299369
739 $userLang = null;
740 $this->getHookRunner()->onPageContentLanguage( $title, $pageLang, $userLang );
742 if ( !$pageLang instanceof Language ) {
743 throw new UnexpectedValueException( 'onPageContentLanguage() hook provided an invalid $pageLang object.' );
746 return $pageLang;
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.
754 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
755 * the user specified a preferred variant.
757 * Note that the pages view language is not cacheable, since it depends on user settings.
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.
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
769 public function getPageViewLanguage( Title $title, ?Content $content = null ) {
770 $pageLang = $this->getPageLanguage( $title, $content );
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 );
782 return $pageLang;
786 * Determines whether the content type handled by this ContentHandler
787 * can be used for the main slot of the given page.
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).
794 * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
795 * content model can be used where.
797 * @stable to override
799 * @see SlotRoleHandler::isAllowedModel
801 * @param Title $title The page's title.
803 * @return bool True if content of this kind can be used on the given page, false otherwise.
805 public function canBeUsedOn( Title $title ) {
806 $ok = true;
808 $this->getHookRunner()->onContentModelCanBeUsedOn( $this->getModelID(), $title, $ok );
810 return $ok;
814 * Returns the name of the diff engine to use.
816 * @stable to override
817 * @since 1.21
819 * @return class-string<DifferenceEngine>
821 protected function getDiffEngineClass() {
822 return DifferenceEngine::class;
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.
829 * This default implementation always returns false.
831 * @stable to override
832 * @since 1.21
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.
838 * @return Content|false Always false.
840 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
841 return false;
845 * Shorthand for getting a Language Converter for specific language
846 * @param Language $language Language of converter
847 * @return ILanguageConverter
849 private function getLanguageConverter( $language ): ILanguageConverter {
850 return MediaWikiServices::getInstance()->getLanguageConverterFactory()
851 ->getLanguageConverter( $language );
855 * Return type of change if one exists for the given edit.
857 * @stable to override
858 * @since 1.31
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.
864 * @return string|null String key representing type of change, or null.
866 private function getChangeType(
867 ?Content $oldContent = null,
868 ?Content $newContent = null,
869 $flags = 0
871 $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null;
872 $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null;
874 // We check for the type of change in the given edit, and return string key accordingly
876 // Blanking of a page
877 if ( $oldContent && $oldContent->getSize() > 0 &&
878 $newContent && $newContent->getSize() === 0
880 return 'blank';
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()
891 // Redirect target changed
892 return 'changed-redirect-target';
894 } elseif ( $oldTarget ) {
895 // Changing an existing redirect into a non-redirect
896 return 'removed-redirect';
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';
909 // Removing more than 90% of the page
910 if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
911 return 'replace';
914 // Content model changed
915 if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
916 return 'contentmodelchange';
919 return null;
923 * Return an applicable auto-summary if one exists for the given edit.
925 * @stable to override
926 * @since 1.21
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.
932 * @return string An appropriate auto-summary, or an empty string.
934 public function getAutosummary(
935 ?Content $oldContent = null,
936 ?Content $newContent = null,
937 $flags = 0
939 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
941 // There's no applicable auto-summary for our case, so our auto-summary is empty.
942 if ( !$changeType ) {
943 return '';
946 // Set the maximum auto-summary length to the general maximum summary length
947 // T221617
948 $summaryLimit = CommentStore::COMMENT_CHARACTER_LIMIT;
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() )
960 return wfMessage( 'autoredircomment', $newTarget->getFullText() )
961 ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
962 case 'changed-redirect-target':
963 $oldTarget = $oldContent->getRedirectTarget();
964 $newTarget = $newContent->getRedirectTarget();
966 $truncatedtext = $newContent->getTextForSummary(
967 $summaryLimit
968 - strlen( wfMessage( 'autosumm-changed-redirect-target' )
969 ->inContentLanguage()->text() )
970 - strlen( $oldTarget->getFullText() )
971 - strlen( $newTarget->getFullText() )
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() ) );
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() ) );
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() ) );
1001 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
1002 ->inContentLanguage()->text();
1003 case 'newblank':
1004 return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
1005 default:
1006 return '';
1011 * Return an applicable tag if one exists for the given edit or return null.
1013 * @stable to override
1014 * @since 1.31
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.
1020 * @return string|null An appropriate tag, or null.
1022 public function getChangeTag(
1023 ?Content $oldContent = null,
1024 ?Content $newContent = null,
1025 $flags = 0
1027 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
1029 // There's no applicable tag for this change.
1030 if ( !$changeType ) {
1031 return null;
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;
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;
1044 return null;
1048 * Auto-generates a deletion reason
1050 * @stable to override
1051 * @since 1.21
1053 * @param Title $title The page's title
1054 * @param bool &$hasHistory Whether the page has a history
1056 * @return string|false String containing deletion reason or empty string, or
1057 * boolean false if no revision occurred
1059 public function getAutoDeleteReason( Title $title, &$hasHistory = false ) {
1060 if ( func_num_args() === 2 ) {
1061 wfDeprecated( __METHOD__ . ': $hasHistory parameter', '1.38' );
1063 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
1064 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
1066 // Get the last revision
1067 $revRecord = $revStore->getRevisionByTitle( $title );
1069 if ( $revRecord === null ) {
1070 return false;
1073 // Get the article's contents
1074 $content = $revRecord->getContent( SlotRecord::MAIN );
1075 $blank = false;
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 );
1083 if ( $prev ) {
1084 $revRecord = $prev;
1085 $content = $prev->getContent( SlotRecord::MAIN );
1086 $blank = true;
1090 $this->checkModelID( $revRecord->getSlot( SlotRecord::MAIN )->getModel() );
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'
1099 ->limit( 20 );
1100 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
1102 if ( !$res->numRows() ) {
1103 // This page has no revisions, which is very weird
1104 return false;
1107 $hasHistory = ( $res->numRows() > 1 );
1108 $row = $res->fetchObject();
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;
1119 } else {
1120 $onlyAuthor = false;
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();
1140 if ( $reason == '-' ) {
1141 // Allow these UI messages to be blanked out cleanly
1142 return '';
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 ) : '';
1149 // Now replace the '$1' placeholder
1150 $reason = str_replace( '$1', $text, $reason );
1152 return $reason;
1156 * Get the Content object that needs to be saved in order to undo all changes
1157 * between $undo and $undoafter.
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
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)
1170 * @return Content|false Content on success, false on failure
1172 public function getUndoContent(
1173 Content $currentContent,
1174 Content $undoContent,
1175 Content $undoAfterContent,
1176 $undoIsLatest = false
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() );
1188 } catch ( MWException $e ) {
1189 // If the revisions have different content models
1190 // just return false
1191 return false;
1194 if ( $currentContent->equals( $undoContent ) ) {
1195 // No use doing a merge if it's just a straight revert.
1196 return $undoAfterContent;
1199 $undone_content = $this->merge3( $undoContent, $undoAfterContent, $currentContent );
1201 return $undone_content;
1205 * Returns true for content models that support caching using the
1206 * ParserCache mechanism. See WikiPage::shouldCheckParserCache().
1208 * @stable to override
1209 * @since 1.21
1211 * @return bool Always false.
1213 public function isParserCacheSupported() {
1214 return false;
1218 * Returns true if this content model supports sections.
1219 * This default implementation returns false.
1221 * Content models that return true here should also implement
1222 * Content::getSection, Content::replaceSection, etc. to handle sections.
1224 * @stable to override
1226 * @return bool Always false.
1228 public function supportsSections() {
1229 return false;
1233 * Returns true if this content model supports categories.
1234 * The default implementation returns true.
1236 * @stable to override
1238 * @return bool Always true.
1240 public function supportsCategories() {
1241 return true;
1245 * Returns true if this content model supports redirects.
1246 * This default implementation returns false.
1248 * Content models that return true here should also implement
1249 * ContentHandler::makeRedirectContent to return a Content object.
1251 * @stable to override
1253 * @return bool Always false.
1255 public function supportsRedirects() {
1256 return false;
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.
1264 * @stable to override
1266 * @return bool Default is false.
1268 public function supportsDirectEditing() {
1269 return false;
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}.
1277 * @stable to override
1278 * @since 1.39
1280 * @return bool
1282 public function supportsPreloadContent(): bool {
1283 return false;
1287 * Whether an edit on the content should trigger an HTML render and ParserCache entry.
1289 * @stable to override
1290 * @since 1.37
1292 * @return bool true if edit should trigger an HTML render false otherwise
1294 public function generateHTMLOnEdit(): bool {
1295 return true;
1299 * Whether or not this content model supports direct editing via ApiEditPage
1301 * @stable to override
1303 * @return bool Default is false, and true for TextContent and derivatives.
1305 public function supportsDirectApiEditing() {
1306 return $this->supportsDirectEditing();
1310 * Get fields definition for search index
1312 * @todo Expose title, redirect, namespace, text, source_text, text_bytes
1313 * field mappings here. (see T142670 and T143409)
1315 * @stable to override
1317 * @param SearchEngine $engine
1318 * @return SearchIndexField[] List of fields this content handler can provide.
1319 * @since 1.28
1321 public function getFieldsForSearchIndex( SearchEngine $engine ) {
1322 $fields = [];
1323 $fields['category'] = $engine->makeSearchFieldMapping(
1324 'category',
1325 SearchIndexField::INDEX_TYPE_TEXT
1327 $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1329 $fields['external_link'] = $engine->makeSearchFieldMapping(
1330 'external_link',
1331 SearchIndexField::INDEX_TYPE_KEYWORD
1334 $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1335 'outgoing_link',
1336 SearchIndexField::INDEX_TYPE_KEYWORD
1339 $fields['template'] = $engine->makeSearchFieldMapping(
1340 'template',
1341 SearchIndexField::INDEX_TYPE_KEYWORD
1343 $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1345 $fields['content_model'] = $engine->makeSearchFieldMapping(
1346 'content_model',
1347 SearchIndexField::INDEX_TYPE_KEYWORD
1350 return $fields;
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
1362 protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) {
1363 $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1364 return $fields;
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.
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()}.
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
1393 public function getDataForSearchIndex(
1394 WikiPage $page,
1395 ParserOutput $output,
1396 SearchEngine $engine,
1397 ?RevisionRecord $revision = null
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() ]
1406 return [];
1408 Assert::invariant( $revision->getPageId() === $page->getId(),
1409 '$revision and $page must target the same page_id' );
1411 $fieldData = [];
1412 $content = $revision->getContent( SlotRecord::MAIN );
1414 if ( $content ) {
1415 $searchDataExtractor = new ParserOutputSearchDataExtractor();
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 );
1422 $text = $content->getTextForSearchIndex();
1424 $fieldData['text'] = $text;
1425 $fieldData['source_text'] = $text;
1426 $fieldData['text_bytes'] = $content->getSize();
1427 $fieldData['content_model'] = $content->getModel();
1430 $this->getHookRunner()->onSearchDataForIndex( $fieldData, $this, $page, $output, $engine );
1431 $this->getHookRunner()->onSearchDataForIndex2( $fieldData, $this, $page, $output, $engine, $revision );
1433 return $fieldData;
1437 * Produce page output suitable for indexing.
1438 * Typically used with {@link getDataForSearchIndex()}.
1440 * Specific content handlers may override it if they need different content handling.
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.)
1447 * @stable to override
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
1455 public function getParserOutputForIndexing(
1456 WikiPage $page,
1457 ?ParserCache $cache = null,
1458 ?RevisionRecord $revision = null
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();
1474 * Get the latest revision of the given $page,
1475 * fetching it from the primary if necessary.
1477 * @param WikiPage $page
1478 * @return RevisionRecord
1479 * @since 1.36 (previously private)
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" );
1495 return $revRecord;
1499 * Returns a list of DeferrableUpdate objects for recording information about the
1500 * given Content in some secondary data store.
1502 * Application logic should not call this method directly. Instead, it should call
1503 * DerivedPageDataUpdater::getSecondaryDataUpdates().
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.
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.
1515 * @stable to override
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
1530 public function getSecondaryDataUpdates(
1531 Title $title,
1532 Content $content,
1533 $role,
1534 SlotRenderingProvider $slotOutput
1536 return [];
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.
1544 * Application logic should not call this method directly. Instead, it should call
1545 * WikiPage::getSecondaryDataUpdates().
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.
1552 * @note Implementations should not rely on the page's current content, but rather the current
1553 * state of the secondary data store.
1555 * @stable to override
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.
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.
1567 * @since 1.32
1569 public function getDeletionUpdates( Title $title, $role ) {
1570 return [];
1574 * Returns a $content object with pre-save transformations applied (or the same
1575 * object if no transformations apply).
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
1582 * @param Content $content
1583 * @param PreSaveTransformParams $pstParams
1585 * @return Content
1587 public function preSaveTransform(
1588 Content $content,
1589 PreSaveTransformParams $pstParams
1590 ): Content {
1591 return $content;
1595 * Returns a $content object with preload transformations applied (or the same
1596 * object if no transformations apply).
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
1603 * @param Content $content
1604 * @param PreloadTransformParams $pltParams
1606 * @return Content
1608 public function preloadTransform(
1609 Content $content,
1610 PreloadTransformParams $pltParams
1611 ): Content {
1612 return $content;
1616 * Validate content for saving it.
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.
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.
1625 * @since 1.38
1626 * @stable to override
1628 * @param Content $content
1629 * @param ValidationParams $validationParams
1631 * @return StatusValue A status object indicating if content can be saved in the given revision.
1633 public function validateSave(
1634 Content $content,
1635 ValidationParams $validationParams
1637 if ( $content->isValid() ) {
1638 return StatusValue::newGood();
1639 } else {
1640 return StatusValue::newFatal( "invalid-content-data" );
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.
1649 * Subclasses that want to control the parser output may override
1650 * fillParserOutput() instead.
1654 * @since 1.38
1656 * @param Content $content
1657 * @param ContentParseParams $cpoParams
1658 * @return ParserOutput Containing information derived from this content.
1660 public function getParserOutput(
1661 Content $content,
1662 ContentParseParams $cpoParams
1664 $services = MediaWikiServices::getInstance();
1665 $title = $services->getTitleFactory()->newFromPageReference( $cpoParams->getPage() );
1666 $parserOptions = $cpoParams->getParserOptions();
1668 if ( $parserOptions->getIsPreview() ) {
1669 $scopedCallback = $parserOptions->setupFakeRevision(
1670 $title,
1671 $content,
1672 $parserOptions->getUserIdentity(),
1673 $cpoParams->getRevId() ?: 0
1677 $hookRunner = new HookRunner( $services->getHookContainer() );
1679 $po = new ParserOutput();
1681 // Initialize to the page language
1682 $po->setLanguage( $title->getPageLanguage() );
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 )
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() );
1695 $po->resetParseStartTime();
1696 $this->fillParserOutput(
1697 $content,
1698 $cpoParams,
1701 $po->recordTimeProfile();
1703 MediaWikiServices::getInstance()->get( '_ParserObserver' )->notifyParse(
1704 $title,
1705 $cpoParams->getRevId(),
1706 $parserOptions,
1707 $content,
1710 $parserOptions->setRedirectTarget( $oldRedir );
1713 $hookRunner->onContentAlterParserOutput( $content, $title, $po );
1714 $parserOptions->registerWatcher( null );
1715 if ( isset( $scopedCallback ) ) {
1716 ScopedCallback::consume( $scopedCallback );
1719 return $po;
1723 * A temporary layer to move AbstractContent::fillParserOutput to ContentHandler::fillParserOutput
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).
1731 public function fillParserOutputInternal(
1732 Content $content,
1733 ContentParseParams $cpoParams,
1734 ParserOutput &$output
1736 $this->fillParserOutput( $content, $cpoParams, $output );
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.
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.
1750 * Subclasses are expected to override this method.
1752 * This placeholder implementation always throws an exception.
1754 * @stable to override
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.
1764 protected function fillParserOutput(
1765 Content $content,
1766 ContentParseParams $cpoParams,
1767 ParserOutput &$output
1769 // Subclasses must override fillParserOutput() to directly don't fail.
1770 throw new LogicException( 'Subclasses of ContentHandler must override fillParserOutput!' );
1775 /** @deprecated class alias since 1.43 */
1776 class_alias( ContentHandler::class, 'ContentHandler' );