Skip to content

Commit

Permalink
Merge "Replace SetupAfterCache hook with MediaWikiServices"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Mar 3, 2025
2 parents ff24f07 + 6d740bc commit ce48825
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
8 changes: 8 additions & 0 deletions docs/topics/hooks-php.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ See also the [WikibaseClientEntityTypes](#WikibaseClientEntityTypes) hook.
Hook handlers may add additional definitions.
See [entitytypes documentation] for details.

This hook runs during early initialization;
its handlers must obey the [MediaWikiServicesHook rules](https://doc.wikimedia.org/mediawiki-core/master/php/interfaceMediaWiki_1_1Hook_1_1MediaWikiServicesHook.html),
i.e. not declare any service dependencies nor access any unsafe services dynamically.

Parameters:
* &$entityTypeDefinitions
* the array of entity type definitions, as defined by WikibaseLib.entitytypes.php.
Expand All @@ -46,6 +50,10 @@ Parameters:
#### WikibaseContentModelMapping {#WikibaseContentModelMapping}
Called by [WikibaseRepo::getContentModelMappings()] to allow additional mappings between Entity types and content model identifiers to be defined.

This hook runs during early initialization;
its handlers must obey the [MediaWikiServicesHook rules](https://doc.wikimedia.org/mediawiki-core/master/php/interfaceMediaWiki_1_1Hook_1_1MediaWikiServicesHook.html),
i.e. not declare any service dependencies nor access any unsafe services dynamically.

Parameters:
* &$map
* An associative array mapping Entity types to content model ids.
Expand Down
2 changes: 1 addition & 1 deletion extension-repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@
"LoadExtensionSchemaUpdates": "DatabaseSchemaUpdater",
"MaintenanceShellStart": "\\Wikibase\\Repo\\RepoHooks::onMaintenanceShellStart",
"MakeGlobalVariablesScript": "MakeGlobalVariablesScript",
"MediaWikiServices": "\\Wikibase\\Repo\\RepoHooks::onMediaWikiServices",
"NamespaceIsMovable": "\\Wikibase\\Repo\\RepoHooks::onNamespaceIsMovable",
"OutputPageBeforeHTML": "OutputPageBeforeHTML",
"OutputPageBodyAttributes": "\\Wikibase\\Repo\\RepoHooks::onOutputPageBodyAttributes",
Expand All @@ -1297,7 +1298,6 @@
"ViewHooks"
],
"RevisionFromEditComplete": "\\Wikibase\\Repo\\RepoHooks::onRevisionFromEditComplete",
"SetupAfterCache": "\\Wikibase\\Repo\\RepoHooks::onSetupAfterCache",
"ShowSearchHit": "ShowSearchHit",
"ShowSearchHitTitle": "ShowSearchHit",
"SidebarBeforeOutput": "\\Wikibase\\Repo\\RepoHooks::onSidebarBeforeOutput",
Expand Down
61 changes: 57 additions & 4 deletions repo/WikibaseRepo.ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,20 @@
},

'WikibaseRepo.ContentModelMappings' => function ( MediaWikiServices $services ): array {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
$map = WikibaseRepo::getEntityTypeDefinitions( $services )
->get( EntityTypeDefinitions::CONTENT_MODEL_ID );

$services->getHookContainer()
->run( 'WikibaseContentModelMapping', [ &$map ] );
$services->getHookContainer()->run(
'WikibaseContentModelMapping',
[ &$map ],
[ 'noServices' => true ] // early initialization
);

return $map;
},
Expand Down Expand Up @@ -862,6 +871,12 @@
},

'WikibaseRepo.EntityNamespaceLookup' => function ( MediaWikiServices $services ): EntityNamespaceLookup {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
$entitySources = array_filter(
WikibaseRepo::getEntitySourceDefinitions( $services )->getSources(),
function ( EntitySource $entitySource ) {
Expand Down Expand Up @@ -996,7 +1011,11 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
$baseEntityTypes
);

$services->getHookContainer()->run( 'WikibaseRepoEntityTypes', [ &$entityTypes ] );
$services->getHookContainer()->run(
'WikibaseRepoEntityTypes',
[ &$entityTypes ],
[ 'noServices' => true ] // to match WikibaseRepo.EntityTypeDefinitions below
);

$entityTypeDefinitionsBySourceType = [ DatabaseEntitySource::TYPE => new EntityTypeDefinitions( $entityTypes ) ];

Expand All @@ -1013,6 +1032,12 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
},

'WikibaseRepo.EntitySourceDefinitions' => function ( MediaWikiServices $services ): EntitySourceDefinitions {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
$settings = WikibaseRepo::getSettings( $services );
$subEntityTypesMapper = WikibaseRepo::getSubEntityTypesMapper( $services );

Expand Down Expand Up @@ -1084,6 +1109,12 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
},

'WikibaseRepo.EntityTypeDefinitions' => function ( MediaWikiServices $services ): EntityTypeDefinitions {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
$baseEntityTypes = require __DIR__ . '/../lib/WikibaseLib.entitytypes.php';
$repoEntityTypes = require __DIR__ . '/WikibaseRepo.entitytypes.php';

Expand All @@ -1092,7 +1123,11 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
$baseEntityTypes
);

$services->getHookContainer()->run( 'WikibaseRepoEntityTypes', [ &$entityTypes ] );
$services->getHookContainer()->run(
'WikibaseRepoEntityTypes',
[ &$entityTypes ],
[ 'noServices' => true ] // early initialization
);

return new EntityTypeDefinitions( $entityTypes );
},
Expand Down Expand Up @@ -1436,6 +1471,12 @@ function ( EntityNamespaceLookup $nsLookup, DatabaseEntitySource $source ): Enti
},

