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 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\HookContainer;
10
11use InvalidArgumentException;
12
13class DeprecatedHooks {
14
15    /**
16     * @var array[] List of deprecated hooks. Value arrays for each hook contain:
17     *  - deprecatedVersion: (string) Version in which the hook was deprecated,
18     *    to pass to wfDeprecated().
19     *  - component: (string, optional) $component to pass to wfDeprecated().
20     *  - silent (bool, optional) If true, no deprecation warning will be raised
21     * @phpcs-require-sorted-array
22     */
23    private $deprecatedHooks = [
24        'APIHelpModifyOutput' => [ 'deprecatedVersion' => '1.47' ],
25        'ArticleDelete' => [ 'deprecatedVersion' => '1.37', 'silent' => true ],
26        'ArticleDeleteComplete' => [ 'deprecatedVersion' => '1.37', 'silent' => true ],
27        'ArticleUndelete' => [ 'deprecatedVersion' => '1.40', 'silent' => true ],
28        'AutopromoteCondition' => [ 'deprecatedVersion' => '1.46' ],
29        'EditPageBeforeConflictDiff' => [ 'deprecatedVersion' => '1.47' ],
30        'EditPageBeforeEditToolbar' => [ 'deprecatedVersion' => '1.36' ],
31        'EmailUser' => [ 'deprecatedVersion' => '1.41', 'silent' => true ],
32        'EmailUserPermissionsErrors' => [ 'deprecatedVersion' => '1.41' ],
33        'InterwikiLoadPrefix' => [ 'deprecatedVersion' => '1.36' ],
34        'LocalFile::getHistory' => [ 'deprecatedVersion' => '1.37' ],
35        'MarkPatrolledComplete' => [ 'deprecatedVersion' => '1.47' ],
36        'MessageCache::get' => [ 'deprecatedVersion' => '1.41' ],
37        'ParserOptionsRegister' => [ 'deprecatedVersion' => '1.47', 'silent' => true ],
38        'ProtectionForm::buildForm' => [ 'deprecatedVersion' => '1.36', 'silent' => true ],
39        'RecentChangesPurgeRows' => [ 'deprecatedVersion' => '1.45', 'silent' => true ],
40        'RollbackComplete' => [ 'deprecatedVersion' => '1.36', 'silent' => true ],
41        'SearchDataForIndex' => [ 'deprecatedVersion' => '1.40', 'silent' => true ],
42        'userCan' => [ 'deprecatedVersion' => '1.37' ],
43        'UserCanSendEmail' => [ 'deprecatedVersion' => '1.41', 'silent' => true ],
44        'WatchedItemQueryServiceExtensionsHook' => [ 'deprecatedVersion' => '1.46' ],
45        'WikiPageDeletionUpdates' => [ 'deprecatedVersion' => '1.32', 'silent' => true ],
46    ];
47
48    /**
49     * @param array[] $deprecatedHooks List of hooks to mark as deprecated.
50     * Value arrays for each hook contain:
51     *  - deprecatedVersion: (string) Version in which the hook was deprecated,
52     *    to pass to wfDeprecated().
53     *  - component: (string, optional) $component to pass to wfDeprecated().
54     *  - silent: (bool, optional) True to not raise any deprecation warning
55     */
56    public function __construct( array $deprecatedHooks = [] ) {
57        foreach ( $deprecatedHooks as $hook => $info ) {
58            $this->markDeprecated(
59                $hook,
60                $info['deprecatedVersion'],
61                $info['component'] ?? false,
62                $info['silent'] ?? false
63            );
64        }
65    }
66
67    /**
68     * For use by extensions, to add to list of deprecated hooks.
69     * Core-defined hooks should instead be added to $this->$deprecatedHooks directly.
70     * However, the preferred method of marking a hook deprecated is by adding it to
71     * the DeprecatedHooks attribute in extension.json
72     *
73     * @param string $hook
74     * @param string $version Version in which the hook was deprecated, to pass to wfDeprecated()
75     * @param string|null $component (optional) component to pass to wfDeprecated().
76     * @param bool $silent True to not raise any deprecation warning
77     * @throws InvalidArgumentException Hook has already been marked deprecated
78     */
79    public function markDeprecated(
80        string $hook, string $version, ?string $component = null, bool $silent = false
81    ): void {
82        if ( isset( $this->deprecatedHooks[$hook] ) ) {
83            throw new InvalidArgumentException(
84                "Cannot mark hook '$hook' deprecated with version $version" .
85                "It is already marked deprecated with version " .
86                $this->deprecatedHooks[$hook]['deprecatedVersion']
87            );
88        }
89        $hookInfo = [
90            'deprecatedVersion' => $version,
91            'silent' => $silent
92        ];
93        if ( $component ) {
94            $hookInfo['component'] = $component;
95        }
96        $this->deprecatedHooks[$hook] = $hookInfo;
97    }
98
99    /**
100     * Checks whether hook is marked deprecated
101     * @param string $hook Hook name
102     * @return bool
103     */
104    public function isHookDeprecated( string $hook ): bool {
105        return isset( $this->deprecatedHooks[$hook] );
106    }
107
108    /**
109     * Gets deprecation info for a specific hook or all hooks if hook not specified
110     * @param string|null $hook (optional) Hook name
111     * @return array|null Value array from $this->deprecatedHooks for a specific hook or all hooks
112     */
113    public function getDeprecationInfo( ?string $hook = null ): ?array {
114        if ( !$hook ) {
115            return $this->deprecatedHooks;
116        }
117        return $this->deprecatedHooks[$hook] ?? null;
118    }
119}