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