Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / import / WikiRevision.php
blob356a79f82dce72d61c80a9b528f6419dc6ed894e
1 <?php
2 /**
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
23 * @file
24 * @ingroup SpecialPage
27 /**
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
33 class WikiRevision {
34 /** @todo Unused? */
35 public $importer = null;
37 /** @var Title */
38 public $title = null;
40 /** @var int */
41 public $id = 0;
43 /** @var string */
44 public $timestamp = "20010115000000";
46 /**
47 * @var int
48 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
49 public $user = 0;
51 /** @var string */
52 public $user_text = "";
54 /** @var User */
55 public $userObj = null;
57 /** @var string */
58 public $model = null;
60 /** @var string */
61 public $format = null;
63 /** @var string */
64 public $text = "";
66 /** @var int */
67 protected $size;
69 /** @var Content */
70 public $content = null;
72 /** @var ContentHandler */
73 protected $contentHandler = null;
75 /** @var string */
76 public $comment = "";
78 /** @var bool */
79 public $minor = false;
81 /** @var string */
82 public $type = "";
84 /** @var string */
85 public $action = "";
87 /** @var string */
88 public $params = "";
90 /** @var string */
91 public $fileSrc = '';
93 /** @var bool|string */
94 public $sha1base36 = false;
96 /**
97 * @var bool
98 * @todo Unused?
100 public $isTemp = false;
102 /** @var string */
103 public $archiveName = '';
105 protected $filename;
107 /** @var mixed */
108 protected $src;
110 /** @todo Unused? */
111 public $fileIsTemp;
113 /** @var bool */
114 private $mNoUpdates = false;
116 /** @var Config $config */
117 private $config;
119 public function __construct( Config $config ) {
120 $this->config = $config;
124 * @param Title $title
125 * @throws MWException
127 function setTitle( $title ) {
128 if ( is_object( $title ) ) {
129 $this->title = $title;
130 } elseif ( is_null( $title ) ) {
131 throw new MWException( "WikiRevision given a null title in import. "
132 . "You may need to adjust \$wgLegalTitleChars." );
133 } else {
134 throw new MWException( "WikiRevision given non-object title in import." );
139 * @param int $id
141 function setID( $id ) {
142 $this->id = $id;
146 * @param string $ts
148 function setTimestamp( $ts ) {
149 # 2003-08-05T18:30:02Z
150 $this->timestamp = wfTimestamp( TS_MW, $ts );
154 * @param string $user
156 function setUsername( $user ) {
157 $this->user_text = $user;
161 * @param User $user
163 function setUserObj( $user ) {
164 $this->userObj = $user;
168 * @param string $ip
170 function setUserIP( $ip ) {
171 $this->user_text = $ip;
175 * @param string $model
177 function setModel( $model ) {
178 $this->model = $model;
182 * @param string $format
184 function setFormat( $format ) {
185 $this->format = $format;
189 * @param string $text
191 function setText( $text ) {
192 $this->text = $text;
196 * @param string $text
198 function setComment( $text ) {
199 $this->comment = $text;
203 * @param bool $minor
205 function setMinor( $minor ) {
206 $this->minor = (bool)$minor;
210 * @param mixed $src
212 function setSrc( $src ) {
213 $this->src = $src;
217 * @param string $src
218 * @param bool $isTemp
220 function setFileSrc( $src, $isTemp ) {
221 $this->fileSrc = $src;
222 $this->fileIsTemp = $isTemp;
226 * @param string $sha1base36
228 function setSha1Base36( $sha1base36 ) {
229 $this->sha1base36 = $sha1base36;
233 * @param string $filename
235 function setFilename( $filename ) {
236 $this->filename = $filename;
240 * @param string $archiveName
242 function setArchiveName( $archiveName ) {
243 $this->archiveName = $archiveName;
247 * @param int $size
249 function setSize( $size ) {
250 $this->size = intval( $size );
254 * @param string $type
256 function setType( $type ) {
257 $this->type = $type;
261 * @param string $action
263 function setAction( $action ) {
264 $this->action = $action;
268 * @param array $params
270 function setParams( $params ) {
271 $this->params = $params;
275 * @param bool $noupdates
277 public function setNoUpdates( $noupdates ) {
278 $this->mNoUpdates = $noupdates;
282 * @return Title
284 function getTitle() {
285 return $this->title;
289 * @return int
291 function getID() {
292 return $this->id;
296 * @return string
298 function getTimestamp() {
299 return $this->timestamp;
303 * @return string
305 function getUser() {
306 return $this->user_text;
310 * @return User
312 function getUserObj() {
313 return $this->userObj;
317 * @return string
319 * @deprecated Since 1.21, use getContent() instead.
321 function getText() {
322 ContentHandler::deprecated( __METHOD__, '1.21' );
324 return $this->text;
328 * @return ContentHandler
330 function getContentHandler() {
331 if ( is_null( $this->contentHandler ) ) {
332 $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
335 return $this->contentHandler;
339 * @return Content
341 function getContent() {
342 if ( is_null( $this->content ) ) {
343 $handler = $this->getContentHandler();
344 $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
347 return $this->content;
351 * @return string
353 function getModel() {
354 if ( is_null( $this->model ) ) {
355 $this->model = $this->getTitle()->getContentModel();
358 return $this->model;
362 * @return string
364 function getFormat() {
365 if ( is_null( $this->format ) ) {
366 $this->format = $this->getContentHandler()->getDefaultFormat();
369 return $this->format;
373 * @return string
375 function getComment() {
376 return $this->comment;
380 * @return bool
382 function getMinor() {
383 return $this->minor;
387 * @return mixed
389 function getSrc() {
390 return $this->src;
394 * @return bool|string
396 function getSha1() {
397 if ( $this->sha1base36 ) {
398 return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
400 return false;
404 * @return string
406 function getFileSrc() {
407 return $this->fileSrc;
411 * @return bool
413 function isTempSrc() {
414 return $this->isTemp;
418 * @return mixed
420 function getFilename() {
421 return $this->filename;
425 * @return string
427 function getArchiveName() {
428 return $this->archiveName;
432 * @return mixed
434 function getSize() {
435 return $this->size;
439 * @return string
441 function getType() {
442 return $this->type;
446 * @return string
448 function getAction() {
449 return $this->action;
453 * @return string
455 function getParams() {
456 return $this->params;
460 * @return bool
462 function importOldRevision() {
463 $dbw = wfGetDB( DB_MASTER );
465 # Sneak a single revision into place
466 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
467 if ( $user ) {
468 $userId = intval( $user->getId() );
469 $userText = $user->getName();
470 } else {
471 $userId = 0;
472 $userText = $this->getUser();
473 $user = new User;
476 // avoid memory leak...?
477 Title::clearCaches();
479 $page = WikiPage::factory( $this->title );
480 $page->loadPageData( 'fromdbmaster' );
481 if ( !$page->exists() ) {
482 // must create the page...
483 $pageId = $page->insertOn( $dbw );
484 $created = true;
485 $oldcountable = null;
486 } else {
487 $pageId = $page->getId();
488 $created = false;
490 $prior = $dbw->selectField( 'revision', '1',
491 [ 'rev_page' => $pageId,
492 'rev_timestamp' => $dbw->timestamp( $this->timestamp ),
493 'rev_user_text' => $userText,
494 'rev_comment' => $this->getComment() ],
495 __METHOD__
497 if ( $prior ) {
498 // @todo FIXME: This could fail slightly for multiple matches :P
499 wfDebug( __METHOD__ . ": skipping existing revision for [[" .
500 $this->title->getPrefixedText() . "]], timestamp " . $this->timestamp . "\n" );
501 return false;
505 if ( !$pageId ) {
506 // This seems to happen if two clients simultaneously try to import the
507 // same page
508 wfDebug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
509 $this->title->getPrefixedText() . ']], timestamp ' . $this->timestamp . "\n" );
510 return false;
513 // Select previous version to make size diffs correct
514 // @todo This assumes that multiple revisions of the same page are imported
515 // in order from oldest to newest.
516 $prevId = $dbw->selectField( 'revision', 'rev_id',
518 'rev_page' => $pageId,
519 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ),
521 __METHOD__,
522 [ 'ORDER BY' => [
523 'rev_timestamp DESC',
524 'rev_id DESC', // timestamp is not unique per page
529 # @todo FIXME: Use original rev_id optionally (better for backups)
530 # Insert the row
531 $revision = new Revision( [
532 'title' => $this->title,
533 'page' => $pageId,
534 'content_model' => $this->getModel(),
535 'content_format' => $this->getFormat(),
536 // XXX: just set 'content' => $this->getContent()?
537 'text' => $this->getContent()->serialize( $this->getFormat() ),
538 'comment' => $this->getComment(),
539 'user' => $userId,
540 'user_text' => $userText,
541 'timestamp' => $this->timestamp,
542 'minor_edit' => $this->minor,
543 'parent_id' => $prevId,
544 ] );
545 $revision->insertOn( $dbw );
546 $changed = $page->updateIfNewerOn( $dbw, $revision );
548 if ( $changed !== false && !$this->mNoUpdates ) {
549 wfDebug( __METHOD__ . ": running updates\n" );
550 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
551 $page->doEditUpdates(
552 $revision,
553 $user,
554 [ 'created' => $created, 'oldcountable' => 'no-change' ]
558 return true;
561 function importLogItem() {
562 $dbw = wfGetDB( DB_MASTER );
564 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
565 if ( $user ) {
566 $userId = intval( $user->getId() );
567 $userText = $user->getName();
568 } else {
569 $userId = 0;
570 $userText = $this->getUser();
573 # @todo FIXME: This will not record autoblocks
574 if ( !$this->getTitle() ) {
575 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
576 $this->timestamp . "\n" );
577 return;
579 # Check if it exists already
580 // @todo FIXME: Use original log ID (better for backups)
581 $prior = $dbw->selectField( 'logging', '1',
582 [ 'log_type' => $this->getType(),
583 'log_action' => $this->getAction(),
584 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
585 'log_namespace' => $this->getTitle()->getNamespace(),
586 'log_title' => $this->getTitle()->getDBkey(),
587 'log_comment' => $this->getComment(),
588 # 'log_user_text' => $this->user_text,
589 'log_params' => $this->params ],
590 __METHOD__
592 // @todo FIXME: This could fail slightly for multiple matches :P
593 if ( $prior ) {
594 wfDebug( __METHOD__
595 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
596 . $this->timestamp . "\n" );
597 return;
599 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
600 $data = [
601 'log_id' => $log_id,
602 'log_type' => $this->type,
603 'log_action' => $this->action,
604 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
605 'log_user' => $userId,
606 'log_user_text' => $userText,
607 'log_namespace' => $this->getTitle()->getNamespace(),
608 'log_title' => $this->getTitle()->getDBkey(),
609 'log_comment' => $this->getComment(),
610 'log_params' => $this->params
612 $dbw->insert( 'logging', $data, __METHOD__ );
616 * @return bool
618 function importUpload() {
619 # Construct a file
620 $archiveName = $this->getArchiveName();
621 if ( $archiveName ) {
622 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
623 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
624 RepoGroup::singleton()->getLocalRepo(), $archiveName );
625 } else {
626 $file = wfLocalFile( $this->getTitle() );
627 $file->load( File::READ_LATEST );
628 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
629 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
630 $archiveName = $file->getTimestamp() . '!' . $file->getName();
631 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
632 RepoGroup::singleton()->getLocalRepo(), $archiveName );
633 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
636 if ( !$file ) {
637 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
638 return false;
641 # Get the file source or download if necessary
642 $source = $this->getFileSrc();
643 $autoDeleteSource = $this->isTempSrc();
644 if ( !strlen( $source ) ) {
645 $source = $this->downloadSource();
646 $autoDeleteSource = true;
648 if ( !strlen( $source ) ) {
649 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
650 return false;
653 $tmpFile = new TempFSFile( $source );
654 if ( $autoDeleteSource ) {
655 $tmpFile->autocollect();
658 $sha1File = ltrim( sha1_file( $source ), '0' );
659 $sha1 = $this->getSha1();
660 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
661 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
662 return false;
665 $user = $this->getUserObj() ?: User::newFromName( $this->getUser() );
667 # Do the actual upload
668 if ( $archiveName ) {
669 $status = $file->uploadOld( $source, $archiveName,
670 $this->getTimestamp(), $this->getComment(), $user );
671 } else {
672 $flags = 0;
673 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
674 $flags, false, $this->getTimestamp(), $user );
677 if ( $status->isGood() ) {
678 wfDebug( __METHOD__ . ": Successful\n" );
679 return true;
680 } else {
681 wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
682 return false;
687 * @return bool|string
689 function downloadSource() {
690 if ( !$this->config->get( 'EnableUploads' ) ) {
691 return false;
694 $tempo = tempnam( wfTempDir(), 'download' );
695 $f = fopen( $tempo, 'wb' );
696 if ( !$f ) {
697 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
698 return false;
701 // @todo FIXME!
702 $src = $this->getSrc();
703 $data = Http::get( $src, [], __METHOD__ );
704 if ( !$data ) {
705 wfDebug( "IMPORT: couldn't fetch source $src\n" );
706 fclose( $f );
707 unlink( $tempo );
708 return false;
711 fwrite( $f, $data );
712 fclose( $f );
714 return $tempo;