3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Search\ParserOutputSearchDataExtractor
;
7 * Base class for content handling.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
29 * @author Daniel Kinzler
32 * A content handler knows how do deal with a specific type of content on a wiki
33 * page. Content is stored in the database in a serialized form (using a
34 * serialization format a.k.a. MIME type) and is unserialized into its native
35 * PHP representation (the content model), which is wrapped in an instance of
36 * the appropriate subclass of Content.
38 * ContentHandler instances are stateless singletons that serve, among other
39 * things, as a factory for Content objects. Generally, there is one subclass
40 * of ContentHandler and one subclass of Content for every type of content model.
42 * Some content types have a flat model, that is, their native representation
43 * is the same as their serialized form. Examples would be JavaScript and CSS
44 * code. As of now, this also applies to wikitext (MediaWiki's default content
45 * type), but wikitext content may be represented by a DOM or AST structure in
50 abstract class ContentHandler
{
52 * Convenience function for getting flat text from a Content object. This
53 * should only be used in the context of backwards compatibility with code
54 * that is not yet able to handle Content objects!
56 * If $content is null, this method returns the empty string.
58 * If $content is an instance of TextContent, this method returns the flat
59 * text as returned by $content->getNativeData().
61 * If $content is not a TextContent object, the behavior of this method
62 * depends on the global $wgContentHandlerTextFallback:
63 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
64 * TextContent object, an MWException is thrown.
65 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
66 * TextContent object, $content->serialize() is called to get a string
67 * form of the content.
68 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
69 * TextContent object, this method returns null.
70 * - otherwise, the behavior is undefined.
74 * @param Content|null $content
76 * @throws MWException If the content is not an instance of TextContent and
77 * wgContentHandlerTextFallback was set to 'fail'.
78 * @return string|null Textual form of the content, if available.
80 public static function getContentText( Content
$content = null ) {
81 global $wgContentHandlerTextFallback;
83 if ( is_null( $content ) ) {
87 if ( $content instanceof TextContent
) {
88 return $content->getNativeData();
91 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
93 if ( $wgContentHandlerTextFallback == 'fail' ) {
94 throw new MWException(
95 "Attempt to get text from Content with model " .
100 if ( $wgContentHandlerTextFallback == 'serialize' ) {
101 return $content->serialize();
108 * Convenience function for creating a Content object from a given textual
111 * $text will be deserialized into a Content object of the model specified
112 * by $modelId (or, if that is not given, $title->getContentModel()) using
117 * @param string $text The textual representation, will be
118 * unserialized to create the Content object
119 * @param Title|null $title The title of the page this text belongs to.
120 * Required if $modelId is not provided.
121 * @param string|null $modelId The model to deserialize to. If not provided,
122 * $title->getContentModel() is used.
123 * @param string|null $format The format to use for deserialization. If not
124 * given, the model's default format is used.
126 * @throws MWException If model ID or format is not supported or if the text can not be
127 * unserialized using the format.
128 * @return Content A Content object representing the text.
130 public static function makeContent( $text, Title
$title = null,
131 $modelId = null, $format = null ) {
132 if ( is_null( $modelId ) ) {
133 if ( is_null( $title ) ) {
134 throw new MWException( "Must provide a Title object or a content model ID." );
137 $modelId = $title->getContentModel();
140 $handler = self
::getForModelID( $modelId );
142 return $handler->unserializeContent( $text, $format );
146 * Returns the name of the default content model to be used for the page
147 * with the given title.
149 * Note: There should rarely be need to call this method directly.
150 * To determine the actual content model for a given page, use
151 * Title::getContentModel().
153 * Which model is to be used by default for the page is determined based
154 * on several factors:
155 * - The global setting $wgNamespaceContentModels specifies a content model
157 * - The hook ContentHandlerDefaultModelFor may be used to override the page's default
159 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
160 * model if they end in .js or .css, respectively.
161 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
162 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
163 * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor
164 * hook should be used instead if possible.
165 * - The hook TitleIsWikitextPage may be used to force a page to use the
166 * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor
167 * hook should be used instead if possible.
169 * If none of the above applies, the wikitext model is used.
171 * Note: this is used by, and may thus not use, Title::getContentModel()
175 * @param Title $title
177 * @return string Default model name for the page given by $title
179 public static function getDefaultModelFor( Title
$title ) {
180 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
181 // because it is used to initialize the mContentModel member.
183 $ns = $title->getNamespace();
187 $model = MWNamespace
::getNamespaceContentModel( $ns );
189 // Hook can determine default model
190 if ( !Hooks
::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
191 if ( !is_null( $model ) ) {
196 // Could this page contain code based on the title?
197 $isCodePage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
202 // Is this a user subpage containing code?
203 $isCodeSubpage = NS_USER
== $ns
205 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
206 if ( $isCodeSubpage ) {
210 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
211 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
212 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
214 if ( !$isWikitext ) {
217 return CONTENT_MODEL_JAVASCRIPT
;
219 return CONTENT_MODEL_CSS
;
221 return CONTENT_MODEL_JSON
;
223 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
227 // We established that it must be wikitext
229 return CONTENT_MODEL_WIKITEXT
;
233 * Returns the appropriate ContentHandler singleton for the given title.
237 * @param Title $title
239 * @return ContentHandler
241 public static function getForTitle( Title
$title ) {
242 $modelId = $title->getContentModel();
244 return self
::getForModelID( $modelId );
248 * Returns the appropriate ContentHandler singleton for the given Content
253 * @param Content $content
255 * @return ContentHandler
257 public static function getForContent( Content
$content ) {
258 $modelId = $content->getModel();
260 return self
::getForModelID( $modelId );
264 * @var array A Cache of ContentHandler instances by model id
266 protected static $handlers;
269 * Returns the ContentHandler singleton for the given model ID. Use the
270 * CONTENT_MODEL_XXX constants to identify the desired content model.
272 * ContentHandler singletons are taken from the global $wgContentHandlers
273 * array. Keys in that array are model names, the values are either
274 * ContentHandler singleton objects, or strings specifying the appropriate
275 * subclass of ContentHandler.
277 * If a class name is encountered when looking up the singleton for a given
278 * model name, the class is instantiated and the class name is replaced by
279 * the resulting singleton in $wgContentHandlers.
281 * If no ContentHandler is defined for the desired $modelId, the
282 * ContentHandler may be provided by the ContentHandlerForModelID hook.
283 * If no ContentHandler can be determined, an MWException is raised.
287 * @param string $modelId The ID of the content model for which to get a
288 * handler. Use CONTENT_MODEL_XXX constants.
290 * @throws MWException For internal errors and problems in the configuration.
291 * @throws MWUnknownContentModelException If no handler is known for the model ID.
292 * @return ContentHandler The ContentHandler singleton for handling the model given by the ID.
294 public static function getForModelID( $modelId ) {
295 global $wgContentHandlers;
297 if ( isset( self
::$handlers[$modelId] ) ) {
298 return self
::$handlers[$modelId];
301 if ( empty( $wgContentHandlers[$modelId] ) ) {
304 Hooks
::run( 'ContentHandlerForModelID', [ $modelId, &$handler ] );
306 if ( $handler === null ) {
307 throw new MWUnknownContentModelException( $modelId );
310 if ( !( $handler instanceof ContentHandler
) ) {
311 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
314 $classOrCallback = $wgContentHandlers[$modelId];
316 if ( is_callable( $classOrCallback ) ) {
317 $handler = call_user_func( $classOrCallback, $modelId );
319 $handler = new $classOrCallback( $modelId );
322 if ( !( $handler instanceof ContentHandler
) ) {
323 throw new MWException( "$classOrCallback from \$wgContentHandlers is not " .
324 "compatible with ContentHandler" );
328 wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
329 . ': ' . get_class( $handler ) );
331 self
::$handlers[$modelId] = $handler;
333 return self
::$handlers[$modelId];
337 * Clean up handlers cache.
339 public static function cleanupHandlersCache() {
340 self
::$handlers = [];
344 * Returns the localized name for a given content model.
346 * Model names are localized using system messages. Message keys
347 * have the form content-model-$name, where $name is getContentModelName( $id ).
349 * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX
350 * constant or returned by Revision::getContentModel().
351 * @param Language|null $lang The language to parse the message in (since 1.26)
353 * @throws MWException If the model ID isn't known.
354 * @return string The content model's localized name.
356 public static function getLocalizedName( $name, Language
$lang = null ) {
357 // Messages: content-model-wikitext, content-model-text,
358 // content-model-javascript, content-model-css
359 $key = "content-model-$name";
361 $msg = wfMessage( $key );
363 $msg->inLanguage( $lang );
366 return $msg->exists() ?
$msg->plain() : $name;
369 public static function getContentModels() {
370 global $wgContentHandlers;
372 $models = array_keys( $wgContentHandlers );
373 Hooks
::run( 'GetContentModels', [ &$models ] );
377 public static function getAllContentFormats() {
378 global $wgContentHandlers;
382 foreach ( $wgContentHandlers as $model => $class ) {
383 $handler = self
::getForModelID( $model );
384 $formats = array_merge( $formats, $handler->getSupportedFormats() );
387 $formats = array_unique( $formats );
392 // ------------------------------------------------------------------------
402 protected $mSupportedFormats;
405 * Constructor, initializing the ContentHandler instance with its model ID
406 * and a list of supported formats. Values for the parameters are typically
407 * provided as literals by subclass's constructors.
409 * @param string $modelId (use CONTENT_MODEL_XXX constants).
410 * @param string[] $formats List for supported serialization formats
411 * (typically as MIME types)
413 public function __construct( $modelId, $formats ) {
414 $this->mModelID
= $modelId;
415 $this->mSupportedFormats
= $formats;
419 * Serializes a Content object of the type supported by this ContentHandler.
423 * @param Content $content The Content object to serialize
424 * @param string|null $format The desired serialization format
426 * @return string Serialized form of the content
428 abstract public function serializeContent( Content
$content, $format = null );
431 * Applies transformations on export (returns the blob unchanged per default).
432 * Subclasses may override this to perform transformations such as conversion
433 * of legacy formats or filtering of internal meta-data.
435 * @param string $blob The blob to be exported
436 * @param string|null $format The blob's serialization format
440 public function exportTransform( $blob, $format = null ) {
445 * Unserializes a Content object of the type supported by this ContentHandler.
449 * @param string $blob Serialized form of the content
450 * @param string|null $format The format used for serialization
452 * @return Content The Content object created by deserializing $blob
454 abstract public function unserializeContent( $blob, $format = null );
457 * Apply import transformation (per default, returns $blob unchanged).
458 * This gives subclasses an opportunity to transform data blobs on import.
462 * @param string $blob
463 * @param string|null $format
467 public function importTransform( $blob, $format = null ) {
472 * Creates an empty Content object of the type supported by this
479 abstract public function makeEmptyContent();
482 * Creates a new Content object that acts as a redirect to the given page,
483 * or null if redirects are not supported by this content model.
485 * This default implementation always returns null. Subclasses supporting redirects
486 * must override this method.
488 * Note that subclasses that override this method to return a Content object
489 * should also override supportsRedirects() to return true.
493 * @param Title $destination The page to redirect to.
494 * @param string $text Text to include in the redirect, if possible.
496 * @return Content Always null.
498 public function makeRedirectContent( Title
$destination, $text = '' ) {
503 * Returns the model id that identifies the content model this
504 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
508 * @return string The model ID
510 public function getModelID() {
511 return $this->mModelID
;
517 * @param string $model_id The model to check
519 * @throws MWException If the model ID is not the ID of the content model supported by this
522 protected function checkModelID( $model_id ) {
523 if ( $model_id !== $this->mModelID
) {
524 throw new MWException( "Bad content model: " .
525 "expected {$this->mModelID} " .
526 "but got $model_id." );
531 * Returns a list of serialization formats supported by the
532 * serializeContent() and unserializeContent() methods of this
537 * @return string[] List of serialization formats as MIME type like strings
539 public function getSupportedFormats() {
540 return $this->mSupportedFormats
;
544 * The format used for serialization/deserialization by default by this
547 * This default implementation will return the first element of the array
548 * of formats that was passed to the constructor.
552 * @return string The name of the default serialization format as a MIME type
554 public function getDefaultFormat() {
555 return $this->mSupportedFormats
[0];
559 * Returns true if $format is a serialization format supported by this
560 * ContentHandler, and false otherwise.
562 * Note that if $format is null, this method always returns true, because
563 * null means "use the default format".
567 * @param string $format The serialization format to check
571 public function isSupportedFormat( $format ) {
573 return true; // this means "use the default"
576 return in_array( $format, $this->mSupportedFormats
);
580 * Convenient for checking whether a format provided as a parameter is actually supported.
582 * @param string $format The serialization format to check
584 * @throws MWException If the format is not supported by this content handler.
586 protected function checkFormat( $format ) {
587 if ( !$this->isSupportedFormat( $format ) ) {
588 throw new MWException(
589 "Format $format is not supported for content model "
590 . $this->getModelID()
596 * Returns overrides for action handlers.
597 * Classes listed here will be used instead of the default one when
598 * (and only when) $wgActions[$action] === true. This allows subclasses
599 * to override the default action handlers.
603 * @return array An array mapping action names (typically "view", "edit", "history" etc.) to
604 * either the full qualified class name of an Action class, a callable taking ( Page $page,
605 * IContextSource $context = null ) as parameters and returning an Action object, or an actual
606 * Action object. An empty array in this default implementation.
608 * @see Action::factory
610 public function getActionOverrides() {
615 * Factory for creating an appropriate DifferenceEngine for this content model.
617 * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The
618 * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or
620 * (Note that in older versions of MediaWiki the hook documentation instructed extensions
621 * to return false from the hook; you should not rely on always being able to decorate
622 * the DifferenceEngine instance from the hook. If the owner of the content type wants to
623 * decorare the instance, overriding this method is a safer approach.)
627 * @param IContextSource $context Context to use, anything else will be ignored.
628 * @param int $old Revision ID we want to show and diff with.
629 * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'.
630 * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0.
631 * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false.
632 * @param bool $unhide If set, allow viewing deleted revs. Defaults to false.
634 * @return DifferenceEngine
636 public function createDifferenceEngine( IContextSource
$context, $old = 0, $new = 0,
637 $rcid = 0, // FIXME: Deprecated, no longer used
638 $refreshCache = false, $unhide = false
640 $diffEngineClass = $this->getDiffEngineClass();
641 $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
642 Hooks
::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
643 &$differenceEngine ] );
644 return $differenceEngine;
648 * Get the language in which the content of the given page is written.
650 * This default implementation just returns the content language (except for pages
651 * in the MediaWiki namespace)
653 * Note that the pages language is not cacheable, since it may in some
654 * cases depend on user settings.
656 * Also note that the page language may or may not depend on the actual content of the page,
657 * that is, this method may load the content in order to determine the language.
661 * @param Title $title The page to determine the language for.
662 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
664 * @return Language The page's language
666 public function getPageLanguage( Title
$title, Content
$content = null ) {
668 $pageLang = MediaWikiServices
::getInstance()->getContentLanguage();
670 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
671 // Parse mediawiki messages with correct target language
672 list( /* $unused */, $lang ) = MessageCache
::singleton()->figureMessage( $title->getText() );
673 $pageLang = Language
::factory( $lang );
676 Hooks
::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
678 return wfGetLangObj( $pageLang );
682 * Get the language in which the content of this page is written when
683 * viewed by user. Defaults to $this->getPageLanguage(), but if the user
684 * specified a preferred variant, the variant will be used.
686 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
687 * the user specified a preferred variant.
689 * Note that the pages view language is not cacheable, since it depends on user settings.
691 * Also note that the page language may or may not depend on the actual content of the page,
692 * that is, this method may load the content in order to determine the language.
696 * @param Title $title The page to determine the language for.
697 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
699 * @return Language The page's language for viewing
701 public function getPageViewLanguage( Title
$title, Content
$content = null ) {
702 $pageLang = $this->getPageLanguage( $title, $content );
704 if ( $title->getNamespace() !== NS_MEDIAWIKI
) {
705 // If the user chooses a variant, the content is actually
706 // in a language whose code is the variant code.
707 $variant = $pageLang->getPreferredVariant();
708 if ( $pageLang->getCode() !== $variant ) {
709 $pageLang = Language
::factory( $variant );
717 * Determines whether the content type handled by this ContentHandler
718 * can be used on the given page.
720 * This default implementation always returns true.
721 * Subclasses may override this to restrict the use of this content model to specific locations,
722 * typically based on the namespace or some other aspect of the title, such as a special suffix
723 * (e.g. ".svg" for SVG content).
725 * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
726 * content model can be used where.
728 * @param Title $title The page's title.
730 * @return bool True if content of this kind can be used on the given page, false otherwise.
732 public function canBeUsedOn( Title
$title ) {
735 Hooks
::run( 'ContentModelCanBeUsedOn', [ $this->getModelID(), $title, &$ok ] );
741 * Returns the name of the diff engine to use.
747 protected function getDiffEngineClass() {
748 return DifferenceEngine
::class;
752 * Attempts to merge differences between three versions. Returns a new
753 * Content object for a clean merge and false for failure or a conflict.
755 * This default implementation always returns false.
759 * @param Content $oldContent The page's previous content.
760 * @param Content $myContent One of the page's conflicting contents.
761 * @param Content $yourContent One of the page's conflicting contents.
763 * @return Content|bool Always false.
765 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
770 * Return type of change if one exists for the given edit.
774 * @param Content|null $oldContent The previous text of the page.
775 * @param Content|null $newContent The submitted text of the page.
776 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
778 * @return string|null String key representing type of change, or null.
780 private function getChangeType(
781 Content
$oldContent = null,
782 Content
$newContent = null,
785 $oldTarget = $oldContent !== null ?
$oldContent->getRedirectTarget() : null;
786 $newTarget = $newContent !== null ?
$newContent->getRedirectTarget() : null;
788 // We check for the type of change in the given edit, and return string key accordingly
790 // Blanking of a page
791 if ( $oldContent && $oldContent->getSize() > 0 &&
792 $newContent && $newContent->getSize() === 0
800 // New redirect page (by creating new page or by changing content page)
801 return 'new-redirect';
802 } elseif ( !$newTarget->equals( $oldTarget ) ||
803 $oldTarget->getFragment() !== $newTarget->getFragment()
805 // Redirect target changed
806 return 'changed-redirect-target';
808 } elseif ( $oldTarget ) {
809 // Changing an existing redirect into a non-redirect
810 return 'removed-redirect';
814 if ( $flags & EDIT_NEW
&& $newContent ) {
815 if ( $newContent->getSize() === 0 ) {
823 // Removing more than 90% of the page
824 if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
828 // Content model changed
829 if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
830 return 'contentmodelchange';
837 * Return an applicable auto-summary if one exists for the given edit.
841 * @param Content|null $oldContent The previous text of the page.
842 * @param Content|null $newContent The submitted text of the page.
843 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
845 * @return string An appropriate auto-summary, or an empty string.
847 public function getAutosummary(
848 Content
$oldContent = null,
849 Content
$newContent = null,
852 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
854 // There's no applicable auto-summary for our case, so our auto-summary is empty.
855 if ( !$changeType ) {
859 // Decide what kind of auto-summary is needed.
860 switch ( $changeType ) {
862 $newTarget = $newContent->getRedirectTarget();
863 $truncatedtext = $newContent->getTextForSummary(
865 - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
866 - strlen( $newTarget->getFullText() )
869 return wfMessage( 'autoredircomment', $newTarget->getFullText() )
870 ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
871 case 'changed-redirect-target':
872 $oldTarget = $oldContent->getRedirectTarget();
873 $newTarget = $newContent->getRedirectTarget();
875 $truncatedtext = $newContent->getTextForSummary(
877 - strlen( wfMessage( 'autosumm-changed-redirect-target' )
878 ->inContentLanguage()->text() )
879 - strlen( $oldTarget->getFullText() )
880 - strlen( $newTarget->getFullText() )
883 return wfMessage( 'autosumm-changed-redirect-target',
884 $oldTarget->getFullText(),
885 $newTarget->getFullText() )
886 ->rawParams( $truncatedtext )->inContentLanguage()->text();
887 case 'removed-redirect':
888 $oldTarget = $oldContent->getRedirectTarget();
889 $truncatedtext = $newContent->getTextForSummary(
891 - strlen( wfMessage( 'autosumm-removed-redirect' )
892 ->inContentLanguage()->text() )
893 - strlen( $oldTarget->getFullText() ) );
895 return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
896 ->rawParams( $truncatedtext )->inContentLanguage()->text();
898 // If they're making a new article, give its text, truncated, in the summary.
899 $truncatedtext = $newContent->getTextForSummary(
900 200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
902 return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
903 ->inContentLanguage()->text();
905 return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
907 $truncatedtext = $newContent->getTextForSummary(
908 200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
910 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
911 ->inContentLanguage()->text();
913 return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
920 * Return an applicable tag if one exists for the given edit or return null.
924 * @param Content|null $oldContent The previous text of the page.
925 * @param Content|null $newContent The submitted text of the page.
926 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
928 * @return string|null An appropriate tag, or null.
930 public function getChangeTag(
931 Content
$oldContent = null,
932 Content
$newContent = null,
935 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
937 // There's no applicable tag for this change.
938 if ( !$changeType ) {
942 // Core tags use the same keys as ones returned from $this->getChangeType()
943 // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
944 // if this type of change should be tagged
945 $tag = 'mw-' . $changeType;
947 // Not all change types are tagged, so we check against the list of defined tags.
948 if ( in_array( $tag, ChangeTags
::getSoftwareTags() ) ) {
956 * Auto-generates a deletion reason
960 * @param Title $title The page's title
961 * @param bool &$hasHistory Whether the page has a history
963 * @return mixed String containing deletion reason or empty string, or
964 * boolean false if no revision occurred
966 * @todo &$hasHistory is extremely ugly, it's here because
967 * WikiPage::getAutoDeleteReason() and Article::generateReason()
970 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
971 $dbr = wfGetDB( DB_REPLICA
);
973 // Get the last revision
974 $rev = Revision
::newFromTitle( $title );
976 if ( is_null( $rev ) ) {
980 // Get the article's contents
981 $content = $rev->getContent();
984 // If the page is blank, use the text from the previous revision,
985 // which can only be blank if there's a move/import/protect dummy
987 if ( !$content ||
$content->isEmpty() ) {
988 $prev = $rev->getPrevious();
992 $content = $rev->getContent();
997 $this->checkModelID( $rev->getContentModel() );
999 // Find out if there was only one contributor
1000 // Only scan the last 20 revisions
1001 $revQuery = Revision
::getQueryInfo();
1002 $res = $dbr->select(
1003 $revQuery['tables'],
1004 [ 'rev_user_text' => $revQuery['fields']['rev_user_text'] ],
1006 'rev_page' => $title->getArticleID(),
1007 $dbr->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0'
1014 if ( $res === false ) {
1015 // This page has no revisions, which is very weird
1019 $hasHistory = ( $res->numRows() > 1 );
1020 $row = $dbr->fetchObject( $res );
1022 if ( $row ) { // $row is false if the only contributor is hidden
1023 $onlyAuthor = $row->rev_user_text
;
1024 // Try to find a second contributor
1025 foreach ( $res as $row ) {
1026 if ( $row->rev_user_text
!= $onlyAuthor ) { // T24999
1027 $onlyAuthor = false;
1032 $onlyAuthor = false;
1035 // Generate the summary with a '$1' placeholder
1037 // The current revision is blank and the one before is also
1038 // blank. It's just not our lucky day
1039 $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
1041 if ( $onlyAuthor ) {
1042 $reason = wfMessage(
1046 )->inContentLanguage()->text();
1048 $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
1052 if ( $reason == '-' ) {
1053 // Allow these UI messages to be blanked out cleanly
1057 // Max content length = max comment length - length of the comment (excl. $1)
1058 $text = $content ?
$content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
1060 // Now replace the '$1' placeholder
1061 $reason = str_replace( '$1', $text, $reason );
1067 * Get the Content object that needs to be saved in order to undo all revisions
1068 * between $undo and $undoafter. Revisions must belong to the same page,
1069 * must exist and must not be deleted.
1073 * @param Revision $current The current text
1074 * @param Revision $undo The revision to undo
1075 * @param Revision $undoafter Must be an earlier revision than $undo
1077 * @return mixed Content on success, false on failure
1079 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
1080 $cur_content = $current->getContent();
1082 if ( empty( $cur_content ) ) {
1083 return false; // no page
1086 $undo_content = $undo->getContent();
1087 $undoafter_content = $undoafter->getContent();
1089 if ( !$undo_content ||
!$undoafter_content ) {
1090 return false; // no content to undo
1094 $this->checkModelID( $cur_content->getModel() );
1095 $this->checkModelID( $undo_content->getModel() );
1096 if ( $current->getId() !== $undo->getId() ) {
1097 // If we are undoing the most recent revision,
1098 // its ok to revert content model changes. However
1099 // if we are undoing a revision in the middle, then
1100 // doing that will be confusing.
1101 $this->checkModelID( $undoafter_content->getModel() );
1103 } catch ( MWException
$e ) {
1104 // If the revisions have different content models
1105 // just return false
1109 if ( $cur_content->equals( $undo_content ) ) {
1110 // No use doing a merge if it's just a straight revert.
1111 return $undoafter_content;
1114 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
1116 return $undone_content;
1120 * Get parser options suitable for rendering and caching the article
1122 * @deprecated since 1.32, use WikiPage::makeParserOptions() or
1123 * ParserOptions::newCanonical() instead.
1124 * @param IContextSource|User|string $context One of the following:
1125 * - IContextSource: Use the User and the Language of the provided
1127 * - User: Use the provided User object and $wgLang for the language,
1128 * so use an IContextSource object if possible.
1129 * - 'canonical': Canonical options (anonymous user with default
1130 * preferences and content language).
1132 * @throws MWException
1133 * @return ParserOptions
1135 public function makeParserOptions( $context ) {
1136 wfDeprecated( __METHOD__
, '1.32' );
1137 return ParserOptions
::newCanonical( $context );
1141 * Returns true for content models that support caching using the
1142 * ParserCache mechanism. See WikiPage::shouldCheckParserCache().
1146 * @return bool Always false.
1148 public function isParserCacheSupported() {
1153 * Returns true if this content model supports sections.
1154 * This default implementation returns false.
1156 * Content models that return true here should also implement
1157 * Content::getSection, Content::replaceSection, etc. to handle sections..
1159 * @return bool Always false.
1161 public function supportsSections() {
1166 * Returns true if this content model supports categories.
1167 * The default implementation returns true.
1169 * @return bool Always true.
1171 public function supportsCategories() {
1176 * Returns true if this content model supports redirects.
1177 * This default implementation returns false.
1179 * Content models that return true here should also implement
1180 * ContentHandler::makeRedirectContent to return a Content object.
1182 * @return bool Always false.
1184 public function supportsRedirects() {
1189 * Return true if this content model supports direct editing, such as via EditPage.
1191 * @return bool Default is false, and true for TextContent and it's derivatives.
1193 public function supportsDirectEditing() {
1198 * Whether or not this content model supports direct editing via ApiEditPage
1200 * @return bool Default is false, and true for TextContent and derivatives.
1202 public function supportsDirectApiEditing() {
1203 return $this->supportsDirectEditing();
1207 * Get fields definition for search index
1209 * @todo Expose title, redirect, namespace, text, source_text, text_bytes
1210 * field mappings here. (see T142670 and T143409)
1212 * @param SearchEngine $engine
1213 * @return SearchIndexField[] List of fields this content handler can provide.
1216 public function getFieldsForSearchIndex( SearchEngine
$engine ) {
1217 $fields['category'] = $engine->makeSearchFieldMapping(
1219 SearchIndexField
::INDEX_TYPE_TEXT
1221 $fields['category']->setFlag( SearchIndexField
::FLAG_CASEFOLD
);
1223 $fields['external_link'] = $engine->makeSearchFieldMapping(
1225 SearchIndexField
::INDEX_TYPE_KEYWORD
1228 $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1230 SearchIndexField
::INDEX_TYPE_KEYWORD
1233 $fields['template'] = $engine->makeSearchFieldMapping(
1235 SearchIndexField
::INDEX_TYPE_KEYWORD
1237 $fields['template']->setFlag( SearchIndexField
::FLAG_CASEFOLD
);
1239 $fields['content_model'] = $engine->makeSearchFieldMapping(
1241 SearchIndexField
::INDEX_TYPE_KEYWORD
1248 * Add new field definition to array.
1249 * @param SearchIndexField[] &$fields
1250 * @param SearchEngine $engine
1251 * @param string $name
1253 * @return SearchIndexField[] new field defs
1256 protected function addSearchField( &$fields, SearchEngine
$engine, $name, $type ) {
1257 $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1262 * Return fields to be indexed by search engine
1263 * as representation of this document.
1264 * Overriding class should call parent function or take care of calling
1265 * the SearchDataForIndex hook.
1266 * @param WikiPage $page Page to index
1267 * @param ParserOutput $output
1268 * @param SearchEngine $engine Search engine for which we are indexing
1269 * @return array Map of name=>value for fields
1272 public function getDataForSearchIndex(
1274 ParserOutput
$output,
1275 SearchEngine
$engine
1278 $content = $page->getContent();
1281 $searchDataExtractor = new ParserOutputSearchDataExtractor();
1283 $fieldData['category'] = $searchDataExtractor->getCategories( $output );
1284 $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output );
1285 $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output );
1286 $fieldData['template'] = $searchDataExtractor->getTemplates( $output );
1288 $text = $content->getTextForSearchIndex();
1290 $fieldData['text'] = $text;
1291 $fieldData['source_text'] = $text;
1292 $fieldData['text_bytes'] = $content->getSize();
1293 $fieldData['content_model'] = $content->getModel();
1296 Hooks
::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );
1301 * Produce page output suitable for indexing.
1303 * Specific content handlers may override it if they need different content handling.
1305 * @param WikiPage $page
1306 * @param ParserCache|null $cache
1307 * @return ParserOutput
1309 public function getParserOutputForIndexing( WikiPage
$page, ParserCache
$cache = null ) {
1310 $parserOptions = $page->makeParserOptions( 'canonical' );
1311 $revId = $page->getRevision()->getId();
1313 $parserOutput = $cache->get( $page, $parserOptions );
1315 if ( empty( $parserOutput ) ) {
1317 $page->getContent()->getParserOutput( $page->getTitle(), $revId, $parserOptions );
1319 $cache->save( $parserOutput, $page, $parserOptions );
1322 return $parserOutput;