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
{
41 * @deprecated in 1.29. Unused.
42 * @note Introduced in 9b3128eb2b654761f21fd4ca1d5a1a4b796dc912, unused there, unused now.
44 public $importer = null;
62 public $timestamp = "20010115000000";
67 * @deprecated in 1.29. Unused.
68 * @note Introduced in 436a028086fb3f01c4605c5ad2964d56f9306aca, unused there, unused now.
76 public $user_text = "";
82 public $userObj = null;
94 public $format = null;
112 public $content = null;
116 * @var ContentHandler
118 protected $contentHandler = null;
124 public $comment = "";
130 public $minor = false;
154 public $fileSrc = '';
160 public $sha1base36 = false;
166 public $archiveName = '';
177 protected $src = null;
184 public $isTemp = false;
188 * @deprecated 1.29 use Wikirevision::isTempSrc()
189 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
190 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
195 private $mNoUpdates = false;
197 /** @var Config $config */
200 public function __construct( Config
$config ) {
201 $this->config
= $config;
205 * @since 1.7 taking a Title object (string before)
206 * @param Title $title
207 * @throws MWException
209 public function setTitle( $title ) {
210 if ( is_object( $title ) ) {
211 $this->title
= $title;
212 } elseif ( is_null( $title ) ) {
213 throw new MWException( "WikiRevision given a null title in import. "
214 . "You may need to adjust \$wgLegalTitleChars." );
216 throw new MWException( "WikiRevision given non-object title in import." );
224 public function setID( $id ) {
232 public function setTimestamp( $ts ) {
233 # 2003-08-05T18:30:02Z
234 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
239 * @param string $user
241 public function setUsername( $user ) {
242 $this->user_text
= $user;
249 public function setUserObj( $user ) {
250 $this->userObj
= $user;
257 public function setUserIP( $ip ) {
258 $this->user_text
= $ip;
263 * @param string $model
265 public function setModel( $model ) {
266 $this->model
= $model;
271 * @param string $format
273 public function setFormat( $format ) {
274 $this->format
= $format;
279 * @param string $text
281 public function setText( $text ) {
287 * @param string $text
289 public function setComment( $text ) {
290 $this->comment
= $text;
297 public function setMinor( $minor ) {
298 $this->minor
= (bool)$minor;
303 * @param string|null $src
305 public function setSrc( $src ) {
312 * @param bool $isTemp
314 public function setFileSrc( $src, $isTemp ) {
315 $this->fileSrc
= $src;
316 $this->fileIsTemp
= $isTemp;
317 $this->isTemp
= $isTemp;
322 * @param string $sha1base36
324 public function setSha1Base36( $sha1base36 ) {
325 $this->sha1base36
= $sha1base36;
330 * @param string $filename
332 public function setFilename( $filename ) {
333 $this->filename
= $filename;
338 * @param string $archiveName
340 public function setArchiveName( $archiveName ) {
341 $this->archiveName
= $archiveName;
348 public function setSize( $size ) {
349 $this->size
= intval( $size );
354 * @param string $type
356 public function setType( $type ) {
362 * @param string $action
364 public function setAction( $action ) {
365 $this->action
= $action;
370 * @param array $params
372 public function setParams( $params ) {
373 $this->params
= $params;
378 * @param bool $noupdates
380 public function setNoUpdates( $noupdates ) {
381 $this->mNoUpdates
= $noupdates;
388 public function getTitle() {
396 public function getID() {
404 public function getTimestamp() {
405 return $this->timestamp
;
412 public function getUser() {
413 return $this->user_text
;
420 public function getUserObj() {
421 return $this->userObj
;
428 public function getText() {
434 * @return ContentHandler
436 public function getContentHandler() {
437 if ( is_null( $this->contentHandler
) ) {
438 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
441 return $this->contentHandler
;
448 public function getContent() {
449 if ( is_null( $this->content
) ) {
450 $handler = $this->getContentHandler();
451 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
454 return $this->content
;
461 public function getModel() {
462 if ( is_null( $this->model
) ) {
463 $this->model
= $this->getTitle()->getContentModel();
473 public function getFormat() {
474 if ( is_null( $this->format
) ) {
475 $this->format
= $this->getContentHandler()->getDefaultFormat();
478 return $this->format
;
485 public function getComment() {
486 return $this->comment
;
493 public function getMinor() {
499 * @return string|null
501 public function getSrc() {
507 * @return bool|string
509 public function getSha1() {
510 if ( $this->sha1base36
) {
511 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
518 * @return bool|string
520 public function getSha1Base36() {
521 if ( $this->sha1base36
) {
522 return $this->sha1base36
;
531 public function getFileSrc() {
532 return $this->fileSrc
;
539 public function isTempSrc() {
540 return $this->isTemp
;
547 public function getFilename() {
548 return $this->filename
;
555 public function getArchiveName() {
556 return $this->archiveName
;
563 public function getSize() {
571 public function getType() {
579 public function getAction() {
580 return $this->action
;
587 public function getParams() {
588 return $this->params
;
593 * @deprecated in 1.31. Use OldRevisionImporter::import
596 public function importOldRevision() {
597 if ( $this->mNoUpdates
) {
598 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
600 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporter();
602 return $importer->import( $this );
609 public function importLogItem() {
610 $dbw = wfGetDB( DB_MASTER
);
612 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser(), false );
614 # @todo FIXME: This will not record autoblocks
615 if ( !$this->getTitle() ) {
616 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
617 $this->timestamp
. "\n" );
620 # Check if it exists already
621 // @todo FIXME: Use original log ID (better for backups)
622 $prior = $dbw->selectField( 'logging', '1',
623 [ 'log_type' => $this->getType(),
624 'log_action' => $this->getAction(),
625 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
626 'log_namespace' => $this->getTitle()->getNamespace(),
627 'log_title' => $this->getTitle()->getDBkey(),
628 'log_params' => $this->params
],
631 // @todo FIXME: This could fail slightly for multiple matches :P
634 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
635 . $this->timestamp
. "\n" );
639 'log_type' => $this->type
,
640 'log_action' => $this->action
,
641 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
642 'log_namespace' => $this->getTitle()->getNamespace(),
643 'log_title' => $this->getTitle()->getDBkey(),
644 'log_params' => $this->params
645 ] + CommentStore
::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
646 + ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $user );
647 $dbw->insert( 'logging', $data, __METHOD__
);
654 * @deprecated in 1.31. Use UploadImporter::import
657 public function importUpload() {
658 $importer = MediaWikiServices
::getInstance()->getWikiRevisionUploadImporter();
659 $statusValue = $importer->import( $this );
660 return $statusValue->isGood();
665 * @deprecated in 1.31. Use UploadImporter::downloadSource
666 * @return bool|string
668 public function downloadSource() {
669 $importer = new ImportableUploadRevisionImporter(
670 $this->config
->get( 'EnableUploads' ),
671 LoggerFactory
::getInstance( 'UploadRevisionImporter' )
673 return $importer->downloadSource( $this );