| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Minerva; |
| 22 |
|
| 23 |
use ChangesList; |
| 24 |
use ChangesListFilterGroup; |
| 25 |
use DifferenceEngine; |
| 26 |
use MediaWiki\Config\Config; |
| 27 |
use MediaWiki\Diff\Hook\DifferenceEngineViewHeaderHook; |
| 28 |
use MediaWiki\Hook\FetchChangesListHook; |
| 29 |
use MediaWiki\Hook\PreferencesGetLayoutHook; |
| 30 |
use MediaWiki\Hook\UserLogoutCompleteHook; |
| 31 |
use MediaWiki\Html\Html; |
| 32 |
use MediaWiki\Minerva\Skins\SkinMinerva; |
| 33 |
use MediaWiki\Output\Hook\OutputPageBodyAttributesHook; |
| 34 |
use MediaWiki\Output\OutputPage; |
| 35 |
use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 36 |
use MediaWiki\Registration\ExtensionRegistry; |
| 37 |
use MediaWiki\ResourceLoader\Context; |
| 38 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook; |
| 39 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook; |
| 40 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 41 |
use MediaWiki\Skins\Hook\SkinPageReadyConfigHook; |
| 42 |
use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook; |
| 43 |
use MediaWiki\SpecialPage\SpecialPage; |
| 44 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 45 |
use MediaWiki\User\User; |
| 46 |
use MobileContext; |
| 47 |
use OldChangesList; |
| 48 |
use Skin; |
| 49 |
use Wikimedia\Rdbms\ConfiguredReadOnlyMode; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
class Hooks implements |
| 58 |
DifferenceEngineViewHeaderHook, |
| 59 |
FetchChangesListHook, |
| 60 |
GetPreferencesHook, |
| 61 |
OutputPageBodyAttributesHook, |
| 62 |
PreferencesGetLayoutHook, |
| 63 |
ResourceLoaderGetConfigVarsHook, |
| 64 |
ResourceLoaderRegisterModulesHook, |
| 65 |
SkinPageReadyConfigHook, |
| 66 |
SpecialPageBeforeExecuteHook, |
| 67 |
UserLogoutCompleteHook |
| 68 |
{ |
| 69 |
public const FEATURE_OVERFLOW_PAGE_ACTIONS = 'MinervaOverflowInPageActions'; |
| 70 |
|
| 71 |
private ConfiguredReadOnlyMode $configuredReadOnlyMode; |
| 72 |
private SkinOptions $skinOptions; |
| 73 |
private UserOptionsLookup $userOptionsLookup; |
| 74 |
private ?MobileContext $mobileContext; |
| 75 |
|
| 76 |
public function __construct( |
| 77 |
ConfiguredReadOnlyMode $configuredReadOnlyMode, |
| 78 |
SkinOptions $skinOptions, |
| 79 |
UserOptionsLookup $userOptionsLookup, |
| 80 |
?MobileContext $mobileContext |
| 81 |
) { |
| 82 |
$this->configuredReadOnlyMode = $configuredReadOnlyMode; |
| 83 |
$this->skinOptions = $skinOptions; |
| 84 |
$this->userOptionsLookup = $userOptionsLookup; |
| 85 |
$this->mobileContext = $mobileContext; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void { |
| 102 |
if ( !ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) { |
| 103 |
$resourceLoader->register( [ |
| 104 |
'mobile.startup' => [ |
| 105 |
'dependencies' => [ 'mediawiki.searchSuggest' ], |
| 106 |
'localBasePath' => dirname( __DIR__ ), |
| 107 |
'remoteExtPath' => 'Minerva', |
| 108 |
'scripts' => 'resources/mobile.startup.stub.js', |
| 109 |
] |
| 110 |
] ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function onGetPreferences( $user, &$prefs ): void { |
| 121 |
$minervaPrefs = [ |
| 122 |
'minerva-theme' => [ |
| 123 |
'type' => 'api' |
| 124 |
], |
| 125 |
]; |
| 126 |
|
| 127 |
$prefs += $minervaPrefs; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function onPreferencesGetLayout( &$useMobileLayout, $skinName, $skinProperties = [] ) { |
| 141 |
if ( $skinName === 'minerva' ) { |
| 142 |
$useMobileLayout = true; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function onFetchChangesList( $user, $skin, &$list, $groups ) { |
| 157 |
if ( $skin->getSkinName() === 'minerva' ) { |
| 158 |
|
| 159 |
$list = new OldChangesList( $skin->getContext(), $groups ); |
| 160 |
|
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function onSpecialPageBeforeExecute( $special, $subpage ) { |
| 177 |
$name = $special->getName(); |
| 178 |
if ( !in_array( $name, [ 'Recentchanges', 'Userlogin', 'CreateAccount' ] ) ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
$skin = $special->getSkin(); |
| 182 |
if ( !$skin instanceof SkinMinerva ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
$request = $special->getRequest(); |
| 186 |
if ( $name === 'Recentchanges' ) { |
| 187 |
$isEnhancedDefaultForUser = $this->userOptionsLookup |
| 188 |
->getBoolOption( $special->getUser(), 'usenewrc' ); |
| 189 |
$enhanced = $request->getBool( 'enhanced', $isEnhancedDefaultForUser ); |
| 190 |
if ( $enhanced ) { |
| 191 |
$special->getOutput()->addHTML( Html::warningBox( |
| 192 |
$special->msg( 'skin-minerva-recentchanges-warning-enhanced-not-supported' )->parse() |
| 193 |
) ); |
| 194 |
} |
| 195 |
} else { |
| 196 |
|
| 197 |
|
| 198 |
if ( |
| 199 |
!$request->getCheck( 'warning' ) && |
| 200 |
!$request->getCheck( 'notice' ) && |
| 201 |
!$special->getUser()->isRegistered() && |
| 202 |
!$request->wasPosted() |
| 203 |
) { |
| 204 |
$request->setVal( 'notice', 'mobile-frontend-generic-login-new' ); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function onUserLogoutComplete( $user, &$inject_html, $oldName ) { |
| 219 |
if ( $this->mobileContext ) { |
| 220 |
$this->skinOptions->setMinervaSkinOptions( $this->mobileContext, $this->mobileContext->getSkin() ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void { |
| 235 |
if ( $skin === 'minerva' ) { |
| 236 |
|
| 237 |
|
| 238 |
$roConf = $this->configuredReadOnlyMode; |
| 239 |
$vars += [ |
| 240 |
'wgMinervaABSamplingRate' => $config->get( 'MinervaABSamplingRate' ), |
| 241 |
'wgMinervaReadOnly' => $roConf->isReadOnly(), |
| 242 |
]; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function onOutputPageBodyAttributes( $out, $skin, &$bodyAttrs ): void { |
| 257 |
$classes = $out->getProperty( 'bodyClassName' ); |
| 258 |
$isMinerva = $skin instanceof SkinMinerva; |
| 259 |
|
| 260 |
if ( $isMinerva && $this->skinOptions->get( SkinOptions::HISTORY_IN_PAGE_ACTIONS ) ) { |
| 261 |
|
| 262 |
$classes .= ' minerva--history-page-action-enabled'; |
| 263 |
} |
| 264 |
|
| 265 |
if ( $isMinerva ) { |
| 266 |
$bodyAttrs['class'] .= ' ' . $classes; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function onSkinPageReadyConfig( |
| 277 |
Context $context, |
| 278 |
array &$config |
| 279 |
): void { |
| 280 |
if ( $context->getSkin() === 'minerva' ) { |
| 281 |
$config['search'] = false; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
$config['collapsible'] = true; |
| 286 |
$config['selectorLogoutLink'] = 'a.menu__item--logout[data-mw="interface"]'; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function onDifferenceEngineViewHeader( $differenceEngine ) { |
| 296 |
$skin = $differenceEngine->getSkin(); |
| 297 |
if ( $skin->getSkinName() !== 'minerva' ) { |
| 298 |
return; |
| 299 |
} |
| 300 |
$differenceEngine->setSlotDiffOptions( [ |
| 301 |
'diff-type' => 'inline', |
| 302 |
] ); |
| 303 |
} |
| 304 |
} |