3 * MediaWiki page data importer.
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup SpecialPage
26 use MediaWiki\Logger\LoggerFactory
;
27 use MediaWiki\MediaWikiServices
;
30 * Represents a revision, log entry or upload during the import process.
31 * This class sticks closely to the structure of the XML dump.
35 * @ingroup SpecialPage
37 class WikiRevision
implements ImportableUploadRevision
, ImportableOldRevision
{
55 public $timestamp = "20010115000000";
61 public $user_text = "";
67 public $userObj = null;
79 public $format = null;
97 public $content = null;
101 * @var ContentHandler
103 protected $contentHandler = null;
109 public $comment = "";
115 public $minor = false;
139 public $fileSrc = '';
145 public $sha1base36 = false;
151 protected $tags = [];
157 public $archiveName = '';
168 protected $src = null;
175 public $isTemp = false;
179 * @deprecated 1.29 use Wikirevision::isTempSrc()
180 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
181 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
186 private $mNoUpdates = false;
188 /** @var Config $config */
191 public function __construct( Config
$config ) {
192 $this->config
= $config;
196 * @since 1.7 taking a Title object (string before)
197 * @param Title $title
198 * @throws MWException
200 public function setTitle( $title ) {
201 if ( is_object( $title ) ) {
202 $this->title
= $title;
203 } elseif ( is_null( $title ) ) {
204 throw new MWException( "WikiRevision given a null title in import. "
205 . "You may need to adjust \$wgLegalTitleChars." );
207 throw new MWException( "WikiRevision given non-object title in import." );
215 public function setID( $id ) {
223 public function setTimestamp( $ts ) {
224 # 2003-08-05T18:30:02Z
225 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
230 * @param string $user
232 public function setUsername( $user ) {
233 $this->user_text
= $user;
240 public function setUserObj( $user ) {
241 $this->userObj
= $user;
248 public function setUserIP( $ip ) {
249 $this->user_text
= $ip;
254 * @param string $model
256 public function setModel( $model ) {
257 $this->model
= $model;
262 * @param string $format
264 public function setFormat( $format ) {
265 $this->format
= $format;
270 * @param string $text
272 public function setText( $text ) {
278 * @param string $text
280 public function setComment( $text ) {
281 $this->comment
= $text;
288 public function setMinor( $minor ) {
289 $this->minor
= (bool)$minor;
294 * @param string|null $src
296 public function setSrc( $src ) {
303 * @param bool $isTemp
305 public function setFileSrc( $src, $isTemp ) {
306 $this->fileSrc
= $src;
307 $this->fileIsTemp
= $isTemp;
308 $this->isTemp
= $isTemp;
313 * @param string $sha1base36
315 public function setSha1Base36( $sha1base36 ) {
316 $this->sha1base36
= $sha1base36;
321 * @param string[] $tags
323 public function setTags( array $tags ) {
329 * @param string $filename
331 public function setFilename( $filename ) {
332 $this->filename
= $filename;
337 * @param string $archiveName
339 public function setArchiveName( $archiveName ) {
340 $this->archiveName
= $archiveName;
347 public function setSize( $size ) {
348 $this->size
= intval( $size );
353 * @param string $type
355 public function setType( $type ) {
361 * @param string $action
363 public function setAction( $action ) {
364 $this->action
= $action;
369 * @param string $params
371 public function setParams( $params ) {
372 $this->params
= $params;
377 * @param bool $noupdates
379 public function setNoUpdates( $noupdates ) {
380 $this->mNoUpdates
= $noupdates;
387 public function getTitle() {
395 public function getID() {
403 public function getTimestamp() {
404 return $this->timestamp
;
411 public function getUser() {
412 return $this->user_text
;
419 public function getUserObj() {
420 return $this->userObj
;
427 public function getText() {
433 * @return ContentHandler
435 public function getContentHandler() {
436 if ( is_null( $this->contentHandler
) ) {
437 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
440 return $this->contentHandler
;
447 public function getContent() {
448 if ( is_null( $this->content
) ) {
449 $handler = $this->getContentHandler();
450 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
453 return $this->content
;
460 public function getModel() {
461 if ( is_null( $this->model
) ) {
462 $this->model
= $this->getTitle()->getContentModel();
472 public function getFormat() {
473 if ( is_null( $this->format
) ) {
474 $this->format
= $this->getContentHandler()->getDefaultFormat();
477 return $this->format
;
484 public function getComment() {
485 return $this->comment
;
492 public function getMinor() {
498 * @return string|null
500 public function getSrc() {
506 * @return bool|string
508 public function getSha1() {
509 if ( $this->sha1base36
) {
510 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
517 * @return bool|string
519 public function getSha1Base36() {
520 if ( $this->sha1base36
) {
521 return $this->sha1base36
;
530 public function getTags() {
538 public function getFileSrc() {
539 return $this->fileSrc
;
546 public function isTempSrc() {
547 return $this->isTemp
;
554 public function getFilename() {
555 return $this->filename
;
562 public function getArchiveName() {
563 return $this->archiveName
;
570 public function getSize() {
578 public function getType() {
586 public function getAction() {
587 return $this->action
;
594 public function getParams() {
595 return $this->params
;
600 * @deprecated in 1.31. Use OldRevisionImporter::import
603 public function importOldRevision() {
604 if ( $this->mNoUpdates
) {
605 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
607 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporter();
609 return $importer->import( $this );
616 public function importLogItem() {
617 $dbw = wfGetDB( DB_MASTER
);
619 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser(), false );
621 # @todo FIXME: This will not record autoblocks
622 if ( !$this->getTitle() ) {
623 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
624 $this->timestamp
. "\n" );
627 # Check if it exists already
628 // @todo FIXME: Use original log ID (better for backups)
629 $prior = $dbw->selectField( 'logging', '1',
630 [ 'log_type' => $this->getType(),
631 'log_action' => $this->getAction(),
632 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
633 'log_namespace' => $this->getTitle()->getNamespace(),
634 'log_title' => $this->getTitle()->getDBkey(),
635 'log_params' => $this->params
],
638 // @todo FIXME: This could fail slightly for multiple matches :P
641 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
642 . $this->timestamp
. "\n" );
646 'log_type' => $this->type
,
647 'log_action' => $this->action
,
648 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
649 'log_namespace' => $this->getTitle()->getNamespace(),
650 'log_title' => $this->getTitle()->getDBkey(),
651 'log_params' => $this->params
652 ] + CommentStore
::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
653 + ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $user );
654 $dbw->insert( 'logging', $data, __METHOD__
);
661 * @deprecated in 1.31. Use UploadRevisionImporter::import
664 public function importUpload() {
665 $importer = MediaWikiServices
::getInstance()->getWikiRevisionUploadImporter();
666 $statusValue = $importer->import( $this );
667 return $statusValue->isGood();
672 * @deprecated in 1.31. No replacement
673 * @return bool|string
675 public function downloadSource() {
676 $importer = new ImportableUploadRevisionImporter(
677 $this->config
->get( 'EnableUploads' ),
678 LoggerFactory
::getInstance( 'UploadRevisionImporter' )
680 return $importer->downloadSource( $this );