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;
189 * @deprecated since 1.31, along with self::downloadSource()
190 * @var Config $config
195 * @param Config $config Deprecated since 1.31, along with self::downloadSource(). Just pass an
198 public function __construct( Config
$config ) {
199 $this->config
= $config;
203 * @since 1.7 taking a Title object (string before)
204 * @param Title $title
205 * @throws MWException
207 public function setTitle( $title ) {
208 if ( is_object( $title ) ) {
209 $this->title
= $title;
210 } elseif ( is_null( $title ) ) {
211 throw new MWException( "WikiRevision given a null title in import. "
212 . "You may need to adjust \$wgLegalTitleChars." );
214 throw new MWException( "WikiRevision given non-object title in import." );
222 public function setID( $id ) {
230 public function setTimestamp( $ts ) {
231 # 2003-08-05T18:30:02Z
232 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
237 * @param string $user
239 public function setUsername( $user ) {
240 $this->user_text
= $user;
247 public function setUserObj( $user ) {
248 $this->userObj
= $user;
255 public function setUserIP( $ip ) {
256 $this->user_text
= $ip;
261 * @param string $model
263 public function setModel( $model ) {
264 $this->model
= $model;
269 * @param string $format
271 public function setFormat( $format ) {
272 $this->format
= $format;
277 * @param string $text
279 public function setText( $text ) {
285 * @param string $text
287 public function setComment( $text ) {
288 $this->comment
= $text;
295 public function setMinor( $minor ) {
296 $this->minor
= (bool)$minor;
301 * @param string|null $src
303 public function setSrc( $src ) {
310 * @param bool $isTemp
312 public function setFileSrc( $src, $isTemp ) {
313 $this->fileSrc
= $src;
314 $this->fileIsTemp
= $isTemp;
315 $this->isTemp
= $isTemp;
320 * @param string $sha1base36
322 public function setSha1Base36( $sha1base36 ) {
323 $this->sha1base36
= $sha1base36;
328 * @param string[] $tags
330 public function setTags( array $tags ) {
336 * @param string $filename
338 public function setFilename( $filename ) {
339 $this->filename
= $filename;
344 * @param string $archiveName
346 public function setArchiveName( $archiveName ) {
347 $this->archiveName
= $archiveName;
354 public function setSize( $size ) {
355 $this->size
= intval( $size );
360 * @param string $type
362 public function setType( $type ) {
368 * @param string $action
370 public function setAction( $action ) {
371 $this->action
= $action;
376 * @param string $params
378 public function setParams( $params ) {
379 $this->params
= $params;
384 * @param bool $noupdates
386 public function setNoUpdates( $noupdates ) {
387 $this->mNoUpdates
= $noupdates;
394 public function getTitle() {
402 public function getID() {
410 public function getTimestamp() {
411 return $this->timestamp
;
418 public function getUser() {
419 return $this->user_text
;
426 public function getUserObj() {
427 return $this->userObj
;
434 public function getText() {
440 * @return ContentHandler
442 public function getContentHandler() {
443 if ( is_null( $this->contentHandler
) ) {
444 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
447 return $this->contentHandler
;
454 public function getContent() {
455 if ( is_null( $this->content
) ) {
456 $handler = $this->getContentHandler();
457 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
460 return $this->content
;
467 public function getModel() {
468 if ( is_null( $this->model
) ) {
469 $this->model
= $this->getTitle()->getContentModel();
479 public function getFormat() {
480 if ( is_null( $this->format
) ) {
481 $this->format
= $this->getContentHandler()->getDefaultFormat();
484 return $this->format
;
491 public function getComment() {
492 return $this->comment
;
499 public function getMinor() {
505 * @return string|null
507 public function getSrc() {
513 * @return bool|string
515 public function getSha1() {
516 if ( $this->sha1base36
) {
517 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
524 * @return bool|string
526 public function getSha1Base36() {
527 if ( $this->sha1base36
) {
528 return $this->sha1base36
;
537 public function getTags() {
545 public function getFileSrc() {
546 return $this->fileSrc
;
553 public function isTempSrc() {
554 return $this->isTemp
;
561 public function getFilename() {
562 return $this->filename
;
569 public function getArchiveName() {
570 return $this->archiveName
;
577 public function getSize() {
585 public function getType() {
593 public function getAction() {
594 return $this->action
;
601 public function getParams() {
602 return $this->params
;
607 * @deprecated in 1.31. Use OldRevisionImporter::import
610 public function importOldRevision() {
611 if ( $this->mNoUpdates
) {
612 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
614 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporter();
616 return $importer->import( $this );
623 public function importLogItem() {
624 $dbw = wfGetDB( DB_MASTER
);
626 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser(), false );
628 # @todo FIXME: This will not record autoblocks
629 if ( !$this->getTitle() ) {
630 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
631 $this->timestamp
. "\n" );
634 # Check if it exists already
635 // @todo FIXME: Use original log ID (better for backups)
636 $prior = $dbw->selectField( 'logging', '1',
637 [ 'log_type' => $this->getType(),
638 'log_action' => $this->getAction(),
639 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
640 'log_namespace' => $this->getTitle()->getNamespace(),
641 'log_title' => $this->getTitle()->getDBkey(),
642 'log_params' => $this->params
],
645 // @todo FIXME: This could fail slightly for multiple matches :P
648 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
649 . $this->timestamp
. "\n" );
653 'log_type' => $this->type
,
654 'log_action' => $this->action
,
655 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
656 'log_namespace' => $this->getTitle()->getNamespace(),
657 'log_title' => $this->getTitle()->getDBkey(),
658 'log_params' => $this->params
659 ] + CommentStore
::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
660 + ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $user );
661 $dbw->insert( 'logging', $data, __METHOD__
);
668 * @deprecated in 1.31. Use UploadRevisionImporter::import
671 public function importUpload() {
672 $importer = MediaWikiServices
::getInstance()->getWikiRevisionUploadImporter();
673 $statusValue = $importer->import( $this );
674 return $statusValue->isGood();
679 * @deprecated in 1.31. No replacement
680 * @return bool|string
682 public function downloadSource() {
683 $importer = new ImportableUploadRevisionImporter(
684 $this->config
->get( 'EnableUploads' ),
685 LoggerFactory
::getInstance( 'UploadRevisionImporter' )
687 return $importer->downloadSource( $this );