| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
m |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\ChangeTags\Taggable; |
| 27 |
use MediaWiki\Context\RequestContext; |
| 28 |
use MediaWiki\Deferred\DeferredUpdates; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\Linker\LinkTarget; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Page\PageReference; |
| 33 |
use MediaWiki\SpecialPage\SpecialPage; |
| 34 |
use MediaWiki\Title\Title; |
| 35 |
use MediaWiki\User\UserIdentity; |
| 36 |
use Wikimedia\Assert\Assert; |
| 37 |
use Wikimedia\Rdbms\IDatabase; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class ManualLogEntry extends LogEntryBase implements Taggable { |
| 50 |
|
| 51 |
protected $type; |
| 52 |
|
| 53 |
|
| 54 |
protected $subtype; |
| 55 |
|
| 56 |
|
| 57 |
protected $parameters = []; |
| 58 |
|
| 59 |
|
| 60 |
protected $relations = []; |
| 61 |
|
| 62 |
|
| 63 |
protected $performer; |
| 64 |
|
| 65 |
|
| 66 |
protected $target; |
| 67 |
|
| 68 |
|
| 69 |
protected $timestamp; |
| 70 |
|
| 71 |
|
| 72 |
protected $comment = ''; |
| 73 |
|
| 74 |
|
| 75 |
protected $revId = 0; |
| 76 |
|
| 77 |
|
| 78 |
protected $tags = []; |
| 79 |
|
| 80 |
|
| 81 |
protected $deleted; |
| 82 |
|
| 83 |
|
| 84 |
protected $id; |
| 85 |
|
| 86 |
|
| 87 |
protected $isPatrollable = false; |
| 88 |
|
| 89 |
|
| 90 |
protected $legacy = false; |
| 91 |
|
| 92 |
|
| 93 |
protected $forceBotFlag = null; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function __construct( $type, $subtype ) { |
| 104 |
$this->type = $type; |
| 105 |
$this->subtype = $subtype; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function setParameters( $parameters ) { |
| 151 |
$this->parameters = $parameters; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function setRelations( array $relations ) { |
| 164 |
$this->relations = $relations; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function setPerformer( UserIdentity $performer ) { |
| 174 |
$this->performer = $performer; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function setTarget( $target ) { |
| 185 |
if ( $target instanceof PageReference ) { |
| 186 |
$this->target = Title::newFromPageReference( $target ); |
| 187 |
} elseif ( $target instanceof LinkTarget ) { |
| 188 |
$this->target = Title::newFromLinkTarget( $target ); |
| 189 |
} else { |
| 190 |
throw new InvalidArgumentException( "Invalid target provided" ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function setTimestamp( $timestamp ) { |
| 201 |
$this->timestamp = $timestamp; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function setComment( string $comment ) { |
| 211 |
$this->comment = $comment; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function setAssociatedRevId( $revId ) { |
| 224 |
$this->revId = $revId; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function addTags( $tags ) { |
| 234 |
if ( $tags === null ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
if ( is_string( $tags ) ) { |
| 239 |
$tags = [ $tags ]; |
| 240 |
} |
| 241 |
Assert::parameterElementType( 'string', $tags, 'tags' ); |
| 242 |
$this->tags = array_unique( array_merge( $this->tags, $tags ) ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function setIsPatrollable( $patrollable ) { |
| 255 |
$this->isPatrollable = (bool)$patrollable; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function setLegacy( $legacy ) { |
| 265 |
$this->legacy = $legacy; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function setDeleted( $deleted ) { |
| 275 |
$this->deleted = $deleted; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function setForceBotFlag( bool $forceBotFlag ): void { |
| 285 |
$this->forceBotFlag = $forceBotFlag; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function insert( ?IDatabase $dbw = null ) { |
| 295 |
$services = MediaWikiServices::getInstance(); |
| 296 |
$dbw = $dbw ?: $services->getConnectionProvider()->getPrimaryDatabase(); |
| 297 |
|
| 298 |
$this->timestamp ??= wfTimestampNow(); |
| 299 |
$actorId = $services->getActorStore()->acquireActorId( $this->getPerformerIdentity(), $dbw ); |
| 300 |
|
| 301 |
|
| 302 |
$comment = trim( $this->getComment() ?? '' ); |
| 303 |
|
| 304 |
$params = $this->getParameters(); |
| 305 |
$relations = $this->relations; |
| 306 |
|
| 307 |
|
| 308 |
$revId = $this->getAssociatedRevId(); |
| 309 |
if ( $revId ) { |
| 310 |
$params['associated_rev_id'] = $revId; |
| 311 |
$relations['associated_rev_id'] = $revId; |
| 312 |
} |
| 313 |
|
| 314 |
$row = [ |
| 315 |
'log_type' => $this->getType(), |
| 316 |
'log_action' => $this->getSubtype(), |
| 317 |
'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ), |
| 318 |
'log_actor' => $actorId, |
| 319 |
'log_namespace' => $this->getTarget()->getNamespace(), |
| 320 |
'log_title' => $this->getTarget()->getDBkey(), |
| 321 |
'log_page' => $this->getTarget()->getArticleID(), |
| 322 |
'log_params' => LogEntryBase::makeParamBlob( $params ), |
| 323 |
]; |
| 324 |
if ( isset( $this->deleted ) ) { |
| 325 |
$row['log_deleted'] = $this->deleted; |
| 326 |
} |
| 327 |
$row += $services->getCommentStore()->insert( $dbw, 'log_comment', $comment ); |
| 328 |
|
| 329 |
$dbw->newInsertQueryBuilder() |
| 330 |
->insertInto( 'logging' ) |
| 331 |
->row( $row ) |
| 332 |
->caller( __METHOD__ ) |
| 333 |
->execute(); |
| 334 |
$this->id = $dbw->insertId(); |
| 335 |
|
| 336 |
$rows = []; |
| 337 |
foreach ( $relations as $tag => $values ) { |
| 338 |
if ( !strlen( $tag ) ) { |
| 339 |
throw new UnexpectedValueException( "Got empty log search tag." ); |
| 340 |
} |
| 341 |
|
| 342 |
if ( !is_array( $values ) ) { |
| 343 |
$values = [ $values ]; |
| 344 |
} |
| 345 |
|
| 346 |
foreach ( $values as $value ) { |
| 347 |
$rows[] = [ |
| 348 |
'ls_field' => $tag, |
| 349 |
'ls_value' => $value, |
| 350 |
'ls_log_id' => $this->id |
| 351 |
]; |
| 352 |
} |
| 353 |
} |
| 354 |
if ( count( $rows ) ) { |
| 355 |
$dbw->newInsertQueryBuilder() |
| 356 |
->insertInto( 'log_search' ) |
| 357 |
->ignore() |
| 358 |
->rows( $rows ) |
| 359 |
->caller( __METHOD__ ) |
| 360 |
->execute(); |
| 361 |
} |
| 362 |
|
| 363 |
return $this->id; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public function getRecentChange( $newId = 0 ) { |
| 374 |
$formatter = MediaWikiServices::getInstance()->getLogFormatterFactory()->newFromEntry( $this ); |
| 375 |
$context = RequestContext::newExtraneousContext( $this->getTarget() ); |
| 376 |
$formatter->setContext( $context ); |
| 377 |
|
| 378 |
$logpage = SpecialPage::getTitleFor( 'Log', $this->getType() ); |
| 379 |
|
| 380 |
return RecentChange::newLogEntry( |
| 381 |
$this->getTimestamp(), |
| 382 |
$logpage, |
| 383 |
$this->getPerformerIdentity(), |
| 384 |
$formatter->getPlainActionText(), |
| 385 |
'', |
| 386 |
$this->getType(), |
| 387 |
$this->getSubtype(), |
| 388 |
$this->getTarget(), |
| 389 |
$this->getComment(), |
| 390 |
LogEntryBase::makeParamBlob( $this->getParameters() ), |
| 391 |
$newId, |
| 392 |
$formatter->getIRCActionComment(), |
| 393 |
$this->getAssociatedRevId(), |
| 394 |
$this->getIsPatrollable(), |
| 395 |
$this->forceBotFlag, |
| 396 |
$this->getDeleted() |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public function publish( $newId, $to = 'rcandudp' ) { |
| 407 |
$canAddTags = true; |
| 408 |
|
| 409 |
if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) { |
| 410 |
\MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning( |
| 411 |
'newId and/or revId must be set when calling ManualLogEntry::publish()', |
| 412 |
[ |
| 413 |
'newId' => $newId, |
| 414 |
'to' => $to, |
| 415 |
'revId' => $this->getAssociatedRevId(), |
| 416 |
|
| 417 |
'exception' => new RuntimeException() |
| 418 |
] |
| 419 |
); |
| 420 |
$canAddTags = false; |
| 421 |
} |
| 422 |
|
| 423 |
DeferredUpdates::addCallableUpdate( |
| 424 |
function () use ( $newId, $to, $canAddTags ) { |
| 425 |
$log = new LogPage( $this->getType() ); |
| 426 |
if ( !$log->isRestricted() ) { |
| 427 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 428 |
->onManualLogEntryBeforePublish( $this ); |
| 429 |
$rc = $this->getRecentChange( $newId ); |
| 430 |
|
| 431 |
if ( $to === 'rc' || $to === 'rcandudp' ) { |
| 432 |
|
| 433 |
$rc->addTags( $this->getTags() ); |
| 434 |
$rc->save( $rc::SEND_NONE ); |
| 435 |
} else { |
| 436 |
$tags = $this->getTags(); |
| 437 |
if ( $tags && $canAddTags ) { |
| 438 |
$revId = $this->getAssociatedRevId(); |
| 439 |
MediaWikiServices::getInstance()->getChangeTagsStore()->addTags( |
| 440 |
$tags, |
| 441 |
null, |
| 442 |
$revId > 0 ? $revId : null, |
| 443 |
$newId > 0 ? $newId : null |
| 444 |
); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
if ( $to === 'udp' || $to === 'rcandudp' ) { |
| 449 |
$rc->notifyRCFeeds(); |
| 450 |
} |
| 451 |
} |
| 452 |
}, |
| 453 |
DeferredUpdates::POSTSEND, |
| 454 |
MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase() |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function getType() { |
| 462 |
return $this->type; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
public function getSubtype() { |
| 469 |
return $this->subtype; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
public function getParameters() { |
| 476 |
return $this->parameters; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public function getPerformerIdentity(): UserIdentity { |
| 483 |
return $this->performer; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public function getTarget() { |
| 490 |
return $this->target; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public function getTimestamp() { |
| 497 |
$ts = $this->timestamp ?? wfTimestampNow(); |
| 498 |
|
| 499 |
return wfTimestamp( TS_MW, $ts ); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public function getComment() { |
| 506 |
return $this->comment; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public function getAssociatedRevId() { |
| 514 |
return $this->revId; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function getTags() { |
| 522 |
return $this->tags; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public function getIsPatrollable() { |
| 532 |
return $this->isPatrollable; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public function isLegacy() { |
| 540 |
return $this->legacy; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public function getDeleted() { |
| 547 |
return (int)$this->deleted; |
| 548 |
} |
| 549 |
} |