| 1 |
<?php |
| 2 |
|
| 3 |
r |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Extension\Gadgets; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use ManualLogEntry; |
| 27 |
use MediaWiki\Api\ApiMessage; |
| 28 |
use MediaWiki\Context\RequestContext; |
| 29 |
use MediaWiki\Extension\Gadgets\Special\SpecialGadgetUsage; |
| 30 |
use MediaWiki\Hook\DeleteUnknownPreferencesHook; |
| 31 |
use MediaWiki\Hook\PreferencesGetIconHook; |
| 32 |
use MediaWiki\Hook\PreferencesGetLegendHook; |
| 33 |
use MediaWiki\Html\Html; |
| 34 |
use MediaWiki\HTMLForm\HTMLForm; |
| 35 |
use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 36 |
use MediaWiki\Output\OutputPage; |
| 37 |
use MediaWiki\Page\Hook\PageDeleteCompleteHook; |
| 38 |
use MediaWiki\Page\ProperPageIdentity; |
| 39 |
use MediaWiki\Permissions\Authority; |
| 40 |
use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsHook; |
| 41 |
use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 42 |
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook; |
| 43 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 44 |
use MediaWiki\Revision\Hook\ContentHandlerDefaultModelForHook; |
| 45 |
use MediaWiki\Revision\RevisionRecord; |
| 46 |
use MediaWiki\SpecialPage\Hook\WgQueryPagesHook; |
| 47 |
use MediaWiki\SpecialPage\SpecialPage; |
| 48 |
use MediaWiki\Storage\Hook\PageSaveCompleteHook; |
| 49 |
use MediaWiki\Title\Title; |
| 50 |
use MediaWiki\Title\TitleValue; |
| 51 |
use MediaWiki\User\Hook\UserGetDefaultOptionsHook; |
| 52 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 53 |
use MediaWiki\User\User; |
| 54 |
use OOUI\HtmlSnippet; |
| 55 |
use Skin; |
| 56 |
use Wikimedia\Message\MessageSpecifier; |
| 57 |
use Wikimedia\Rdbms\IExpression; |
| 58 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 59 |
use Wikimedia\Rdbms\LikeValue; |
| 60 |
use Wikimedia\WrappedString; |
| 61 |
use WikiPage; |
| 62 |
|
| 63 |
class Hooks implements |
| 64 |
PageDeleteCompleteHook, |
| 65 |
PageSaveCompleteHook, |
| 66 |
UserGetDefaultOptionsHook, |
| 67 |
GetPreferencesHook, |
| 68 |
PreferencesGetIconHook, |
| 69 |
PreferencesGetLegendHook, |
| 70 |
ResourceLoaderRegisterModulesHook, |
| 71 |
BeforePageDisplayHook, |
| 72 |
ContentHandlerDefaultModelForHook, |
| 73 |
WgQueryPagesHook, |
| 74 |
DeleteUnknownPreferencesHook, |
| 75 |
GetUserPermissionsErrorsHook |
| 76 |
{ |
| 77 |
private GadgetRepo $gadgetRepo; |
| 78 |
private UserOptionsLookup $userOptionsLookup; |
| 79 |
|
| 80 |
public function __construct( |
| 81 |
GadgetRepo $gadgetRepo, |
| 82 |
UserOptionsLookup $userOptionsLookup |
| 83 |
) { |
| 84 |
$this->gadgetRepo = $gadgetRepo; |
| 85 |
$this->userOptionsLookup = $userOptionsLookup; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function onPageSaveComplete( |
| 99 |
$wikiPage, |
| 100 |
$userIdentity, |
| 101 |
$summary, |
| 102 |
$flags, |
| 103 |
$revisionRecord, |
| 104 |
$editResult |
| 105 |
): void { |
| 106 |
$title = $wikiPage->getTitle(); |
| 107 |
$this->gadgetRepo->handlePageUpdate( $title ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function onPageDeleteComplete( |
| 122 |
ProperPageIdentity $page, |
| 123 |
Authority $deleter, |
| 124 |
string $reason, |
| 125 |
int $pageID, |
| 126 |
RevisionRecord $deletedRev, |
| 127 |
ManualLogEntry $logEntry, |
| 128 |
int $archivedRevisionCount |
| 129 |
): void { |
| 130 |
$title = TitleValue::newFromPage( $page ); |
| 131 |
$this->gadgetRepo->handlePageUpdate( $title ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function onUserGetDefaultOptions( &$defaultOptions ) { |
| 139 |
$gadgets = $this->gadgetRepo->getStructuredList(); |
| 140 |
if ( !$gadgets ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
foreach ( $gadgets as $thisSection ) { |
| 148 |
foreach ( $thisSection as $gadgetId => $gadget ) { |
| 149 |
|
| 150 |
if ( !$gadget->isHidden() ) { |
| 151 |
$defaultOptions['gadget-' . $gadgetId] = $gadget->isOnByDefault() ? 1 : 0; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function onGetPreferences( $user, &$preferences ) { |
| 163 |
$gadgets = $this->gadgetRepo->getStructuredList(); |
| 164 |
if ( !$gadgets ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$preferences['gadgets-intro'] = [ |
| 169 |
'type' => 'info', |
| 170 |
'default' => wfMessage( 'gadgets-prefstext' )->parseAsBlock(), |
| 171 |
'section' => 'gadgets', |
| 172 |
'raw' => true, |
| 173 |
]; |
| 174 |
|
| 175 |
$safeMode = $this->userOptionsLookup->getOption( $user, 'forcesafemode' ); |
| 176 |
if ( $safeMode ) { |
| 177 |
$preferences['gadgets-safemode'] = [ |
| 178 |
'type' => 'info', |
| 179 |
'default' => Html::warningBox( wfMessage( 'gadgets-prefstext-safemode' )->parse() ), |
| 180 |
'section' => 'gadgets', |
| 181 |
'raw' => true, |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
$skin = RequestContext::getMain()->getSkin(); |
| 186 |
foreach ( $gadgets as $section => $thisSection ) { |
| 187 |
foreach ( $thisSection as $gadget ) { |
| 188 |
|
| 189 |
$type = 'api'; |
| 190 |
if ( |
| 191 |
!$safeMode |
| 192 |
&& !$gadget->isHidden() |
| 193 |
&& $gadget->isAllowed( $user ) |
| 194 |
&& $gadget->isSkinSupported( $skin ) |
| 195 |
) { |
| 196 |
$type = 'check'; |
| 197 |
} |
| 198 |
$gname = $gadget->getName(); |
| 199 |
$sectionLabelMsg = "gadget-section-$section"; |
| 200 |
|
| 201 |
$preferences["gadget-$gname"] = [ |
| 202 |
'type' => $type, |
| 203 |
'label-message' => $gadget->getDescriptionMessageKey(), |
| 204 |
'section' => $section !== '' ? "gadgets/$sectionLabelMsg" : 'gadgets', |
| 205 |
'default' => $gadget->isEnabled( $user ), |
| 206 |
'noglobal' => true, |
| 207 |
]; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function onPreferencesGetLegend( $form, $key, &$legend ) { |
| 226 |
if ( str_starts_with( $key, 'gadget-section-' ) ) { |
| 227 |
$legend = new HtmlSnippet( $form->msg( $key )->parse() ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function onPreferencesGetIcon( &$iconNames ) { |
| 237 |
$iconNames[ 'gadgets' ] = 'puzzle'; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void { |
| 245 |
foreach ( $this->gadgetRepo->getGadgetIds() as $id ) { |
| 246 |
$resourceLoader->register( Gadget::getModuleName( $id ), [ |
| 247 |
'class' => GadgetResourceLoaderModule::class, |
| 248 |
'id' => $id, |
| 249 |
] ); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function onBeforePageDisplay( $out, $skin ): void { |
| 259 |
$repo = $this->gadgetRepo; |
| 260 |
$ids = $repo->getGadgetIds(); |
| 261 |
if ( !$ids ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
$enabledLegacyGadgets = []; |
| 266 |
$conditions = new GadgetLoadConditions( $out ); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
foreach ( $ids as $id ) { |
| 272 |
try { |
| 273 |
$gadget = $repo->getGadget( $id ); |
| 274 |
} catch ( InvalidArgumentException $e ) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
if ( $conditions->check( $gadget ) ) { |
| 279 |
if ( $gadget->hasModule() ) { |
| 280 |
if ( $gadget->getType() === 'styles' ) { |
| 281 |
$out->addModuleStyles( Gadget::getModuleName( $gadget->getName() ) ); |
| 282 |
} else { |
| 283 |
$out->addModules( Gadget::getModuleName( $gadget->getName() ) ); |
| 284 |
|
| 285 |
$peers = []; |
| 286 |
foreach ( $gadget->getPeers() as $peerName ) { |
| 287 |
try { |
| 288 |
$peers[] = $repo->getGadget( $peerName ); |
| 289 |
} catch ( InvalidArgumentException $e ) { |
| 290 |
|
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
foreach ( $peers as $peer ) { |
| 295 |
if ( $peer->getType() === 'styles' ) { |
| 296 |
$out->addModuleStyles( Gadget::getModuleName( $peer->getName() ) ); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if ( $gadget->getLegacyScripts() ) { |
| 306 |
$enabledLegacyGadgets[] = $id; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
$strings = []; |
| 312 |
foreach ( $enabledLegacyGadgets as $id ) { |
| 313 |
$strings[] = $this->makeLegacyWarning( $id ); |
| 314 |
} |
| 315 |
$out->addHTML( WrappedString::join( "\n", $strings ) ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private function makeLegacyWarning( $id ) { |
| 323 |
$special = SpecialPage::getTitleFor( 'Gadgets' ); |
| 324 |
|
| 325 |
return ResourceLoader::makeInlineScript( |
| 326 |
Html::encodeJsCall( 'mw.log.warn', [ |
| 327 |
"Gadget \"$id\" was not loaded. Please migrate it to use ResourceLoader. " . |
| 328 |
'See <' . $special->getCanonicalURL() . '>.' |
| 329 |
] ) |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public function onContentHandlerDefaultModelFor( $title, &$model ) { |
| 341 |
if ( MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ) ) { |
| 342 |
$model = 'GadgetDefinition'; |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
return true; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function onWgQueryPages( &$queryPages ) { |
| 354 |
$queryPages[] = [ SpecialGadgetUsage::class, 'GadgetUsage' ]; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function onDeleteUnknownPreferences( &$where, $db ) { |
| 364 |
$where[] = $db->expr( |
| 365 |
'up_property', |
| 366 |
IExpression::NOT_LIKE, |
| 367 |
new LikeValue( 'gadget-', $db->anyString() ) |
| 368 |
); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function onGetUserPermissionsErrors( $title, $user, $action, &$result ) { |
| 382 |
if ( $action === 'edit' |
| 383 |
&& MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ) |
| 384 |
) { |
| 385 |
if ( !$user->isAllowed( 'editsitejs' ) ) { |
| 386 |
$result = ApiMessage::create( wfMessage( 'sitejsprotected' ), 'sitejsprotected' ); |
| 387 |
return false; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |