XmlDumpWriter.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * XmlDumpWriter
  4. *
  5. * Copyright © 2003, 2005, 2006 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. */
  25. /**
  26. * @ingroup Dump
  27. */
  28. class XmlDumpWriter {
  29. /**
  30. * Opens the XML output stream's root "<mediawiki>" element.
  31. * This does not include an xml directive, so is safe to include
  32. * as a subelement in a larger XML stream. Namespace and XML Schema
  33. * references are included.
  34. *
  35. * Output will be encoded in UTF-8.
  36. *
  37. * @return string
  38. */
  39. function openStream() {
  40. global $wgContLang;
  41. $ver = WikiExporter::schemaVersion();
  42. return Xml::element( 'mediawiki', [
  43. 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
  44. 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
  45. /*
  46. * When a new version of the schema is created, it needs staging on mediawiki.org.
  47. * This requires a change in the operations/mediawiki-config git repo.
  48. *
  49. * Create a changeset like https://gerrit.wikimedia.org/r/#/c/149643/ in which
  50. * you copy in the new xsd file.
  51. *
  52. * After it is reviewed, merged and deployed (sync-docroot), the index.html needs purging.
  53. * echo "https://www.mediawiki.org/xml/index.html" | mwscript purgeList.php --wiki=aawiki
  54. */
  55. 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
  56. "http://www.mediawiki.org/xml/export-$ver.xsd",
  57. 'version' => $ver,
  58. 'xml:lang' => $wgContLang->getHtmlCode() ],
  59. null ) .
  60. "\n" .
  61. $this->siteInfo();
  62. }
  63. /**
  64. * @return string
  65. */
  66. function siteInfo() {
  67. $info = [
  68. $this->sitename(),
  69. $this->dbname(),
  70. $this->homelink(),
  71. $this->generator(),
  72. $this->caseSetting(),
  73. $this->namespaces() ];
  74. return " <siteinfo>\n " .
  75. implode( "\n ", $info ) .
  76. "\n </siteinfo>\n";
  77. }
  78. /**
  79. * @return string
  80. */
  81. function sitename() {
  82. global $wgSitename;
  83. return Xml::element( 'sitename', [], $wgSitename );
  84. }
  85. /**
  86. * @return string
  87. */
  88. function dbname() {
  89. global $wgDBname;
  90. return Xml::element( 'dbname', [], $wgDBname );
  91. }
  92. /**
  93. * @return string
  94. */
  95. function generator() {
  96. global $wgVersion;
  97. return Xml::element( 'generator', [], "MediaWiki $wgVersion" );
  98. }
  99. /**
  100. * @return string
  101. */
  102. function homelink() {
  103. return Xml::element( 'base', [], Title::newMainPage()->getCanonicalURL() );
  104. }
  105. /**
  106. * @return string
  107. */
  108. function caseSetting() {
  109. global $wgCapitalLinks;
  110. // "case-insensitive" option is reserved for future
  111. $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
  112. return Xml::element( 'case', [], $sensitivity );
  113. }
  114. /**
  115. * @return string
  116. */
  117. function namespaces() {
  118. global $wgContLang;
  119. $spaces = "<namespaces>\n";
  120. foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
  121. $spaces .= ' ' .
  122. Xml::element( 'namespace',
  123. [
  124. 'key' => $ns,
  125. 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
  126. ], $title ) . "\n";
  127. }
  128. $spaces .= " </namespaces>";
  129. return $spaces;
  130. }
  131. /**
  132. * Closes the output stream with the closing root element.
  133. * Call when finished dumping things.
  134. *
  135. * @return string
  136. */
  137. function closeStream() {
  138. return "</mediawiki>\n";
  139. }
  140. /**
  141. * Opens a "<page>" section on the output stream, with data
  142. * from the given database row.
  143. *
  144. * @param object $row
  145. * @return string
  146. */
  147. public function openPage( $row ) {
  148. $out = " <page>\n";
  149. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  150. $out .= ' ' . Xml::elementClean( 'title', [], self::canonicalTitle( $title ) ) . "\n";
  151. $out .= ' ' . Xml::element( 'ns', [], strval( $row->page_namespace ) ) . "\n";
  152. $out .= ' ' . Xml::element( 'id', [], strval( $row->page_id ) ) . "\n";
  153. if ( $row->page_is_redirect ) {
  154. $page = WikiPage::factory( $title );
  155. $redirect = $page->getRedirectTarget();
  156. if ( $redirect instanceof Title && $redirect->isValidRedirectTarget() ) {
  157. $out .= ' ';
  158. $out .= Xml::element( 'redirect', [ 'title' => self::canonicalTitle( $redirect ) ] );
  159. $out .= "\n";
  160. }
  161. }
  162. if ( $row->page_restrictions != '' ) {
  163. $out .= ' ' . Xml::element( 'restrictions', [],
  164. strval( $row->page_restrictions ) ) . "\n";
  165. }
  166. Hooks::run( 'XmlDumpWriterOpenPage', [ $this, &$out, $row, $title ] );
  167. return $out;
  168. }
  169. /**
  170. * Closes a "<page>" section on the output stream.
  171. *
  172. * @access private
  173. * @return string
  174. */
  175. function closePage() {
  176. return " </page>\n";
  177. }
  178. /**
  179. * Dumps a "<revision>" section on the output stream, with
  180. * data filled in from the given database row.
  181. *
  182. * @param object $row
  183. * @return string
  184. * @access private
  185. */
  186. function writeRevision( $row ) {
  187. $out = " <revision>\n";
  188. $out .= " " . Xml::element( 'id', null, strval( $row->rev_id ) ) . "\n";
  189. if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
  190. $out .= " " . Xml::element( 'parentid', null, strval( $row->rev_parent_id ) ) . "\n";
  191. }
  192. $out .= $this->writeTimestamp( $row->rev_timestamp );
  193. if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_USER ) ) {
  194. $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
  195. } else {
  196. $out .= $this->writeContributor( $row->rev_user, $row->rev_user_text );
  197. }
  198. if ( isset( $row->rev_minor_edit ) && $row->rev_minor_edit ) {
  199. $out .= " <minor/>\n";
  200. }
  201. if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_COMMENT ) ) {
  202. $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
  203. } else {
  204. $comment = CommentStore::getStore()->getComment( 'rev_comment', $row )->text;
  205. if ( $comment != '' ) {
  206. $out .= " " . Xml::elementClean( 'comment', [], strval( $comment ) ) . "\n";
  207. }
  208. }
  209. if ( isset( $row->rev_content_model ) && !is_null( $row->rev_content_model ) ) {
  210. $content_model = strval( $row->rev_content_model );
  211. } else {
  212. // probably using $wgContentHandlerUseDB = false;
  213. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  214. $content_model = ContentHandler::getDefaultModelFor( $title );
  215. }
  216. $content_handler = ContentHandler::getForModelID( $content_model );
  217. if ( isset( $row->rev_content_format ) && !is_null( $row->rev_content_format ) ) {
  218. $content_format = strval( $row->rev_content_format );
  219. } else {
  220. // probably using $wgContentHandlerUseDB = false;
  221. $content_format = $content_handler->getDefaultFormat();
  222. }
  223. $out .= " " . Xml::element( 'model', null, strval( $content_model ) ) . "\n";
  224. $out .= " " . Xml::element( 'format', null, strval( $content_format ) ) . "\n";
  225. $text = '';
  226. if ( isset( $row->rev_deleted ) && ( $row->rev_deleted & Revision::DELETED_TEXT ) ) {
  227. $out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
  228. } elseif ( isset( $row->old_text ) ) {
  229. // Raw text from the database may have invalid chars
  230. $text = strval( Revision::getRevisionText( $row ) );
  231. $text = $content_handler->exportTransform( $text, $content_format );
  232. $out .= " " . Xml::elementClean( 'text',
  233. [ 'xml:space' => 'preserve', 'bytes' => intval( $row->rev_len ) ],
  234. strval( $text ) ) . "\n";
  235. } else {
  236. // Stub output
  237. $out .= " " . Xml::element( 'text',
  238. [ 'id' => $row->rev_text_id, 'bytes' => intval( $row->rev_len ) ],
  239. "" ) . "\n";
  240. }
  241. if ( isset( $row->rev_sha1 )
  242. && $row->rev_sha1
  243. && !( $row->rev_deleted & Revision::DELETED_TEXT )
  244. ) {
  245. $out .= " " . Xml::element( 'sha1', null, strval( $row->rev_sha1 ) ) . "\n";
  246. } else {
  247. $out .= " <sha1/>\n";
  248. }
  249. // Avoid PHP 7.1 warning from passing $this by reference
  250. $writer = $this;
  251. Hooks::run( 'XmlDumpWriterWriteRevision', [ &$writer, &$out, $row, $text ] );
  252. $out .= " </revision>\n";
  253. return $out;
  254. }
  255. /**
  256. * Dumps a "<logitem>" section on the output stream, with
  257. * data filled in from the given database row.
  258. *
  259. * @param object $row
  260. * @return string
  261. * @access private
  262. */
  263. function writeLogItem( $row ) {
  264. $out = " <logitem>\n";
  265. $out .= " " . Xml::element( 'id', null, strval( $row->log_id ) ) . "\n";
  266. $out .= $this->writeTimestamp( $row->log_timestamp, " " );
  267. if ( $row->log_deleted & LogPage::DELETED_USER ) {
  268. $out .= " " . Xml::element( 'contributor', [ 'deleted' => 'deleted' ] ) . "\n";
  269. } else {
  270. $out .= $this->writeContributor( $row->log_user, $row->user_name, " " );
  271. }
  272. if ( $row->log_deleted & LogPage::DELETED_COMMENT ) {
  273. $out .= " " . Xml::element( 'comment', [ 'deleted' => 'deleted' ] ) . "\n";
  274. } else {
  275. $comment = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
  276. if ( $comment != '' ) {
  277. $out .= " " . Xml::elementClean( 'comment', null, strval( $comment ) ) . "\n";
  278. }
  279. }
  280. $out .= " " . Xml::element( 'type', null, strval( $row->log_type ) ) . "\n";
  281. $out .= " " . Xml::element( 'action', null, strval( $row->log_action ) ) . "\n";
  282. if ( $row->log_deleted & LogPage::DELETED_ACTION ) {
  283. $out .= " " . Xml::element( 'text', [ 'deleted' => 'deleted' ] ) . "\n";
  284. } else {
  285. $title = Title::makeTitle( $row->log_namespace, $row->log_title );
  286. $out .= " " . Xml::elementClean( 'logtitle', null, self::canonicalTitle( $title ) ) . "\n";
  287. $out .= " " . Xml::elementClean( 'params',
  288. [ 'xml:space' => 'preserve' ],
  289. strval( $row->log_params ) ) . "\n";
  290. }
  291. $out .= " </logitem>\n";
  292. return $out;
  293. }
  294. /**
  295. * @param string $timestamp
  296. * @param string $indent Default to six spaces
  297. * @return string
  298. */
  299. function writeTimestamp( $timestamp, $indent = " " ) {
  300. $ts = wfTimestamp( TS_ISO_8601, $timestamp );
  301. return $indent . Xml::element( 'timestamp', null, $ts ) . "\n";
  302. }
  303. /**
  304. * @param int $id
  305. * @param string $text
  306. * @param string $indent Default to six spaces
  307. * @return string
  308. */
  309. function writeContributor( $id, $text, $indent = " " ) {
  310. $out = $indent . "<contributor>\n";
  311. if ( $id || !IP::isValid( $text ) ) {
  312. $out .= $indent . " " . Xml::elementClean( 'username', null, strval( $text ) ) . "\n";
  313. $out .= $indent . " " . Xml::element( 'id', null, strval( $id ) ) . "\n";
  314. } else {
  315. $out .= $indent . " " . Xml::elementClean( 'ip', null, strval( $text ) ) . "\n";
  316. }
  317. $out .= $indent . "</contributor>\n";
  318. return $out;
  319. }
  320. /**
  321. * Warning! This data is potentially inconsistent. :(
  322. * @param object $row
  323. * @param bool $dumpContents
  324. * @return string
  325. */
  326. function writeUploads( $row, $dumpContents = false ) {
  327. if ( $row->page_namespace == NS_FILE ) {
  328. $img = wfLocalFile( $row->page_title );
  329. if ( $img && $img->exists() ) {
  330. $out = '';
  331. foreach ( array_reverse( $img->getHistory() ) as $ver ) {
  332. $out .= $this->writeUpload( $ver, $dumpContents );
  333. }
  334. $out .= $this->writeUpload( $img, $dumpContents );
  335. return $out;
  336. }
  337. }
  338. return '';
  339. }
  340. /**
  341. * @param File $file
  342. * @param bool $dumpContents
  343. * @return string
  344. */
  345. function writeUpload( $file, $dumpContents = false ) {
  346. if ( $file->isOld() ) {
  347. $archiveName = " " .
  348. Xml::element( 'archivename', null, $file->getArchiveName() ) . "\n";
  349. } else {
  350. $archiveName = '';
  351. }
  352. if ( $dumpContents ) {
  353. $be = $file->getRepo()->getBackend();
  354. # Dump file as base64
  355. # Uses only XML-safe characters, so does not need escaping
  356. # @todo Too bad this loads the contents into memory (script might swap)
  357. $contents = ' <contents encoding="base64">' .
  358. chunk_split( base64_encode(
  359. $be->getFileContents( [ 'src' => $file->getPath() ] ) ) ) .
  360. " </contents>\n";
  361. } else {
  362. $contents = '';
  363. }
  364. if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
  365. $comment = Xml::element( 'comment', [ 'deleted' => 'deleted' ] );
  366. } else {
  367. $comment = Xml::elementClean( 'comment', null, strval( $file->getDescription() ) );
  368. }
  369. return " <upload>\n" .
  370. $this->writeTimestamp( $file->getTimestamp() ) .
  371. $this->writeContributor( $file->getUser( 'id' ), $file->getUser( 'text' ) ) .
  372. " " . $comment . "\n" .
  373. " " . Xml::element( 'filename', null, $file->getName() ) . "\n" .
  374. $archiveName .
  375. " " . Xml::element( 'src', null, $file->getCanonicalUrl() ) . "\n" .
  376. " " . Xml::element( 'size', null, $file->getSize() ) . "\n" .
  377. " " . Xml::element( 'sha1base36', null, $file->getSha1() ) . "\n" .
  378. " " . Xml::element( 'rel', null, $file->getRel() ) . "\n" .
  379. $contents .
  380. " </upload>\n";
  381. }
  382. /**
  383. * Return prefixed text form of title, but using the content language's
  384. * canonical namespace. This skips any special-casing such as gendered
  385. * user namespaces -- which while useful, are not yet listed in the
  386. * XML "<siteinfo>" data so are unsafe in export.
  387. *
  388. * @param Title $title
  389. * @return string
  390. * @since 1.18
  391. */
  392. public static function canonicalTitle( Title $title ) {
  393. if ( $title->isExternal() ) {
  394. return $title->getPrefixedText();
  395. }
  396. global $wgContLang;
  397. $prefix = $wgContLang->getFormattedNsText( $title->getNamespace() );
  398. // @todo Emit some kind of warning to the user if $title->getNamespace() !==
  399. // NS_MAIN and $prefix === '' (viz. pages in an unregistered namespace)
  400. if ( $prefix !== '' ) {
  401. $prefix .= ':';
  402. }
  403. return $prefix . $title->getText();
  404. }
  405. }