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
28 * Represents a revision, log entry or upload during the import process.
29 * This class sticks closely to the structure of the XML dump.
31 * @ingroup SpecialPage
37 * @deprecated in 1.29. Unused.
38 * @note Introduced in 9b3128eb2b654761f21fd4ca1d5a1a4b796dc912, unused there, unused now.
40 public $importer = null;
49 public $timestamp = "20010115000000";
53 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
57 public $user_text = "";
60 public $userObj = null;
66 public $format = null;
75 public $content = null;
77 /** @var ContentHandler */
78 protected $contentHandler = null;
84 public $minor = false;
98 /** @var bool|string */
99 public $sha1base36 = false;
103 public $archiveName = '';
114 public $isTemp = false;
118 * @deprecated 1.29 use Wikirevision::isTempSrc()
119 * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
120 * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
125 private $mNoUpdates = false;
127 /** @var Config $config */
130 public function __construct( Config
$config ) {
131 $this->config
= $config;
135 * @param Title $title
136 * @throws MWException
138 public function setTitle( $title ) {
139 if ( is_object( $title ) ) {
140 $this->title
= $title;
141 } elseif ( is_null( $title ) ) {
142 throw new MWException( "WikiRevision given a null title in import. "
143 . "You may need to adjust \$wgLegalTitleChars." );
145 throw new MWException( "WikiRevision given non-object title in import." );
152 public function setID( $id ) {
159 public function setTimestamp( $ts ) {
160 # 2003-08-05T18:30:02Z
161 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
165 * @param string $user
167 public function setUsername( $user ) {
168 $this->user_text
= $user;
174 public function setUserObj( $user ) {
175 $this->userObj
= $user;
181 public function setUserIP( $ip ) {
182 $this->user_text
= $ip;
186 * @param string $model
188 public function setModel( $model ) {
189 $this->model
= $model;
193 * @param string $format
195 public function setFormat( $format ) {
196 $this->format
= $format;
200 * @param string $text
202 public function setText( $text ) {
207 * @param string $text
209 public function setComment( $text ) {
210 $this->comment
= $text;
216 public function setMinor( $minor ) {
217 $this->minor
= (bool)$minor;
223 public function setSrc( $src ) {
229 * @param bool $isTemp
231 public function setFileSrc( $src, $isTemp ) {
232 $this->fileSrc
= $src;
233 $this->fileIsTemp
= $isTemp;
234 $this->isTemp
= $isTemp;
238 * @param string $sha1base36
240 public function setSha1Base36( $sha1base36 ) {
241 $this->sha1base36
= $sha1base36;
245 * @param string $filename
247 public function setFilename( $filename ) {
248 $this->filename
= $filename;
252 * @param string $archiveName
254 public function setArchiveName( $archiveName ) {
255 $this->archiveName
= $archiveName;
261 public function setSize( $size ) {
262 $this->size
= intval( $size );
266 * @param string $type
268 public function setType( $type ) {
273 * @param string $action
275 public function setAction( $action ) {
276 $this->action
= $action;
280 * @param array $params
282 public function setParams( $params ) {
283 $this->params
= $params;
287 * @param bool $noupdates
289 public function setNoUpdates( $noupdates ) {
290 $this->mNoUpdates
= $noupdates;
296 public function getTitle() {
303 public function getID() {
310 public function getTimestamp() {
311 return $this->timestamp
;
317 public function getUser() {
318 return $this->user_text
;
324 public function getUserObj() {
325 return $this->userObj
;
331 public function getText() {
336 * @return ContentHandler
338 public function getContentHandler() {
339 if ( is_null( $this->contentHandler
) ) {
340 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
343 return $this->contentHandler
;
349 public function getContent() {
350 if ( is_null( $this->content
) ) {
351 $handler = $this->getContentHandler();
352 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
355 return $this->content
;
361 public function getModel() {
362 if ( is_null( $this->model
) ) {
363 $this->model
= $this->getTitle()->getContentModel();
372 public function getFormat() {
373 if ( is_null( $this->format
) ) {
374 $this->format
= $this->getContentHandler()->getDefaultFormat();
377 return $this->format
;
383 public function getComment() {
384 return $this->comment
;
390 public function getMinor() {
397 public function getSrc() {
402 * @return bool|string
404 public function getSha1() {
405 if ( $this->sha1base36
) {
406 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
414 public function getFileSrc() {
415 return $this->fileSrc
;
421 public function isTempSrc() {
422 return $this->isTemp
;
428 public function getFilename() {
429 return $this->filename
;
435 public function getArchiveName() {
436 return $this->archiveName
;
442 public function getSize() {
449 public function getType() {
456 public function getAction() {
457 return $this->action
;
463 public function getParams() {
464 return $this->params
;
470 public function importOldRevision() {
471 $dbw = wfGetDB( DB_MASTER
);
473 # Sneak a single revision into place
474 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
476 $userId = intval( $user->getId() );
477 $userText = $user->getName();
480 $userText = $this->getUser();
484 // avoid memory leak...?
485 Title
::clearCaches();
487 $page = WikiPage
::factory( $this->title
);
488 $page->loadPageData( 'fromdbmaster' );
489 if ( !$page->exists() ) {
490 // must create the page...
491 $pageId = $page->insertOn( $dbw );
493 $oldcountable = null;
495 $pageId = $page->getId();
498 $prior = $dbw->selectField( 'revision', '1',
499 [ 'rev_page' => $pageId,
500 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
501 'rev_user_text' => $userText,
502 'rev_comment' => $this->getComment() ],
506 // @todo FIXME: This could fail slightly for multiple matches :P
507 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
508 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
514 // This seems to happen if two clients simultaneously try to import the
516 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
517 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
521 // Select previous version to make size diffs correct
522 // @todo This assumes that multiple revisions of the same page are imported
523 // in order from oldest to newest.
524 $prevId = $dbw->selectField( 'revision', 'rev_id',
526 'rev_page' => $pageId,
527 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
531 'rev_timestamp DESC',
532 'rev_id DESC', // timestamp is not unique per page
537 # @todo FIXME: Use original rev_id optionally (better for backups)
539 $revision = new Revision( [
540 'title' => $this->title
,
542 'content_model' => $this->getModel(),
543 'content_format' => $this->getFormat(),
544 // XXX: just set 'content' => $this->getContent()?
545 'text' => $this->getContent()->serialize( $this->getFormat() ),
546 'comment' => $this->getComment(),
548 'user_text' => $userText,
549 'timestamp' => $this->timestamp
,
550 'minor_edit' => $this->minor
,
551 'parent_id' => $prevId,
553 $revision->insertOn( $dbw );
554 $changed = $page->updateIfNewerOn( $dbw, $revision );
556 if ( $changed !== false && !$this->mNoUpdates
) {
557 wfDebug( __METHOD__
. ": running updates\n" );
558 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
559 $page->doEditUpdates(
562 [ 'created' => $created, 'oldcountable' => 'no-change' ]
569 public function importLogItem() {
570 $dbw = wfGetDB( DB_MASTER
);
572 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
574 $userId = intval( $user->getId() );
575 $userText = $user->getName();
578 $userText = $this->getUser();
581 # @todo FIXME: This will not record autoblocks
582 if ( !$this->getTitle() ) {
583 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
584 $this->timestamp
. "\n" );
587 # Check if it exists already
588 // @todo FIXME: Use original log ID (better for backups)
589 $prior = $dbw->selectField( 'logging', '1',
590 [ 'log_type' => $this->getType(),
591 'log_action' => $this->getAction(),
592 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
593 'log_namespace' => $this->getTitle()->getNamespace(),
594 'log_title' => $this->getTitle()->getDBkey(),
595 'log_comment' => $this->getComment(),
596 # 'log_user_text' => $this->user_text,
597 'log_params' => $this->params
],
600 // @todo FIXME: This could fail slightly for multiple matches :P
603 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
604 . $this->timestamp
. "\n" );
607 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
610 'log_type' => $this->type
,
611 'log_action' => $this->action
,
612 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
613 'log_user' => $userId,
614 'log_user_text' => $userText,
615 'log_namespace' => $this->getTitle()->getNamespace(),
616 'log_title' => $this->getTitle()->getDBkey(),
617 'log_comment' => $this->getComment(),
618 'log_params' => $this->params
620 $dbw->insert( 'logging', $data, __METHOD__
);
628 public function importUpload() {
630 $archiveName = $this->getArchiveName();
631 if ( $archiveName ) {
632 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
633 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
634 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
636 $file = wfLocalFile( $this->getTitle() );
637 $file->load( File
::READ_LATEST
);
638 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
639 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
640 $archiveName = $file->getTimestamp() . '!' . $file->getName();
641 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
642 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
643 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
647 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
651 # Get the file source or download if necessary
652 $source = $this->getFileSrc();
653 $autoDeleteSource = $this->isTempSrc();
654 if ( !strlen( $source ) ) {
655 $source = $this->downloadSource();
656 $autoDeleteSource = true;
658 if ( !strlen( $source ) ) {
659 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
663 $tmpFile = new TempFSFile( $source );
664 if ( $autoDeleteSource ) {
665 $tmpFile->autocollect();
668 $sha1File = ltrim( sha1_file( $source ), '0' );
669 $sha1 = $this->getSha1();
670 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
671 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
675 $user = $this->getUserObj() ?
: User
::newFromName( $this->getUser() );
677 # Do the actual upload
678 if ( $archiveName ) {
679 $status = $file->uploadOld( $source, $archiveName,
680 $this->getTimestamp(), $this->getComment(), $user );
683 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
684 $flags, false, $this->getTimestamp(), $user );
687 if ( $status->isGood() ) {
688 wfDebug( __METHOD__
. ": Successful\n" );
691 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
697 * @return bool|string
699 public function downloadSource() {
700 if ( !$this->config
->get( 'EnableUploads' ) ) {
704 $tempo = tempnam( wfTempDir(), 'download' );
705 $f = fopen( $tempo, 'wb' );
707 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
712 $src = $this->getSrc();
713 $data = Http
::get( $src, [], __METHOD__
);
715 wfDebug( "IMPORT: couldn't fetch source $src\n" );