| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Context\IContextSource; |
| 23 |
use MediaWiki\Deferred\DeferredUpdates; |
| 24 |
use MediaWiki\Page\PageIdentity; |
| 25 |
use MediaWiki\Revision\RevisionRecord; |
| 26 |
use MediaWiki\RevisionList\RevisionListBase; |
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\Rdbms\LBFactory; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
abstract class RevDelList extends RevisionListBase { |
| 43 |
|
| 44 |
|
| 45 |
protected const SUPPRESS_BIT = RevisionRecord::DELETED_RESTRICTED; |
| 46 |
|
| 47 |
|
| 48 |
private $lbFactory; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct( |
| 57 |
IContextSource $context, |
| 58 |
PageIdentity $page, |
| 59 |
array $ids, |
| 60 |
LBFactory $lbFactory |
| 61 |
) { |
| 62 |
parent::__construct( $context, $page ); |
| 63 |
|
| 64 |
|
| 65 |
$this->ids = $ids; |
| 66 |
$this->lbFactory = $lbFactory; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function getRelationType() { |
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static function getRestriction() { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static function getRevdelConstant() { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static function suggestTarget( $target, array $ids ) { |
| 108 |
return $target; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function areAnySuppressed() { |
| 117 |
|
| 118 |
foreach ( $this as $item ) { |
| 119 |
if ( $item->getBits() & self::SUPPRESS_BIT ) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function setVisibility( array $params ) { |
| 140 |
$status = Status::newGood(); |
| 141 |
|
| 142 |
$bitPars = $params['value']; |
| 143 |
$comment = $params['comment']; |
| 144 |
$perItemStatus = $params['perItemStatus'] ?? false; |
| 145 |
|
| 146 |
|
| 147 |
if ( $perItemStatus ) { |
| 148 |
$status->value['itemStatuses'] = []; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 154 |
$this->res = $this->doQuery( $dbw ); |
| 155 |
|
| 156 |
$status->merge( $this->acquireItemLocks() ); |
| 157 |
if ( !$status->isGood() ) { |
| 158 |
return $status; |
| 159 |
} |
| 160 |
|
| 161 |
$dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE ); |
| 162 |
$dbw->onTransactionResolution( |
| 163 |
function () { |
| 164 |
|
| 165 |
$this->releaseItemLocks(); |
| 166 |
}, |
| 167 |
__METHOD__ |
| 168 |
); |
| 169 |
|
| 170 |
$missing = array_fill_keys( $this->ids, true ); |
| 171 |
$this->clearFileOps(); |
| 172 |
$idsForLog = []; |
| 173 |
$authorActors = []; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
$virtualOldBits = 0; |
| 179 |
$virtualNewBits = 0; |
| 180 |
$logType = 'delete'; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
$visibilityChangeMap = []; |
| 185 |
|
| 186 |
|
| 187 |
foreach ( $this as $item ) { |
| 188 |
unset( $missing[$item->getId()] ); |
| 189 |
|
| 190 |
if ( $perItemStatus ) { |
| 191 |
$itemStatus = Status::newGood(); |
| 192 |
$status->value['itemStatuses'][$item->getId()] = $itemStatus; |
| 193 |
} else { |
| 194 |
$itemStatus = $status; |
| 195 |
} |
| 196 |
|
| 197 |
$oldBits = $item->getBits(); |
| 198 |
|
| 199 |
$newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits ); |
| 200 |
|
| 201 |
if ( $oldBits == $newBits ) { |
| 202 |
$itemStatus->warning( |
| 203 |
'revdelete-no-change', $item->formatDate(), $item->formatTime() ); |
| 204 |
$status->failCount++; |
| 205 |
continue; |
| 206 |
} elseif ( $oldBits == 0 && $newBits != 0 ) { |
| 207 |
$opType = 'hide'; |
| 208 |
} elseif ( $oldBits != 0 && $newBits == 0 ) { |
| 209 |
$opType = 'show'; |
| 210 |
} else { |
| 211 |
$opType = 'modify'; |
| 212 |
} |
| 213 |
|
| 214 |
if ( $item->isHideCurrentOp( $newBits ) ) { |
| 215 |
|
| 216 |
$itemStatus->error( |
| 217 |
'revdelete-hide-current', $item->formatDate(), $item->formatTime() ); |
| 218 |
$status->failCount++; |
| 219 |
continue; |
| 220 |
} elseif ( !$item->canView() ) { |
| 221 |
|
| 222 |
$msg = ( $opType == 'show' ) ? |
| 223 |
'revdelete-show-no-access' : 'revdelete-modify-no-access'; |
| 224 |
$itemStatus->error( $msg, $item->formatDate(), $item->formatTime() ); |
| 225 |
$status->failCount++; |
| 226 |
continue; |
| 227 |
|
| 228 |
} elseif ( $newBits == self::SUPPRESS_BIT ) { |
| 229 |
$itemStatus->warning( |
| 230 |
'revdelete-only-restricted', $item->formatDate(), $item->formatTime() ); |
| 231 |
$status->failCount++; |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
$ok = $item->setBits( $newBits ); |
| 237 |
|
| 238 |
if ( $ok ) { |
| 239 |
$idsForLog[] = $item->getId(); |
| 240 |
|
| 241 |
if ( ( $oldBits | $newBits ) & self::SUPPRESS_BIT ) { |
| 242 |
$logType = 'suppress'; |
| 243 |
} |
| 244 |
|
| 245 |
$addedBits = ( $oldBits ^ $newBits ) & $newBits; |
| 246 |
$removedBits = ( $oldBits ^ $newBits ) & $oldBits; |
| 247 |
$virtualNewBits |= $addedBits; |
| 248 |
$virtualOldBits |= $removedBits; |
| 249 |
|
| 250 |
$status->successCount++; |
| 251 |
$authorActors[] = $item->getAuthorActor(); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
$visibilityChangeMap[$item->getId()] = [ |
| 256 |
'oldBits' => $oldBits, |
| 257 |
'newBits' => $newBits, |
| 258 |
]; |
| 259 |
} else { |
| 260 |
$itemStatus->error( |
| 261 |
'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() ); |
| 262 |
$status->failCount++; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
foreach ( $missing as $id => $unused ) { |
| 268 |
if ( $perItemStatus ) { |
| 269 |
$status->value['itemStatuses'][$id] = Status::newFatal( 'revdelete-modify-missing', $id ); |
| 270 |
} else { |
| 271 |
$status->error( 'revdelete-modify-missing', $id ); |
| 272 |
} |
| 273 |
$status->failCount++; |
| 274 |
} |
| 275 |
|
| 276 |
if ( $status->successCount == 0 ) { |
| 277 |
$dbw->endAtomic( __METHOD__ ); |
| 278 |
return $status; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
$successCount = $status->successCount; |
| 283 |
|
| 284 |
|
| 285 |
$status->merge( $this->doPreCommitUpdates() ); |
| 286 |
if ( !$status->isOK() ) { |
| 287 |
|
| 288 |
$dbw->cancelAtomic( __METHOD__ ); |
| 289 |
return $status; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
$authorFields = []; |
| 294 |
$authorFields['authorActors'] = $authorActors; |
| 295 |
$this->updateLog( |
| 296 |
$logType, |
| 297 |
[ |
| 298 |
'page' => $this->page, |
| 299 |
'count' => $successCount, |
| 300 |
'newBits' => $virtualNewBits, |
| 301 |
'oldBits' => $virtualOldBits, |
| 302 |
'comment' => $comment, |
| 303 |
'ids' => $idsForLog, |
| 304 |
'tags' => $params['tags'] ?? [], |
| 305 |
] + $authorFields |
| 306 |
); |
| 307 |
|
| 308 |
|
| 309 |
DeferredUpdates::addCallableUpdate( |
| 310 |
function () use ( $visibilityChangeMap ) { |
| 311 |
$this->doPostCommitUpdates( $visibilityChangeMap ); |
| 312 |
}, |
| 313 |
DeferredUpdates::PRESEND, |
| 314 |
$dbw |
| 315 |
); |
| 316 |
|
| 317 |
$dbw->endAtomic( __METHOD__ ); |
| 318 |
|
| 319 |
return $status; |
| 320 |
} |
| 321 |
|
| 322 |
final protected function acquireItemLocks() { |
| 323 |
$status = Status::newGood(); |
| 324 |
|
| 325 |
foreach ( $this as $item ) { |
| 326 |
$status->merge( $item->lock() ); |
| 327 |
} |
| 328 |
|
| 329 |
return $status; |
| 330 |
} |
| 331 |
|
| 332 |
final protected function releaseItemLocks() { |
| 333 |
$status = Status::newGood(); |
| 334 |
|
| 335 |
foreach ( $this as $item ) { |
| 336 |
$status->merge( $item->unlock() ); |
| 337 |
} |
| 338 |
|
| 339 |
return $status; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function reloadFromPrimary() { |
| 348 |
$dbw = $this->lbFactory->getPrimaryDatabase(); |
| 349 |
$this->res = $this->doQuery( $dbw ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
private function updateLog( $logType, $params ) { |
| 365 |
|
| 366 |
$field = RevisionDeleter::getRelationType( $this->getType() ); |
| 367 |
if ( !$field ) { |
| 368 |
throw new UnexpectedValueException( "Bad log URL param type!" ); |
| 369 |
} |
| 370 |
|
| 371 |
$logParams = $this->getLogParams( $params ); |
| 372 |
|
| 373 |
$logEntry = new ManualLogEntry( $logType, $this->getLogAction() ); |
| 374 |
$logEntry->setTarget( $params['page'] ); |
| 375 |
$logEntry->setComment( $params['comment'] ); |
| 376 |
$logEntry->setParameters( $logParams ); |
| 377 |
$logEntry->setPerformer( $this->getUser() ); |
| 378 |
|
| 379 |
$relations = [ |
| 380 |
$field => $params['ids'], |
| 381 |
]; |
| 382 |
if ( isset( $params['authorActors'] ) ) { |
| 383 |
$relations += [ |
| 384 |
'target_author_actor' => $params['authorActors'], |
| 385 |
]; |
| 386 |
} |
| 387 |
$logEntry->setRelations( $relations ); |
| 388 |
|
| 389 |
$logEntry->addTags( $params['tags'] ); |
| 390 |
$logId = $logEntry->insert(); |
| 391 |
$logEntry->publish( $logId ); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function getLogAction() { |
| 399 |
return 'revision'; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function getLogParams( $params ) { |
| 408 |
return [ |
| 409 |
'4::type' => $this->getType(), |
| 410 |
'5::ids' => $params['ids'], |
| 411 |
'6::ofield' => $params['oldBits'], |
| 412 |
'7::nfield' => $params['newBits'], |
| 413 |
]; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public function clearFileOps() { |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function doPreCommitUpdates() { |
| 429 |
return Status::newGood(); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public function doPostCommitUpdates( array $visibilityChangeMap ) { |
| 439 |
return Status::newGood(); |
| 440 |
} |
| 441 |
|
| 442 |
} |