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