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 
174  protected $filename;
175 
180  protected $src = null;
181 
187  public $isTemp = false;
188 
195  public $fileIsTemp;
196 
198  private $mNoUpdates = false;
199 
204  private $config;
205 
210  public function __construct( Config $config ) {
211  $this->config = $config;
212  $this->slots = new MutableRevisionSlots();
213  }
214 
220  public function setTitle( $title ) {
221  if ( is_object( $title ) ) {
222  $this->title = $title;
223  } elseif ( $title === null ) {
224  throw new MWException( "WikiRevision given a null title in import. "
225  . "You may need to adjust \$wgLegalTitleChars." );
226  } else {
227  throw new MWException( "WikiRevision given non-object title in import." );
228  }
229  }
230 
235  public function setID( $id ) {
236  $this->id = $id;
237  }
238 
243  public function setTimestamp( $ts ) {
244  # 2003-08-05T18:30:02Z
245  $this->timestamp = wfTimestamp( TS_MW, $ts );
246  }
247 
252  public function setUsername( $user ) {
253  $this->user_text = $user;
254  }
255 
260  public function setUserObj( $user ) {
261  $this->userObj = $user;
262  }
263 
268  public function setUserIP( $ip ) {
269  $this->user_text = $ip;
270  }
271 
277  public function setModel( $model ) {
278  $this->model = $model;
279  }
280 
286  public function setFormat( $format ) {
287  $this->format = $format;
288  }
289 
295  public function setText( $text ) {
296  $handler = ContentHandler::getForModelID( $this->getModel() );
297  $content = $handler->unserializeContent( $text );
298  $this->setContent( SlotRecord::MAIN, $content );
299  }
300 
306  public function setContent( $role, $content ) {
307  $this->slots->setContent( $role, $content );
308 
309  // backwards compat
310  if ( $role === SlotRecord::MAIN ) {
311  $this->content = $content;
312  $this->model = $content->getModel();
313  $this->format = $content->getDefaultFormat();
314  $this->text = $content->serialize();
315  }
316  }
317 
322  public function setComment( string $text ) {
323  $this->comment = $text;
324  }
325 
330  public function setMinor( $minor ) {
331  $this->minor = (bool)$minor;
332  }
333 
338  public function setSrc( $src ) {
339  $this->src = $src;
340  }
341 
347  public function setFileSrc( $src, $isTemp ) {
348  $this->fileSrc = $src;
349  $this->fileIsTemp = $isTemp;
350  $this->isTemp = $isTemp;
351  }
352 
357  public function setSha1Base36( $sha1base36 ) {
358  $this->sha1base36 = $sha1base36;
359  }
360 
365  public function setTags( array $tags ) {
366  $this->tags = $tags;
367  }
368 
373  public function setFilename( $filename ) {
374  $this->filename = $filename;
375  }
376 
381  public function setArchiveName( $archiveName ) {
382  $this->archiveName = $archiveName;
383  }
384 
389  public function setSize( $size ) {
390  $this->size = intval( $size );
391  }
392 
397  public function setType( $type ) {
398  $this->type = $type;
399  }
400 
405  public function setAction( $action ) {
406  $this->action = $action;
407  }
408 
413  public function setParams( $params ) {
414  $this->params = $params;
415  }
416 
421  public function setNoUpdates( $noupdates ) {
422  $this->mNoUpdates = $noupdates;
423  }
424 
429  public function getTitle() {
430  return $this->title;
431  }
432 
437  public function getID() {
438  return $this->id;
439  }
440 
445  public function getTimestamp() {
446  return $this->timestamp;
447  }
448 
453  public function getUser() {
454  return $this->user_text;
455  }
456 
461  public function getUserObj() {
462  return $this->userObj;
463  }
464 
469  public function getText() {
470  return $this->text;
471  }
472 
479  public function getContentHandler() {
480  if ( $this->contentHandler === null ) {
481  $this->contentHandler = MediaWikiServices::getInstance()
482  ->getContentHandlerFactory()
483  ->getContentHandler( $this->getModel() );
484  }
485 
486  return $this->contentHandler;
487  }
488 
494  public function getContent( $role = SlotRecord::MAIN ) {
495  return $this->slots->getContent( $role );
496  }
497 
503  public function getSlot( $role ) {
504  return $this->slots->getSlot( $role );
505  }
506 
511  public function getSlotRoles() {
512  return $this->slots->getSlotRoles();
513  }
514 
520  public function getModel() {
521  if ( $this->model === null ) {
522  $this->model = $this->getTitle()->getContentModel();
523  }
524 
525  return $this->model;
526  }
527 
533  public function getFormat() {
534  if ( $this->format === null ) {
535  $this->format = $this->getContentHandler()->getDefaultFormat();
536  }
537 
538  return $this->format;
539  }
540 
545  public function getComment(): string {
546  return $this->comment;
547  }
548 
553  public function getMinor() {
554  return $this->minor;
555  }
556 
561  public function getSrc() {
562  return $this->src;
563  }
564 
569  public function getSha1() {
570  if ( $this->sha1base36 ) {
571  return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
572  }
573  return false;
574  }
575 
580  public function getSha1Base36() {
581  if ( $this->sha1base36 ) {
582  return $this->sha1base36;
583  }
584  return false;
585  }
586 
591  public function getTags() {
592  return $this->tags;
593  }
594 
599  public function getFileSrc() {
600  return $this->fileSrc;
601  }
602 
607  public function isTempSrc() {
608  return $this->isTemp;
609  }
610 
615  public function getFilename() {
616  return $this->filename;
617  }
618 
623  public function getArchiveName() {
624  return $this->archiveName;
625  }
626 
631  public function getSize() {
632  return $this->size;
633  }
634 
639  public function getType() {
640  return $this->type;
641  }
642 
647  public function getAction() {
648  return $this->action;
649  }
650 
655  public function getParams() {
656  return $this->params;
657  }
658 
664  public function importOldRevision() {
665  if ( $this->mNoUpdates ) {
666  $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
667  } else {
668  $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporter();
669  }
670  return $importer->import( $this );
671  }
672 
677  public function importLogItem() {
678  $dbw = wfGetDB( DB_PRIMARY );
679 
680  $user = $this->getUserObj() ?: User::newFromName( $this->getUser(), false );
681 
682  # @todo FIXME: This will not record autoblocks
683  if ( !$this->getTitle() ) {
684  wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
685  $this->timestamp );
686  return false;
687  }
688  # Check if it exists already
689  // @todo FIXME: Use original log ID (better for backups)
690  $prior = (bool)$dbw->selectField( 'logging', '1',
691  [ 'log_type' => $this->getType(),
692  'log_action' => $this->getAction(),
693  'log_timestamp' => $dbw->timestamp( $this->timestamp ),
694  'log_namespace' => $this->getTitle()->getNamespace(),
695  'log_title' => $this->getTitle()->getDBkey(),
696  'log_params' => $this->params ],
697  __METHOD__
698  );
699  // @todo FIXME: This could fail slightly for multiple matches :P
700  if ( $prior ) {
701  wfDebug( __METHOD__
702  . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
703  . $this->timestamp );
704  return false;
705  }
706  $actorId = MediaWikiServices::getInstance()->getActorNormalization()
707  ->acquireActorId( $user, $dbw );
708  $data = [
709  'log_type' => $this->type,
710  'log_action' => $this->action,
711  'log_timestamp' => $dbw->timestamp( $this->timestamp ),
712  'log_actor' => $actorId,
713  'log_namespace' => $this->getTitle()->getNamespace(),
714  'log_title' => $this->getTitle()->getDBkey(),
715  'log_params' => $this->params
716  ] + CommentStore::getStore()->insert( $dbw, 'log_comment', $this->getComment() );
717  $dbw->insert( 'logging', $data, __METHOD__ );
718 
719  return true;
720  }
721 
727  public function importUpload() {
728  wfDeprecated( __METHOD__, '1.31' );
729 
730  $importer = MediaWikiServices::getInstance()->getWikiRevisionUploadImporter();
731  $statusValue = $importer->import( $this );
732  return $statusValue->isGood();
733  }
734 
740  public function downloadSource() {
741  $importer = new ImportableUploadRevisionImporter(
742  $this->config->get( 'EnableUploads' ),
743  LoggerFactory::getInstance( 'UploadRevisionImporter' )
744  );
745  return $importer->downloadSource( $this );
746  }
747 
748 }
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:60
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:275
WikiRevision\getUser
getUser()
Definition: WikiRevision.php:453
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
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:511
WikiRevision\setUserObj
setUserObj( $user)
Definition: WikiRevision.php:260
WikiRevision\$src
string null $src
Definition: WikiRevision.php:180
WikiRevision\getComment
getComment()
Definition: WikiRevision.php:545
WikiRevision\setTags
setTags(array $tags)
Definition: WikiRevision.php:365
WikiRevision\downloadSource
downloadSource()
Definition: WikiRevision.php:740
WikiRevision\setNoUpdates
setNoUpdates( $noupdates)
Definition: WikiRevision.php:421
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
WikiRevision\$userObj
User $userObj
Definition: WikiRevision.php:69
WikiRevision\isTempSrc
isTempSrc()
Definition: WikiRevision.php:607
WikiRevision\importUpload
importUpload()
Definition: WikiRevision.php:727
WikiRevision\$user_text
string $user_text
Definition: WikiRevision.php:63
WikiRevision\getSrc
getSrc()
Definition: WikiRevision.php:561
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:1668
WikiRevision\getTitle
getTitle()
Definition: WikiRevision.php:429
WikiRevision\getSha1
getSha1()
Definition: WikiRevision.php:569
WikiRevision\setSha1Base36
setSha1Base36( $sha1base36)
Definition: WikiRevision.php:357
WikiRevision\getFilename
getFilename()
Definition: WikiRevision.php:615
WikiRevision\$model
string $model
Definition: WikiRevision.php:76
WikiRevision\setText
setText( $text)
Definition: WikiRevision.php:295
WikiRevision\setFilename
setFilename( $filename)
Definition: WikiRevision.php:373
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:606
WikiRevision\setUsername
setUsername( $user)
Definition: WikiRevision.php:252
MediaWiki\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:639
WikiRevision\__construct
__construct(Config $config)
Definition: WikiRevision.php:210
WikiRevision\getModel
getModel()
Definition: WikiRevision.php:520
WikiRevision\$isTemp
bool $isTemp
Definition: WikiRevision.php:187
WikiRevision\$minor
bool $minor
Definition: WikiRevision.php:126
WikiRevision\$params
string $params
Definition: WikiRevision.php:144
WikiRevision\$mNoUpdates
bool $mNoUpdates
Definition: WikiRevision.php:198
Config
Interface for configuration instances.
Definition: Config.php:30
WikiRevision\getParams
getParams()
Definition: WikiRevision.php:655
WikiRevision\setUserIP
setUserIP( $ip)
Definition: WikiRevision.php:268
MWException
MediaWiki exception.
Definition: MWException.php:29
WikiRevision\getTags
getTags()
Definition: WikiRevision.php:591
WikiRevision\$content
Content $content
Definition: WikiRevision.php:103
WikiRevision\getFormat
getFormat()
Definition: WikiRevision.php:533
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
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:445
WikiRevision\setModel
setModel( $model)
Definition: WikiRevision.php:277
WikiRevision\getSha1Base36
getSha1Base36()
Definition: WikiRevision.php:580
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2202
WikiRevision\setMinor
setMinor( $minor)
Definition: WikiRevision.php:330
WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition: WikiRevision.php:494
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:894
WikiRevision\setID
setID( $id)
Definition: WikiRevision.php:235
WikiRevision\getArchiveName
getArchiveName()
Definition: WikiRevision.php:623
WikiRevision\setFormat
setFormat( $format)
Definition: WikiRevision.php:286
WikiRevision\getMinor
getMinor()
Definition: WikiRevision.php:553
WikiRevision\getText
getText()
Definition: WikiRevision.php:469
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:397
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
WikiRevision\getSlot
getSlot( $role)
Definition: WikiRevision.php:503
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:479
WikiRevision\setTitle
setTitle( $title)
Definition: WikiRevision.php:220
WikiRevision\setTimestamp
setTimestamp( $ts)
Definition: WikiRevision.php:243
Content
Base interface for content objects.
Definition: Content.php:35
WikiRevision\setContent
setContent( $role, $content)
Definition: WikiRevision.php:306
WikiRevision\setParams
setParams( $params)
Definition: WikiRevision.php:413
Title
Represents a title within MediaWiki.
Definition: Title.php:48
WikiRevision\importLogItem
importLogItem()
Definition: WikiRevision.php:677
ImportableUploadRevisionImporter
Definition: ImportableUploadRevisionImporter.php:9
WikiRevision\$fileIsTemp
$fileIsTemp
Definition: WikiRevision.php:195
WikiRevision\setSrc
setSrc( $src)
Definition: WikiRevision.php:338
WikiRevision
Represents a revision, log entry or upload during the import process.
Definition: WikiRevision.php:39
WikiRevision\setFileSrc
setFileSrc( $src, $isTemp)
Definition: WikiRevision.php:347
WikiRevision\setArchiveName
setArchiveName( $archiveName)
Definition: WikiRevision.php:381
WikiRevision\getFileSrc
getFileSrc()
Definition: WikiRevision.php:599
WikiRevision\$title
Title $title
Definition: WikiRevision.php:45
WikiRevision\getUserObj
getUserObj()
Definition: WikiRevision.php:461
WikiRevision\importOldRevision
importOldRevision()
Definition: WikiRevision.php:664
WikiRevision\setComment
setComment(string $text)
Definition: WikiRevision.php:322
Content\getModel
getModel()
Returns the ID of the content model used by this Content object.
WikiRevision\getID
getID()
Definition: WikiRevision.php:437
WikiRevision\setAction
setAction( $action)
Definition: WikiRevision.php:405
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:389
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
WikiRevision\$id
int $id
Definition: WikiRevision.php:51
WikiRevision\getSize
getSize()
Definition: WikiRevision.php:631
WikiRevision\$config
Config $config
Definition: WikiRevision.php:204
WikiRevision\getAction
getAction()
Definition: WikiRevision.php:647
WikiRevision\$fileSrc
string $fileSrc
Definition: WikiRevision.php:150
WikiRevision\$filename
string null $filename
Definition: WikiRevision.php:174
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40