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 public $archiveName = '';
162 protected $src = null;
169 public $isTemp = false;
173 * @deprecated 1.29 use Wikirevision::isTempSrc()
174 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
175 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
180 private $mNoUpdates = false;
182 /** @var Config $config */
185 public function __construct( Config
$config ) {
186 $this->config
= $config;
190 * @since 1.7 taking a Title object (string before)
191 * @param Title $title
192 * @throws MWException
194 public function setTitle( $title ) {
195 if ( is_object( $title ) ) {
196 $this->title
= $title;
197 } elseif ( is_null( $title ) ) {
198 throw new MWException( "WikiRevision given a null title in import. "
199 . "You may need to adjust \$wgLegalTitleChars." );
201 throw new MWException( "WikiRevision given non-object title in import." );
209 public function setID( $id ) {
217 public function setTimestamp( $ts ) {
218 # 2003-08-05T18:30:02Z
219 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
224 * @param string $user
226 public function setUsername( $user ) {
227 $this->user_text
= $user;
234 public function setUserObj( $user ) {
235 $this->userObj
= $user;
242 public function setUserIP( $ip ) {
243 $this->user_text
= $ip;
248 * @param string $model
250 public function setModel( $model ) {
251 $this->model
= $model;
256 * @param string $format
258 public function setFormat( $format ) {
259 $this->format
= $format;
264 * @param string $text
266 public function setText( $text ) {
272 * @param string $text
274 public function setComment( $text ) {
275 $this->comment
= $text;
282 public function setMinor( $minor ) {
283 $this->minor
= (bool)$minor;
288 * @param string|null $src
290 public function setSrc( $src ) {
297 * @param bool $isTemp
299 public function setFileSrc( $src, $isTemp ) {
300 $this->fileSrc
= $src;
301 $this->fileIsTemp
= $isTemp;
302 $this->isTemp
= $isTemp;
307 * @param string $sha1base36
309 public function setSha1Base36( $sha1base36 ) {
310 $this->sha1base36
= $sha1base36;
315 * @param string $filename
317 public function setFilename( $filename ) {
318 $this->filename
= $filename;
323 * @param string $archiveName
325 public function setArchiveName( $archiveName ) {
326 $this->archiveName
= $archiveName;
333 public function setSize( $size ) {
334 $this->size
= intval( $size );
339 * @param string $type
341 public function setType( $type ) {
347 * @param string $action
349 public function setAction( $action ) {
350 $this->action
= $action;
355 * @param string $params
357 public function setParams( $params ) {
358 $this->params
= $params;
363 * @param bool $noupdates
365 public function setNoUpdates( $noupdates ) {
366 $this->mNoUpdates
= $noupdates;
373 public function getTitle() {
381 public function getID() {
389 public function getTimestamp() {
390 return $this->timestamp
;
397 public function getUser() {
398 return $this->user_text
;
405 public function getUserObj() {
406 return $this->userObj
;
413 public function getText() {
419 * @return ContentHandler
421 public function getContentHandler() {
422 if ( is_null( $this->contentHandler
) ) {
423 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
426 return $this->contentHandler
;
433 public function getContent() {
434 if ( is_null( $this->content
) ) {
435 $handler = $this->getContentHandler();
436 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
439 return $this->content
;
446 public function getModel() {
447 if ( is_null( $this->model
) ) {
448 $this->model
= $this->getTitle()->getContentModel();
458 public function getFormat() {
459 if ( is_null( $this->format
) ) {
460 $this->format
= $this->getContentHandler()->getDefaultFormat();
463 return $this->format
;
470 public function getComment() {
471 return $this->comment
;
478 public function getMinor() {
484 * @return string|null
486 public function getSrc() {
492 * @return bool|string
494 public function getSha1() {
495 if ( $this->sha1base36
) {
496 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
503 * @return bool|string
505 public function getSha1Base36() {
506 if ( $this->sha1base36
) {
507 return $this->sha1base36
;
516 public function getFileSrc() {
517 return $this->fileSrc
;
524 public function isTempSrc() {
525 return $this->isTemp
;
532 public function getFilename() {
533 return $this->filename
;
540 public function getArchiveName() {
541 return $this->archiveName
;
548 public function getSize() {
556 public function getType() {
564 public function getAction() {
565 return $this->action
;
572 public function getParams() {
573 return $this->params
;
578 * @deprecated in 1.31. Use OldRevisionImporter::import
581 public function importOldRevision() {
582 if ( $this->mNoUpdates
) {
583 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
585 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporter();
587 return $importer->import( $this );
594 public function importLogItem() {
595 $dbw = wfGetDB( DB_MASTER
);
597 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser(), false );
599 # @todo FIXME: This will not record autoblocks
600 if ( !$this->getTitle() ) {
601 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
602 $this->timestamp
. "\n" );
605 # Check if it exists already
606 // @todo FIXME: Use original log ID (better for backups)
607 $prior = $dbw->selectField( 'logging', '1',
608 [ 'log_type' => $this->getType(),
609 'log_action' => $this->getAction(),
610 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
611 'log_namespace' => $this->getTitle()->getNamespace(),
612 'log_title' => $this->getTitle()->getDBkey(),
613 'log_params' => $this->params
],
616 // @todo FIXME: This could fail slightly for multiple matches :P
619 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
620 . $this->timestamp
. "\n" );
624 'log_type' => $this->type
,
625 'log_action' => $this->action
,
626 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
627 'log_namespace' => $this->getTitle()->getNamespace(),
628 'log_title' => $this->getTitle()->getDBkey(),
629 'log_params' => $this->params
630 ] + CommentStore
::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
631 + ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $user );
632 $dbw->insert( 'logging', $data, __METHOD__
);
639 * @deprecated in 1.31. Use UploadImporter::import
642 public function importUpload() {
643 $importer = MediaWikiServices
::getInstance()->getWikiRevisionUploadImporter();
644 $statusValue = $importer->import( $this );
645 return $statusValue->isGood();
650 * @deprecated in 1.31. Use UploadImporter::downloadSource
651 * @return bool|string
653 public function downloadSource() {
654 $importer = new ImportableUploadRevisionImporter(
655 $this->config
->get( 'EnableUploads' ),
656 LoggerFactory
::getInstance( 'UploadRevisionImporter' )
658 return $importer->downloadSource( $this );