| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
use MediaWiki\Content\Content; |
| 28 |
use MediaWiki\Content\ContentHandler; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Revision\MutableRevisionSlots; |
| 31 |
use MediaWiki\Revision\SlotRecord; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\ExternalUserNames; |
| 34 |
use MediaWiki\User\User; |
| 35 |
use MediaWiki\User\UserIdentityValue; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class WikiRevision implements ImportableUploadRevision, ImportableOldRevision { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public $title = null; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public $id = 0; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public $timestamp = "20010115000000"; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public $user_text = ""; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public $userObj = null; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public $model = null; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public $format = null; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public $text = ""; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
protected $size; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public $content = null; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
protected $contentHandler = null; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public $comment = ""; |
| 123 |
|
| 124 |
private MutableRevisionSlots $slots; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public $minor = false; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public $type = ""; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public $action = ""; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public $params = ""; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public $fileSrc = ''; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private $sha1base36; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected $tags = []; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public $archiveName = ''; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
protected $filename; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
protected $src = null; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public $isTemp = false; |
| 191 |
|
| 192 |
|
| 193 |
private $mNoUpdates = false; |
| 194 |
|
| 195 |
public function __construct() { |
| 196 |
$this->slots = new MutableRevisionSlots(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function setTitle( Title $title ) { |
| 203 |
$this->title = $title; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function setID( $id ) { |
| 211 |
$this->id = $id; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function setTimestamp( $ts ) { |
| 219 |
|
| 220 |
$this->timestamp = wfTimestamp( TS_MW, $ts ); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function setUsername( $user ) { |
| 228 |
$this->user_text = $user; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function setUserObj( $user ) { |
| 237 |
|
| 238 |
$this->userObj = $user ?: null; |
| 239 |
if ( $this->user_text === '' && $user ) { |
| 240 |
$this->user_text = $user->getName(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function setUserIP( $ip ) { |
| 250 |
$this->user_text = $ip; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function setModel( $model ) { |
| 259 |
$this->model = $model; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function setFormat( $format ) { |
| 268 |
$this->format = $format; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function setText( $text ) { |
| 277 |
$handler = ContentHandler::getForModelID( $this->getModel() ); |
| 278 |
$content = $handler->unserializeContent( $text ); |
| 279 |
$this->setContent( SlotRecord::MAIN, $content ); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function setContent( $role, $content ) { |
| 288 |
$this->slots->setContent( $role, $content ); |
| 289 |
|
| 290 |
|
| 291 |
if ( $role === SlotRecord::MAIN ) { |
| 292 |
$this->content = $content; |
| 293 |
$this->model = $content->getModel(); |
| 294 |
$this->format = $content->getDefaultFormat(); |
| 295 |
$this->text = $content->serialize(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function setComment( string $text ) { |
| 304 |
$this->comment = $text; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function setMinor( $minor ) { |
| 312 |
$this->minor = (bool)$minor; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function setSrc( $src ) { |
| 320 |
$this->src = $src; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function setFileSrc( $src, $isTemp ) { |
| 329 |
$this->fileSrc = $src; |
| 330 |
$this->isTemp = $isTemp; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function setSha1Base36( $sha1base36 ) { |
| 338 |
$this->sha1base36 = $sha1base36 ?: null; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function setTags( array $tags ) { |
| 346 |
$this->tags = $tags; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function setFilename( $filename ) { |
| 354 |
$this->filename = $filename; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function setArchiveName( $archiveName ) { |
| 362 |
$this->archiveName = $archiveName; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public function setSize( $size ) { |
| 370 |
$this->size = intval( $size ); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function setType( $type ) { |
| 378 |
$this->type = $type; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function setAction( $action ) { |
| 386 |
$this->action = $action; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function setParams( $params ) { |
| 394 |
$this->params = $params; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function setNoUpdates( $noupdates ) { |
| 402 |
$this->mNoUpdates = $noupdates; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
public function getTitle() { |
| 410 |
return $this->title; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function getID() { |
| 418 |
return $this->id; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function getTimestamp() { |
| 426 |
return $this->timestamp; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public function getUser() { |
| 434 |
return $this->user_text; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public function getUserObj() { |
| 443 |
return $this->userObj; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public function getText() { |
| 451 |
return $this->text; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function getContentHandler() { |
| 461 |
$this->contentHandler ??= MediaWikiServices::getInstance() |
| 462 |
->getContentHandlerFactory() |
| 463 |
->getContentHandler( $this->getModel() ); |
| 464 |
|
| 465 |
return $this->contentHandler; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function getContent( $role = SlotRecord::MAIN ) { |
| 474 |
return $this->slots->getContent( $role ); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public function getSlot( $role ) { |
| 483 |
return $this->slots->getSlot( $role ); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public function getSlotRoles() { |
| 491 |
return $this->slots->getSlotRoles(); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function getModel() { |
| 500 |
$this->model ??= $this->getTitle()->getContentModel(); |
| 501 |
|
| 502 |
return $this->model; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public function getFormat() { |
| 511 |
$this->format ??= $this->getContentHandler()->getDefaultFormat(); |
| 512 |
|
| 513 |
return $this->format; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function getComment(): string { |
| 521 |
return $this->comment; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public function getMinor() { |
| 529 |
return $this->minor; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public function getSrc() { |
| 537 |
return $this->src; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public function getSha1() { |
| 545 |
if ( $this->sha1base36 ) { |
| 546 |
return Wikimedia\base_convert( $this->sha1base36, 36, 16 ); |
| 547 |
} |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public function getSha1Base36() { |
| 556 |
return $this->sha1base36 ?? false; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public function getTags() { |
| 564 |
return $this->tags; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public function getFileSrc() { |
| 572 |
return $this->fileSrc; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public function isTempSrc() { |
| 580 |
return $this->isTemp; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public function getFilename() { |
| 588 |
return $this->filename; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public function getArchiveName() { |
| 596 |
return $this->archiveName; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
public function getSize() { |
| 604 |
return $this->size; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public function getType() { |
| 612 |
return $this->type; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public function getAction() { |
| 620 |
return $this->action; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
public function getParams() { |
| 628 |
return $this->params; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public function importOldRevision() { |
| 637 |
if ( $this->mNoUpdates ) { |
| 638 |
$importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates(); |
| 639 |
} else { |
| 640 |
$importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporter(); |
| 641 |
} |
| 642 |
return $importer->import( $this ); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public function importLogItem() { |
| 650 |
$services = MediaWikiServices::getInstance(); |
| 651 |
$dbw = $services->getConnectionProvider()->getPrimaryDatabase(); |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if ( LogEntryBase::containsUnsafeParams( |
| 656 |
LogEntryBase::extractParams( $this->params, "{$this->type}/{$this->action}" ), |
| 657 |
) ) { |
| 658 |
wfDebug( __METHOD__ |
| 659 |
. ": skipping {$this->type}/{$this->action} with unsafe params" ); |
| 660 |
return false; |
| 661 |
} |
| 662 |
|
| 663 |
$userName = $this->getUser(); |
| 664 |
if ( ExternalUserNames::isExternal( $userName ) ) { |
| 665 |
|
| 666 |
$user = UserIdentityValue::newAnonymous( $userName ); |
| 667 |
} else { |
| 668 |
$user = $this->getUserObj() ?: User::newFromName( $userName, false ); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
if ( !$this->getTitle() ) { |
| 673 |
wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " . |
| 674 |
$this->timestamp ); |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
$prior = (bool)$dbw->newSelectQueryBuilder() |
| 680 |
->select( '1' ) |
| 681 |
->from( 'logging' ) |
| 682 |
->where( [ |
| 683 |
'log_type' => $this->getType(), |
| 684 |
'log_action' => $this->getAction(), |
| 685 |
'log_timestamp' => $dbw->timestamp( $this->timestamp ), |
| 686 |
'log_namespace' => $this->getTitle()->getNamespace(), |
| 687 |
'log_title' => $this->getTitle()->getDBkey(), |
| 688 |
'log_params' => $this->params |
| 689 |
] ) |
| 690 |
->caller( __METHOD__ )->fetchField(); |
| 691 |
|
| 692 |
if ( $prior ) { |
| 693 |
wfDebug( __METHOD__ |
| 694 |
. ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp " |
| 695 |
. $this->timestamp ); |
| 696 |
return false; |
| 697 |
} |
| 698 |
$actorId = $services->getActorNormalization()->acquireActorId( $user, $dbw ); |
| 699 |
$dbw->newInsertQueryBuilder() |
| 700 |
->insertInto( 'logging' ) |
| 701 |
->row( [ |
| 702 |
'log_type' => $this->type, |
| 703 |
'log_action' => $this->action, |
| 704 |
'log_timestamp' => $dbw->timestamp( $this->timestamp ), |
| 705 |
'log_actor' => $actorId, |
| 706 |
'log_namespace' => $this->getTitle()->getNamespace(), |
| 707 |
'log_title' => $this->getTitle()->getDBkey(), |
| 708 |
'log_params' => $this->params |
| 709 |
] + $services->getCommentStore()->insert( $dbw, 'log_comment', $this->getComment() ) ) |
| 710 |
->caller( __METHOD__ )->execute(); |
| 711 |
return true; |
| 712 |
} |
| 713 |
|
| 714 |
} |