'WikibaseRepo.LocalEntitySource' => function ( MediaWikiServices $services ): EntitySource {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
$localEntitySourceName = WikibaseRepo::getSettings( $services )->getSetting( 'localEntitySourceName' );
$sources = WikibaseRepo::getEntitySourceDefinitions( $services )->getSources();
foreach ( $sources as $source ) {
Expand Down Expand Up @@ -1723,6 +1764,12 @@ function ( $types, $localTypeName ) use ( $subEntityTypes ) {
},

'WikibaseRepo.Settings' => function ( MediaWikiServices $services ): SettingsArray {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
return WikibaseSettings::getRepoSettings();
},

Expand Down Expand Up @@ -1864,6 +1911,12 @@ function ( $types, $localTypeName ) use ( $subEntityTypes ) {
},

'WikibaseRepo.SubEntityTypesMapper' => function ( MediaWikiServices $services ): SubEntityTypesMapper {
/**
* Warning: This is an early initialization service.
* We must not use any MediaWiki services here (except the HookContainer),
* and the same is true for any other Wikibase services we use;
* see the warning in {@link MediaWikiServicesHook::onMediaWikiServices()}.
*/
return new SubEntityTypesMapper( WikibaseRepo::getEntityTypeDefinitions( $services )
->get( EntityTypeDefinitions::SUB_ENTITY_TYPES ) );
},
Expand Down
18 changes: 10 additions & 8 deletions repo/includes/RepoHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,28 @@ public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
}

/**
* Handler for the SetupAfterCache hook, completing the content and namespace setup.
* Handler for the MediaWikiServices hook, completing the content and namespace setup.
* This updates the $wgContentHandlers and $wgNamespaceContentModels registries
* according to information provided by entity type definitions and the entityNamespaces
* setting for the local entity source.
* Note that we must not access any MediaWiki core services here (except for the hook container);
* see the warning in {@link \MediaWiki\Hook\MediaWikiServicesHook::onMediaWikiServices()}.
*/
public static function onSetupAfterCache() {
public static function onMediaWikiServices( MediaWikiServices $services ) {
global $wgContentHandlers,
$wgNamespaceContentModels;

if ( WikibaseRepo::getSettings()->getSetting( 'defaultEntityNamespaces' ) ) {
if ( WikibaseRepo::getSettings( $services )->getSetting( 'defaultEntityNamespaces' ) ) {
self::defaultEntityNamespaces();
}

$namespaces = WikibaseRepo::getLocalEntitySource()->getEntityNamespaceIds();
$namespaceLookup = WikibaseRepo::getEntityNamespaceLookup();
$namespaces = WikibaseRepo::getLocalEntitySource( $services )->getEntityNamespaceIds();
$namespaceLookup = WikibaseRepo::getEntityNamespaceLookup( $services );

// Register entity namespaces.
// Note that $wgExtraNamespaces and $wgNamespaceAliases have already been processed at this
// point and should no longer be touched.
$contentModelIds = WikibaseRepo::getContentModelMappings();
$contentModelIds = WikibaseRepo::getContentModelMappings( $services );

foreach ( $namespaces as $entityType => $namespace ) {
// TODO: once there is a mechanism for registering the default content model for
Expand All @@ -151,8 +153,8 @@ public static function onSetupAfterCache() {

// Register callbacks for instantiating ContentHandlers for EntityContent.
foreach ( $contentModelIds as $entityType => $model ) {
$wgContentHandlers[$model] = function () use ( $entityType ) {
$entityContentFactory = WikibaseRepo::getEntityContentFactory();
$wgContentHandlers[$model] = function () use ( $services, $entityType ) {
$entityContentFactory = WikibaseRepo::getEntityContentFactory( $services );
return $entityContentFactory->getContentHandlerForType( $entityType );
};
}
Expand Down
4 changes: 2 additions & 2 deletions repo/tests/phpunit/includes/RepoHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private function setupTestOnContentModelCanBeUsedOn() {
$this->assertSame( 'someSlot', $nsLookup->getEntitySlotRole( $type ) );
}

public function testOnSetupAfterCache() {
public function testOnMediaWikiServices() {

global $wgWBRepoSettings, $wgNamespaceContentModels, $wgContentHandlers;

Expand Down Expand Up @@ -488,7 +488,7 @@ public function testOnSetupAfterCache() {
$contentModelMappings
);

RepoHooks::onSetupAfterCache();
RepoHooks::onMediaWikiServices( $this->getServiceContainer() );

$this->assertSame( [ WB_NS_ITEM => 'wikibase-item' ], $wgNamespaceContentModels );
$this->assertSame( array_values( $contentModelMappings ), array_keys( $wgContentHandlers ) );
Expand Down

0 comments on commit ce48825

Please sign in to comment.