Import: Try to stop revisions getting created with rev_page = 0
[lhc/web/wiklou.git] / includes / import / WikiRevision.php
1 <?php
2 /**
3 * MediaWiki page data importer.
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup SpecialPage
25 */
26
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.
30 *
31 * @ingroup SpecialPage
32 */
33 class WikiRevision {
34 /** @todo Unused? */
35 public $importer = null;
36
37 /** @var Title */
38 public $title = null;
39
40 /** @var int */
41 public $id = 0;
42
43 /** @var string */
44 public $timestamp = "20010115000000";
45
46 /**
47 * @var int
48 * @todo Can't find any uses. Public, because that's suspicious. Get clarity. */
49 public $user = 0;
50
51 /** @var string */
52 public $user_text = "";
53
54 /** @var string */
55 public $model = null;
56
57 /** @var string */
58 public $format = null;
59
60 /** @var string */
61 public $text = "";
62
63 /** @var int */
64 protected $size;
65
66 /** @var Content */
67 public $content = null;
68
69 /** @var ContentHandler */
70 protected $contentHandler = null;
71
72 /** @var string */
73 public $comment = "";
74
75 /** @var bool */
76 public $minor = false;
77
78 /** @var string */
79 public $type = "";
80
81 /** @var string */
82 public $action = "";
83
84 /** @var string */
85 public $params = "";
86
87 /** @var string */
88 public $fileSrc = '';
89
90 /** @var bool|string */
91 public $sha1base36 = false;
92
93 /**
94 * @var bool
95 * @todo Unused?
96 */
97 public $isTemp = false;
98
99 /** @var string */
100 public $archiveName = '';
101
102 protected $filename;
103
104 /** @var mixed */
105 protected $src;
106
107 /** @todo Unused? */
108 public $fileIsTemp;
109
110 /** @var bool */
111 private $mNoUpdates = false;
112
113 /** @var Config $config */
114 private $config;
115
116 public function __construct( Config $config ) {
117 $this->config = $config;
118 }
119
120 /**
121 * @param Title $title
122 * @throws MWException
123 */
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." );
130 } else {
131 throw new MWException( "WikiRevision given non-object title in import." );
132 }
133 }
134
135 /**
136 * @param int $id
137 */
138 function setID( $id ) {
139 $this->id = $id;
140 }
141
142 /**
143 * @param string $ts
144 */
145 function setTimestamp( $ts ) {
146 # 2003-08-05T18:30:02Z
147 $this->timestamp = wfTimestamp( TS_MW, $ts );
148 }
149
150 /**
151 * @param string $user
152 */
153 function setUsername( $user ) {
154 $this->user_text = $user;
155 }
156
157 /**
158 * @param string $ip
159 */
160 function setUserIP( $ip ) {
161 $this->user_text = $ip;
162 }
163
164 /**
165 * @param string $model
166 */
167 function setModel( $model ) {
168 $this->model = $model;
169 }
170
171 /**
172 * @param string $format
173 */
174 function setFormat( $format ) {
175 $this->format = $format;
176 }
177
178 /**
179 * @param string $text
180 */
181 function setText( $text ) {
182 $this->text = $text;
183 }
184
185 /**
186 * @param string $text
187 */
188 function setComment( $text ) {
189 $this->comment = $text;
190 }
191
192 /**
193 * @param bool $minor
194 */
195 function setMinor( $minor ) {
196 $this->minor = (bool)$minor;
197 }
198
199 /**
200 * @param mixed $src
201 */
202 function setSrc( $src ) {
203 $this->src = $src;
204 }
205
206 /**
207 * @param string $src
208 * @param bool $isTemp
209 */
210 function setFileSrc( $src, $isTemp ) {
211 $this->fileSrc = $src;
212 $this->fileIsTemp = $isTemp;
213 }
214
215 /**
216 * @param string $sha1base36
217 */
218 function setSha1Base36( $sha1base36 ) {
219 $this->sha1base36 = $sha1base36;
220 }
221
222 /**
223 * @param string $filename
224 */
225 function setFilename( $filename ) {
226 $this->filename = $filename;
227 }
228
229 /**
230 * @param string $archiveName
231 */
232 function setArchiveName( $archiveName ) {
233 $this->archiveName = $archiveName;
234 }
235
236 /**
237 * @param int $size
238 */
239 function setSize( $size ) {
240 $this->size = intval( $size );
241 }
242
243 /**
244 * @param string $type
245 */
246 function setType( $type ) {
247 $this->type = $type;
248 }
249
250 /**
251 * @param string $action
252 */
253 function setAction( $action ) {
254 $this->action = $action;
255 }
256
257 /**
258 * @param array $params
259 */
260 function setParams( $params ) {
261 $this->params = $params;
262 }
263
264 /**
265 * @param bool $noupdates
266 */
267 public function setNoUpdates( $noupdates ) {
268 $this->mNoUpdates = $noupdates;
269 }
270
271 /**
272 * @return Title
273 */
274 function getTitle() {
275 return $this->title;
276 }
277
278 /**
279 * @return int
280 */
281 function getID() {
282 return $this->id;
283 }
284
285 /**
286 * @return string
287 */
288 function getTimestamp() {
289 return $this->timestamp;
290 }
291
292 /**
293 * @return string
294 */
295 function getUser() {
296 return $this->user_text;
297 }
298
299 /**
300 * @return string
301 *
302 * @deprecated Since 1.21, use getContent() instead.
303 */
304 function getText() {
305 ContentHandler::deprecated( __METHOD__, '1.21' );
306
307 return $this->text;
308 }
309
310 /**
311 * @return ContentHandler
312 */
313 function getContentHandler() {
314 if ( is_null( $this->contentHandler ) ) {
315 $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
316 }
317
318 return $this->contentHandler;
319 }
320
321 /**
322 * @return Content
323 */
324 function getContent() {
325 if ( is_null( $this->content ) ) {
326 $handler = $this->getContentHandler();
327 $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
328 }
329
330 return $this->content;
331 }
332
333 /**
334 * @return string
335 */
336 function getModel() {
337 if ( is_null( $this->model ) ) {
338 $this->model = $this->getTitle()->getContentModel();
339 }
340
341 return $this->model;
342 }
343
344 /**
345 * @return string
346 */
347 function getFormat() {
348 if ( is_null( $this->format ) ) {
349 $this->format = $this->getContentHandler()->getDefaultFormat();
350 }
351
352 return $this->format;
353 }
354
355 /**
356 * @return string
357 */
358 function getComment() {
359 return $this->comment;
360 }
361
362 /**
363 * @return bool
364 */
365 function getMinor() {
366 return $this->minor;
367 }
368
369 /**
370 * @return mixed
371 */
372 function getSrc() {
373 return $this->src;
374 }
375
376 /**
377 * @return bool|string
378 */
379 function getSha1() {
380 if ( $this->sha1base36 ) {
381 return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
382 }
383 return false;
384 }
385
386 /**
387 * @return string
388 */
389 function getFileSrc() {
390 return $this->fileSrc;
391 }
392
393 /**
394 * @return bool
395 */
396 function isTempSrc() {
397 return $this->isTemp;
398 }
399
400 /**
401 * @return mixed
402 */
403 function getFilename() {
404 return $this->filename;
405 }
406
407 /**
408 * @return string
409 */
410 function getArchiveName() {
411 return $this->archiveName;
412 }
413
414 /**
415 * @return mixed
416 */
417 function getSize() {
418 return $this->size;
419 }
420
421 /**
422 * @return string
423 */
424 function getType() {
425 return $this->type;
426 }
427
428 /**
429 * @return string
430 */
431 function getAction() {
432 return $this->action;
433 }
434
435 /**
436 * @return string
437 */
438 function getParams() {
439 return $this->params;
440 }
441
442 /**
443 * @return bool
444 */
445 function importOldRevision() {
446 $dbw = wfGetDB( DB_MASTER );
447
448 # Sneak a single revision into place
449 $user = User::newFromName( $this->getUser() );
450 if ( $user ) {
451 $userId = intval( $user->getId() );
452 $userText = $user->getName();
453 $userObj = $user;
454 } else {
455 $userId = 0;
456 $userText = $this->getUser();
457 $userObj = new User;
458 }
459
460 // avoid memory leak...?
461 Title::clearCaches();
462
463 $page = WikiPage::factory( $this->title );
464 $page->loadPageData( 'fromdbmaster' );
465 if ( !$page->exists() ) {
466 // must create the page...
467 $pageId = $page->insertOn( $dbw );
468 $created = true;
469 $oldcountable = null;
470 } else {
471 $pageId = $page->getId();
472 $created = false;
473
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() ),
479 __METHOD__
480 );
481 if ( $prior ) {
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" );
485 return false;
486 }
487 }
488
489 if ( !$pageId ) {
490 // This seems to happen if two clients simultaneously try to import the
491 // same page
492 wfDebug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
493 $this->title->getPrefixedText() . ']], timestamp ' . $this->timestamp . "\n" );
494 return false;
495 }
496
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',
501 array(
502 'rev_page' => $pageId,
503 'rev_timestamp <= ' . $dbw->timestamp( $this->timestamp ),
504 ),
505 __METHOD__,
506 array( 'ORDER BY' => array(
507 'rev_timestamp DESC',
508 'rev_id DESC', // timestamp is not unique per page
509 )
510 )
511 );
512
513 # @todo FIXME: Use original rev_id optionally (better for backups)
514 # Insert the row
515 $revision = new Revision( array(
516 'title' => $this->title,
517 'page' => $pageId,
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(),
523 'user' => $userId,
524 'user_text' => $userText,
525 'timestamp' => $this->timestamp,
526 'minor_edit' => $this->minor,
527 'parent_id' => $prevId,
528 ) );
529 $revision->insertOn( $dbw );
530 $changed = $page->updateIfNewerOn( $dbw, $revision );
531
532 if ( $changed !== false && !$this->mNoUpdates ) {
533 wfDebug( __METHOD__ . ": running updates\n" );
534 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
535 $page->doEditUpdates(
536 $revision,
537 $userObj,
538 array( 'created' => $created, 'oldcountable' => 'no-change' )
539 );
540 }
541
542 return true;
543 }
544
545 function importLogItem() {
546 $dbw = wfGetDB( DB_MASTER );
547 # @todo FIXME: This will not record autoblocks
548 if ( !$this->getTitle() ) {
549 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
550 $this->timestamp . "\n" );
551 return;
552 }
553 # Check if it exists already
554 // @todo FIXME: Use original log ID (better for backups)
555 $prior = $dbw->selectField( 'logging', '1',
556 array( 'log_type' => $this->getType(),
557 'log_action' => $this->getAction(),
558 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
559 'log_namespace' => $this->getTitle()->getNamespace(),
560 'log_title' => $this->getTitle()->getDBkey(),
561 'log_comment' => $this->getComment(),
562 # 'log_user_text' => $this->user_text,
563 'log_params' => $this->params ),
564 __METHOD__
565 );
566 // @todo FIXME: This could fail slightly for multiple matches :P
567 if ( $prior ) {
568 wfDebug( __METHOD__
569 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
570 . $this->timestamp . "\n" );
571 return;
572 }
573 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
574 $data = array(
575 'log_id' => $log_id,
576 'log_type' => $this->type,
577 'log_action' => $this->action,
578 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
579 'log_user' => User::idFromName( $this->user_text ),
580 # 'log_user_text' => $this->user_text,
581 'log_namespace' => $this->getTitle()->getNamespace(),
582 'log_title' => $this->getTitle()->getDBkey(),
583 'log_comment' => $this->getComment(),
584 'log_params' => $this->params
585 );
586 $dbw->insert( 'logging', $data, __METHOD__ );
587 }
588
589 /**
590 * @return bool
591 */
592 function importUpload() {
593 # Construct a file
594 $archiveName = $this->getArchiveName();
595 if ( $archiveName ) {
596 wfDebug( __METHOD__ . "Importing archived file as $archiveName\n" );
597 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
598 RepoGroup::singleton()->getLocalRepo(), $archiveName );
599 } else {
600 $file = wfLocalFile( $this->getTitle() );
601 $file->load( File::READ_LATEST );
602 wfDebug( __METHOD__ . 'Importing new file as ' . $file->getName() . "\n" );
603 if ( $file->exists() && $file->getTimestamp() > $this->getTimestamp() ) {
604 $archiveName = $file->getTimestamp() . '!' . $file->getName();
605 $file = OldLocalFile::newFromArchiveName( $this->getTitle(),
606 RepoGroup::singleton()->getLocalRepo(), $archiveName );
607 wfDebug( __METHOD__ . "File already exists; importing as $archiveName\n" );
608 }
609 }
610 if ( !$file ) {
611 wfDebug( __METHOD__ . ': Bad file for ' . $this->getTitle() . "\n" );
612 return false;
613 }
614
615 # Get the file source or download if necessary
616 $source = $this->getFileSrc();
617 $flags = $this->isTempSrc() ? File::DELETE_SOURCE : 0;
618 if ( !$source ) {
619 $source = $this->downloadSource();
620 $flags |= File::DELETE_SOURCE;
621 }
622 if ( !$source ) {
623 wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
624 return false;
625 }
626 $sha1File = ltrim( sha1_file( $source ), '0' );
627 $sha1 = $this->getSha1();
628 if ( $sha1 && ( $sha1 !== $sha1File ) ) {
629 if ( $flags & File::DELETE_SOURCE ) {
630 # Broken file; delete it if it is a temporary file
631 unlink( $source );
632 }
633 wfDebug( __METHOD__ . ": Corrupt file $source.\n" );
634 return false;
635 }
636
637 $user = User::newFromName( $this->user_text );
638
639 # Do the actual upload
640 if ( $archiveName ) {
641 $status = $file->uploadOld( $source, $archiveName,
642 $this->getTimestamp(), $this->getComment(), $user, $flags );
643 } else {
644 $status = $file->upload( $source, $this->getComment(), $this->getComment(),
645 $flags, false, $this->getTimestamp(), $user );
646 }
647
648 if ( $status->isGood() ) {
649 wfDebug( __METHOD__ . ": Successful\n" );
650 return true;
651 } else {
652 wfDebug( __METHOD__ . ': failed: ' . $status->getHTML() . "\n" );
653 return false;
654 }
655 }
656
657 /**
658 * @return bool|string
659 */
660 function downloadSource() {
661 if ( !$this->config->get( 'EnableUploads' ) ) {
662 return false;
663 }
664
665 $tempo = tempnam( wfTempDir(), 'download' );
666 $f = fopen( $tempo, 'wb' );
667 if ( !$f ) {
668 wfDebug( "IMPORT: couldn't write to temp file $tempo\n" );
669 return false;
670 }
671
672 // @todo FIXME!
673 $src = $this->getSrc();
674 $data = Http::get( $src, array(), __METHOD__ );
675 if ( !$data ) {
676 wfDebug( "IMPORT: couldn't fetch source $src\n" );
677 fclose( $f );
678 unlink( $tempo );
679 return false;
680 }
681
682 fwrite( $f, $data );
683 fclose( $f );
684
685 return $tempo;
686 }
687
688 }