Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
4 / 14 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| FileImporterHooks | |
28.57% |
4 / 14 |
|
60.00% |
3 / 5 |
38.52 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onBeforeInitialize | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| onChangeTagsListActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onListDefinedTags | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| onUserGetReservedNames | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace FileImporter; |
| 4 | |
| 5 | use FileImporter\Html\ImportSuccessSnippet; |
| 6 | use MediaWiki\Actions\ActionEntryPoint; |
| 7 | use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook; |
| 8 | use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook; |
| 9 | use MediaWiki\Config\Config; |
| 10 | use MediaWiki\Hook\BeforeInitializeHook; |
| 11 | use MediaWiki\Output\OutputPage; |
| 12 | use MediaWiki\Request\WebRequest; |
| 13 | use MediaWiki\Title\Title; |
| 14 | use MediaWiki\User\Hook\UserGetReservedNamesHook; |
| 15 | use MediaWiki\User\User; |
| 16 | |
| 17 | /** |
| 18 | * @license GPL-2.0-or-later |
| 19 | * @author Andrew Kostka |
| 20 | */ |
| 21 | class FileImporterHooks implements |
| 22 | BeforeInitializeHook, |
| 23 | ChangeTagsListActiveHook, |
| 24 | ListDefinedTagsHook, |
| 25 | UserGetReservedNamesHook |
| 26 | { |
| 27 | |
| 28 | public function __construct( |
| 29 | private readonly Config $config |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Show an import success message when appropriate. |
| 35 | * |
| 36 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeInitialize |
| 37 | * |
| 38 | * @param Title $title |
| 39 | * @param null $unused |
| 40 | * @param OutputPage $output |
| 41 | * @param User $user |
| 42 | * @param WebRequest $request |
| 43 | * @param ActionEntryPoint $mediaWiki |
| 44 | */ |
| 45 | public function onBeforeInitialize( |
| 46 | $title, |
| 47 | $unused, |
| 48 | $output, |
| 49 | $user, |
| 50 | $request, |
| 51 | $mediaWiki |
| 52 | ) { |
| 53 | if ( $request->getVal( ImportSuccessSnippet::NOTICE_URL_KEY ) === null |
| 54 | || !$title->inNamespace( NS_FILE ) |
| 55 | || !$title->exists() |
| 56 | || !$user->isNamed() |
| 57 | ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $output->enableOOUI(); |
| 62 | $output->prependHTML( |
| 63 | ( new ImportSuccessSnippet() )->getHtml( |
| 64 | $output->getContext(), $title, $user ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ChangeTagsListActive |
| 69 | * |
| 70 | * @param string[] &$tags |
| 71 | */ |
| 72 | public function onChangeTagsListActive( &$tags ) { |
| 73 | $this->onListDefinedTags( $tags ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ListDefinedTags |
| 78 | * |
| 79 | * @param string[] &$tags |
| 80 | */ |
| 81 | public function onListDefinedTags( &$tags ) { |
| 82 | $tags[] = 'fileimporter'; |
| 83 | $tags[] = 'fileimporter-imported'; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Add FileImporter username to the list of reserved ones for |
| 88 | * replacing suppressed usernames in certain revisions |
| 89 | * |
| 90 | * @param string[] &$reservedUsernames |
| 91 | */ |
| 92 | public function onUserGetReservedNames( &$reservedUsernames ) { |
| 93 | $reservedUsernames[] = $this->config->get( 'FileImporterAccountForSuppressedUsername' ); |
| 94 | } |
| 95 | |
| 96 | } |