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";
60 * @deprecated in 1.29. Unused.
61 * @note Introduced in 436a028086fb3f01c4605c5ad2964d56f9306aca, unused there, unused now.
69 public $user_text = "";
75 public $userObj = null;
87 public $format = null;
105 public $content = null;
109 * @var ContentHandler
111 protected $contentHandler = null;
117 public $comment = "";
123 public $minor = false;
147 public $fileSrc = '';
153 public $sha1base36 = false;
159 public $archiveName = '';
170 protected $src = null;
177 public $isTemp = false;
181 * @deprecated 1.29 use Wikirevision::isTempSrc()
182 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
183 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
188 private $mNoUpdates = false;
190 /** @var Config $config */
193 public function __construct( Config
$config ) {
194 $this->config
= $config;
198 * @since 1.7 taking a Title object (string before)
199 * @param Title $title
200 * @throws MWException
202 public function setTitle( $title ) {
203 if ( is_object( $title ) ) {
204 $this->title
= $title;
205 } elseif ( is_null( $title ) ) {
206 throw new MWException( "WikiRevision given a null title in import. "
207 . "You may need to adjust \$wgLegalTitleChars." );
209 throw new MWException( "WikiRevision given non-object title in import." );
217 public function setID( $id ) {
225 public function setTimestamp( $ts ) {
226 # 2003-08-05T18:30:02Z
227 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
232 * @param string $user
234 public function setUsername( $user ) {
235 $this->user_text
= $user;
242 public function setUserObj( $user ) {
243 $this->userObj
= $user;
250 public function setUserIP( $ip ) {
251 $this->user_text
= $ip;
256 * @param string $model
258 public function setModel( $model ) {
259 $this->model
= $model;
264 * @param string $format
266 public function setFormat( $format ) {
267 $this->format
= $format;
272 * @param string $text
274 public function setText( $text ) {
280 * @param string $text
282 public function setComment( $text ) {
283 $this->comment
= $text;
290 public function setMinor( $minor ) {
291 $this->minor
= (bool)$minor;
296 * @param string|null $src
298 public function setSrc( $src ) {
305 * @param bool $isTemp
307 public function setFileSrc( $src, $isTemp ) {
308 $this->fileSrc
= $src;
309 $this->fileIsTemp
= $isTemp;
310 $this->isTemp
= $isTemp;
315 * @param string $sha1base36
317 public function setSha1Base36( $sha1base36 ) {
318 $this->sha1base36
= $sha1base36;
323 * @param string $filename
325 public function setFilename( $filename ) {
326 $this->filename
= $filename;
331 * @param string $archiveName
333 public function setArchiveName( $archiveName ) {
334 $this->archiveName
= $archiveName;
341 public function setSize( $size ) {
342 $this->size
= intval( $size );
347 * @param string $type
349 public function setType( $type ) {
355 * @param string $action
357 public function setAction( $action ) {
358 $this->action
= $action;
363 * @param array $params
365 public function setParams( $params ) {
366 $this->params
= $params;
371 * @param bool $noupdates
373 public function setNoUpdates( $noupdates ) {
374 $this->mNoUpdates
= $noupdates;
381 public function getTitle() {
389 public function getID() {
397 public function getTimestamp() {
398 return $this->timestamp
;
405 public function getUser() {
406 return $this->user_text
;
413 public function getUserObj() {
414 return $this->userObj
;
421 public function getText() {
427 * @return ContentHandler
429 public function getContentHandler() {
430 if ( is_null( $this->contentHandler
) ) {
431 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
434 return $this->contentHandler
;
441 public function getContent() {
442 if ( is_null( $this->content
) ) {
443 $handler = $this->getContentHandler();
444 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
447 return $this->content
;
454 public function getModel() {
455 if ( is_null( $this->model
) ) {
456 $this->model
= $this->getTitle()->getContentModel();
466 public function getFormat() {
467 if ( is_null( $this->format
) ) {
468 $this->format
= $this->getContentHandler()->getDefaultFormat();
471 return $this->format
;
478 public function getComment() {
479 return $this->comment
;
486 public function getMinor() {
492 * @return string|null
494 public function getSrc() {
500 * @return bool|string
502 public function getSha1() {
503 if ( $this->sha1base36
) {
504 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
511 * @return bool|string
513 public function getSha1Base36() {
514 if ( $this->sha1base36
) {
515 return $this->sha1base36
;
524 public function getFileSrc() {
525 return $this->fileSrc
;
532 public function isTempSrc() {
533 return $this->isTemp
;
540 public function getFilename() {
541 return $this->filename
;
548 public function getArchiveName() {
549 return $this->archiveName
;
556 public function getSize() {
564 public function getType() {
572 public function getAction() {
573 return $this->action
;
580 public function getParams() {
581 return $this->params
;
586 * @deprecated in 1.31. Use OldRevisionImporter::import
589 public function importOldRevision() {
590 if ( $this->mNoUpdates
) {
591 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
593 $importer = MediaWikiServices
::getInstance()->getWikiRevisionOldRevisionImporter();
595 return $importer->import( $this );
602 public function importLogItem() {
603 $dbw = wfGetDB( DB_MASTER
);
605 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser(), false );
607 # @todo FIXME: This will not record autoblocks
608 if ( !$this->getTitle() ) {
609 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
610 $this->timestamp
. "\n" );
613 # Check if it exists already
614 // @todo FIXME: Use original log ID (better for backups)
615 $prior = $dbw->selectField( 'logging', '1',
616 [ 'log_type' => $this->getType(),
617 'log_action' => $this->getAction(),
618 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
619 'log_namespace' => $this->getTitle()->getNamespace(),
620 'log_title' => $this->getTitle()->getDBkey(),
621 'log_params' => $this->params
],
624 // @todo FIXME: This could fail slightly for multiple matches :P
627 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
628 . $this->timestamp
. "\n" );
632 'log_type' => $this->type
,
633 'log_action' => $this->action
,
634 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
635 'log_namespace' => $this->getTitle()->getNamespace(),
636 'log_title' => $this->getTitle()->getDBkey(),
637 'log_params' => $this->params
638 ] + CommentStore
::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
639 + ActorMigration
::newMigration()->getInsertValues( $dbw, 'log_user', $user );
640 $dbw->insert( 'logging', $data, __METHOD__
);
647 * @deprecated in 1.31. Use UploadImporter::import
650 public function importUpload() {
651 $importer = MediaWikiServices
::getInstance()->getWikiRevisionUploadImporter();
652 $statusValue = $importer->import( $this );
653 return $statusValue->isGood();
658 * @deprecated in 1.31. Use UploadImporter::downloadSource
659 * @return bool|string
661 public function downloadSource() {
662 $importer = new ImportableUploadRevisionImporter(
663 $this->config
->get( 'EnableUploads' ),
664 LoggerFactory
::getInstance( 'UploadRevisionImporter' )
666 return $importer->downloadSource( $this );