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