MediaWiki  master
WikiRevision.php
Go to the documentation of this file.
1 <?php
30 
40 
45  public $title = null;
46 
51  public $id = 0;
52 
57  public $timestamp = "20010115000000";
58 
63  public $user_text = "";
64 
69  public $userObj = null;
70 
76  public $model = null;
77 
83  public $format = null;
84 
90  public $text = "";
91 
96  protected $size;
97 
103  public $content = null;
104 
109  protected $contentHandler = null;
110 
115  public $comment = "";
116 
120  private $slots;
121 
126  public $minor = false;
127 
132  public $type = "";
133 
138  public $action = "";
139 
144  public $params = "";
145 
150  public $fileSrc = '';
151 
156  public $sha1base36 = false;
157 
162  protected $tags = [];
163 
168  public $archiveName = '';
169 
173  protected $filename;
174 
179  protected $src = null;
180 
186  public $isTemp = false;
187 
194  public $fileIsTemp;
195 
197  private $mNoUpdates = false;
198 
203  private $config;
204 
209  public function __construct( Config $config ) {
210  $this->config = $config;
211  $this->slots = new MutableRevisionSlots();
212  }
213 
219  public function setTitle( $title ) {
220  if ( is_object( $title ) ) {
221  $this->title = $title;
222  } elseif ( $title === null ) {
223  throw new MWException( "WikiRevision given a null title in import. "
224  . "You may need to adjust \$wgLegalTitleChars." );
225  } else {
226  throw new MWException( "WikiRevision given non-object title in import." );
227  }
228  }
229 
234  public function setID( $id ) {
235  $this->id = $id;
236  }
237 
242  public function setTimestamp( $ts ) {
243  # 2003-08-05T18:30:02Z
244  $this->timestamp = wfTimestamp( TS_MW, $ts );
245  }
246 
251  public function setUsername( $user ) {
252  $this->user_text = $user;
253  }
254 
259  public function setUserObj( $user ) {
260  $this->userObj = $user;
261  }
262 
267  public function setUserIP( $ip ) {
268  $this->user_text = $ip;
269  }
270 
276  public function setModel( $model ) {
277  $this->model = $model;
278  }
279 
285  public function setFormat( $format ) {
286  $this->format = $format;
287  }
288 
294  public function setText( $text ) {
295  $handler = ContentHandler::getForModelID( $this->model );
296  $content = $handler->unserializeContent( $text );
297  $this->setContent( SlotRecord::MAIN, $content );
298  }
299 
305  public function setContent( $role, $content ) {
306  $this->slots->setContent( $role, $content );
307 
308  // backwards compat
309  if ( $role === SlotRecord::MAIN ) {
310  $this->content = $content;
311  $this->model = $content->getModel();
312  $this->format = $content->getDefaultFormat();
313  $this->text = $content->serialize();
314  }
315  }
316 
321  public function setComment( $text ) {
322  $this->comment = $text;
323  }
324 
329  public function setMinor( $minor ) {
330  $this->minor = (bool)$minor;
331  }
332 
337  public function setSrc( $src ) {
338  $this->src = $src;
339  }
340 
346  public function setFileSrc( $src, $isTemp ) {
347  $this->fileSrc = $src;
348  $this->fileIsTemp = $isTemp;
349  $this->isTemp = $isTemp;
350  }
351 
356  public function setSha1Base36( $sha1base36 ) {
357  $this->sha1base36 = $sha1base36;
358  }
359 
364  public function setTags( array $tags ) {
365  $this->tags = $tags;
366  }
367 
372  public function setFilename( $filename ) {
373  $this->filename = $filename;
374  }
375 
380  public function setArchiveName( $archiveName ) {
381  $this->archiveName = $archiveName;
382  }
383 
388  public function setSize( $size ) {
389  $this->size = intval( $size );
390  }
391 
396  public function setType( $type ) {
397  $this->type = $type;
398  }
399 
404  public function setAction( $action ) {
405  $this->action = $action;
406  }
407 
412  public function setParams( $params ) {
413  $this->params = $params;
414  }
415 
420  public function setNoUpdates( $noupdates ) {
421  $this->mNoUpdates = $noupdates;
422  }
423 
428  public function getTitle() {
429  return $this->title;
430  }
431 
436  public function getID() {
437  return $this->id;
438  }
439 
444  public function getTimestamp() {
445  return $this->timestamp;
446  }
447 
452  public function getUser() {
453  return $this->user_text;
454  }
455 
460  public function getUserObj() {
461  return $this->userObj;
462  }
463 
468  public function getText() {
469  return $this->text;
470  }
471 
478  public function getContentHandler() {
479  if ( $this->contentHandler === null ) {
480  $this->contentHandler = MediaWikiServices::getInstance()
481  ->getContentHandlerFactory()
482  ->getContentHandler( $this->getModel() );
483  }
484 
485  return $this->contentHandler;
486  }
487 
493  public function getContent( $role = SlotRecord::MAIN ) {
494  return $this->slots->getContent( $role );
495  }
496 
502  public function getSlot( $role ) {
503  return $this->slots->getSlot( $role );
504  }
505 
510  public function getSlotRoles() {
511  return $this->slots->getSlotRoles();
512  }
513 
519  public function getModel() {
520  if ( $this->model === null ) {
521  $this->model = $this->getTitle()->getContentModel();
522  }
523 
524  return $this->model;
525  }
526 
532  public function getFormat() {
533  if ( $this->format === null ) {
534  $this->format = $this->getContentHandler()->getDefaultFormat();
535  }
536 
537  return $this->format;
538  }
539 
544  public function getComment() {
545  return $this->comment;
546  }
547 
552  public function getMinor() {
553  return $this->minor;
554  }
555 
560  public function getSrc() {
561  return $this->src;
562  }
563 
568  public function getSha1() {
569  if ( $this->sha1base36 ) {
570  return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
571  }
572  return false;
573  }
574 
579  public function getSha1Base36() {
580  if ( $this->sha1base36 ) {
581  return $this->sha1base36;
582  }
583  return false;
584  }
585 
590  public function getTags() {
591  return $this->tags;
592  }
593 
598  public function getFileSrc() {
599  return $this->fileSrc;
600  }
601 
606  public function isTempSrc() {
607  return $this->isTemp;
608  }
609 
614  public function getFilename() {
615  return $this->filename;
616  }
617 
622  public function getArchiveName() {
623  return $this->archiveName;
624  }
625 
630  public function getSize() {
631  return $this->size;
632  }
633 
638  public function getType() {
639  return $this->type;
640  }
641 
646  public function getAction() {
647  return $this->action;
648  }
649 
654  public function getParams() {
655  return $this->params;
656  }
657 
663  public function importOldRevision() {
664  if ( $this->mNoUpdates ) {
665  $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
666  } else {
667  $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporter();
668  }
669  return $importer->import( $this );
670  }
671 
676  public function importLogItem() {
677  $dbw = wfGetDB( DB_MASTER );
678 
679  $user = $this->getUserObj() ?: User::newFromName( $this->getUser(), false );
680 
681  # @todo FIXME: This will not record autoblocks
682  if ( !$this->getTitle() ) {
683  wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
684  $this->timestamp );
685  return false;
686  }
687  # Check if it exists already
688  // @todo FIXME: Use original log ID (better for backups)
689  $prior = $dbw->selectField( 'logging', '1',
690  [ 'log_type' => $this->getType(),
691  'log_action' => $this->getAction(),
692  'log_timestamp' => $dbw->timestamp( $this->timestamp ),
693  'log_namespace' => $this->getTitle()->getNamespace(),
694  'log_title' => $this->getTitle()->getDBkey(),
695  'log_params' => $this->params ],
696  __METHOD__
697  );
698  // @todo FIXME: This could fail slightly for multiple matches :P
699  if ( $prior ) {
700  wfDebug( __METHOD__
701  . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
702  . $this->timestamp );
703  return false;
704  }
705  $data = [
706  'log_type' => $this->type,
707  'log_action' => $this->action,
708  'log_timestamp' => $dbw->timestamp( $this->timestamp ),
709  'log_namespace' => $this->getTitle()->getNamespace(),
710  'log_title' => $this->getTitle()->getDBkey(),
711  'log_params' => $this->params
712  ] + CommentStore::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
713  + ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $user );
714  $dbw->insert( 'logging', $data, __METHOD__ );
715 
716  return true;
717  }
718 
724  public function importUpload() {
725  $importer = MediaWikiServices::getInstance()->getWikiRevisionUploadImporter();
726  $statusValue = $importer->import( $this );
727  return $statusValue->isGood();
728  }
729 
735  public function downloadSource() {
736  $importer = new ImportableUploadRevisionImporter(
737  $this->config->get( 'EnableUploads' ),
738  LoggerFactory::getInstance( 'UploadRevisionImporter' )
739  );
740  return $importer->downloadSource( $this );
741  }
742 
743 }
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:59
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:269
WikiRevision\getUser
getUser()
Definition: WikiRevision.php:452
Content\getDefaultFormat
getDefaultFormat()
Convenience method that returns the default serialization format for the content model that this Cont...
WikiRevision\$format
string $format
Definition: WikiRevision.php:83
WikiRevision\$filename
$filename
Definition: WikiRevision.php:173
Content\serialize
serialize( $format=null)
Convenience method for serializing this Content object.
WikiRevision\$type
string $type
Definition: WikiRevision.php:132
WikiRevision\getSlotRoles
getSlotRoles()
Definition: WikiRevision.php:510
WikiRevision\setUserObj
setUserObj( $user)
Definition: WikiRevision.php:259
WikiRevision\$src
string null $src
Definition: WikiRevision.php:179
WikiRevision\getComment
getComment()
Definition: WikiRevision.php:544
WikiRevision\setTags
setTags(array $tags)
Definition: WikiRevision.php:364
WikiRevision\downloadSource
downloadSource()
Definition: WikiRevision.php:735
WikiRevision\setNoUpdates
setNoUpdates( $noupdates)
Definition: WikiRevision.php:420
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:160
WikiRevision\setComment
setComment( $text)
Definition: WikiRevision.php:321
WikiRevision\$userObj
User $userObj
Definition: WikiRevision.php:69
WikiRevision\isTempSrc
isTempSrc()
Definition: WikiRevision.php:606
WikiRevision\importUpload
importUpload()
Definition: WikiRevision.php:724
WikiRevision\$user_text
string $user_text
Definition: WikiRevision.php:63
WikiRevision\getSrc
getSrc()
Definition: WikiRevision.php:560
WikiRevision\$size
int $size
Definition: WikiRevision.php:96
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
WikiRevision\getTitle
getTitle()
Definition: WikiRevision.php:428
WikiRevision\getSha1
getSha1()
Definition: WikiRevision.php:568
WikiRevision\setSha1Base36
setSha1Base36( $sha1base36)
Definition: WikiRevision.php:356
WikiRevision\getFilename
getFilename()
Definition: WikiRevision.php:614
WikiRevision\$model
string $model
Definition: WikiRevision.php:76
WikiRevision\setText
setText( $text)
Definition: WikiRevision.php:294
WikiRevision\setFilename
setFilename( $filename)
Definition: WikiRevision.php:372
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:542
WikiRevision\setUsername
setUsername( $user)
Definition: WikiRevision.php:251
Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
ImportableUploadRevision
Definition: ImportableUploadRevision.php:6
WikiRevision\getType
getType()
Definition: WikiRevision.php:638
WikiRevision\__construct
__construct(Config $config)
Definition: WikiRevision.php:209
WikiRevision\getModel
getModel()
Definition: WikiRevision.php:519
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:140
WikiRevision\$isTemp
bool $isTemp
Definition: WikiRevision.php:186
WikiRevision\$minor
bool $minor
Definition: WikiRevision.php:126
WikiRevision\$params
string $params
Definition: WikiRevision.php:144
WikiRevision\$mNoUpdates
bool $mNoUpdates
Definition: WikiRevision.php:197
Config
Interface for configuration instances.
Definition: Config.php:30
WikiRevision\getParams
getParams()
Definition: WikiRevision.php:654
WikiRevision\setUserIP
setUserIP( $ip)
Definition: WikiRevision.php:267
MWException
MediaWiki exception.
Definition: MWException.php:29
WikiRevision\getTags
getTags()
Definition: WikiRevision.php:590
WikiRevision\$content
Content $content
Definition: WikiRevision.php:103
WikiRevision\getFormat
getFormat()
Definition: WikiRevision.php:532
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
WikiRevision\$contentHandler
ContentHandler $contentHandler
Definition: WikiRevision.php:109
WikiRevision\getTimestamp
getTimestamp()
Definition: WikiRevision.php:444
WikiRevision\setModel
setModel( $model)
Definition: WikiRevision.php:276
WikiRevision\getSha1Base36
getSha1Base36()
Definition: WikiRevision.php:579
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2475
WikiRevision\setMinor
setMinor( $minor)
Definition: WikiRevision.php:329
WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition: WikiRevision.php:493
DB_MASTER
const DB_MASTER
Definition: defines.php:26
WikiRevision\$action
string $action
Definition: WikiRevision.php:138
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:910
WikiRevision\setID
setID( $id)
Definition: WikiRevision.php:234
WikiRevision\getArchiveName
getArchiveName()
Definition: WikiRevision.php:622
WikiRevision\setFormat
setFormat( $format)
Definition: WikiRevision.php:285
WikiRevision\getMinor
getMinor()
Definition: WikiRevision.php:552
WikiRevision\getText
getText()
Definition: WikiRevision.php:468
WikiRevision\$text
string $text
Definition: WikiRevision.php:90
WikiRevision\$sha1base36
bool string $sha1base36
Definition: WikiRevision.php:156
ImportableOldRevision
Definition: ImportableOldRevision.php:8
WikiRevision\setType
setType( $type)
Definition: WikiRevision.php:396
WikiRevision\getSlot
getSlot( $role)
Definition: WikiRevision.php:502
WikiRevision\$timestamp
string $timestamp
Definition: WikiRevision.php:57
WikiRevision\$slots
MutableRevisionSlots $slots
Definition: WikiRevision.php:120
WikiRevision\$tags
string[] $tags
Definition: WikiRevision.php:162
WikiRevision\getContentHandler
getContentHandler()
Definition: WikiRevision.php:478
WikiRevision\setTitle
setTitle( $title)
Definition: WikiRevision.php:219
WikiRevision\setTimestamp
setTimestamp( $ts)
Definition: WikiRevision.php:242
Content
Base interface for content objects.
Definition: Content.php:35
WikiRevision\setContent
setContent( $role, $content)
Definition: WikiRevision.php:305
WikiRevision\setParams
setParams( $params)
Definition: WikiRevision.php:412
Title
Represents a title within MediaWiki.
Definition: Title.php:41
WikiRevision\importLogItem
importLogItem()
Definition: WikiRevision.php:676
ImportableUploadRevisionImporter
Definition: ImportableUploadRevisionImporter.php:9
WikiRevision\$fileIsTemp
$fileIsTemp
Definition: WikiRevision.php:194
WikiRevision\setSrc
setSrc( $src)
Definition: WikiRevision.php:337
WikiRevision
Represents a revision, log entry or upload during the import process.
Definition: WikiRevision.php:39
WikiRevision\setFileSrc
setFileSrc( $src, $isTemp)
Definition: WikiRevision.php:346
WikiRevision\setArchiveName
setArchiveName( $archiveName)
Definition: WikiRevision.php:380
WikiRevision\getFileSrc
getFileSrc()
Definition: WikiRevision.php:598
WikiRevision\$title
Title $title
Definition: WikiRevision.php:45
WikiRevision\getUserObj
getUserObj()
Definition: WikiRevision.php:460
WikiRevision\importOldRevision
importOldRevision()
Definition: WikiRevision.php:663
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
WikiRevision\getID
getID()
Definition: WikiRevision.php:436
WikiRevision\setAction
setAction( $action)
Definition: WikiRevision.php:404
WikiRevision\$comment
string $comment
Definition: WikiRevision.php:115
WikiRevision\$archiveName
string $archiveName
Definition: WikiRevision.php:168
CommentStore\getStore
static getStore()
Definition: CommentStore.php:120
WikiRevision\setSize
setSize( $size)
Definition: WikiRevision.php:388
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
WikiRevision\$id
int $id
Definition: WikiRevision.php:51
WikiRevision\getSize
getSize()
Definition: WikiRevision.php:630
WikiRevision\$config
Config $config
Definition: WikiRevision.php:203
WikiRevision\getAction
getAction()
Definition: WikiRevision.php:646
WikiRevision\$fileSrc
string $fileSrc
Definition: WikiRevision.php:150
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40