3 use MediaWiki\Logger\LoggerFactory
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Search\ParserOutputSearchDataExtractor
;
8 * Base class for content handling.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
30 * @author Daniel Kinzler
33 * A content handler knows how do deal with a specific type of content on a wiki
34 * page. Content is stored in the database in a serialized form (using a
35 * serialization format a.k.a. MIME type) and is unserialized into its native
36 * PHP representation (the content model), which is wrapped in an instance of
37 * the appropriate subclass of Content.
39 * ContentHandler instances are stateless singletons that serve, among other
40 * things, as a factory for Content objects. Generally, there is one subclass
41 * of ContentHandler and one subclass of Content for every type of content model.
43 * Some content types have a flat model, that is, their native representation
44 * is the same as their serialized form. Examples would be JavaScript and CSS
45 * code. As of now, this also applies to wikitext (MediaWiki's default content
46 * type), but wikitext content may be represented by a DOM or AST structure in
51 abstract class ContentHandler
{
53 * Convenience function for getting flat text from a Content object. This
54 * should only be used in the context of backwards compatibility with code
55 * that is not yet able to handle Content objects!
57 * If $content is null, this method returns the empty string.
59 * If $content is an instance of TextContent, this method returns the flat
60 * text as returned by $content->getNativeData().
62 * If $content is not a TextContent object, the behavior of this method
63 * depends on the global $wgContentHandlerTextFallback:
64 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
65 * TextContent object, an MWException is thrown.
66 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
67 * TextContent object, $content->serialize() is called to get a string
68 * form of the content.
69 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
70 * TextContent object, this method returns null.
71 * - otherwise, the behavior is undefined.
75 * @param Content|null $content
77 * @throws MWException If the content is not an instance of TextContent and
78 * wgContentHandlerTextFallback was set to 'fail'.
79 * @return string|null Textual form of the content, if available.
81 public static function getContentText( Content
$content = null ) {
82 global $wgContentHandlerTextFallback;
84 if ( is_null( $content ) ) {
88 if ( $content instanceof TextContent
) {
89 return $content->getNativeData();
92 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
94 if ( $wgContentHandlerTextFallback == 'fail' ) {
95 throw new MWException(
96 "Attempt to get text from Content with model " .
101 if ( $wgContentHandlerTextFallback == 'serialize' ) {
102 return $content->serialize();
109 * Convenience function for creating a Content object from a given textual
112 * $text will be deserialized into a Content object of the model specified
113 * by $modelId (or, if that is not given, $title->getContentModel()) using
118 * @param string $text The textual representation, will be
119 * unserialized to create the Content object
120 * @param Title|null $title The title of the page this text belongs to.
121 * Required if $modelId is not provided.
122 * @param string|null $modelId The model to deserialize to. If not provided,
123 * $title->getContentModel() is used.
124 * @param string|null $format The format to use for deserialization. If not
125 * given, the model's default format is used.
127 * @throws MWException If model ID or format is not supported or if the text can not be
128 * unserialized using the format.
129 * @return Content A Content object representing the text.
131 public static function makeContent( $text, Title
$title = null,
132 $modelId = null, $format = null ) {
133 if ( is_null( $modelId ) ) {
134 if ( is_null( $title ) ) {
135 throw new MWException( "Must provide a Title object or a content model ID." );
138 $modelId = $title->getContentModel();
141 $handler = self
::getForModelID( $modelId );
143 return $handler->unserializeContent( $text, $format );
147 * Returns the name of the default content model to be used for the page
148 * with the given title.
150 * Note: There should rarely be need to call this method directly.
151 * To determine the actual content model for a given page, use
152 * Title::getContentModel().
154 * Which model is to be used by default for the page is determined based
155 * on several factors:
156 * - The global setting $wgNamespaceContentModels specifies a content model
158 * - The hook ContentHandlerDefaultModelFor may be used to override the page's default
160 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
161 * model if they end in .js or .css, respectively.
162 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
163 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
164 * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor
165 * hook should be used instead if possible.
166 * - The hook TitleIsWikitextPage may be used to force a page to use the
167 * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor
168 * hook should be used instead if possible.
170 * If none of the above applies, the wikitext model is used.
172 * Note: this is used by, and may thus not use, Title::getContentModel()
176 * @param Title $title
178 * @return string Default model name for the page given by $title
180 public static function getDefaultModelFor( Title
$title ) {
181 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
182 // because it is used to initialize the mContentModel member.
184 $ns = $title->getNamespace();
188 $model = MWNamespace
::getNamespaceContentModel( $ns );
190 // Hook can determine default model
191 if ( !Hooks
::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
192 if ( !is_null( $model ) ) {
197 // Could this page contain code based on the title?
198 $isCodePage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
203 // Is this a user subpage containing code?
204 $isCodeSubpage = NS_USER
== $ns
206 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
207 if ( $isCodeSubpage ) {
211 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
212 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
213 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
215 if ( !$isWikitext ) {
218 return CONTENT_MODEL_JAVASCRIPT
;
220 return CONTENT_MODEL_CSS
;
222 return CONTENT_MODEL_JSON
;
224 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
228 // We established that it must be wikitext
230 return CONTENT_MODEL_WIKITEXT
;
234 * Returns the appropriate ContentHandler singleton for the given title.
238 * @param Title $title
240 * @return ContentHandler
242 public static function getForTitle( Title
$title ) {
243 $modelId = $title->getContentModel();
245 return self
::getForModelID( $modelId );
249 * Returns the appropriate ContentHandler singleton for the given Content
254 * @param Content $content
256 * @return ContentHandler
258 public static function getForContent( Content
$content ) {
259 $modelId = $content->getModel();
261 return self
::getForModelID( $modelId );
265 * @var array A Cache of ContentHandler instances by model id
267 protected static $handlers;
270 * Returns the ContentHandler singleton for the given model ID. Use the
271 * CONTENT_MODEL_XXX constants to identify the desired content model.
273 * ContentHandler singletons are taken from the global $wgContentHandlers
274 * array. Keys in that array are model names, the values are either
275 * ContentHandler singleton objects, or strings specifying the appropriate
276 * subclass of ContentHandler.
278 * If a class name is encountered when looking up the singleton for a given
279 * model name, the class is instantiated and the class name is replaced by
280 * the resulting singleton in $wgContentHandlers.
282 * If no ContentHandler is defined for the desired $modelId, the
283 * ContentHandler may be provided by the ContentHandlerForModelID hook.
284 * If no ContentHandler can be determined, an MWException is raised.
288 * @param string $modelId The ID of the content model for which to get a
289 * handler. Use CONTENT_MODEL_XXX constants.
291 * @throws MWException For internal errors and problems in the configuration.
292 * @throws MWUnknownContentModelException If no handler is known for the model ID.
293 * @return ContentHandler The ContentHandler singleton for handling the model given by the ID.
295 public static function getForModelID( $modelId ) {
296 global $wgContentHandlers;
298 if ( isset( self
::$handlers[$modelId] ) ) {
299 return self
::$handlers[$modelId];
302 if ( empty( $wgContentHandlers[$modelId] ) ) {
305 Hooks
::run( 'ContentHandlerForModelID', [ $modelId, &$handler ] );
307 if ( $handler === null ) {
308 throw new MWUnknownContentModelException( $modelId );
311 if ( !( $handler instanceof ContentHandler
) ) {
312 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
315 $classOrCallback = $wgContentHandlers[$modelId];
317 if ( is_callable( $classOrCallback ) ) {
318 $handler = call_user_func( $classOrCallback, $modelId );
320 $handler = new $classOrCallback( $modelId );
323 if ( !( $handler instanceof ContentHandler
) ) {
324 throw new MWException( "$classOrCallback from \$wgContentHandlers is not " .
325 "compatible with ContentHandler" );
329 wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
330 . ': ' . get_class( $handler ) );
332 self
::$handlers[$modelId] = $handler;
334 return self
::$handlers[$modelId];
338 * Clean up handlers cache.
340 public static function cleanupHandlersCache() {
341 self
::$handlers = [];
345 * Returns the localized name for a given content model.
347 * Model names are localized using system messages. Message keys
348 * have the form content-model-$name, where $name is getContentModelName( $id ).
350 * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX
351 * constant or returned by Revision::getContentModel().
352 * @param Language|null $lang The language to parse the message in (since 1.26)
354 * @throws MWException If the model ID isn't known.
355 * @return string The content model's localized name.
357 public static function getLocalizedName( $name, Language
$lang = null ) {
358 // Messages: content-model-wikitext, content-model-text,
359 // content-model-javascript, content-model-css
360 $key = "content-model-$name";
362 $msg = wfMessage( $key );
364 $msg->inLanguage( $lang );
367 return $msg->exists() ?
$msg->plain() : $name;
370 public static function getContentModels() {
371 global $wgContentHandlers;
373 $models = array_keys( $wgContentHandlers );
374 Hooks
::run( 'GetContentModels', [ &$models ] );
378 public static function getAllContentFormats() {
379 global $wgContentHandlers;
383 foreach ( $wgContentHandlers as $model => $class ) {
384 $handler = self
::getForModelID( $model );
385 $formats = array_merge( $formats, $handler->getSupportedFormats() );
388 $formats = array_unique( $formats );
393 // ------------------------------------------------------------------------
403 protected $mSupportedFormats;
406 * Constructor, initializing the ContentHandler instance with its model ID
407 * and a list of supported formats. Values for the parameters are typically
408 * provided as literals by subclass's constructors.
410 * @param string $modelId (use CONTENT_MODEL_XXX constants).
411 * @param string[] $formats List for supported serialization formats
412 * (typically as MIME types)
414 public function __construct( $modelId, $formats ) {
415 $this->mModelID
= $modelId;
416 $this->mSupportedFormats
= $formats;
420 * Serializes a Content object of the type supported by this ContentHandler.
424 * @param Content $content The Content object to serialize
425 * @param string|null $format The desired serialization format
427 * @return string Serialized form of the content
429 abstract public function serializeContent( Content
$content, $format = null );
432 * Applies transformations on export (returns the blob unchanged per default).
433 * Subclasses may override this to perform transformations such as conversion
434 * of legacy formats or filtering of internal meta-data.
436 * @param string $blob The blob to be exported
437 * @param string|null $format The blob's serialization format
441 public function exportTransform( $blob, $format = null ) {
446 * Unserializes a Content object of the type supported by this ContentHandler.
450 * @param string $blob Serialized form of the content
451 * @param string|null $format The format used for serialization
453 * @return Content The Content object created by deserializing $blob
455 abstract public function unserializeContent( $blob, $format = null );
458 * Apply import transformation (per default, returns $blob unchanged).
459 * This gives subclasses an opportunity to transform data blobs on import.
463 * @param string $blob
464 * @param string|null $format
468 public function importTransform( $blob, $format = null ) {
473 * Creates an empty Content object of the type supported by this
480 abstract public function makeEmptyContent();
483 * Creates a new Content object that acts as a redirect to the given page,
484 * or null if redirects are not supported by this content model.
486 * This default implementation always returns null. Subclasses supporting redirects
487 * must override this method.
489 * Note that subclasses that override this method to return a Content object
490 * should also override supportsRedirects() to return true.
494 * @param Title $destination The page to redirect to.
495 * @param string $text Text to include in the redirect, if possible.
497 * @return Content Always null.
499 public function makeRedirectContent( Title
$destination, $text = '' ) {
504 * Returns the model id that identifies the content model this
505 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
509 * @return string The model ID
511 public function getModelID() {
512 return $this->mModelID
;
518 * @param string $model_id The model to check
520 * @throws MWException If the model ID is not the ID of the content model supported by this
523 protected function checkModelID( $model_id ) {
524 if ( $model_id !== $this->mModelID
) {
525 throw new MWException( "Bad content model: " .
526 "expected {$this->mModelID} " .
527 "but got $model_id." );
532 * Returns a list of serialization formats supported by the
533 * serializeContent() and unserializeContent() methods of this
538 * @return string[] List of serialization formats as MIME type like strings
540 public function getSupportedFormats() {
541 return $this->mSupportedFormats
;
545 * The format used for serialization/deserialization by default by this
548 * This default implementation will return the first element of the array
549 * of formats that was passed to the constructor.
553 * @return string The name of the default serialization format as a MIME type
555 public function getDefaultFormat() {
556 return $this->mSupportedFormats
[0];
560 * Returns true if $format is a serialization format supported by this
561 * ContentHandler, and false otherwise.
563 * Note that if $format is null, this method always returns true, because
564 * null means "use the default format".
568 * @param string $format The serialization format to check
572 public function isSupportedFormat( $format ) {
574 return true; // this means "use the default"
577 return in_array( $format, $this->mSupportedFormats
);
581 * Convenient for checking whether a format provided as a parameter is actually supported.
583 * @param string $format The serialization format to check
585 * @throws MWException If the format is not supported by this content handler.
587 protected function checkFormat( $format ) {
588 if ( !$this->isSupportedFormat( $format ) ) {
589 throw new MWException(
590 "Format $format is not supported for content model "
591 . $this->getModelID()
597 * Returns overrides for action handlers.
598 * Classes listed here will be used instead of the default one when
599 * (and only when) $wgActions[$action] === true. This allows subclasses
600 * to override the default action handlers.
604 * @return array An array mapping action names (typically "view", "edit", "history" etc.) to
605 * either the full qualified class name of an Action class, a callable taking ( Page $page,
606 * IContextSource $context = null ) as parameters and returning an Action object, or an actual
607 * Action object. An empty array in this default implementation.
609 * @see Action::factory
611 public function getActionOverrides() {
616 * Factory for creating an appropriate DifferenceEngine for this content model.
617 * Since 1.32, this is only used for page-level diffs; to diff two content objects,
618 * use getSlotDiffRenderer.
620 * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The
621 * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or
623 * (Note that in older versions of MediaWiki the hook documentation instructed extensions
624 * to return false from the hook; you should not rely on always being able to decorate
625 * the DifferenceEngine instance from the hook. If the owner of the content type wants to
626 * decorare the instance, overriding this method is a safer approach.)
628 * @todo This is page-level functionality so it should not belong to ContentHandler.
629 * Move it to a better place once one exists (e.g. PageTypeHandler).
633 * @param IContextSource $context Context to use, anything else will be ignored.
634 * @param int $old Revision ID we want to show and diff with.
635 * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'.
636 * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0.
637 * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false.
638 * @param bool $unhide If set, allow viewing deleted revs. Defaults to false.
640 * @return DifferenceEngine
642 public function createDifferenceEngine( IContextSource
$context, $old = 0, $new = 0,
643 $rcid = 0, // FIXME: Deprecated, no longer used
644 $refreshCache = false, $unhide = false
646 $diffEngineClass = $this->getDiffEngineClass();
647 $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
648 Hooks
::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
649 &$differenceEngine ] );
650 return $differenceEngine;
654 * Get an appropriate SlotDiffRenderer for this content model.
656 * @param IContextSource $context
657 * @return SlotDiffRenderer
659 final public function getSlotDiffRenderer( IContextSource
$context ) {
660 $slotDiffRenderer = $this->getSlotDiffRendererInternal( $context );
661 if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer
::class ) {
662 // To keep B/C, when SlotDiffRenderer is not overridden for a given content type
663 // but DifferenceEngine is, use that instead.
664 $differenceEngine = $this->createDifferenceEngine( $context );
665 if ( get_class( $differenceEngine ) !== DifferenceEngine
::class ) {
666 // TODO turn this into a deprecation warning in a later release
667 LoggerFactory
::getInstance( 'diff' )->notice(
668 'Falling back to DifferenceEngineSlotDiffRenderer', [
669 'modelID' => $this->getModelID(),
670 'DifferenceEngine' => get_class( $differenceEngine ),
672 $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine );
675 Hooks
::run( 'GetSlotDiffRenderer', [ $this, &$slotDiffRenderer, $context ] );
676 return $slotDiffRenderer;
680 * Return the SlotDiffRenderer appropriate for this content handler.
681 * @param IContextSource $context
682 * @return SlotDiffRenderer
684 protected function getSlotDiffRendererInternal( IContextSource
$context ) {
685 $contentLanguage = MediaWikiServices
::getInstance()->getContentLanguage();
686 $statsdDataFactory = MediaWikiServices
::getInstance()->getStatsdDataFactory();
687 $slotDiffRenderer = new TextSlotDiffRenderer();
688 $slotDiffRenderer->setStatsdDataFactory( $statsdDataFactory );
689 // XXX using the page language would be better, but it's unclear how that should be injected
690 $slotDiffRenderer->setLanguage( $contentLanguage );
691 $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff(
692 $context->getConfig()->get( 'WikiDiff2MovedParagraphDetectionCutoff' )
695 $engine = DifferenceEngine
::getEngine();
696 if ( $engine === false ) {
697 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_PHP
);
698 } elseif ( $engine === 'wikidiff2' ) {
699 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_WIKIDIFF2
);
701 $slotDiffRenderer->setEngine( TextSlotDiffRenderer
::ENGINE_EXTERNAL
, $engine );
704 return $slotDiffRenderer;
708 * Get the language in which the content of the given page is written.
710 * This default implementation just returns the content language (except for pages
711 * in the MediaWiki namespace)
713 * Note that the pages language is not cacheable, since it may in some
714 * cases depend on user settings.
716 * Also note that the page language may or may not depend on the actual content of the page,
717 * that is, this method may load the content in order to determine the language.
721 * @param Title $title The page to determine the language for.
722 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
724 * @return Language The page's language
726 public function getPageLanguage( Title
$title, Content
$content = null ) {
728 $pageLang = MediaWikiServices
::getInstance()->getContentLanguage();
730 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
731 // Parse mediawiki messages with correct target language
732 list( /* $unused */, $lang ) = MessageCache
::singleton()->figureMessage( $title->getText() );
733 $pageLang = Language
::factory( $lang );
736 Hooks
::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
738 return wfGetLangObj( $pageLang );
742 * Get the language in which the content of this page is written when
743 * viewed by user. Defaults to $this->getPageLanguage(), but if the user
744 * specified a preferred variant, the variant will be used.
746 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
747 * the user specified a preferred variant.
749 * Note that the pages view language is not cacheable, since it depends on user settings.
751 * Also note that the page language may or may not depend on the actual content of the page,
752 * that is, this method may load the content in order to determine the language.
756 * @param Title $title The page to determine the language for.
757 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
759 * @return Language The page's language for viewing
761 public function getPageViewLanguage( Title
$title, Content
$content = null ) {
762 $pageLang = $this->getPageLanguage( $title, $content );
764 if ( $title->getNamespace() !== NS_MEDIAWIKI
) {
765 // If the user chooses a variant, the content is actually
766 // in a language whose code is the variant code.
767 $variant = $pageLang->getPreferredVariant();
768 if ( $pageLang->getCode() !== $variant ) {
769 $pageLang = Language
::factory( $variant );
777 * Determines whether the content type handled by this ContentHandler
778 * can be used on the given page.
780 * This default implementation always returns true.
781 * Subclasses may override this to restrict the use of this content model to specific locations,
782 * typically based on the namespace or some other aspect of the title, such as a special suffix
783 * (e.g. ".svg" for SVG content).
785 * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
786 * content model can be used where.
788 * @param Title $title The page's title.
790 * @return bool True if content of this kind can be used on the given page, false otherwise.
792 public function canBeUsedOn( Title
$title ) {
795 Hooks
::run( 'ContentModelCanBeUsedOn', [ $this->getModelID(), $title, &$ok ] );
801 * Returns the name of the diff engine to use.
807 protected function getDiffEngineClass() {
808 return DifferenceEngine
::class;
812 * Attempts to merge differences between three versions. Returns a new
813 * Content object for a clean merge and false for failure or a conflict.
815 * This default implementation always returns false.
819 * @param Content $oldContent The page's previous content.
820 * @param Content $myContent One of the page's conflicting contents.
821 * @param Content $yourContent One of the page's conflicting contents.
823 * @return Content|bool Always false.
825 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
830 * Return type of change if one exists for the given edit.
834 * @param Content|null $oldContent The previous text of the page.
835 * @param Content|null $newContent The submitted text of the page.
836 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
838 * @return string|null String key representing type of change, or null.
840 private function getChangeType(
841 Content
$oldContent = null,
842 Content
$newContent = null,
845 $oldTarget = $oldContent !== null ?
$oldContent->getRedirectTarget() : null;
846 $newTarget = $newContent !== null ?
$newContent->getRedirectTarget() : null;
848 // We check for the type of change in the given edit, and return string key accordingly
850 // Blanking of a page
851 if ( $oldContent && $oldContent->getSize() > 0 &&
852 $newContent && $newContent->getSize() === 0
860 // New redirect page (by creating new page or by changing content page)
861 return 'new-redirect';
862 } elseif ( !$newTarget->equals( $oldTarget ) ||
863 $oldTarget->getFragment() !== $newTarget->getFragment()
865 // Redirect target changed
866 return 'changed-redirect-target';
868 } elseif ( $oldTarget ) {
869 // Changing an existing redirect into a non-redirect
870 return 'removed-redirect';
874 if ( $flags & EDIT_NEW
&& $newContent ) {
875 if ( $newContent->getSize() === 0 ) {
883 // Removing more than 90% of the page
884 if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
888 // Content model changed
889 if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
890 return 'contentmodelchange';
897 * Return an applicable auto-summary if one exists for the given edit.
901 * @param Content|null $oldContent The previous text of the page.
902 * @param Content|null $newContent The submitted text of the page.
903 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
905 * @return string An appropriate auto-summary, or an empty string.
907 public function getAutosummary(
908 Content
$oldContent = null,
909 Content
$newContent = null,
912 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
914 // There's no applicable auto-summary for our case, so our auto-summary is empty.
915 if ( !$changeType ) {
919 // Decide what kind of auto-summary is needed.
920 switch ( $changeType ) {
922 $newTarget = $newContent->getRedirectTarget();
923 $truncatedtext = $newContent->getTextForSummary(
925 - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
926 - strlen( $newTarget->getFullText() )
929 return wfMessage( 'autoredircomment', $newTarget->getFullText() )
930 ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
931 case 'changed-redirect-target':
932 $oldTarget = $oldContent->getRedirectTarget();
933 $newTarget = $newContent->getRedirectTarget();
935 $truncatedtext = $newContent->getTextForSummary(
937 - strlen( wfMessage( 'autosumm-changed-redirect-target' )
938 ->inContentLanguage()->text() )
939 - strlen( $oldTarget->getFullText() )
940 - strlen( $newTarget->getFullText() )
943 return wfMessage( 'autosumm-changed-redirect-target',
944 $oldTarget->getFullText(),
945 $newTarget->getFullText() )
946 ->rawParams( $truncatedtext )->inContentLanguage()->text();
947 case 'removed-redirect':
948 $oldTarget = $oldContent->getRedirectTarget();
949 $truncatedtext = $newContent->getTextForSummary(
951 - strlen( wfMessage( 'autosumm-removed-redirect' )
952 ->inContentLanguage()->text() )
953 - strlen( $oldTarget->getFullText() ) );
955 return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
956 ->rawParams( $truncatedtext )->inContentLanguage()->text();
958 // If they're making a new article, give its text, truncated, in the summary.
959 $truncatedtext = $newContent->getTextForSummary(
960 200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
962 return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
963 ->inContentLanguage()->text();
965 return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
967 $truncatedtext = $newContent->getTextForSummary(
968 200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
970 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
971 ->inContentLanguage()->text();
973 return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
980 * Return an applicable tag if one exists for the given edit or return null.
984 * @param Content|null $oldContent The previous text of the page.
985 * @param Content|null $newContent The submitted text of the page.
986 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
988 * @return string|null An appropriate tag, or null.
990 public function getChangeTag(
991 Content
$oldContent = null,
992 Content
$newContent = null,
995 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
997 // There's no applicable tag for this change.
998 if ( !$changeType ) {
1002 // Core tags use the same keys as ones returned from $this->getChangeType()
1003 // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
1004 // if this type of change should be tagged
1005 $tag = 'mw-' . $changeType;
1007 // Not all change types are tagged, so we check against the list of defined tags.
1008 if ( in_array( $tag, ChangeTags
::getSoftwareTags() ) ) {
1016 * Auto-generates a deletion reason
1020 * @param Title $title The page's title
1021 * @param bool &$hasHistory Whether the page has a history
1023 * @return mixed String containing deletion reason or empty string, or
1024 * boolean false if no revision occurred
1026 * @todo &$hasHistory is extremely ugly, it's here because
1027 * WikiPage::getAutoDeleteReason() and Article::generateReason()
1028 * have it / want it.
1030 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
1031 $dbr = wfGetDB( DB_REPLICA
);
1033 // Get the last revision
1034 $rev = Revision
::newFromTitle( $title );
1036 if ( is_null( $rev ) ) {
1040 // Get the article's contents
1041 $content = $rev->getContent();
1044 // If the page is blank, use the text from the previous revision,
1045 // which can only be blank if there's a move/import/protect dummy
1046 // revision involved
1047 if ( !$content ||
$content->isEmpty() ) {
1048 $prev = $rev->getPrevious();
1052 $content = $rev->getContent();
1057 $this->checkModelID( $rev->getContentModel() );
1059 // Find out if there was only one contributor
1060 // Only scan the last 20 revisions
1061 $revQuery = Revision
::getQueryInfo();
1062 $res = $dbr->select(
1063 $revQuery['tables'],
1064 [ 'rev_user_text' => $revQuery['fields']['rev_user_text'] ],
1066 'rev_page' => $title->getArticleID(),
1067 $dbr->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0'
1074 if ( $res === false ) {
1075 // This page has no revisions, which is very weird
1079 $hasHistory = ( $res->numRows() > 1 );
1080 $row = $dbr->fetchObject( $res );
1082 if ( $row ) { // $row is false if the only contributor is hidden
1083 $onlyAuthor = $row->rev_user_text
;
1084 // Try to find a second contributor
1085 foreach ( $res as $row ) {
1086 if ( $row->rev_user_text
!= $onlyAuthor ) { // T24999
1087 $onlyAuthor = false;
1092 $onlyAuthor = false;
1095 // Generate the summary with a '$1' placeholder
1097 // The current revision is blank and the one before is also
1098 // blank. It's just not our lucky day
1099 $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
1101 if ( $onlyAuthor ) {
1102 $reason = wfMessage(
1106 )->inContentLanguage()->text();
1108 $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
1112 if ( $reason == '-' ) {
1113 // Allow these UI messages to be blanked out cleanly
1117 // Max content length = max comment length - length of the comment (excl. $1)
1118 $text = $content ?
$content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
1120 // Now replace the '$1' placeholder
1121 $reason = str_replace( '$1', $text, $reason );
1127 * Get the Content object that needs to be saved in order to undo all revisions
1128 * between $undo and $undoafter. Revisions must belong to the same page,
1129 * must exist and must not be deleted.
1133 * @param Revision $current The current text
1134 * @param Revision $undo The revision to undo
1135 * @param Revision $undoafter Must be an earlier revision than $undo
1137 * @return mixed Content on success, false on failure
1139 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
1140 $cur_content = $current->getContent();
1142 if ( empty( $cur_content ) ) {
1143 return false; // no page
1146 $undo_content = $undo->getContent();
1147 $undoafter_content = $undoafter->getContent();
1149 if ( !$undo_content ||
!$undoafter_content ) {
1150 return false; // no content to undo
1154 $this->checkModelID( $cur_content->getModel() );
1155 $this->checkModelID( $undo_content->getModel() );
1156 if ( $current->getId() !== $undo->getId() ) {
1157 // If we are undoing the most recent revision,
1158 // its ok to revert content model changes. However
1159 // if we are undoing a revision in the middle, then
1160 // doing that will be confusing.
1161 $this->checkModelID( $undoafter_content->getModel() );
1163 } catch ( MWException
$e ) {
1164 // If the revisions have different content models
1165 // just return false
1169 if ( $cur_content->equals( $undo_content ) ) {
1170 // No use doing a merge if it's just a straight revert.
1171 return $undoafter_content;
1174 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
1176 return $undone_content;
1180 * Get parser options suitable for rendering and caching the article
1182 * @deprecated since 1.32, use WikiPage::makeParserOptions() or
1183 * ParserOptions::newCanonical() instead.
1184 * @param IContextSource|User|string $context One of the following:
1185 * - IContextSource: Use the User and the Language of the provided
1187 * - User: Use the provided User object and $wgLang for the language,
1188 * so use an IContextSource object if possible.
1189 * - 'canonical': Canonical options (anonymous user with default
1190 * preferences and content language).
1192 * @throws MWException
1193 * @return ParserOptions
1195 public function makeParserOptions( $context ) {
1196 wfDeprecated( __METHOD__
, '1.32' );
1197 return ParserOptions
::newCanonical( $context );
1201 * Returns true for content models that support caching using the
1202 * ParserCache mechanism. See WikiPage::shouldCheckParserCache().
1206 * @return bool Always false.
1208 public function isParserCacheSupported() {
1213 * Returns true if this content model supports sections.
1214 * This default implementation returns false.
1216 * Content models that return true here should also implement
1217 * Content::getSection, Content::replaceSection, etc. to handle sections..
1219 * @return bool Always false.
1221 public function supportsSections() {
1226 * Returns true if this content model supports categories.
1227 * The default implementation returns true.
1229 * @return bool Always true.
1231 public function supportsCategories() {
1236 * Returns true if this content model supports redirects.
1237 * This default implementation returns false.
1239 * Content models that return true here should also implement
1240 * ContentHandler::makeRedirectContent to return a Content object.
1242 * @return bool Always false.
1244 public function supportsRedirects() {
1249 * Return true if this content model supports direct editing, such as via EditPage.
1251 * @return bool Default is false, and true for TextContent and it's derivatives.
1253 public function supportsDirectEditing() {
1258 * Whether or not this content model supports direct editing via ApiEditPage
1260 * @return bool Default is false, and true for TextContent and derivatives.
1262 public function supportsDirectApiEditing() {
1263 return $this->supportsDirectEditing();
1267 * Get fields definition for search index
1269 * @todo Expose title, redirect, namespace, text, source_text, text_bytes
1270 * field mappings here. (see T142670 and T143409)
1272 * @param SearchEngine $engine
1273 * @return SearchIndexField[] List of fields this content handler can provide.
1276 public function getFieldsForSearchIndex( SearchEngine
$engine ) {
1277 $fields['category'] = $engine->makeSearchFieldMapping(
1279 SearchIndexField
::INDEX_TYPE_TEXT
1281 $fields['category']->setFlag( SearchIndexField
::FLAG_CASEFOLD
);
1283 $fields['external_link'] = $engine->makeSearchFieldMapping(
1285 SearchIndexField
::INDEX_TYPE_KEYWORD
1288 $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1290 SearchIndexField
::INDEX_TYPE_KEYWORD
1293 $fields['template'] = $engine->makeSearchFieldMapping(
1295 SearchIndexField
::INDEX_TYPE_KEYWORD
1297 $fields['template']->setFlag( SearchIndexField
::FLAG_CASEFOLD
);
1299 $fields['content_model'] = $engine->makeSearchFieldMapping(
1301 SearchIndexField
::INDEX_TYPE_KEYWORD
1308 * Add new field definition to array.
1309 * @param SearchIndexField[] &$fields
1310 * @param SearchEngine $engine
1311 * @param string $name
1313 * @return SearchIndexField[] new field defs
1316 protected function addSearchField( &$fields, SearchEngine
$engine, $name, $type ) {
1317 $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1322 * Return fields to be indexed by search engine
1323 * as representation of this document.
1324 * Overriding class should call parent function or take care of calling
1325 * the SearchDataForIndex hook.
1326 * @param WikiPage $page Page to index
1327 * @param ParserOutput $output
1328 * @param SearchEngine $engine Search engine for which we are indexing
1329 * @return array Map of name=>value for fields
1332 public function getDataForSearchIndex(
1334 ParserOutput
$output,
1335 SearchEngine
$engine
1338 $content = $page->getContent();
1341 $searchDataExtractor = new ParserOutputSearchDataExtractor();
1343 $fieldData['category'] = $searchDataExtractor->getCategories( $output );
1344 $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output );
1345 $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output );
1346 $fieldData['template'] = $searchDataExtractor->getTemplates( $output );
1348 $text = $content->getTextForSearchIndex();
1350 $fieldData['text'] = $text;
1351 $fieldData['source_text'] = $text;
1352 $fieldData['text_bytes'] = $content->getSize();
1353 $fieldData['content_model'] = $content->getModel();
1356 Hooks
::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );
1361 * Produce page output suitable for indexing.
1363 * Specific content handlers may override it if they need different content handling.
1365 * @param WikiPage $page
1366 * @param ParserCache|null $cache
1367 * @return ParserOutput
1369 public function getParserOutputForIndexing( WikiPage
$page, ParserCache
$cache = null ) {
1370 $parserOptions = $page->makeParserOptions( 'canonical' );
1371 $revId = $page->getRevision()->getId();
1373 $parserOutput = $cache->get( $page, $parserOptions );
1375 if ( empty( $parserOutput ) ) {
1377 $page->getContent()->getParserOutput( $page->getTitle(), $revId, $parserOptions );
1379 $cache->save( $parserOutput, $page, $parserOptions );
1382 return $parserOutput;