MediaWiki  master
RevisionDeleter.php
Go to the documentation of this file.
1 <?php
27 
39  private const ALLOWED_TYPES = [
40  'revision' => [
41  'class' => RevDelRevisionList::class,
42  'services' => [
43  'DBLoadBalancerFactory',
44  'HookContainer',
45  'HtmlCacheUpdater',
46  'RevisionStore',
47  'MainWANObjectCache',
48  ],
49  ],
50  'archive' => [
51  'class' => RevDelArchiveList::class,
52  'services' => [
53  'DBLoadBalancerFactory',
54  'HookContainer',
55  'HtmlCacheUpdater',
56  'RevisionStore',
57  'MainWANObjectCache',
58  ],
59  ],
60  'oldimage' => [
61  'class' => RevDelFileList::class,
62  'services' => [
63  'DBLoadBalancerFactory',
64  'HtmlCacheUpdater',
65  'RepoGroup',
66  ],
67  ],
68  'filearchive' => [
69  'class' => RevDelArchivedFileList::class,
70  'services' => [
71  'DBLoadBalancerFactory',
72  'HtmlCacheUpdater',
73  'RepoGroup',
74  ],
75  ],
76  'logging' => [
77  'class' => RevDelLogList::class,
78  'services' => [
79  'DBLoadBalancerFactory',
80  'CommentStore',
81  ],
82  ],
83  ];
84 
86  private const DEPRECATED_TYPE_MAP = [
87  'oldid' => 'revision',
88  'artimestamp' => 'archive',
89  'oldimage' => 'oldimage',
90  'fileid' => 'filearchive',
91  'logid' => 'logging',
92  ];
93 
100  public static function getTypes() {
101  return array_keys( self::ALLOWED_TYPES );
102  }
103 
111  public static function getCanonicalTypeName( $typeName ) {
112  if ( isset( self::DEPRECATED_TYPE_MAP[$typeName] ) ) {
113  $typeName = self::DEPRECATED_TYPE_MAP[$typeName];
114  }
115  return isset( self::ALLOWED_TYPES[$typeName] ) ? $typeName : null;
116  }
117 
129  public static function createList( $typeName, IContextSource $context, PageIdentity $page, array $ids ) {
130  $typeName = self::getCanonicalTypeName( $typeName );
131  if ( !$typeName ) {
132  throw new MWException( __METHOD__ . ": Unknown RevDel type '$typeName'" );
133  }
134  $spec = self::ALLOWED_TYPES[$typeName];
135  $objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
136 
137  // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
138  // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey
139  return $objectFactory->createObject(
140  $spec,
141  [
142  'extraArgs' => [ $context, $page, $ids ],
143  'assertClass' => RevDelList::class,
144  ]
145  );
146  }
147 
159  protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
160  if ( $diff & $field ) {
161  $arr[( $new & $field ) ? 0 : 1][] = $desc;
162  }
163  }
164 
183  public static function getChanges( $n, $o ) {
184  $diff = $n ^ $o;
185  $ret = [ 0 => [], 1 => [], 2 => [] ];
186  // Build bitfield changes in language
187  self::checkItem( 'revdelete-content',
188  RevisionRecord::DELETED_TEXT, $diff, $n, $ret );
189  self::checkItem( 'revdelete-summary',
190  RevisionRecord::DELETED_COMMENT, $diff, $n, $ret );
191  self::checkItem( 'revdelete-uname',
192  RevisionRecord::DELETED_USER, $diff, $n, $ret );
193  // Restriction application to sysops
194  if ( $diff & RevisionRecord::DELETED_RESTRICTED ) {
195  if ( $n & RevisionRecord::DELETED_RESTRICTED ) {
196  $ret[2][] = 'revdelete-restricted';
197  } else {
198  $ret[2][] = 'revdelete-unrestricted';
199  }
200  }
201  return $ret;
202  }
203 
210  public static function getRelationType( $typeName ) {
211  $typeName = self::getCanonicalTypeName( $typeName );
212  if ( !$typeName ) {
213  return null;
214  }
215  return call_user_func( [ self::ALLOWED_TYPES[$typeName]['class'], 'getRelationType' ] );
216  }
217 
224  public static function getRestriction( $typeName ) {
225  $typeName = self::getCanonicalTypeName( $typeName );
226  if ( !$typeName ) {
227  return null;
228  }
229  return call_user_func( [ self::ALLOWED_TYPES[$typeName]['class'], 'getRestriction' ] );
230  }
231 
238  public static function getRevdelConstant( $typeName ) {
239  $typeName = self::getCanonicalTypeName( $typeName );
240  if ( !$typeName ) {
241  return null;
242  }
243  return call_user_func( [ self::ALLOWED_TYPES[$typeName]['class'], 'getRevdelConstant' ] );
244  }
245 
254  public static function suggestTarget( $typeName, $target, array $ids ) {
255  $typeName = self::getCanonicalTypeName( $typeName );
256  if ( !$typeName ) {
257  return $target;
258  }
259  return call_user_func(
260  [ self::ALLOWED_TYPES[$typeName]['class'], 'suggestTarget' ],
261  $target,
262  $ids
263  );
264  }
265 
273  public static function extractBitfield( array $bitPars, $oldfield ) {
274  // Build the actual new rev_deleted bitfield
275  $newBits = 0;
276  foreach ( $bitPars as $const => $val ) {
277  if ( $val == 1 ) {
278  $newBits |= $const; // $const is the *_deleted const
279  } elseif ( $val == -1 ) {
280  $newBits |= ( $oldfield & $const ); // use existing
281  }
282  }
283  return $newBits;
284  }
285 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
RevisionDeleter
General controller for RevDel, used by both SpecialRevisiondelete and ApiRevisionDelete.
Definition: RevisionDeleter.php:33
RevisionDeleter\DEPRECATED_TYPE_MAP
const DEPRECATED_TYPE_MAP
Type map to support old log entries.
Definition: RevisionDeleter.php:86
MWException
MediaWiki exception.
Definition: MWException.php:29
RevisionDeleter\getTypes
static getTypes()
Lists the valid possible types for revision deletion.
Definition: RevisionDeleter.php:100
RevisionDeleter\checkItem
static checkItem( $desc, $field, $diff, $new, &$arr)
Checks for a change in the bitfield for a certain option and updates the provided array accordingly.
Definition: RevisionDeleter.php:159
RevisionDeleter\getRevdelConstant
static getRevdelConstant( $typeName)
Get the revision deletion constant for the RevDel type.
Definition: RevisionDeleter.php:238
RevisionDeleter\getChanges
static getChanges( $n, $o)
Gets an array of message keys describing the changes made to the visibility of the revision.
Definition: RevisionDeleter.php:183
RevisionDeleter\createList
static createList( $typeName, IContextSource $context, PageIdentity $page, array $ids)
Instantiate the appropriate list class for a given list of IDs.
Definition: RevisionDeleter.php:129
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
RevisionDeleter\getRelationType
static getRelationType( $typeName)
Get DB field name for URL param...
Definition: RevisionDeleter.php:210
RevisionDeleter\getCanonicalTypeName
static getCanonicalTypeName( $typeName)
Gets the canonical type name, if any.
Definition: RevisionDeleter.php:111
RevisionDeleter\ALLOWED_TYPES
const ALLOWED_TYPES
List of known revdel types, with their corresponding ObjectFactory spec to create the relevant class.
Definition: RevisionDeleter.php:39
RevisionDeleter\extractBitfield
static extractBitfield(array $bitPars, $oldfield)
Put together a rev_deleted bitfield.
Definition: RevisionDeleter.php:273
RevisionDeleter\suggestTarget
static suggestTarget( $typeName, $target, array $ids)
Suggest a target for the revision deletion.
Definition: RevisionDeleter.php:254
RevisionDeleter\getRestriction
static getRestriction( $typeName)
Get the user right required for the RevDel type.
Definition: RevisionDeleter.php:224