MediaWiki  1.34.0
RevisionDeleter.php
Go to the documentation of this file.
1 <?php
25 
33  private static $allowedTypes = [
34  'revision' => RevDelRevisionList::class,
35  'archive' => RevDelArchiveList::class,
36  'oldimage' => RevDelFileList::class,
37  'filearchive' => RevDelArchivedFileList::class,
38  'logging' => RevDelLogList::class,
39  ];
40 
42  private static $deprecatedTypeMap = [
43  'oldid' => 'revision',
44  'artimestamp' => 'archive',
45  'oldimage' => 'oldimage',
46  'fileid' => 'filearchive',
47  'logid' => 'logging',
48  ];
49 
56  public static function getTypes() {
57  return array_keys( self::$allowedTypes );
58  }
59 
67  public static function getCanonicalTypeName( $typeName ) {
68  if ( isset( self::$deprecatedTypeMap[$typeName] ) ) {
69  $typeName = self::$deprecatedTypeMap[$typeName];
70  }
71  return isset( self::$allowedTypes[$typeName] ) ? $typeName : null;
72  }
73 
85  public static function createList( $typeName, IContextSource $context, Title $title, array $ids ) {
86  $typeName = self::getCanonicalTypeName( $typeName );
87  if ( !$typeName ) {
88  throw new MWException( __METHOD__ . ": Unknown RevDel type '$typeName'" );
89  }
90  $class = self::$allowedTypes[$typeName];
91  return new $class( $context, $title, $ids );
92  }
93 
105  protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
106  if ( $diff & $field ) {
107  $arr[( $new & $field ) ? 0 : 1][] = $desc;
108  }
109  }
110 
129  public static function getChanges( $n, $o ) {
130  $diff = $n ^ $o;
131  $ret = [ 0 => [], 1 => [], 2 => [] ];
132  // Build bitfield changes in language
133  self::checkItem( 'revdelete-content',
134  RevisionRecord::DELETED_TEXT, $diff, $n, $ret );
135  self::checkItem( 'revdelete-summary',
136  RevisionRecord::DELETED_COMMENT, $diff, $n, $ret );
137  self::checkItem( 'revdelete-uname',
138  RevisionRecord::DELETED_USER, $diff, $n, $ret );
139  // Restriction application to sysops
140  if ( $diff & RevisionRecord::DELETED_RESTRICTED ) {
141  if ( $n & RevisionRecord::DELETED_RESTRICTED ) {
142  $ret[2][] = 'revdelete-restricted';
143  } else {
144  $ret[2][] = 'revdelete-unrestricted';
145  }
146  }
147  return $ret;
148  }
149 
156  public static function getRelationType( $typeName ) {
157  $typeName = self::getCanonicalTypeName( $typeName );
158  if ( !$typeName ) {
159  return null;
160  }
161  return call_user_func( [ self::$allowedTypes[$typeName], 'getRelationType' ] );
162  }
163 
170  public static function getRestriction( $typeName ) {
171  $typeName = self::getCanonicalTypeName( $typeName );
172  if ( !$typeName ) {
173  return null;
174  }
175  return call_user_func( [ self::$allowedTypes[$typeName], 'getRestriction' ] );
176  }
177 
184  public static function getRevdelConstant( $typeName ) {
185  $typeName = self::getCanonicalTypeName( $typeName );
186  if ( !$typeName ) {
187  return null;
188  }
189  return call_user_func( [ self::$allowedTypes[$typeName], 'getRevdelConstant' ] );
190  }
191 
200  public static function suggestTarget( $typeName, $target, array $ids ) {
201  $typeName = self::getCanonicalTypeName( $typeName );
202  if ( !$typeName ) {
203  return $target;
204  }
205  return call_user_func( [ self::$allowedTypes[$typeName], 'suggestTarget' ], $target, $ids );
206  }
207 
217  public static function checkRevisionExistence( $title, $revid ) {
218  $dbr = wfGetDB( DB_REPLICA );
219  $exists = $dbr->selectField( 'revision', '1',
220  [ 'rev_id' => $revid ], __METHOD__ );
221 
222  if ( $exists ) {
223  return true;
224  }
225 
226  $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
227  [ 'ar_namespace' => $title->getNamespace(),
228  'ar_title' => $title->getDBkey(),
229  'ar_rev_id' => $revid ], __METHOD__ );
230 
231  return $timestamp;
232  }
233 
241  public static function extractBitfield( array $bitPars, $oldfield ) {
242  // Build the actual new rev_deleted bitfield
243  $newBits = 0;
244  foreach ( $bitPars as $const => $val ) {
245  if ( $val == 1 ) {
246  $newBits |= $const; // $const is the *_deleted const
247  } elseif ( $val == -1 ) {
248  $newBits |= ( $oldfield & $const ); // use existing
249  }
250  }
251  return $newBits;
252  }
253 }
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
RevisionDeleter
General controller for RevDel, used by both SpecialRevisiondelete and ApiRevisionDelete.
Definition: RevisionDeleter.php:31
RevisionDeleter\$allowedTypes
static $allowedTypes
List of known revdel types, with their corresponding list classes.
Definition: RevisionDeleter.php:33
RevisionDeleter\checkRevisionExistence
static checkRevisionExistence( $title, $revid)
Checks if a revision still exists in the revision table.
Definition: RevisionDeleter.php:217
$dbr
$dbr
Definition: testCompression.php:50
MWException
MediaWiki exception.
Definition: MWException.php:26
RevisionDeleter\getTypes
static getTypes()
Lists the valid possible types for revision deletion.
Definition: RevisionDeleter.php:56
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
$title
$title
Definition: testCompression.php:34
RevisionDeleter\$deprecatedTypeMap
static $deprecatedTypeMap
Type map to support old log entries.
Definition: RevisionDeleter.php:42
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RevisionDeleter\createList
static createList( $typeName, IContextSource $context, Title $title, array $ids)
Instantiate the appropriate list class for a given list of IDs.
Definition: RevisionDeleter.php:85
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:105
RevisionDeleter\getRevdelConstant
static getRevdelConstant( $typeName)
Get the revision deletion constant for the RevDel type.
Definition: RevisionDeleter.php:184
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:129
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
$context
$context
Definition: load.php:45
Title
Represents a title within MediaWiki.
Definition: Title.php:42
RevisionDeleter\getRelationType
static getRelationType( $typeName)
Get DB field name for URL param...
Definition: RevisionDeleter.php:156
RevisionDeleter\getCanonicalTypeName
static getCanonicalTypeName( $typeName)
Gets the canonical type name, if any.
Definition: RevisionDeleter.php:67
RevisionDeleter\extractBitfield
static extractBitfield(array $bitPars, $oldfield)
Put together a rev_deleted bitfield.
Definition: RevisionDeleter.php:241
RevisionDeleter\suggestTarget
static suggestTarget( $typeName, $target, array $ids)
Suggest a target for the revision deletion.
Definition: RevisionDeleter.php:200
RevisionDeleter\getRestriction
static getRestriction( $typeName)
Get the user right required for the RevDel type.
Definition: RevisionDeleter.php:170