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
35 public $importer = null;
44 public $timestamp = "20010115000000";
48 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
52 public $user_text = "";
58 public $format = null;
67 public $content = null;
69 /** @var ContentHandler */
70 protected $contentHandler = null;
76 public $minor = false;
90 /** @var bool|string */
91 public $sha1base36 = false;
97 public $isTemp = false;
100 public $archiveName = '';
111 private $mNoUpdates = false;
113 /** @var Config $config */
116 public function __construct( Config
$config ) {
117 $this->config
= $config;
121 * @param Title $title
122 * @throws MWException
124 function setTitle( $title ) {
125 if ( is_object( $title ) ) {
126 $this->title
= $title;
127 } elseif ( is_null( $title ) ) {
128 throw new MWException( "WikiRevision given a null title in import. "
129 . "You may need to adjust \$wgLegalTitleChars." );
131 throw new MWException( "WikiRevision given non-object title in import." );
138 function setID( $id ) {
145 function setTimestamp( $ts ) {
146 # 2003-08-05T18:30:02Z
147 $this->timestamp
= wfTimestamp( TS_MW
, $ts );
151 * @param string $user
153 function setUsername( $user ) {
154 $this->user_text
= $user;
160 function setUserIP( $ip ) {
161 $this->user_text
= $ip;
165 * @param string $model
167 function setModel( $model ) {
168 $this->model
= $model;
172 * @param string $format
174 function setFormat( $format ) {
175 $this->format
= $format;
179 * @param string $text
181 function setText( $text ) {
186 * @param string $text
188 function setComment( $text ) {
189 $this->comment
= $text;
195 function setMinor( $minor ) {
196 $this->minor
= (bool)$minor;
202 function setSrc( $src ) {
208 * @param bool $isTemp
210 function setFileSrc( $src, $isTemp ) {
211 $this->fileSrc
= $src;
212 $this->fileIsTemp
= $isTemp;
216 * @param string $sha1base36
218 function setSha1Base36( $sha1base36 ) {
219 $this->sha1base36
= $sha1base36;
223 * @param string $filename
225 function setFilename( $filename ) {
226 $this->filename
= $filename;
230 * @param string $archiveName
232 function setArchiveName( $archiveName ) {
233 $this->archiveName
= $archiveName;
239 function setSize( $size ) {
240 $this->size
= intval( $size );
244 * @param string $type
246 function setType( $type ) {
251 * @param string $action
253 function setAction( $action ) {
254 $this->action
= $action;
258 * @param array $params
260 function setParams( $params ) {
261 $this->params
= $params;
265 * @param bool $noupdates
267 public function setNoUpdates( $noupdates ) {
268 $this->mNoUpdates
= $noupdates;
274 function getTitle() {
288 function getTimestamp() {
289 return $this->timestamp
;
296 return $this->user_text
;
302 * @deprecated Since 1.21, use getContent() instead.
305 ContentHandler
::deprecated( __METHOD__
, '1.21' );
311 * @return ContentHandler
313 function getContentHandler() {
314 if ( is_null( $this->contentHandler
) ) {
315 $this->contentHandler
= ContentHandler
::getForModelID( $this->getModel() );
318 return $this->contentHandler
;
324 function getContent() {
325 if ( is_null( $this->content
) ) {
326 $handler = $this->getContentHandler();
327 $this->content
= $handler->unserializeContent( $this->text
, $this->getFormat() );
330 return $this->content
;
336 function getModel() {
337 if ( is_null( $this->model
) ) {
338 $this->model
= $this->getTitle()->getContentModel();
347 function getFormat() {
348 if ( is_null( $this->format
) ) {
349 $this->format
= $this->getContentHandler()->getDefaultFormat();
352 return $this->format
;
358 function getComment() {
359 return $this->comment
;
365 function getMinor() {
377 * @return bool|string
380 if ( $this->sha1base36
) {
381 return Wikimedia\base_convert
( $this->sha1base36
, 36, 16 );
389 function getFileSrc() {
390 return $this->fileSrc
;
396 function isTempSrc() {
397 return $this->isTemp
;
403 function getFilename() {
404 return $this->filename
;
410 function getArchiveName() {
411 return $this->archiveName
;
431 function getAction() {
432 return $this->action
;
438 function getParams() {
439 return $this->params
;
445 function importOldRevision() {
446 $dbw = wfGetDB( DB_MASTER
);
448 # Sneak a single revision into place
449 $user = User
::newFromName( $this->getUser() );
451 $userId = intval( $user->getId() );
452 $userText = $user->getName();
456 $userText = $this->getUser();
460 // avoid memory leak...?
461 Title
::clearCaches();
463 $page = WikiPage
::factory( $this->title
);
464 $page->loadPageData( 'fromdbmaster' );
465 if ( !$page->exists() ) {
466 // must create the page...
467 $pageId = $page->insertOn( $dbw );
469 $oldcountable = null;
471 $pageId = $page->getId();
474 $prior = $dbw->selectField( 'revision', '1',
475 array( 'rev_page' => $pageId,
476 'rev_timestamp' => $dbw->timestamp( $this->timestamp
),
477 'rev_user_text' => $userText,
478 'rev_comment' => $this->getComment() ),
482 // @todo FIXME: This could fail slightly for multiple matches :P
483 wfDebug( __METHOD__
. ": skipping existing revision for [[" .
484 $this->title
->getPrefixedText() . "]], timestamp " . $this->timestamp
. "\n" );
490 // This seems to happen if two clients simultaneously try to import the
492 wfDebug( __METHOD__
. ': got invalid $pageId when importing revision of [[' .
493 $this->title
->getPrefixedText() . ']], timestamp ' . $this->timestamp
. "\n" );
497 // Select previous version to make size diffs correct
498 // @todo This assumes that multiple revisions of the same page are imported
499 // in order from oldest to newest.
500 $prevId = $dbw->selectField( 'revision', 'rev_id',
502 'rev_page' => $pageId,
503 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) ),
506 array( 'ORDER BY' => array(
507 'rev_timestamp DESC',
508 'rev_id DESC', // timestamp is not unique per page
513 # @todo FIXME: Use original rev_id optionally (better for backups)
515 $revision = new Revision( array(
516 'title' => $this->title
,
518 'content_model' => $this->getModel(),
519 'content_format' => $this->getFormat(),
520 // XXX: just set 'content' => $this->getContent()?
521 'text' => $this->getContent()->serialize( $this->getFormat() ),
522 'comment' => $this->getComment(),
524 'user_text' => $userText,
525 'timestamp' => $this->timestamp
,
526 'minor_edit' => $this->minor
,
527 'parent_id' => $prevId,
529 $revision->insertOn( $dbw );
530 $changed = $page->updateIfNewerOn( $dbw, $revision );
532 if ( $changed !== false && !$this->mNoUpdates
) {
533 wfDebug( __METHOD__
. ": running updates\n" );
534 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
535 $page->doEditUpdates(
538 array( 'created' => $created, 'oldcountable' => 'no-change' )
545 function importLogItem() {
546 $dbw = wfGetDB( DB_MASTER
);
548 $user = User
::newFromName( $this->getUser() );
550 $userId = intval( $user->getId() );
551 $userText = $user->getName();
554 $userText = $this->getUser();
557 # @todo FIXME: This will not record autoblocks
558 if ( !$this->getTitle() ) {
559 wfDebug( __METHOD__
. ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
560 $this->timestamp
. "\n" );
563 # Check if it exists already
564 // @todo FIXME: Use original log ID (better for backups)
565 $prior = $dbw->selectField( 'logging', '1',
566 array( 'log_type' => $this->getType(),
567 'log_action' => $this->getAction(),
568 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
569 'log_namespace' => $this->getTitle()->getNamespace(),
570 'log_title' => $this->getTitle()->getDBkey(),
571 'log_comment' => $this->getComment(),
572 # 'log_user_text' => $this->user_text,
573 'log_params' => $this->params
),
576 // @todo FIXME: This could fail slightly for multiple matches :P
579 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
580 . $this->timestamp
. "\n" );
583 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
586 'log_type' => $this->type
,
587 'log_action' => $this->action
,
588 'log_timestamp' => $dbw->timestamp( $this->timestamp
),
589 'log_user' => $userId,
590 'log_user_text' => $userText,
591 'log_namespace' => $this->getTitle()->getNamespace(),
592 'log_title' => $this->getTitle()->getDBkey(),
593 'log_comment' => $this->getComment(),
594 'log_params' => $this->params
596 $dbw->insert( 'logging', $data, __METHOD__
);
602 function importUpload() {
604 $archiveName = $this->getArchiveName();
605 if ( $archiveName ) {
606 wfDebug( __METHOD__
. "Importing archived file as $archiveName\n" );
607 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
608 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
610 $file = wfLocalFile( $this->getTitle() );
611 $file->load( File
::READ_LATEST
);
612 wfDebug( __METHOD__
. 'Importing new file as ' . $file->getName() . "\n" );
613 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
614 $archiveName = $file->getTimestamp() . '!' . $file->getName();
615 $file = OldLocalFile
::newFromArchiveName( $this->getTitle(),
616 RepoGroup
::singleton()->getLocalRepo(), $archiveName );
617 wfDebug( __METHOD__
. "File already exists; importing as $archiveName\n" );
621 wfDebug( __METHOD__
. ': Bad file for ' . $this->getTitle() . "\n" );
625 # Get the file source or download if necessary
626 $source = $this->getFileSrc();
627 $flags = $this->isTempSrc() ? File
::DELETE_SOURCE
: 0;
629 $source = $this->downloadSource();
630 $flags |
= File
::DELETE_SOURCE
;
633 wfDebug( __METHOD__
. ": Could not fetch remote file.\n" );
636 $sha1File = ltrim( sha1_file( $source ), '0' );
637 $sha1 = $this->getSha1();
638 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
639 if ( $flags & File
::DELETE_SOURCE
) {
640 # Broken file; delete it if it is a temporary file
643 wfDebug( __METHOD__
. ": Corrupt file $source.\n" );
647 $user = User
::newFromName( $this->user_text
);
649 # Do the actual upload
650 if ( $archiveName ) {
651 $status = $file->uploadOld( $source, $archiveName,
652 $this->getTimestamp(), $this->getComment(), $user, $flags );
654 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
655 $flags, false, $this->getTimestamp(), $user );
658 if ( $status->isGood() ) {
659 wfDebug( __METHOD__
. ": Successful\n" );
662 wfDebug( __METHOD__
. ': failed: ' . $status->getHTML() . "\n" );
668 * @return bool|string
670 function downloadSource() {
671 if ( !$this->config
->get( 'EnableUploads' ) ) {
675 $tempo = tempnam( wfTempDir(), 'download' );
676 $f = fopen( $tempo, 'wb' );
678 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
683 $src = $this->getSrc();
684 $data = Http
::get( $src, array(), __METHOD__
);
686 wfDebug( "IMPORT: couldn't fetch source $src\n" );