Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionDeleter
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 10
600
0.00% covered (danger)
0.00%
0 / 1
 getTypes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCanonicalTypeName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 createList
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 checkItem
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 getChanges
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getRelationType
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getRestriction
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getRevdelConstant
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 suggestTarget
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 extractBitfield
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Revision/log/file deletion backend
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup RevisionDelete
8 */
9
10namespace MediaWiki\RevisionDelete;
11
12use InvalidArgumentException;
13use MediaWiki\Context\IContextSource;
14use MediaWiki\MediaWikiServices;
15use MediaWiki\Page\PageIdentity;
16use MediaWiki\Revision\RevisionRecord;
17use MediaWiki\Title\Title;
18
19/**
20 * General controller for RevDel, used by both SpecialRevisiondelete and
21 * ApiRevisionDelete.
22 * @ingroup RevisionDelete
23 */
24class RevisionDeleter {
25    /**
26     * List of known revdel types, with their corresponding ObjectFactory spec to
27     * create the relevant class. All specs need to include DBLoadBalancerFactory,
28     * which is used in the base RevDelList class
29     */
30    private const ALLOWED_TYPES = [
31        'revision' => [
32            'class' => RevDelRevisionList::class,
33            'services' => [
34                'DBLoadBalancerFactory',
35                'HookContainer',
36                'HtmlCacheUpdater',
37                'RevisionStore',
38                'DomainEventDispatcher'
39            ],
40        ],
41        'archive' => [
42            'class' => RevDelArchiveList::class,
43            'services' => [
44                'DBLoadBalancerFactory',
45                'HookContainer',
46                'HtmlCacheUpdater',
47                'RevisionStore',
48                'DomainEventDispatcher'
49            ],
50        ],
51        'oldimage' => [
52            'class' => RevDelFileList::class,
53            'services' => [
54                'DBLoadBalancerFactory',
55                'HtmlCacheUpdater',
56                'RepoGroup',
57            ],
58        ],
59        'filearchive' => [
60            'class' => RevDelArchivedFileList::class,
61            'services' => [
62                'DBLoadBalancerFactory',
63                'HtmlCacheUpdater',
64                'RepoGroup',
65            ],
66        ],
67        'logging' => [
68            'class' => RevDelLogList::class,
69            'services' => [
70                'DBLoadBalancerFactory',
71                'CommentStore',
72                'LogFormatterFactory',
73            ],
74        ],
75    ];
76
77    /** Type map to support old log entries */
78    private const DEPRECATED_TYPE_MAP = [
79        'oldid' => 'revision',
80        'artimestamp' => 'archive',
81        'oldimage' => 'oldimage',
82        'fileid' => 'filearchive',
83        'logid' => 'logging',
84    ];
85
86    /**
87     * Lists the valid possible types for revision deletion.
88     *
89     * @since 1.22
90     * @return array
91     */
92    public static function getTypes() {
93        return array_keys( self::ALLOWED_TYPES );
94    }
95
96    /**
97     * Gets the canonical type name, if any.
98     *
99     * @since 1.22
100     * @param string $typeName
101     * @return string|null
102     */
103    public static function getCanonicalTypeName( $typeName ) {
104        if ( isset( self::DEPRECATED_TYPE_MAP[$typeName] ) ) {
105            $typeName = self::DEPRECATED_TYPE_MAP[$typeName];
106        }
107        return isset( self::ALLOWED_TYPES[$typeName] ) ? $typeName : null;
108    }
109
110    /**
111     * Instantiate the appropriate list class for a given list of IDs.
112     *
113     * @since 1.22
114     * @param string $typeName RevDel type, see RevisionDeleter::getTypes()
115     * @param IContextSource $context
116     * @param PageIdentity $page
117     * @param array $ids
118     * @return RevDelList
119     */
120    public static function createList( $typeName, IContextSource $context, PageIdentity $page, array $ids ) {
121        $typeName = self::getCanonicalTypeName( $typeName );
122        if ( !$typeName ) {
123            throw new InvalidArgumentException( __METHOD__ . ": Unknown RevDel type '$typeName'" );
124        }
125        $spec = self::ALLOWED_TYPES[$typeName];
126        $objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
127
128        return $objectFactory->createObject(
129            $spec,
130            [
131                'extraArgs' => [ $context, $page, $ids ],
132                'assertClass' => RevDelList::class,
133            ]
134        );
135    }
136
137    /**
138     * Checks for a change in the bitfield for a certain option and updates the
139     * provided array accordingly.
140     *
141     * @param string $desc Description to add to the array if the option was
142     * enabled / disabled.
143     * @param int $field The bitmask describing the single option.
144     * @param int $diff The xor of the old and new bitfields.
145     * @param int $new The new bitfield
146     * @param array &$arr The array to update.
147     */
148    protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
149        if ( $diff & $field ) {
150            $arr[( $new & $field ) ? 0 : 1][] = $desc;
151        }
152    }
153
154    /**
155     * Gets an array of message keys describing the changes made to the
156     * visibility of the revision.
157     *
158     * If the resulting array is $arr, then $arr[0] will contain an array of
159     * keys describing the items that were hidden, $arr[1] will contain
160     * an array of keys describing the items that were unhidden, and $arr[2]
161     * will contain an array with a single message key, which can be one of
162     * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression
163     * or null to indicate nothing in particular.
164     * You can turn the keys in $arr[0] and $arr[1] into message keys by
165     * appending -hid and -unhid to the keys respectively.
166     *
167     * @param int $n The new bitfield.
168     * @param int $o The old bitfield.
169     * @return array An array as described above.
170     * @since 1.19 public
171     */
172    public static function getChanges( $n, $o ) {
173        $diff = $n ^ $o;
174        $ret = [ 0 => [], 1 => [], 2 => [] ];
175        // Build bitfield changes in language
176        self::checkItem( 'revdelete-content',
177            RevisionRecord::DELETED_TEXT, $diff, $n, $ret );
178        self::checkItem( 'revdelete-summary',
179            RevisionRecord::DELETED_COMMENT, $diff, $n, $ret );
180        self::checkItem( 'revdelete-uname',
181            RevisionRecord::DELETED_USER, $diff, $n, $ret );
182        // Restriction application to sysops
183        if ( $diff & RevisionRecord::DELETED_RESTRICTED ) {
184            if ( $n & RevisionRecord::DELETED_RESTRICTED ) {
185                $ret[2][] = 'revdelete-restricted';
186            } else {
187                $ret[2][] = 'revdelete-unrestricted';
188            }
189        }
190        return $ret;
191    }
192
193    /** Get DB field name for URL param...
194     * Future code for other things may also track
195     * other types of revision-specific changes.
196     * @param string $typeName
197     * @return string|null One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
198     */
199    public static function getRelationType( $typeName ) {
200        $typeName = self::getCanonicalTypeName( $typeName );
201        if ( !$typeName ) {
202            return null;
203        }
204        $class = self::ALLOWED_TYPES[$typeName]['class'];
205        return $class::getRelationType();
206    }
207
208    /**
209     * Get the user right required for the RevDel type
210     * @since 1.22
211     * @param string $typeName
212     * @return string|null User right
213     */
214    public static function getRestriction( $typeName ) {
215        $typeName = self::getCanonicalTypeName( $typeName );
216        if ( !$typeName ) {
217            return null;
218        }
219        $class = self::ALLOWED_TYPES[$typeName]['class'];
220        return $class::getRestriction();
221    }
222
223    /**
224     * Get the revision deletion constant for the RevDel type
225     * @since 1.22
226     * @param string $typeName
227     * @return int|null RevDel constant
228     */
229    public static function getRevdelConstant( $typeName ) {
230        $typeName = self::getCanonicalTypeName( $typeName );
231        if ( !$typeName ) {
232            return null;
233        }
234        $class = self::ALLOWED_TYPES[$typeName]['class'];
235        return $class::getRevdelConstant();
236    }
237
238    /**
239     * Suggest a target for the revision deletion
240     * @since 1.22
241     * @param string $typeName
242     * @param Title|null $target User-supplied target
243     * @param array $ids
244     * @return Title|null
245     */
246    public static function suggestTarget( $typeName, $target, array $ids ) {
247        $typeName = self::getCanonicalTypeName( $typeName );
248        if ( !$typeName ) {
249            return $target;
250        }
251        $class = self::ALLOWED_TYPES[$typeName]['class'];
252        return $class::suggestTarget(
253            $target,
254            $ids
255        );
256    }
257
258    /**
259     * Put together a rev_deleted bitfield
260     * @since 1.22
261     * @param array $bitPars associative array mapping bit masks to 0, 1 or -1.
262     *        A value of 0 unsets the bits in the mask, 1 will set the bits in
263     *        the mask, and any other value will retain the bits already present
264     *        in $oldfield.
265     * @param int $oldfield Current bitfield
266     *
267     * @internal
268     * @return int
269     */
270    public static function extractBitfield( array $bitPars, $oldfield ) {
271        // Build the actual new rev_deleted bitfield
272        $newBits = $oldfield;
273        foreach ( $bitPars as $const => $val ) {
274            // $const is the XXX_DELETED const
275
276            if ( $val == 1 ) {
277                $newBits |= $const; // set the bit
278            } elseif ( $val == 0 ) {
279                $newBits &= ~$const; // unset the bit
280            }
281        }
282        return $newBits;
283    }
284}
285
286/** @deprecated class alias since 1.46 */
287class_alias( RevisionDeleter::class, 'RevisionDeleter' );