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