Debian Codegraph @

includes/import/WikiRevision.php

raw symbols SCIP
1 <?php
2 /**
3 * MediaWiki page data importer.
4 *
5 * Copyright © 2003,2005 Brooke Vibber <[email protected]>
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 use MediaWiki\Content\Content;
28 use MediaWiki\Content\ContentHandler;
29 use MediaWiki\MediaWikiServices;
30 use MediaWiki\Revision\MutableRevisionSlots;
31 use MediaWiki\Revision\SlotRecord;
32 use MediaWiki\Title\Title;
33 use MediaWiki\User\ExternalUserNames;
34 use MediaWiki\User\User;
35 use MediaWiki\User\UserIdentityValue;
36
37 /**
38 * Represents a revision, log entry or upload during the import process.
39 * This class sticks closely to the structure of the XML dump.
40 *
41 * @since 1.2
42 *
43 * @ingroup SpecialPage
44 */
45 class WikiRevision implements ImportableUploadRevision, ImportableOldRevision {
46
47 /**
48 * @since 1.2
49 * @var Title
50 */
51 public $title = null;
52
53 /**
54 * @since 1.6.4
55 * @var int
56 */
57 public $id = 0;
58
59 /**
60 * @since 1.2
61 * @var string TS_MW timestamp, a string with 14 digits
62 */
63 public $timestamp = "20010115000000";
64
65 /**
66 * @since 1.2
67 * @var string
68 */
69 public $user_text = "";
70
71 /**
72 * @deprecated since 1.39, use {@see $user_text} instead
73 * @since 1.27
74 * @var User|null
75 */
76 public $userObj = null;
77
78 /**
79 * @since 1.21
80 * @deprecated since 1.35, use getContent
81 * @var string
82 */
83 public $model = null;
84
85 /**
86 * @since 1.21
87 * @deprecated since 1.35, use getContent
88 * @var string
89 */
90 public $format = null;
91
92 /**
93 * @since 1.2
94 * @deprecated since 1.35, use getContent
95 * @var string
96 */
97 public $text = "";
98
99 /**
100 * @since 1.12.2
101 * @var int
102 */
103 protected $size;
104
105 /**
106 * @since 1.21
107 * @deprecated since 1.35, use getContent
108 * @var Content
109 */
110 public $content = null;
111
112 /**
113 * @since 1.24
114 * @var ContentHandler
115 */
116 protected $contentHandler = null;
117
118 /**
119 * @since 1.2.6
120 * @var string
121 */
122 public $comment = "";
123
124 private MutableRevisionSlots $slots;
125
126 /**
127 * @since 1.5.7
128 * @var bool
129 */
130 public $minor = false;
131
132 /**
133 * @since 1.12.2
134 * @var string
135 */
136 public $type = "";
137
138 /**
139 * @since 1.12.2
140 * @var string
141 */
142 public $action = "";
143
144 /**
145 * @since 1.12.2
146 * @var string
147 */
148 public $params = "";
149
150 /**
151 * @since 1.17
152 * @var string
153 */
154 public $fileSrc = '';
155
156 /**
157 * @var string|null
158 */
159 private $sha1base36;
160
161 /**
162 * @since 1.34
163 * @var string[]
164 */
165 protected $tags = [];
166
167 /**
168 * @since 1.17
169 * @var string
170 */
171 public $archiveName = '';
172
173 /**
174 * @since 1.12.2
175 * @var string|null
176 */
177 protected $filename;
178
179 /**
180 * @since 1.12.2
181 * @var string|null
182 */
183 protected $src = null;
184
185 /**
186 * @since 1.18
187 * @var bool
188 * @todo Unused?
189 */
190 public $isTemp = false;
191
192 /** @var bool */
193 private $mNoUpdates = false;
194
195 public function __construct() {
196 $this->slots = new MutableRevisionSlots();
197 }
198
199 /**
200 * @param Title $title
201 */
202 public function setTitle( Title $title ) {
203 $this->title = $title;
204 }
205
206 /**
207 * @since 1.6.4
208 * @param int $id
209 */
210 public function setID( $id ) {
211 $this->id = $id;
212 }
213
214 /**
215 * @since 1.2
216 * @param string $ts
217 */
218 public function setTimestamp( $ts ) {
219 # 2003-08-05T18:30:02Z
220 $this->timestamp = wfTimestamp( TS_MW, $ts );
221 }
222
223 /**
224 * @since 1.2
225 * @param string $user
226 */
227 public function setUsername( $user ) {
228 $this->user_text = $user;
229 }
230
231 /**
232 * @deprecated since 1.39, use {@see setUsername} instead
233 * @since 1.27
234 * @param User $user
235 */
236 public function setUserObj( $user ) {
237 // Not officially supported, but some callers pass false from e.g. User::newFromName()
238 $this->userObj = $user ?: null;
239 if ( $this->user_text === '' && $user ) {
240 $this->user_text = $user->getName();
241 }
242 }
243
244 /**
245 * @deprecated since 1.39, use {@see setUsername} instead, it does the same anyway
246 * @since 1.2
247 * @param string $ip
248 */
249 public function setUserIP( $ip ) {
250 $this->user_text = $ip;
251 }
252
253 /**
254 * @since 1.21
255 * @deprecated since 1.35, use setContent instead.
256 * @param string $model
257 */
258 public function setModel( $model ) {
259 $this->model = $model;
260 }
261
262 /**
263 * @since 1.21
264 * @deprecated since 1.35, use setContent instead.
265 * @param string $format
266 */
267 public function setFormat( $format ) {
268 $this->format = $format;
269 }
270
271 /**
272 * @since 1.2
273 * @deprecated since 1.35, use setContent instead.
274 * @param string $text
275 */
276 public function setText( $text ) {
277 $handler = ContentHandler::getForModelID( $this->getModel() );
278 $content = $handler->unserializeContent( $text );
279 $this->setContent( SlotRecord::MAIN, $content );
280 }
281
282 /**
283 * @since 1.35
284 * @param string $role
285 * @param Content $content
286 */
287 public function setContent( $role, $content ) {
288 $this->slots->setContent( $role, $content );
289
290 // backwards compat
291 if ( $role === SlotRecord::MAIN ) {
292 $this->content = $content;
293 $this->model = $content->getModel();
294 $this->format = $content->getDefaultFormat();
295 $this->text = $content->serialize();
296 }
297 }
298
299 /**
300 * @since 1.2.6
301 * @param string $text
302 */
303 public function setComment( string $text ) {
304 $this->comment = $text;
305 }
306
307 /**
308 * @since 1.5.7
309 * @param bool $minor
310 */
311 public function setMinor( $minor ) {
312 $this->minor = (bool)$minor;
313 }
314
315 /**
316 * @since 1.12.2
317 * @param string|null $src
318 */
319 public function setSrc( $src ) {
320 $this->src = $src;
321 }
322
323 /**
324 * @since 1.17
325 * @param string $src
326 * @param bool $isTemp
327 */
328 public function setFileSrc( $src, $isTemp ) {
329 $this->fileSrc = $src;
330 $this->isTemp = $isTemp;
331 }
332
333 /**
334 * @since 1.17
335 * @param string $sha1base36
336 */
337 public function setSha1Base36( $sha1base36 ) {
338 $this->sha1base36 = $sha1base36 ?: null;
339 }
340
341 /**
342 * @since 1.34
343 * @param string[] $tags
344 */
345 public function setTags( array $tags ) {
346 $this->tags = $tags;
347 }
348
349 /**
350 * @since 1.12.2
351 * @param string $filename
352 */
353 public function setFilename( $filename ) {
354 $this->filename = $filename;
355 }
356
357 /**
358 * @since 1.17
359 * @param string $archiveName
360 */
361 public function setArchiveName( $archiveName ) {
362 $this->archiveName = $archiveName;
363 }
364
365 /**
366 * @since 1.12.2
367 * @param int $size
368 */
369 public function setSize( $size ) {
370 $this->size = intval( $size );
371 }
372
373 /**
374 * @since 1.12.2
375 * @param string $type
376 */
377 public function setType( $type ) {
378 $this->type = $type;
379 }
380
381 /**
382 * @since 1.12.2
383 * @param string $action
384 */
385 public function setAction( $action ) {
386 $this->action = $action;
387 }
388
389 /**
390 * @since 1.12.2
391 * @param string $params
392 */
393 public function setParams( $params ) {
394 $this->params = $params;
395 }
396
397 /**
398 * @since 1.18
399 * @param bool $noupdates
400 */
401 public function setNoUpdates( $noupdates ) {
402 $this->mNoUpdates = $noupdates;
403 }
404
405 /**
406 * @since 1.2
407 * @return Title
408 */
409 public function getTitle() {
410 return $this->title;
411 }
412
413 /**
414 * @since 1.6.4
415 * @return int
416 */
417 public function getID() {
418 return $this->id;
419 }
420
421 /**
422 * @since 1.2
423 * @return string TS_MW timestamp, a string with 14 digits
424 */
425 public function getTimestamp() {
426 return $this->timestamp;
427 }
428
429 /**
430 * @since 1.2
431 * @return string
432 */
433 public function getUser() {
434 return $this->user_text;
435 }
436
437 /**
438 * @deprecated since 1.39, use {@see getUser} instead; this is almost always null anyway
439 * @since 1.27
440 * @return User|null Typically null, use {@see getUser} instead
441 */
442 public function getUserObj() {
443 return $this->userObj;
444 }
445
446 /**
447 * @since 1.2
448 * @return string
449 */
450 public function getText() {
451 return $this->text;
452 }
453
454 /**
455 * @since 1.24
456 * @deprecated since 1.35, use getContent
457 * @return ContentHandler
458 * @throws MWUnknownContentModelException
459 */
460 public function getContentHandler() {
461 $this->contentHandler ??= MediaWikiServices::getInstance()
462 ->getContentHandlerFactory()
463 ->getContentHandler( $this->getModel() );
464
465 return $this->contentHandler;
466 }
467
468 /**
469 * @since 1.21
470 * @param string $role added in 1.35
471 * @return Content
472 */
473 public function getContent( $role = SlotRecord::MAIN ) {
474 return $this->slots->getContent( $role );
475 }
476
477 /**
478 * @since 1.35
479 * @param string $role
480 * @return SlotRecord
481 */
482 public function getSlot( $role ) {
483 return $this->slots->getSlot( $role );
484 }
485
486 /**
487 * @since 1.35
488 * @return string[]
489 */
490 public function getSlotRoles() {
491 return $this->slots->getSlotRoles();
492 }
493
494 /**
495 * @since 1.21
496 * @deprecated since 1.35, use getContent
497 * @return string
498 */
499 public function getModel() {
500 $this->model ??= $this->getTitle()->getContentModel();
501
502 return $this->model;
503 }
504
505 /**
506 * @since 1.21
507 * @deprecated since 1.35, use getContent
508 * @return string
509 */
510 public function getFormat() {
511 $this->format ??= $this->getContentHandler()->getDefaultFormat();
512
513 return $this->format;
514 }
515
516 /**
517 * @since 1.2.6
518 * @return string
519 */
520 public function getComment(): string {
521 return $this->comment;
522 }
523
524 /**
525 * @since 1.5.7
526 * @return bool
527 */
528 public function getMinor() {
529 return $this->minor;
530 }
531
532 /**
533 * @since 1.12.2
534 * @return string|null
535 */
536 public function getSrc() {
537 return $this->src;
538 }
539
540 /**
541 * @since 1.17
542 * @return string|false
543 */
544 public function getSha1() {
545 if ( $this->sha1base36 ) {
546 return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
547 }
548 return false;
549 }
550
551 /**
552 * @since 1.31
553 * @return string|false
554 */
555 public function getSha1Base36() {
556 return $this->sha1base36 ?? false;
557 }
558
559 /**
560 * @since 1.34
561 * @return string[]
562 */
563 public function getTags() {
564 return $this->tags;
565 }
566
567 /**
568 * @since 1.17
569 * @return string
570 */
571 public function getFileSrc() {
572 return $this->fileSrc;
573 }
574
575 /**
576 * @since 1.17
577 * @return bool
578 */
579 public function isTempSrc() {
580 return $this->isTemp;
581 }
582
583 /**
584 * @since 1.12.2
585 * @return mixed
586 */
587 public function getFilename() {
588 return $this->filename;
589 }
590
591 /**
592 * @since 1.17
593 * @return string
594 */
595 public function getArchiveName() {
596 return $this->archiveName;
597 }
598
599 /**
600 * @since 1.12.2
601 * @return mixed
602 */
603 public function getSize() {
604 return $this->size;
605 }
606
607 /**
608 * @since 1.12.2
609 * @return string
610 */
611 public function getType() {
612 return $this->type;
613 }
614
615 /**
616 * @since 1.12.2
617 * @return string
618 */
619 public function getAction() {
620 return $this->action;
621 }
622
623 /**
624 * @since 1.12.2
625 * @return string
626 */
627 public function getParams() {
628 return $this->params;
629 }
630
631 /**
632 * @since 1.4.1
633 * @deprecated since 1.31. Use OldRevisionImporter::import
634 * @return bool
635 */
636 public function importOldRevision() {
637 if ( $this->mNoUpdates ) {
638 $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
639 } else {
640 $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporter();
641 }
642 return $importer->import( $this );
643 }
644
645 /**
646 * @since 1.12.2
647 * @return bool
648 */
649 public function importLogItem() {
650 $services = MediaWikiServices::getInstance();
651 $dbw = $services->getConnectionProvider()->getPrimaryDatabase();
652
653 // Check for unsafe log_params, in case other code elsewhere uses unserialize() directly
654 // instead of safely calling LogEntryBase::extractParams(). (T422244)
655 if ( LogEntryBase::containsUnsafeParams(
656 LogEntryBase::extractParams( $this->params, "{$this->type}/{$this->action}" ),
657 ) ) {
658 wfDebug( __METHOD__
659 . ": skipping {$this->type}/{$this->action} with unsafe params" );
660 return false;
661 }
662
663 $userName = $this->getUser();
664 if ( ExternalUserNames::isExternal( $userName ) ) {
665 // Use newAnonymous() since the user name is already prefixed.
666 $user = UserIdentityValue::newAnonymous( $userName );
667 } else {
668 $user = $this->getUserObj() ?: User::newFromName( $userName, false );
669 }
670
671 # @todo FIXME: This will not record autoblocks
672 if ( !$this->getTitle() ) {
673 wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
674 $this->timestamp );
675 return false;
676 }
677 # Check if it exists already
678 // @todo FIXME: Use original log ID (better for backups)
679 $prior = (bool)$dbw->newSelectQueryBuilder()
680 ->select( '1' )
681 ->from( 'logging' )
682 ->where( [
683 'log_type' => $this->getType(),
684 'log_action' => $this->getAction(),
685 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
686 'log_namespace' => $this->getTitle()->getNamespace(),
687 'log_title' => $this->getTitle()->getDBkey(),
688 'log_params' => $this->params
689 ] )
690 ->caller( __METHOD__ )->fetchField();
691 // @todo FIXME: This could fail slightly for multiple matches :P
692 if ( $prior ) {
693 wfDebug( __METHOD__
694 . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
695 . $this->timestamp );
696 return false;
697 }
698 $actorId = $services->getActorNormalization()->acquireActorId( $user, $dbw );
699 $dbw->newInsertQueryBuilder()
700 ->insertInto( 'logging' )
701 ->row( [
702 'log_type' => $this->type,
703 'log_action' => $this->action,
704 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
705 'log_actor' => $actorId,
706 'log_namespace' => $this->getTitle()->getNamespace(),
707 'log_title' => $this->getTitle()->getDBkey(),
708 'log_params' => $this->params
709 ] + $services->getCommentStore()->insert( $dbw, 'log_comment', $this->getComment() ) )
710 ->caller( __METHOD__ )->execute();
711 return true;
712 }
713
714 }