2013-08-07 08:59:08 +00:00
|
|
|
<?php
|
2018-04-14 07:49:21 +00:00
|
|
|
/**
|
2013-08-07 08:59:08 +00:00
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
2013-11-23 01:18:25 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-08-07 08:59:08 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup extensions
|
|
|
|
* @author Mark Holmquist <mtraceur@member.fsf.org>
|
|
|
|
* @copyright Copyright © 2013, Mark Holmquist
|
|
|
|
*/
|
2021-11-26 11:09:23 +00:00
|
|
|
|
2022-02-06 15:20:58 +00:00
|
|
|
namespace MediaWiki\Extension\MultimediaViewer;
|
|
|
|
|
|
|
|
use CategoryPage;
|
|
|
|
use ExtensionRegistry;
|
2023-05-12 14:33:11 +00:00
|
|
|
use MediaWiki\Category\Category;
|
2023-10-23 20:10:29 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-08-14 18:47:02 +00:00
|
|
|
use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
|
2024-06-09 16:50:10 +00:00
|
|
|
use MediaWiki\Output\Hook\BeforePageDisplayHook;
|
|
|
|
use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook;
|
2023-10-23 20:10:29 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-08-14 18:47:02 +00:00
|
|
|
use MediaWiki\Page\Hook\CategoryPageViewHook;
|
|
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
|
|
|
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
|
2023-09-22 22:03:01 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPageFactory;
|
2023-08-14 18:47:02 +00:00
|
|
|
use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
|
2023-11-29 12:39:36 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-10-23 20:10:29 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-04-02 19:34:12 +00:00
|
|
|
use MobileContext;
|
2022-02-06 15:20:58 +00:00
|
|
|
use Skin;
|
|
|
|
use ThumbnailImage;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2023-08-14 18:47:02 +00:00
|
|
|
class Hooks implements
|
|
|
|
MakeGlobalVariablesScriptHook,
|
|
|
|
UserGetDefaultOptionsHook,
|
|
|
|
GetPreferencesHook,
|
|
|
|
BeforePageDisplayHook,
|
|
|
|
CategoryPageViewHook,
|
|
|
|
ResourceLoaderGetConfigVarsHook,
|
|
|
|
ThumbnailBeforeProduceHTMLHook
|
|
|
|
{
|
2023-10-23 20:53:13 +00:00
|
|
|
/** @var string Link to more information about this module */
|
2017-05-19 13:26:17 +00:00
|
|
|
protected static $infoLink =
|
2018-04-14 07:38:17 +00:00
|
|
|
'https://mediawiki.org/wiki/Special:MyLanguage/Extension:Media_Viewer/About';
|
2013-10-29 20:26:07 +00:00
|
|
|
|
2023-10-23 20:53:13 +00:00
|
|
|
/** @var string Link to a page where this module can be discussed */
|
2017-05-19 13:26:17 +00:00
|
|
|
protected static $discussionLink =
|
2018-04-14 07:38:17 +00:00
|
|
|
'https://mediawiki.org/wiki/Special:MyLanguage/Extension_talk:Media_Viewer/About';
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2023-10-23 20:53:13 +00:00
|
|
|
/** @var string Link to help about this module */
|
2018-04-14 07:38:17 +00:00
|
|
|
protected static $helpLink =
|
|
|
|
'https://mediawiki.org/wiki/Special:MyLanguage/Help:Extension:Media_Viewer';
|
2014-03-24 13:35:34 +00:00
|
|
|
|
2023-10-23 20:28:32 +00:00
|
|
|
private Config $config;
|
2023-09-22 22:03:01 +00:00
|
|
|
private SpecialPageFactory $specialPageFactory;
|
2023-10-23 20:14:57 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
2024-04-02 19:34:12 +00:00
|
|
|
private ?MobileContext $mobileContext;
|
2021-11-26 11:09:23 +00:00
|
|
|
|
|
|
|
/**
|
2023-10-23 20:28:32 +00:00
|
|
|
* @param Config $config
|
2023-09-22 22:03:01 +00:00
|
|
|
* @param SpecialPageFactory $specialPageFactory
|
2023-10-23 20:14:57 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2021-11-26 11:09:23 +00:00
|
|
|
*/
|
2023-09-22 22:03:01 +00:00
|
|
|
public function __construct(
|
2023-10-23 20:28:32 +00:00
|
|
|
Config $config,
|
2023-10-23 20:14:57 +00:00
|
|
|
SpecialPageFactory $specialPageFactory,
|
2024-04-02 19:34:12 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup,
|
|
|
|
?MobileContext $mobileContext
|
2023-09-22 22:03:01 +00:00
|
|
|
) {
|
2023-10-23 20:28:32 +00:00
|
|
|
$this->config = $config;
|
2023-09-22 22:03:01 +00:00
|
|
|
$this->specialPageFactory = $specialPageFactory;
|
2023-10-23 20:14:57 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2024-04-02 19:34:12 +00:00
|
|
|
$this->mobileContext = $mobileContext;
|
2021-11-26 11:09:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 12:25:16 +00:00
|
|
|
/**
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetDefaultOptions
|
|
|
|
* @param array &$defaultOptions
|
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onUserGetDefaultOptions( &$defaultOptions ) {
|
2023-10-23 20:28:32 +00:00
|
|
|
if ( $this->config->get( 'MediaViewerEnableByDefault' ) ) {
|
2016-10-18 16:59:36 +00:00
|
|
|
$defaultOptions['multimediaviewer-enable'] = 1;
|
2017-05-02 21:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 01:56:56 +00:00
|
|
|
/**
|
|
|
|
* Checks the context for whether to load the viewer.
|
2023-07-04 10:41:49 +00:00
|
|
|
* @param User $performer
|
2014-03-07 01:56:56 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2023-07-04 10:41:49 +00:00
|
|
|
protected function shouldHandleClicks( User $performer ): bool {
|
|
|
|
if ( $performer->isNamed() ) {
|
2021-11-26 11:09:23 +00:00
|
|
|
return (bool)$this->userOptionsLookup->getOption( $performer, 'multimediaviewer-enable' );
|
2014-03-14 00:25:33 +00:00
|
|
|
}
|
2022-09-29 13:58:13 +00:00
|
|
|
|
2023-10-23 20:28:32 +00:00
|
|
|
return (bool)(
|
|
|
|
$this->config->get( 'MediaViewerEnableByDefaultForAnonymous' ) ??
|
|
|
|
$this->config->get( 'MediaViewerEnableByDefault' )
|
|
|
|
);
|
2014-03-07 01:56:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-04 21:40:31 +00:00
|
|
|
/**
|
|
|
|
* Handler for all places where we add the modules
|
|
|
|
* Could be on article pages or on Category pages
|
2019-05-10 12:30:44 +00:00
|
|
|
* @param OutputPage $out
|
2013-10-25 23:27:01 +00:00
|
|
|
*/
|
2024-04-02 19:34:12 +00:00
|
|
|
protected function getModules( OutputPage $out ) {
|
2019-10-31 22:35:59 +00:00
|
|
|
// The MobileFrontend extension provides its own implementation of MultimediaViewer.
|
|
|
|
// See https://phabricator.wikimedia.org/T65504 and subtasks for more details.
|
|
|
|
// To avoid loading MMV twice, we check the environment we are running in.
|
|
|
|
$isMobileFrontendView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
|
2024-04-02 19:34:12 +00:00
|
|
|
$this->mobileContext && $this->mobileContext->shouldDisplayMobileView();
|
2019-10-31 22:35:59 +00:00
|
|
|
if ( !$isMobileFrontendView ) {
|
|
|
|
$out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
|
|
|
|
}
|
2013-11-04 21:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
2013-11-04 21:40:31 +00:00
|
|
|
* Add JavaScript to the page when an image is on it
|
2019-05-08 18:21:45 +00:00
|
|
|
* and the user has enabled the feature
|
2019-05-10 12:30:44 +00:00
|
|
|
* @param OutputPage $out
|
|
|
|
* @param Skin $skin
|
2013-11-04 21:40:31 +00:00
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
2014-12-22 20:42:49 +00:00
|
|
|
$pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0;
|
|
|
|
$pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE );
|
2019-03-11 12:26:03 +00:00
|
|
|
// TODO: Have Flow work out if there are any images on the page
|
|
|
|
$pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) &&
|
|
|
|
// CONTENT_MODEL_FLOW_BOARD
|
|
|
|
$out->getTitle()->getContentModel() === 'flow-board';
|
2023-09-22 22:03:01 +00:00
|
|
|
$fileRelatedSpecialPages = [ 'Newimages', 'Listfiles', 'Mostimages',
|
|
|
|
'MostGloballyLinkedFiles', 'Uncategorizedimages', 'Unusedimages', 'Search' ];
|
2014-12-22 20:42:49 +00:00
|
|
|
$pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL )
|
2023-09-22 22:03:01 +00:00
|
|
|
&& in_array( $this->specialPageFactory->resolveAlias( $out->getTitle()->getDBkey() )[0],
|
|
|
|
$fileRelatedSpecialPages );
|
2014-12-22 20:42:49 +00:00
|
|
|
|
2019-03-11 12:26:03 +00:00
|
|
|
if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
|
2024-04-02 19:34:12 +00:00
|
|
|
$this->getModules( $out );
|
2013-11-04 21:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/CategoryPageView
|
2013-11-04 21:40:31 +00:00
|
|
|
* Add JavaScript to the page if there are images in the category
|
2019-05-10 12:30:44 +00:00
|
|
|
* @param CategoryPage $catPage
|
2013-11-04 21:40:31 +00:00
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onCategoryPageView( $catPage ) {
|
2013-11-04 21:40:31 +00:00
|
|
|
$title = $catPage->getTitle();
|
|
|
|
$cat = Category::newFromTitle( $title );
|
|
|
|
if ( $cat->getFileCount() > 0 ) {
|
|
|
|
$out = $catPage->getContext()->getOutput();
|
2024-04-02 19:34:12 +00:00
|
|
|
$this->getModules( $out );
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 15:31:59 +00:00
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
2019-05-08 18:21:45 +00:00
|
|
|
* Adds a default-enabled preference to gate the feature
|
2017-10-07 15:31:59 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param array &$prefs
|
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onGetPreferences( $user, &$prefs ) {
|
2019-05-08 18:21:45 +00:00
|
|
|
$prefs['multimediaviewer-enable'] = [
|
|
|
|
'type' => 'toggle',
|
|
|
|
'label-message' => 'multimediaviewer-optin-pref',
|
|
|
|
'section' => 'rendering/files',
|
|
|
|
];
|
2014-03-14 00:25:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 20:26:07 +00:00
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
|
2013-10-29 20:26:07 +00:00
|
|
|
* Export variables used in both PHP and JS to keep DRY
|
2017-10-07 15:31:59 +00:00
|
|
|
* @param array &$vars
|
2023-08-14 18:47:02 +00:00
|
|
|
* @param string $skin
|
|
|
|
* @param Config $config
|
2013-10-29 20:26:07 +00:00
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
|
2016-12-04 18:14:09 +00:00
|
|
|
$vars['wgMultimediaViewer'] = [
|
2013-10-29 20:26:07 +00:00
|
|
|
'infoLink' => self::$infoLink,
|
|
|
|
'discussionLink' => self::$discussionLink,
|
2014-03-24 13:35:34 +00:00
|
|
|
'helpLink' => self::$helpLink,
|
2023-10-23 20:28:32 +00:00
|
|
|
'useThumbnailGuessing' => (bool)$this->config->get( 'MediaViewerUseThumbnailGuessing' ),
|
2024-08-09 13:39:15 +00:00
|
|
|
'thumbnailBucketSizes' => $this->config->get( 'MediaViewerThumbnailBucketSizes' ),
|
2023-10-23 20:28:32 +00:00
|
|
|
'imageQueryParameter' => $this->config->get( 'MediaViewerImageQueryParameter' ),
|
|
|
|
'recordVirtualViewBeaconURI' => $this->config->get( 'MediaViewerRecordVirtualViewBeaconURI' ),
|
|
|
|
'extensions' => $this->config->get( 'MediaViewerExtensions' ),
|
2016-12-04 18:14:09 +00:00
|
|
|
];
|
2014-03-14 22:19:26 +00:00
|
|
|
$vars['wgMediaViewer'] = true;
|
2013-10-29 20:26:07 +00:00
|
|
|
}
|
2013-11-27 21:57:45 +00:00
|
|
|
|
2014-03-27 22:46:51 +00:00
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
|
2014-03-27 22:46:51 +00:00
|
|
|
* Export variables which depend on the current user
|
2017-10-07 15:31:59 +00:00
|
|
|
* @param array &$vars
|
2014-03-27 22:46:51 +00:00
|
|
|
* @param OutputPage $out
|
2021-11-26 11:09:23 +00:00
|
|
|
* @return void
|
2014-03-27 22:46:51 +00:00
|
|
|
*/
|
2021-11-26 11:09:23 +00:00
|
|
|
public function onMakeGlobalVariablesScript( &$vars, $out ): void {
|
2014-03-27 22:46:51 +00:00
|
|
|
$user = $out->getUser();
|
2024-01-04 12:07:24 +00:00
|
|
|
$isMultimediaViewerEnable = $this->userOptionsLookup->getDefaultOption(
|
|
|
|
'multimediaviewer-enable',
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
2021-11-26 11:09:23 +00:00
|
|
|
$vars['wgMediaViewerOnClick'] = $this->shouldHandleClicks( $user );
|
2019-05-08 18:21:45 +00:00
|
|
|
// needed because of T71942; could be different for anon and logged-in
|
2021-11-26 11:09:23 +00:00
|
|
|
$vars['wgMediaViewerEnabledByDefault'] = (bool)$isMultimediaViewerEnable;
|
2014-03-27 22:46:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 10:06:17 +00:00
|
|
|
/**
|
2019-05-10 12:25:16 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ThumbnailBeforeProduceHTML
|
2014-03-28 10:06:17 +00:00
|
|
|
* Modify thumbnail DOM
|
|
|
|
* @param ThumbnailImage $thumbnail
|
2017-10-07 15:31:59 +00:00
|
|
|
* @param array &$attribs Attributes of the <img> element
|
|
|
|
* @param array|bool &$linkAttribs Attributes of the wrapping <a> element
|
2014-03-28 10:06:17 +00:00
|
|
|
*/
|
2023-08-14 18:47:02 +00:00
|
|
|
public function onThumbnailBeforeProduceHTML(
|
|
|
|
$thumbnail,
|
|
|
|
&$attribs,
|
2017-05-19 13:26:17 +00:00
|
|
|
&$linkAttribs
|
|
|
|
) {
|
2014-03-28 10:06:17 +00:00
|
|
|
$file = $thumbnail->getFile();
|
|
|
|
|
|
|
|
if ( $file ) {
|
2021-10-19 15:13:13 +00:00
|
|
|
$attribs['data-file-width'] = $file->getWidth();
|
|
|
|
$attribs['data-file-height'] = $file->getHeight();
|
2014-03-28 10:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|