Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
DeprecatedHooks
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 markDeprecated
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 isHookDeprecated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDeprecationInfo
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Holds list of deprecated hooks and methods for retrieval
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
23namespace MediaWiki\HookContainer;
24
25use InvalidArgumentException;
26
27class DeprecatedHooks {
28
29    /**
30     * @var array[] List of deprecated hooks. Value arrays for each hook contain:
31     *  - deprecatedVersion: (string) Version in which the hook was deprecated,
32     *    to pass to wfDeprecated().
33     *  - component: (string, optional) $component to pass to wfDeprecated().
34     *  - silent (bool, optional) If true, no deprecation warning will be raised
35     * @phpcs-require-sorted-array
36     */
37    private $deprecatedHooks = [
38        'AddNewAccount' => [ 'deprecatedVersion' => '1.27' ],
39        'ArticleDelete' => [ 'deprecatedVersion' => '1.37', 'silent' => true ],
40        'ArticleDeleteComplete' => [ 'deprecatedVersion' => '1.37', 'silent' => true ],
41        'ArticleUndelete' => [ 'deprecatedVersion' => '1.40', 'silent' => true ],
42        'EditPageBeforeEditToolbar' => [ 'deprecatedVersion' => '1.36' ],
43        'EmailUser' => [ 'deprecatedVersion' => '1.41', 'silent' => true ],
44        'EmailUserPermissionsErrors' => [ 'deprecatedVersion' => '1.41' ],
45        'InterwikiLoadPrefix' => [ 'deprecatedVersion' => '1.36' ],
46        'LocalFile::getHistory' => [ 'deprecatedVersion' => '1.37' ],
47        'MagicWordwgVariableIDs' => [ 'deprecatedVersion' => '1.35', 'silent' => true ],
48        'MessageCache::get' => [ 'deprecatedVersion' => '1.41', 'silent' => true ],
49        'PageContentSave' => [ 'deprecatedVersion' => '1.35', 'silent' => true ],
50        'PrefixSearchBackend' => [ 'deprecatedVersion' => '1.27' ],
51        'ProtectionForm::buildForm' => [ 'deprecatedVersion' => '1.36', 'silent' => true ],
52        'RollbackComplete' => [ 'deprecatedVersion' => '1.36', 'silent' => true ],
53        'SearchDataForIndex' => [ 'deprecatedVersion' => '1.40', 'silent' => true ],
54        'userCan' => [ 'deprecatedVersion' => '1.37' ],
55        'UserCanSendEmail' => [ 'deprecatedVersion' => '1.41', 'silent' => true ],
56        'WikiPageDeletionUpdates' => [ 'deprecatedVersion' => '1.32', 'silent' => true ],
57    ];
58
59    /**
60     * @param array[] $deprecatedHooks List of hooks to mark as deprecated.
61     * Value arrays for each hook contain:
62     *  - deprecatedVersion: (string) Version in which the hook was deprecated,
63     *    to pass to wfDeprecated().
64     *  - component: (string, optional) $component to pass to wfDeprecated().
65     *  - silent: (bool, optional) True to not raise any deprecation warning
66     */
67    public function __construct( array $deprecatedHooks = [] ) {
68        foreach ( $deprecatedHooks as $hook => $info ) {
69            $this->markDeprecated(
70                $hook,
71                $info['deprecatedVersion'],
72                $info['component'] ?? false,
73                $info['silent'] ?? false
74            );
75        }
76    }
77
78    /**
79     * For use by extensions, to add to list of deprecated hooks.
80     * Core-defined hooks should instead be added to $this->$deprecatedHooks directly.
81     * However, the preferred method of marking a hook deprecated is by adding it to
82     * the DeprecatedHooks attribute in extension.json
83     *
84     * @param string $hook
85     * @param string $version Version in which the hook was deprecated, to pass to wfDeprecated()
86     * @param string|null $component (optional) component to pass to wfDeprecated().
87     * @param bool $silent True to not raise any deprecation warning
88     * @throws InvalidArgumentException Hook has already been marked deprecated
89     */
90    public function markDeprecated(
91        string $hook, string $version, ?string $component = null, bool $silent = false
92    ): void {
93        if ( isset( $this->deprecatedHooks[$hook] ) ) {
94            throw new InvalidArgumentException(
95                "Cannot mark hook '$hook' deprecated with version $version" .
96                "It is already marked deprecated with version " .
97                $this->deprecatedHooks[$hook]['deprecatedVersion']
98            );
99        }
100        $hookInfo = [
101            'deprecatedVersion' => $version,
102            'silent' => $silent
103        ];
104        if ( $component ) {
105            $hookInfo['component'] = $component;
106        }
107        $this->deprecatedHooks[$hook] = $hookInfo;
108    }
109
110    /**
111     * Checks whether hook is marked deprecated
112     * @param string $hook Hook name
113     * @return bool
114     */
115    public function isHookDeprecated( string $hook ): bool {
116        return isset( $this->deprecatedHooks[$hook] );
117    }
118
119    /**
120     * Gets deprecation info for a specific hook or all hooks if hook not specified
121     * @param string|null $hook (optional) Hook name
122     * @return array|null Value array from $this->deprecatedHooks for a specific hook or all hooks
123     */
124    public function getDeprecationInfo( ?string $hook = null ): ?array {
125        if ( !$hook ) {
126            return $this->deprecatedHooks;
127        }
128        return $this->deprecatedHooks[$hook] ?? null;
129    }
130}