| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Context\IContextSource; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\Page\PageIdentity; |
| 27 |
use MediaWiki\Revision\RevisionRecord; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class RevisionDeleter { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private const ALLOWED_TYPES = [ |
| 42 |
'revision' => [ |
| 43 |
'class' => RevDelRevisionList::class, |
| 44 |
'services' => [ |
| 45 |
'DBLoadBalancerFactory', |
| 46 |
'HookContainer', |
| 47 |
'HtmlCacheUpdater', |
| 48 |
'RevisionStore', |
| 49 |
], |
| 50 |
], |
| 51 |
'archive' => [ |
| 52 |
'class' => RevDelArchiveList::class, |
| 53 |
'services' => [ |
| 54 |
'DBLoadBalancerFactory', |
| 55 |
'HookContainer', |
| 56 |
'HtmlCacheUpdater', |
| 57 |
'RevisionStore', |
| 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 |
'LogFormatterFactory', |
| 82 |
], |
| 83 |
], |
| 84 |
]; |
| 85 |
|
| 86 |
|
| 87 |
private const DEPRECATED_TYPE_MAP = [ |
| 88 |
'oldid' => 'revision', |
| 89 |
'artimestamp' => 'archive', |
| 90 |
'oldimage' => 'oldimage', |
| 91 |
'fileid' => 'filearchive', |
| 92 |
'logid' => 'logging', |
| 93 |
]; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static function getTypes() { |
| 102 |
return array_keys( self::ALLOWED_TYPES ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static function getCanonicalTypeName( $typeName ) { |
| 113 |
if ( isset( self::DEPRECATED_TYPE_MAP[$typeName] ) ) { |
| 114 |
$typeName = self::DEPRECATED_TYPE_MAP[$typeName]; |
| 115 |
} |
| 116 |
return isset( self::ALLOWED_TYPES[$typeName] ) ? $typeName : null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static function createList( $typeName, IContextSource $context, PageIdentity $page, array $ids ) { |
| 130 |
$typeName = self::getCanonicalTypeName( $typeName ); |
| 131 |
if ( !$typeName ) { |
| 132 |
throw new InvalidArgumentException( __METHOD__ . ": Unknown RevDel type '$typeName'" ); |
| 133 |
} |
| 134 |
$spec = self::ALLOWED_TYPES[$typeName]; |
| 135 |
$objectFactory = MediaWikiServices::getInstance()->getObjectFactory(); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
return $objectFactory->createObject( |
| 140 |
$spec, |
| 141 |
[ |
| 142 |
'extraArgs' => [ $context, $page, $ids ], |
| 143 |
'assertClass' => RevDelList::class, |
| 144 |
] |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected static function checkItem( $desc, $field, $diff, $new, &$arr ) { |
| 160 |
if ( $diff & $field ) { |
| 161 |
$arr[( $new & $field ) ? 0 : 1][] = $desc; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static function getChanges( $n, $o ) { |
| 184 |
$diff = $n ^ $o; |
| 185 |
$ret = [ 0 => [], 1 => [], 2 => [] ]; |
| 186 |
|
| 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 |
|
| 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 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static function extractBitfield( array $bitPars, $oldfield ) { |
| 274 |
|
| 275 |
$newBits = 0; |
| 276 |
foreach ( $bitPars as $const => $val ) { |
| 277 |
if ( $val == 1 ) { |
| 278 |
$newBits |= $const; |
| 279 |
} elseif ( $val == -1 ) { |
| 280 |
$newBits |= ( $oldfield & $const ); |
| 281 |
} |
| 282 |
} |
| 283 |
return $newBits; |
| 284 |
} |
| 285 |
} |