Remove HWLDFWordAccumulator, deprecated in 1.28
[lhc/web/wiklou.git] / includes / content / ContentHandler.php
index 7a378b3..decbb0c 100644 (file)
@@ -28,6 +28,7 @@
 use Wikimedia\Assert\Assert;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
+use MediaWiki\Revision\SlotRenderingProvider;
 use MediaWiki\Search\ParserOutputSearchDataExtractor;
 
 /**
@@ -58,7 +59,7 @@ abstract class ContentHandler {
         * If $content is null, this method returns the empty string.
         *
         * If $content is an instance of TextContent, this method returns the flat
-        * text as returned by $content->getNativeData().
+        * text as returned by $content->getText().
         *
         * If $content is not a TextContent object, the behavior of this method
         * depends on the global $wgContentHandlerTextFallback:
@@ -87,7 +88,7 @@ abstract class ContentHandler {
                }
 
                if ( $content instanceof TextContent ) {
-                       return $content->getNativeData();
+                       return $content->getText();
                }
 
                wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
@@ -173,62 +174,17 @@ abstract class ContentHandler {
         * Note: this is used by, and may thus not use, Title::getContentModel()
         *
         * @since 1.21
+        * @deprecated since 1.33, use SlotRoleHandler::getDefaultModel() together with
+        * SlotRoleRegistry::getRoleHandler().
         *
         * @param Title $title
         *
         * @return string Default model name for the page given by $title
         */
        public static function getDefaultModelFor( Title $title ) {
-               // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
-               //       because it is used to initialize the mContentModel member.
-
-               $ns = $title->getNamespace();
-
-               $ext = false;
-               $m = null;
-               $model = MWNamespace::getNamespaceContentModel( $ns );
-
-               // Hook can determine default model
-               if ( !Hooks::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
-                       if ( !is_null( $model ) ) {
-                               return $model;
-                       }
-               }
-
-               // Could this page contain code based on the title?
-               $isCodePage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
-               if ( $isCodePage ) {
-                       $ext = $m[1];
-               }
-
-               // Is this a user subpage containing code?
-               $isCodeSubpage = NS_USER == $ns
-                       && !$isCodePage
-                       && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
-               if ( $isCodeSubpage ) {
-                       $ext = $m[1];
-               }
-
-               // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
-               $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
-               $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
-
-               if ( !$isWikitext ) {
-                       switch ( $ext ) {
-                               case 'js':
-                                       return CONTENT_MODEL_JAVASCRIPT;
-                               case 'css':
-                                       return CONTENT_MODEL_CSS;
-                               case 'json':
-                                       return CONTENT_MODEL_JSON;
-                               default:
-                                       return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
-                       }
-               }
-
-               // We established that it must be wikitext
-
-               return CONTENT_MODEL_WIKITEXT;
+               $slotRoleregistry = MediaWikiServices::getInstance()->getSlotRoleRegistry();
+               $mainSlotHandler = $slotRoleregistry->getRoleHandler( 'main' );
+               return $mainSlotHandler->getDefaultModel( $title );
        }
 
        /**
@@ -711,7 +667,7 @@ abstract class ContentHandler {
         * This default implementation just returns the content language (except for pages
         * in the MediaWiki namespace)
         *
-        * Note that the pages language is not cacheable, since it may in some
+        * Note that the page's language is not cacheable, since it may in some
         * cases depend on user settings.
         *
         * Also note that the page language may or may not depend on the actual content of the page,
@@ -728,7 +684,7 @@ abstract class ContentHandler {
                global $wgLang;
                $pageLang = MediaWikiServices::getInstance()->getContentLanguage();
 
-               if ( $title->getNamespace() == NS_MEDIAWIKI ) {
+               if ( $title->inNamespace( NS_MEDIAWIKI ) ) {
                        // Parse mediawiki messages with correct target language
                        list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $title->getText() );
                        $pageLang = Language::factory( $lang );
@@ -776,7 +732,7 @@ abstract class ContentHandler {
 
        /**
         * Determines whether the content type handled by this ContentHandler
-        * can be used on the given page.
+        * can be used for the main slot of the given page.
         *
         * This default implementation always returns true.
         * Subclasses may override this to restrict the use of this content model to specific locations,
@@ -786,6 +742,8 @@ abstract class ContentHandler {
         * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
         * content model can be used where.
         *
+        * @see SlotRoleHandler::isAllowedModel
+        *
         * @param Title $title The page's title.
         *
         * @return bool True if content of this kind can be used on the given page, false otherwise.
@@ -1410,4 +1368,75 @@ abstract class ContentHandler {
                return $parserOutput;
        }
 
+       /**
+        * Returns a list of DeferrableUpdate objects for recording information about the
+        * given Content in some secondary data store.
+        *
+        * Application logic should not call this method directly. Instead, it should call
+        * DerivedPageDataUpdater::getSecondaryDataUpdates().
+        *
+        * @note Implementations must not return a LinksUpdate instance. Instead, a LinksUpdate
+        * is created by the calling code in DerivedPageDataUpdater, on the combined ParserOutput
+        * of all slots, not for each slot individually. This is in contrast to the old
+        * getSecondaryDataUpdates method defined by AbstractContent, which returned a LinksUpdate.
+        *
+        * @note Implementations should not call $content->getParserOutput, they should call
+        * $slotOutput->getSlotRendering( $role, false ) instead if they need to access a ParserOutput
+        * of $content. This allows existing ParserOutput objects to be re-used, while avoiding
+        * creating a ParserOutput when none is needed.
+        *
+        * @param Title $title The title of the page to supply the updates for
+        * @param Content $content The content to generate data updates for.
+        * @param string $role The role (slot) in which the content is being used. Which updates
+        *        are performed should generally not depend on the role the content has, but the
+        *        DeferrableUpdates themselves may need to know the role, to track to which slot the
+        *        data refers, and to avoid overwriting data of the same kind from another slot.
+        * @param SlotRenderingProvider $slotOutput A provider that can be used to gain access to
+        *        a ParserOutput of $content by calling $slotOutput->getSlotParserOutput( $role, false ).
+        * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information
+        *        about this content object somewhere. The default implementation returns an empty
+        *        array.
+        * @since 1.32
+        */
+       public function getSecondaryDataUpdates(
+               Title $title,
+               Content $content,
+               $role,
+               SlotRenderingProvider $slotOutput
+       ) {
+               return [];
+       }
+
+       /**
+        * Returns a list of DeferrableUpdate objects for removing information about content
+        * in some secondary data store. This is used when a page is deleted, and also when
+        * a slot is removed from a page.
+        *
+        * Application logic should not call this method directly. Instead, it should call
+        * WikiPage::getSecondaryDataUpdates().
+        *
+        * @note Implementations must not return a LinksDeletionUpdate instance. Instead, a
+        * LinksDeletionUpdate is created by the calling code in WikiPage.
+        * This is in contrast to the old getDeletionUpdates method defined by AbstractContent,
+        * which returned a LinksUpdate.
+        *
+        * @note Implementations should not rely on the page's current content, but rather the current
+        * state of the secondary data store.
+        *
+        * @param Title $title The title of the page to supply the updates for
+        * @param string $role The role (slot) in which the content is being used. Which updates
+        *        are performed should generally not depend on the role the content has, but the
+        *        DeferrableUpdates themselves may need to know the role, to track to which slot the
+        *        data refers, and to avoid overwriting data of the same kind from another slot.
+        *
+        * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information
+        *        about this content object somewhere. The default implementation returns an empty
+        *        array.
+        *
+        * @since 1.32
+        */
+       public function getDeletionUpdates( Title $title, $role ) {
+               return [];
+       }
+
 }