WikiRevision.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. use MediaWiki\Logger\LoggerFactory;
  27. use MediaWiki\MediaWikiServices;
  28. /**
  29. * Represents a revision, log entry or upload during the import process.
  30. * This class sticks closely to the structure of the XML dump.
  31. *
  32. * @since 1.2
  33. *
  34. * @ingroup SpecialPage
  35. */
  36. class WikiRevision implements ImportableUploadRevision, ImportableOldRevision {
  37. /**
  38. * @since 1.2
  39. * @var Title
  40. */
  41. public $title = null;
  42. /**
  43. * @since 1.6.4
  44. * @var int
  45. */
  46. public $id = 0;
  47. /**
  48. * @since 1.2
  49. * @var string
  50. */
  51. public $timestamp = "20010115000000";
  52. /**
  53. * @since 1.2
  54. * @var string
  55. */
  56. public $user_text = "";
  57. /**
  58. * @since 1.27
  59. * @var User
  60. */
  61. public $userObj = null;
  62. /**
  63. * @since 1.21
  64. * @var string
  65. */
  66. public $model = null;
  67. /**
  68. * @since 1.21
  69. * @var string
  70. */
  71. public $format = null;
  72. /**
  73. * @since 1.2
  74. * @var string
  75. */
  76. public $text = "";
  77. /**
  78. * @since 1.12.2
  79. * @var int
  80. */
  81. protected $size;
  82. /**
  83. * @since 1.21
  84. * @var Content
  85. */
  86. public $content = null;
  87. /**
  88. * @since 1.24
  89. * @var ContentHandler
  90. */
  91. protected $contentHandler = null;
  92. /**
  93. * @since 1.2.6
  94. * @var string
  95. */
  96. public $comment = "";
  97. /**
  98. * @since 1.5.7
  99. * @var bool
  100. */
  101. public $minor = false;
  102. /**
  103. * @since 1.12.2
  104. * @var string
  105. */
  106. public $type = "";
  107. /**
  108. * @since 1.12.2
  109. * @var string
  110. */
  111. public $action = "";
  112. /**
  113. * @since 1.12.2
  114. * @var string
  115. */
  116. public $params = "";
  117. /**
  118. * @since 1.17
  119. * @var string
  120. */
  121. public $fileSrc = '';
  122. /**
  123. * @since 1.17
  124. * @var bool|string
  125. */
  126. public $sha1base36 = false;
  127. /**
  128. * @since 1.34
  129. * @var string[]
  130. */
  131. protected $tags = [];
  132. /**
  133. * @since 1.17
  134. * @var string
  135. */
  136. public $archiveName = '';
  137. /**
  138. * @since 1.12.2
  139. */
  140. protected $filename;
  141. /**
  142. * @since 1.12.2
  143. * @var string|null
  144. */
  145. protected $src = null;
  146. /**
  147. * @since 1.18
  148. * @var bool
  149. * @todo Unused?
  150. */
  151. public $isTemp = false;
  152. /**
  153. * @since 1.18
  154. * @deprecated 1.29 use Wikirevision::isTempSrc()
  155. * First written to in 43d5d3b682cc1733ad01a837d11af4a402d57e6a
  156. * Actually introduced in 52cd34acf590e5be946b7885ffdc13a157c1c6cf
  157. */
  158. public $fileIsTemp;
  159. /** @var bool */
  160. private $mNoUpdates = false;
  161. /**
  162. * @deprecated since 1.31, along with self::downloadSource()
  163. * @var Config $config
  164. */
  165. private $config;
  166. /**
  167. * @param Config $config Deprecated since 1.31, along with self::downloadSource(). Just pass an
  168. * empty HashConfig.
  169. */
  170. public function __construct( Config $config ) {
  171. $this->config = $config;
  172. }
  173. /**
  174. * @since 1.7 taking a Title object (string before)
  175. * @param Title $title
  176. * @throws MWException
  177. */
  178. public function setTitle( $title ) {
  179. if ( is_object( $title ) ) {
  180. $this->title = $title;
  181. } elseif ( is_null( $title ) ) {
  182. throw new MWException( "WikiRevision given a null title in import. "
  183. . "You may need to adjust \$wgLegalTitleChars." );
  184. } else {
  185. throw new MWException( "WikiRevision given non-object title in import." );
  186. }
  187. }
  188. /**
  189. * @since 1.6.4
  190. * @param int $id
  191. */
  192. public function setID( $id ) {
  193. $this->id = $id;
  194. }
  195. /**
  196. * @since 1.2
  197. * @param string $ts
  198. */
  199. public function setTimestamp( $ts ) {
  200. # 2003-08-05T18:30:02Z
  201. $this->timestamp = wfTimestamp( TS_MW, $ts );
  202. }
  203. /**
  204. * @since 1.2
  205. * @param string $user
  206. */
  207. public function setUsername( $user ) {
  208. $this->user_text = $user;
  209. }
  210. /**
  211. * @since 1.27
  212. * @param User $user
  213. */
  214. public function setUserObj( $user ) {
  215. $this->userObj = $user;
  216. }
  217. /**
  218. * @since 1.2
  219. * @param string $ip
  220. */
  221. public function setUserIP( $ip ) {
  222. $this->user_text = $ip;
  223. }
  224. /**
  225. * @since 1.21
  226. * @param string $model
  227. */
  228. public function setModel( $model ) {
  229. $this->model = $model;
  230. }
  231. /**
  232. * @since 1.21
  233. * @param string $format
  234. */
  235. public function setFormat( $format ) {
  236. $this->format = $format;
  237. }
  238. /**
  239. * @since 1.2
  240. * @param string $text
  241. */
  242. public function setText( $text ) {
  243. $this->text = $text;
  244. }
  245. /**
  246. * @since 1.2.6
  247. * @param string $text
  248. */
  249. public function setComment( $text ) {
  250. $this->comment = $text;
  251. }
  252. /**
  253. * @since 1.5.7
  254. * @param bool $minor
  255. */
  256. public function setMinor( $minor ) {
  257. $this->minor = (bool)$minor;
  258. }
  259. /**
  260. * @since 1.12.2
  261. * @param string|null $src
  262. */
  263. public function setSrc( $src ) {
  264. $this->src = $src;
  265. }
  266. /**
  267. * @since 1.17
  268. * @param string $src
  269. * @param bool $isTemp
  270. */
  271. public function setFileSrc( $src, $isTemp ) {
  272. $this->fileSrc = $src;
  273. $this->fileIsTemp = $isTemp;
  274. $this->isTemp = $isTemp;
  275. }
  276. /**
  277. * @since 1.17
  278. * @param string $sha1base36
  279. */
  280. public function setSha1Base36( $sha1base36 ) {
  281. $this->sha1base36 = $sha1base36;
  282. }
  283. /**
  284. * @since 1.34
  285. * @param string[] $tags
  286. */
  287. public function setTags( array $tags ) {
  288. $this->tags = $tags;
  289. }
  290. /**
  291. * @since 1.12.2
  292. * @param string $filename
  293. */
  294. public function setFilename( $filename ) {
  295. $this->filename = $filename;
  296. }
  297. /**
  298. * @since 1.17
  299. * @param string $archiveName
  300. */
  301. public function setArchiveName( $archiveName ) {
  302. $this->archiveName = $archiveName;
  303. }
  304. /**
  305. * @since 1.12.2
  306. * @param int $size
  307. */
  308. public function setSize( $size ) {
  309. $this->size = intval( $size );
  310. }
  311. /**
  312. * @since 1.12.2
  313. * @param string $type
  314. */
  315. public function setType( $type ) {
  316. $this->type = $type;
  317. }
  318. /**
  319. * @since 1.12.2
  320. * @param string $action
  321. */
  322. public function setAction( $action ) {
  323. $this->action = $action;
  324. }
  325. /**
  326. * @since 1.12.2
  327. * @param string $params
  328. */
  329. public function setParams( $params ) {
  330. $this->params = $params;
  331. }
  332. /**
  333. * @since 1.18
  334. * @param bool $noupdates
  335. */
  336. public function setNoUpdates( $noupdates ) {
  337. $this->mNoUpdates = $noupdates;
  338. }
  339. /**
  340. * @since 1.2
  341. * @return Title
  342. */
  343. public function getTitle() {
  344. return $this->title;
  345. }
  346. /**
  347. * @since 1.6.4
  348. * @return int
  349. */
  350. public function getID() {
  351. return $this->id;
  352. }
  353. /**
  354. * @since 1.2
  355. * @return string
  356. */
  357. public function getTimestamp() {
  358. return $this->timestamp;
  359. }
  360. /**
  361. * @since 1.2
  362. * @return string
  363. */
  364. public function getUser() {
  365. return $this->user_text;
  366. }
  367. /**
  368. * @since 1.27
  369. * @return User
  370. */
  371. public function getUserObj() {
  372. return $this->userObj;
  373. }
  374. /**
  375. * @since 1.2
  376. * @return string
  377. */
  378. public function getText() {
  379. return $this->text;
  380. }
  381. /**
  382. * @since 1.24
  383. * @return ContentHandler
  384. */
  385. public function getContentHandler() {
  386. if ( is_null( $this->contentHandler ) ) {
  387. $this->contentHandler = ContentHandler::getForModelID( $this->getModel() );
  388. }
  389. return $this->contentHandler;
  390. }
  391. /**
  392. * @since 1.21
  393. * @return Content
  394. */
  395. public function getContent() {
  396. if ( is_null( $this->content ) ) {
  397. $handler = $this->getContentHandler();
  398. $this->content = $handler->unserializeContent( $this->text, $this->getFormat() );
  399. }
  400. return $this->content;
  401. }
  402. /**
  403. * @since 1.21
  404. * @return string
  405. */
  406. public function getModel() {
  407. if ( is_null( $this->model ) ) {
  408. $this->model = $this->getTitle()->getContentModel();
  409. }
  410. return $this->model;
  411. }
  412. /**
  413. * @since 1.21
  414. * @return string
  415. */
  416. public function getFormat() {
  417. if ( is_null( $this->format ) ) {
  418. $this->format = $this->getContentHandler()->getDefaultFormat();
  419. }
  420. return $this->format;
  421. }
  422. /**
  423. * @since 1.2.6
  424. * @return string
  425. */
  426. public function getComment() {
  427. return $this->comment;
  428. }
  429. /**
  430. * @since 1.5.7
  431. * @return bool
  432. */
  433. public function getMinor() {
  434. return $this->minor;
  435. }
  436. /**
  437. * @since 1.12.2
  438. * @return string|null
  439. */
  440. public function getSrc() {
  441. return $this->src;
  442. }
  443. /**
  444. * @since 1.17
  445. * @return bool|string
  446. */
  447. public function getSha1() {
  448. if ( $this->sha1base36 ) {
  449. return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
  450. }
  451. return false;
  452. }
  453. /**
  454. * @since 1.31
  455. * @return bool|string
  456. */
  457. public function getSha1Base36() {
  458. if ( $this->sha1base36 ) {
  459. return $this->sha1base36;
  460. }
  461. return false;
  462. }
  463. /**
  464. * @since 1.34
  465. * @return string[]
  466. */
  467. public function getTags() {
  468. return $this->tags;
  469. }
  470. /**
  471. * @since 1.17
  472. * @return string
  473. */
  474. public function getFileSrc() {
  475. return $this->fileSrc;
  476. }
  477. /**
  478. * @since 1.17
  479. * @return bool
  480. */
  481. public function isTempSrc() {
  482. return $this->isTemp;
  483. }
  484. /**
  485. * @since 1.12.2
  486. * @return mixed
  487. */
  488. public function getFilename() {
  489. return $this->filename;
  490. }
  491. /**
  492. * @since 1.17
  493. * @return string
  494. */
  495. public function getArchiveName() {
  496. return $this->archiveName;
  497. }
  498. /**
  499. * @since 1.12.2
  500. * @return mixed
  501. */
  502. public function getSize() {
  503. return $this->size;
  504. }
  505. /**
  506. * @since 1.12.2
  507. * @return string
  508. */
  509. public function getType() {
  510. return $this->type;
  511. }
  512. /**
  513. * @since 1.12.2
  514. * @return string
  515. */
  516. public function getAction() {
  517. return $this->action;
  518. }
  519. /**
  520. * @since 1.12.2
  521. * @return string
  522. */
  523. public function getParams() {
  524. return $this->params;
  525. }
  526. /**
  527. * @since 1.4.1
  528. * @deprecated in 1.31. Use OldRevisionImporter::import
  529. * @return bool
  530. */
  531. public function importOldRevision() {
  532. if ( $this->mNoUpdates ) {
  533. $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporterNoUpdates();
  534. } else {
  535. $importer = MediaWikiServices::getInstance()->getWikiRevisionOldRevisionImporter();
  536. }
  537. return $importer->import( $this );
  538. }
  539. /**
  540. * @since 1.12.2
  541. * @return bool
  542. */
  543. public function importLogItem() {
  544. $dbw = wfGetDB( DB_MASTER );
  545. $user = $this->getUserObj() ?: User::newFromName( $this->getUser(), false );
  546. # @todo FIXME: This will not record autoblocks
  547. if ( !$this->getTitle() ) {
  548. wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
  549. $this->timestamp . "\n" );
  550. return false;
  551. }
  552. # Check if it exists already
  553. // @todo FIXME: Use original log ID (better for backups)
  554. $prior = $dbw->selectField( 'logging', '1',
  555. [ 'log_type' => $this->getType(),
  556. 'log_action' => $this->getAction(),
  557. 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
  558. 'log_namespace' => $this->getTitle()->getNamespace(),
  559. 'log_title' => $this->getTitle()->getDBkey(),
  560. 'log_params' => $this->params ],
  561. __METHOD__
  562. );
  563. // @todo FIXME: This could fail slightly for multiple matches :P
  564. if ( $prior ) {
  565. wfDebug( __METHOD__
  566. . ": skipping existing item for Log:{$this->type}/{$this->action}, timestamp "
  567. . $this->timestamp . "\n" );
  568. return false;
  569. }
  570. $data = [
  571. 'log_type' => $this->type,
  572. 'log_action' => $this->action,
  573. 'log_timestamp' => $dbw->timestamp( $this->timestamp ),
  574. 'log_namespace' => $this->getTitle()->getNamespace(),
  575. 'log_title' => $this->getTitle()->getDBkey(),
  576. 'log_params' => $this->params
  577. ] + CommentStore::getStore()->insert( $dbw, 'log_comment', $this->getComment() )
  578. + ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $user );
  579. $dbw->insert( 'logging', $data, __METHOD__ );
  580. return true;
  581. }
  582. /**
  583. * @since 1.12.2
  584. * @deprecated in 1.31. Use UploadRevisionImporter::import
  585. * @return bool
  586. */
  587. public function importUpload() {
  588. $importer = MediaWikiServices::getInstance()->getWikiRevisionUploadImporter();
  589. $statusValue = $importer->import( $this );
  590. return $statusValue->isGood();
  591. }
  592. /**
  593. * @since 1.12.2
  594. * @deprecated in 1.31. No replacement
  595. * @return bool|string
  596. */
  597. public function downloadSource() {
  598. $importer = new ImportableUploadRevisionImporter(
  599. $this->config->get( 'EnableUploads' ),
  600. LoggerFactory::getInstance( 'UploadRevisionImporter' )
  601. );
  602. return $importer->downloadSource( $this );
  603. }
  604. }