Use HookHandlers for core hooks

The use of "HookHandlers" attribute in extension.json makes it possible
to inject services into hook handler classes in a future patch.

Bug: T271019
Change-Id: I57dac8d590b5afa7524000d93c8477a10148c052
This commit is contained in:
Umherirrender 2023-08-14 20:47:02 +02:00
parent 5ed21318bc
commit ad1f6a6ee0
2 changed files with 32 additions and 15 deletions

View file

@ -417,13 +417,13 @@
] ]
}, },
"Hooks": { "Hooks": {
"UserGetDefaultOptions": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onUserGetDefaultOptions", "UserGetDefaultOptions": "main",
"GetPreferences": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onGetPreferences", "GetPreferences": "main",
"BeforePageDisplay": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onBeforePageDisplay", "BeforePageDisplay": "main",
"CategoryPageView": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onCategoryPageView", "CategoryPageView": "main",
"ResourceLoaderGetConfigVars": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onResourceLoaderGetConfigVars", "ResourceLoaderGetConfigVars": "main",
"MakeGlobalVariablesScript": "main", "MakeGlobalVariablesScript": "main",
"ThumbnailBeforeProduceHTML": "MediaWiki\\Extension\\MultimediaViewer\\Hooks::onThumbnailBeforeProduceHTML" "ThumbnailBeforeProduceHTML": "main"
}, },
"HookHandlers": { "HookHandlers": {
"main": { "main": {

View file

@ -24,17 +24,32 @@
namespace MediaWiki\Extension\MultimediaViewer; namespace MediaWiki\Extension\MultimediaViewer;
use CategoryPage; use CategoryPage;
use Config;
use ExtensionRegistry; use ExtensionRegistry;
use MediaWiki\Category\Category; use MediaWiki\Category\Category;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\MakeGlobalVariablesScriptHook; use MediaWiki\Hook\MakeGlobalVariablesScriptHook;
use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Page\Hook\CategoryPageViewHook;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
use MediaWiki\User\UserOptionsLookup; use MediaWiki\User\UserOptionsLookup;
use OutputPage; use OutputPage;
use Skin; use Skin;
use ThumbnailImage; use ThumbnailImage;
use User; use User;
class Hooks implements MakeGlobalVariablesScriptHook { class Hooks implements
MakeGlobalVariablesScriptHook,
UserGetDefaultOptionsHook,
GetPreferencesHook,
BeforePageDisplayHook,
CategoryPageViewHook,
ResourceLoaderGetConfigVarsHook,
ThumbnailBeforeProduceHTMLHook
{
/** Link to more information about this module */ /** Link to more information about this module */
protected static $infoLink = protected static $infoLink =
'https://mediawiki.org/wiki/Special:MyLanguage/Extension:Media_Viewer/About'; 'https://mediawiki.org/wiki/Special:MyLanguage/Extension:Media_Viewer/About';
@ -63,7 +78,7 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions * @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions
* @param array &$defaultOptions * @param array &$defaultOptions
*/ */
public static function onUserGetDefaultOptions( array &$defaultOptions ) { public function onUserGetDefaultOptions( &$defaultOptions ) {
global $wgMediaViewerEnableByDefault; global $wgMediaViewerEnableByDefault;
if ( $wgMediaViewerEnableByDefault ) { if ( $wgMediaViewerEnableByDefault ) {
@ -111,7 +126,7 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* @param OutputPage $out * @param OutputPage $out
* @param Skin $skin * @param Skin $skin
*/ */
public static function onBeforePageDisplay( OutputPage $out, $skin ) { public function onBeforePageDisplay( $out, $skin ): void {
$pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0; $pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
$pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE ); $pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
// TODO: Have Flow work out if there are any images on the page // TODO: Have Flow work out if there are any images on the page
@ -139,7 +154,7 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* Add JavaScript to the page if there are images in the category * Add JavaScript to the page if there are images in the category
* @param CategoryPage $catPage * @param CategoryPage $catPage
*/ */
public static function onCategoryPageView( CategoryPage $catPage ) { public function onCategoryPageView( $catPage ) {
$title = $catPage->getTitle(); $title = $catPage->getTitle();
$cat = Category::newFromTitle( $title ); $cat = Category::newFromTitle( $title );
if ( $cat->getFileCount() > 0 ) { if ( $cat->getFileCount() > 0 ) {
@ -154,7 +169,7 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* @param User $user * @param User $user
* @param array &$prefs * @param array &$prefs
*/ */
public static function onGetPreferences( $user, &$prefs ) { public function onGetPreferences( $user, &$prefs ) {
$prefs['multimediaviewer-enable'] = [ $prefs['multimediaviewer-enable'] = [
'type' => 'toggle', 'type' => 'toggle',
'label-message' => 'multimediaviewer-optin-pref', 'label-message' => 'multimediaviewer-optin-pref',
@ -166,8 +181,10 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
* Export variables used in both PHP and JS to keep DRY * Export variables used in both PHP and JS to keep DRY
* @param array &$vars * @param array &$vars
* @param string $skin
* @param Config $config
*/ */
public static function onResourceLoaderGetConfigVars( array &$vars ) { public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
global $wgMediaViewerUseThumbnailGuessing, $wgMediaViewerExtensions, global $wgMediaViewerUseThumbnailGuessing, $wgMediaViewerExtensions,
$wgMediaViewerImageQueryParameter, $wgMediaViewerRecordVirtualViewBeaconURI; $wgMediaViewerImageQueryParameter, $wgMediaViewerRecordVirtualViewBeaconURI;
@ -206,9 +223,9 @@ class Hooks implements MakeGlobalVariablesScriptHook {
* @param array &$attribs Attributes of the <img> element * @param array &$attribs Attributes of the <img> element
* @param array|bool &$linkAttribs Attributes of the wrapping <a> element * @param array|bool &$linkAttribs Attributes of the wrapping <a> element
*/ */
public static function onThumbnailBeforeProduceHTML( public function onThumbnailBeforeProduceHTML(
ThumbnailImage $thumbnail, $thumbnail,
array &$attribs, &$attribs,
&$linkAttribs &$linkAttribs
) { ) {
$file = $thumbnail->getFile(); $file = $thumbnail->getFile();