Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageProtectionChangedEvent
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 haveSameKeys
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getExpiryAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isCascadingAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getReason
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRestrictionMapBefore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRestrictionMapAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Page\Event;
8
9use MediaWiki\Page\PageIdentity;
10use MediaWiki\Permissions\RestrictionStore;
11use MediaWiki\Storage\PageUpdateCauses;
12use MediaWiki\User\UserIdentity;
13use Wikimedia\Assert\Assert;
14
15/**
16 * Domain event representing changes to page protection (aka restriction levels).
17 * They are emitted by WikiPage::doUpdateRestrictions().
18 *
19 * @see RestrictionStore
20 *
21 * @since 1.45
22 */
23class PageProtectionChangedEvent extends PageEvent {
24    public const TYPE = 'PageProtectionChanged';
25    private string $reason;
26
27    /**
28     * @var array<string,string>
29     */
30    private array $expiryAfter;
31    private bool $isCascadingAfter;
32    private PageIdentity $page;
33
34    /**
35     * @var array<string,list<string>>
36     */
37    private array $restrictionMapAfter;
38
39    /**
40     * @var array<string,list<string>>
41     */
42    private array $restrictionMapBefore;
43
44    /**
45     * @param PageIdentity $page
46     * @param array<string,list<string>> $restrictionMapBefore Page protection
47     *  before the change, given as an associative array that maps actions to a
48     *  list of permissions a user must have to perform it.
49     * @param array<string,list<string>> $restrictionMapAfter Page protection
50     *   after the change, given as an associative array that maps actions to a
51     *   list of permissions a user must have to perform it.
52     * @param array<string,string> $expiryAfter An expiry timestamp (or infinite)
53     *   for each action in $restrictionMapAfter.
54     * @param bool $isCascadingAfter
55     * @param UserIdentity $performer
56     * @param string $reason
57     * @param array $tags
58     */
59    public function __construct(
60        PageIdentity $page,
61        array $restrictionMapBefore,
62        array $restrictionMapAfter,
63        array $expiryAfter,
64        bool $isCascadingAfter,
65        UserIdentity $performer,
66        string $reason,
67        array $tags = []
68    ) {
69        parent::__construct(
70            PageUpdateCauses::CAUSE_PROTECTION_CHANGE,
71            $page->getId(),
72            $performer,
73            $tags
74        );
75
76        $this->declareEventType( self::TYPE );
77
78        Assert::parameter(
79            self::haveSameKeys( $restrictionMapAfter, $restrictionMapBefore ),
80            '$restrictionMapAfter and $restrictionMapBefore',
81            'must contain the same keys'
82        );
83
84        Assert::parameter(
85            self::haveSameKeys( $restrictionMapAfter, $expiryAfter ),
86            '$restrictionMapAfter and $expiryAfter',
87            'must contain the same keys'
88        );
89
90        $this->expiryAfter = $expiryAfter;
91        $this->isCascadingAfter = $isCascadingAfter;
92        $this->page = $page;
93        $this->reason = $reason;
94        $this->restrictionMapAfter = $restrictionMapAfter;
95        $this->restrictionMapBefore = $restrictionMapBefore;
96    }
97
98    private static function haveSameKeys( array $array1, array $array2 ): bool {
99        $keys1 = array_keys( $array1 );
100        $keys2 = array_keys( $array2 );
101
102        return ( count( array_diff( $keys1, $keys2 ) )
103            + count( array_diff( $keys2, $keys1 ) ) ) === 0;
104    }
105
106    /**
107     * The expiry timestamp for the restrictions on each action
108     * returned by getRestrictionMapAfter().
109     *
110     * @return array<string,string>
111     */
112    public function getExpiryAfter(): array {
113        return $this->expiryAfter;
114    }
115
116    /**
117     * Whether the updated restrictions are applied in cascading mode.
118     * Actions not affected by this change may retain a different cascading mode.
119     */
120    public function isCascadingAfter(): bool {
121        return $this->isCascadingAfter;
122    }
123
124    public function getPage(): PageIdentity {
125        return $this->page;
126    }
127
128    /**
129     * Returns the reason supplied be the user.
130     */
131    public function getReason(): string {
132        return $this->reason;
133    }
134
135    /**
136     * Returns the restrictions that are updated by this change.
137     *
138     * This map should provide information for all actions that are also
139     * present in the return value of getRestrictionMapAfter().
140     *
141     * Actions not mentioned in this map are unaffected by the change,
142     * they may or may not have been restricted.
143     *
144     * @return array<string,list<string>> An associative array that maps
145     * actions to a list of permissions a user must have to perform it.
146     */
147    public function getRestrictionMapBefore(): array {
148        return $this->restrictionMapBefore;
149    }
150
151    /**
152     * Returns the restrictions that are updated by this change.
153     * Actions not mentioned in this map are unaffected by the change,
154     * they may or may not be restricted.
155     *
156     * @return array<string,list<string>> An associative array that maps
157     * actions to a list of permissions a user must have to perform it.
158     */
159    public function getRestrictionMapAfter(): array {
160        return $this->restrictionMapAfter;
161    }
162}