| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
t |
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Extension\MultimediaViewer; |
| 25 |
|
| 26 |
use CategoryPage; |
| 27 |
use MediaWiki\Category\Category; |
| 28 |
use MediaWiki\Config\Config; |
| 29 |
use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook; |
| 30 |
use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 31 |
use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook; |
| 32 |
use MediaWiki\Output\OutputPage; |
| 33 |
use MediaWiki\Page\Hook\CategoryPageViewHook; |
| 34 |
use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 35 |
use MediaWiki\Registration\ExtensionRegistry; |
| 36 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook; |
| 37 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 38 |
use MediaWiki\User\Hook\UserGetDefaultOptionsHook; |
| 39 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 40 |
use MediaWiki\User\User; |
| 41 |
use MobileContext; |
| 42 |
use Skin; |
| 43 |
use ThumbnailImage; |
| 44 |
|
| 45 |
class Hooks implements |
| 46 |
MakeGlobalVariablesScriptHook, |
| 47 |
UserGetDefaultOptionsHook, |
| 48 |
GetPreferencesHook, |
| 49 |
BeforePageDisplayHook, |
| 50 |
CategoryPageViewHook, |
| 51 |
ResourceLoaderGetConfigVarsHook, |
| 52 |
ThumbnailBeforeProduceHTMLHook |
| 53 |
{ |
| 54 |
private Config $config; |
| 55 |
private SpecialPageFactory $specialPageFactory; |
| 56 |
private UserOptionsLookup $userOptionsLookup; |
| 57 |
private ?MobileContext $mobileContext; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct( |
| 65 |
Config $config, |
| 66 |
SpecialPageFactory $specialPageFactory, |
| 67 |
UserOptionsLookup $userOptionsLookup, |
| 68 |
?MobileContext $mobileContext |
| 69 |
) { |
| 70 |
$this->config = $config; |
| 71 |
$this->specialPageFactory = $specialPageFactory; |
| 72 |
$this->userOptionsLookup = $userOptionsLookup; |
| 73 |
$this->mobileContext = $mobileContext; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function onUserGetDefaultOptions( &$defaultOptions ) { |
| 81 |
if ( $this->config->get( 'MediaViewerEnableByDefault' ) ) { |
| 82 |
$defaultOptions['multimediaviewer-enable'] = 1; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected function shouldHandleClicks( User $performer ): bool { |
| 92 |
if ( $performer->isNamed() ) { |
| 93 |
return (bool)$this->userOptionsLookup->getOption( $performer, 'multimediaviewer-enable' ); |
| 94 |
} |
| 95 |
|
| 96 |
return (bool)( |
| 97 |
$this->config->get( 'MediaViewerEnableByDefaultForAnonymous' ) ?? |
| 98 |
$this->config->get( 'MediaViewerEnableByDefault' ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
protected function getModules( OutputPage $out ) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$isMobileFrontendView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) && |
| 112 |
$this->mobileContext && $this->mobileContext->shouldDisplayMobileView(); |
| 113 |
if ( !$isMobileFrontendView ) { |
| 114 |
$out->addModules( [ 'mmv.bootstrap' ] ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function onBeforePageDisplay( $out, $skin ): void { |
| 126 |
$pageHasThumbnails = count( $out->getFileSearchOptions() ) > 0; |
| 127 |
$pageIsFilePage = $out->getTitle()->inNamespace( NS_FILE ); |
| 128 |
|
| 129 |
$pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) && |
| 130 |
|
| 131 |
$out->getTitle()->getContentModel() === 'flow-board'; |
| 132 |
$fileRelatedSpecialPages = [ 'Newimages', 'Listfiles', 'Mostimages', |
| 133 |
'MostGloballyLinkedFiles', 'Uncategorizedimages', 'Unusedimages', 'Search' ]; |
| 134 |
$pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL ) |
| 135 |
&& in_array( $this->specialPageFactory->resolveAlias( $out->getTitle()->getDBkey() )[0], |
| 136 |
$fileRelatedSpecialPages ); |
| 137 |
|
| 138 |
if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) { |
| 139 |
$this->getModules( $out ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function onCategoryPageView( $catPage ) { |
| 149 |
$title = $catPage->getTitle(); |
| 150 |
$cat = Category::newFromTitle( $title ); |
| 151 |
if ( $cat->getFileCount() > 0 ) { |
| 152 |
$out = $catPage->getContext()->getOutput(); |
| 153 |
$this->getModules( $out ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function onGetPreferences( $user, &$prefs ) { |
| 164 |
$prefs['multimediaviewer-enable'] = [ |
| 165 |
'type' => 'toggle', |
| 166 |
'label-message' => 'multimediaviewer-optin-pref', |
| 167 |
'section' => 'rendering/files', |
| 168 |
]; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void { |
| 179 |
$vars['wgMediaViewer'] = true; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function onMakeGlobalVariablesScript( &$vars, $out ): void { |
| 190 |
$user = $out->getUser(); |
| 191 |
$isMultimediaViewerEnable = $this->userOptionsLookup->getDefaultOption( |
| 192 |
'multimediaviewer-enable', |
| 193 |
$user |
| 194 |
); |
| 195 |
|
| 196 |
$vars['wgMediaViewerOnClick'] = $this->shouldHandleClicks( $user ); |
| 197 |
|
| 198 |
$vars['wgMediaViewerEnabledByDefault'] = (bool)$isMultimediaViewerEnable; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function onThumbnailBeforeProduceHTML( |
| 209 |
$thumbnail, |
| 210 |
&$attribs, |
| 211 |
&$linkAttribs |
| 212 |
) { |
| 213 |
$file = $thumbnail->getFile(); |
| 214 |
|
| 215 |
if ( $file ) { |
| 216 |
$attribs['data-file-width'] = $file->getWidth(); |
| 217 |
$attribs['data-file-height'] = $file->getHeight(); |
| 218 |
} |
| 219 |
} |
| 220 |
} |