Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
54.37% |
56 / 103 |
|
30.77% |
4 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
54.37% |
56 / 103 |
|
30.77% |
4 / 13 |
237.40 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onUserGetDefaultOptions | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
| onGetPreferences | |
62.00% |
31 / 50 |
|
0.00% |
0 / 1 |
19.90 | |||
| onPreferencesGetLegend | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| onPreferencesGetIcon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onResourceLoaderRegisterModules | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| onApiParseMakeOutputPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onBeforePageDisplay | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addGadgetsToOutput | |
65.00% |
13 / 20 |
|
0.00% |
0 / 1 |
16.19 | |||
| onContentHandlerDefaultModelFor | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
| onWgQueryPages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onDeleteUnknownPreferences | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| onGetUserPermissionsErrors | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2007 Daniel Kinzler |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Extension\Gadgets; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use MediaWiki\Api\ApiMessage; |
| 27 | use MediaWiki\Api\Hook\ApiParseMakeOutputPageHook; |
| 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\HTMLForm\HTMLForm; |
| 33 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 34 | use MediaWiki\Output\OutputPage; |
| 35 | use MediaWiki\Permissions\Hook\GetUserPermissionsErrorsHook; |
| 36 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 37 | use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook; |
| 38 | use MediaWiki\ResourceLoader\ResourceLoader; |
| 39 | use MediaWiki\Revision\Hook\ContentHandlerDefaultModelForHook; |
| 40 | use MediaWiki\SpecialPage\Hook\WgQueryPagesHook; |
| 41 | use MediaWiki\Specials\Hook\PreferencesGetLegendHook; |
| 42 | use MediaWiki\Title\Title; |
| 43 | use MediaWiki\User\Hook\UserGetDefaultOptionsHook; |
| 44 | use MediaWiki\User\Options\UserOptionsLookup; |
| 45 | use MediaWiki\User\User; |
| 46 | use OOUI\FieldLayout; |
| 47 | use OOUI\HtmlSnippet; |
| 48 | use OOUI\MessageWidget; |
| 49 | use Wikimedia\Message\MessageSpecifier; |
| 50 | use Wikimedia\Rdbms\IExpression; |
| 51 | use Wikimedia\Rdbms\IReadableDatabase; |
| 52 | use Wikimedia\Rdbms\LikeValue; |
| 53 | |
| 54 | class Hooks implements |
| 55 | UserGetDefaultOptionsHook, |
| 56 | GetPreferencesHook, |
| 57 | PreferencesGetIconHook, |
| 58 | PreferencesGetLegendHook, |
| 59 | ResourceLoaderRegisterModulesHook, |
| 60 | ApiParseMakeOutputPageHook, |
| 61 | BeforePageDisplayHook, |
| 62 | ContentHandlerDefaultModelForHook, |
| 63 | WgQueryPagesHook, |
| 64 | DeleteUnknownPreferencesHook, |
| 65 | GetUserPermissionsErrorsHook |
| 66 | { |
| 67 | public function __construct( |
| 68 | private readonly GadgetRepo $gadgetRepo, |
| 69 | private readonly UserOptionsLookup $userOptionsLookup, |
| 70 | ) { |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * UserGetDefaultOptions hook handler |
| 75 | * @param array &$defaultOptions Array of default preference keys and values |
| 76 | */ |
| 77 | public function onUserGetDefaultOptions( &$defaultOptions ) { |
| 78 | $gadgets = $this->gadgetRepo->getStructuredList(); |
| 79 | if ( !$gadgets ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | foreach ( $gadgets as $thisSection ) { |
| 84 | foreach ( $thisSection as $gadgetId => $gadget ) { |
| 85 | // Hidden gadgets don't need to be added here, T299071 |
| 86 | if ( !$gadget->isHidden() ) { |
| 87 | $defaultOptions['gadget-' . $gadgetId] = $gadget->isOnByDefault() ? 1 : 0; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * GetPreferences hook handler. |
| 95 | * @param User $user |
| 96 | * @param array &$preferences Preference descriptions |
| 97 | */ |
| 98 | public function onGetPreferences( $user, &$preferences ) { |
| 99 | $gadgets = $this->gadgetRepo->getStructuredList(); |
| 100 | if ( !$gadgets ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | $preferences['gadgets-intro'] = [ |
| 105 | 'type' => 'info', |
| 106 | 'default' => wfMessage( 'gadgets-prefstext' )->parseAsBlock(), |
| 107 | 'section' => 'gadgets', |
| 108 | 'raw' => true, |
| 109 | ]; |
| 110 | |
| 111 | $safeMode = $this->userOptionsLookup->getOption( $user, 'forcesafemode' ); |
| 112 | if ( $safeMode ) { |
| 113 | $preferences['gadgets-safemode'] = [ |
| 114 | 'type' => 'info', |
| 115 | 'section' => 'gadgets', |
| 116 | 'raw' => true, |
| 117 | 'rawrow' => true, |
| 118 | 'default' => new FieldLayout( |
| 119 | new MessageWidget( [ |
| 120 | 'label' => new HtmlSnippet( wfMessage( 'gadgets-prefstext-safemode' )->parse() ), |
| 121 | 'type' => 'warning', |
| 122 | ] ) |
| 123 | ), |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | $skin = RequestContext::getMain()->getSkin(); |
| 128 | foreach ( $gadgets as $section => $thisSection ) { |
| 129 | if ( $section !== '' ) { |
| 130 | $sectionInfoMsg = wfMessage( "gadget-section-info-$section" ); |
| 131 | if ( !$sectionInfoMsg->isDisabled() ) { |
| 132 | $preferences['gadget-section-info-' . $section] = [ |
| 133 | 'type' => 'info', |
| 134 | 'default' => $sectionInfoMsg->parse(), |
| 135 | 'section' => "gadgets/gadget-section-$section", |
| 136 | 'raw' => true, |
| 137 | ]; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | foreach ( $thisSection as $gadget ) { |
| 142 | // Only show option to enable gadget if it can be enabled |
| 143 | $type = 'api'; |
| 144 | if ( |
| 145 | !$safeMode |
| 146 | && !$gadget->isHidden() |
| 147 | && $gadget->isAllowed( $user ) |
| 148 | && $gadget->isSkinSupported( $skin ) |
| 149 | ) { |
| 150 | $type = 'check'; |
| 151 | } |
| 152 | $gname = $gadget->getName(); |
| 153 | $sectionLabelMsg = "gadget-section-$section"; |
| 154 | |
| 155 | $preferences["gadget-$gname"] = [ |
| 156 | 'type' => $type, |
| 157 | 'label-message' => $gadget->getDescriptionMessageKey(), |
| 158 | 'section' => $section !== '' ? "gadgets/$sectionLabelMsg" : 'gadgets', |
| 159 | 'default' => $gadget->isEnabled( $user ), |
| 160 | 'noglobal' => true, |
| 161 | ]; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * PreferencesGetLegend hook handler. |
| 168 | * |
| 169 | * Used to override the subsection heading labels for the gadget groups. The default message would |
| 170 | * be "prefs-$key", but we've previously used different messages, and they have on-wiki overrides |
| 171 | * that would have to be moved if the message keys changed. |
| 172 | * |
| 173 | * @param HTMLForm $form the HTMLForm object. This is a ContextSource as well |
| 174 | * @param string $key the section name |
| 175 | * @param string &$legend the legend text. Defaults to wfMessage( "prefs-$key" )->text() but may |
| 176 | * be overridden |
| 177 | * @return bool|void True or no return value to continue or false to abort |
| 178 | */ |
| 179 | public function onPreferencesGetLegend( $form, $key, &$legend ) { |
| 180 | if ( str_starts_with( $key, 'gadget-section-' ) ) { |
| 181 | $legend = new HtmlSnippet( $form->msg( $key )->parse() ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Add icon for Special:Preferences mobile layout |
| 187 | * |
| 188 | * @param array &$iconNames Array of icon names for their respective sections. |
| 189 | */ |
| 190 | public function onPreferencesGetIcon( &$iconNames ) { |
| 191 | $iconNames[ 'gadgets' ] = 'puzzle'; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * ResourceLoaderRegisterModules hook handler. |
| 196 | */ |
| 197 | public function onResourceLoaderRegisterModules( ResourceLoader $resourceLoader ): void { |
| 198 | foreach ( $this->gadgetRepo->getGadgetIds() as $id ) { |
| 199 | $resourceLoader->register( Gadget::getModuleName( $id ), [ |
| 200 | 'class' => GadgetResourceLoaderModule::class, |
| 201 | 'id' => $id, |
| 202 | ] ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** @inheritDoc */ |
| 207 | public function onApiParseMakeOutputPage( $module, $output ): void { |
| 208 | $this->addGadgetsToOutput( $output ); |
| 209 | } |
| 210 | |
| 211 | /** @inheritDoc */ |
| 212 | public function onBeforePageDisplay( $out, $skin ): void { |
| 213 | $this->addGadgetsToOutput( $out ); |
| 214 | } |
| 215 | |
| 216 | private function addGadgetsToOutput( OutputPage $out ): void { |
| 217 | $repo = $this->gadgetRepo; |
| 218 | $ids = $repo->getGadgetIds(); |
| 219 | if ( !$ids ) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | $conditions = new GadgetLoadConditions( $out ); |
| 224 | |
| 225 | foreach ( $ids as $id ) { |
| 226 | try { |
| 227 | $gadget = $repo->getGadget( $id ); |
| 228 | } catch ( InvalidArgumentException ) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | if ( $conditions->check( $gadget ) && $gadget->hasModule() ) { |
| 233 | if ( $gadget->getType() === 'styles' ) { |
| 234 | $out->addModuleStyles( Gadget::getModuleName( $gadget->getName() ) ); |
| 235 | } else { |
| 236 | $out->addModules( Gadget::getModuleName( $gadget->getName() ) ); |
| 237 | |
| 238 | $peers = []; |
| 239 | foreach ( $gadget->getPeers() as $peerName ) { |
| 240 | try { |
| 241 | $peers[] = $repo->getGadget( $peerName ); |
| 242 | } catch ( InvalidArgumentException ) { |
| 243 | // Ignore, warning is emitted on Special:Gadgets |
| 244 | } |
| 245 | } |
| 246 | // Load peer modules |
| 247 | foreach ( $peers as $peer ) { |
| 248 | if ( $peer->getType() === 'styles' ) { |
| 249 | $out->addModuleStyles( Gadget::getModuleName( $peer->getName() ) ); |
| 250 | } |
| 251 | // Else, if not type=styles: Use dependencies instead. |
| 252 | // Note: No need for recursion as styles modules don't support |
| 253 | // either of 'dependencies' and 'peers'. |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Create "MediaWiki:Gadgets/<id>.json" pages with GadgetDefinitionContent |
| 262 | * |
| 263 | * @param Title $title |
| 264 | * @param string &$model |
| 265 | * @return bool |
| 266 | */ |
| 267 | public function onContentHandlerDefaultModelFor( $title, &$model ) { |
| 268 | if ( MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ) ) { |
| 269 | $model = 'GadgetDefinition'; |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Add the GadgetUsage special page to the list of QueryPages. |
| 278 | * @param array &$queryPages |
| 279 | */ |
| 280 | public function onWgQueryPages( &$queryPages ) { |
| 281 | $queryPages[] = [ SpecialGadgetUsage::class, 'GadgetUsage' ]; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Prevent gadget preferences from being deleted. |
| 286 | * @link https://www.mediawiki.org/wiki/Manual:Hooks/DeleteUnknownPreferences |
| 287 | * @param string[] &$where Array of where clause conditions to add to. |
| 288 | * @param IReadableDatabase $db |
| 289 | */ |
| 290 | public function onDeleteUnknownPreferences( &$where, $db ) { |
| 291 | $where[] = $db->expr( |
| 292 | 'up_property', |
| 293 | IExpression::NOT_LIKE, |
| 294 | new LikeValue( 'gadget-', $db->anyString() ) |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @param Title $title Title being checked against |
| 300 | * @param User $user Current user |
| 301 | * @param string $action Action being checked |
| 302 | * @param array|string|MessageSpecifier &$result User permissions error to add. If none, return true. |
| 303 | * For consistency, error messages should be plain text with no special coloring, |
| 304 | * bolding, etc. to show that they're errors; presenting them properly to the |
| 305 | * user as errors is done by the caller. |
| 306 | * @return bool|void |
| 307 | */ |
| 308 | public function onGetUserPermissionsErrors( $title, $user, $action, &$result ) { |
| 309 | if ( $action === 'edit' |
| 310 | && MediaWikiGadgetsJsonRepo::isGadgetDefinitionTitle( $title ) |
| 311 | ) { |
| 312 | if ( !$user->isAllowed( 'editsitejs' ) ) { |
| 313 | $result = ApiMessage::create( wfMessage( 'sitejsprotected' ), 'sitejsprotected' ); |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |