use Wikimedia\Assert\Assert;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
+use MediaWiki\Revision\SlotRenderingProvider;
use MediaWiki\Search\ParserOutputSearchDataExtractor;
/**
* 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 );
}
/**
/**
* 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,
* @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.
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 [];
+ }
+
}