SiteExporter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Utility for exporting site entries to XML.
  4. * For the output file format, see docs/sitelist.txt and docs/sitelist-1.0.xsd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @since 1.25
  22. *
  23. * @file
  24. * @ingroup Site
  25. *
  26. * @license GPL-2.0-or-later
  27. * @author Daniel Kinzler
  28. */
  29. class SiteExporter {
  30. /**
  31. * @var resource
  32. */
  33. private $sink;
  34. /**
  35. * @param resource $sink A file handle open for writing
  36. */
  37. public function __construct( $sink ) {
  38. if ( !is_resource( $sink ) || get_resource_type( $sink ) !== 'stream' ) {
  39. throw new InvalidArgumentException( '$sink must be a file handle' );
  40. }
  41. $this->sink = $sink;
  42. }
  43. /**
  44. * Writes a <site> tag for each Site object in $sites, and encloses the entire list
  45. * between <sites> tags.
  46. *
  47. * @param Site[]|SiteList $sites
  48. */
  49. public function exportSites( $sites ) {
  50. $attributes = [
  51. 'version' => '1.0',
  52. 'xmlns' => 'http://www.mediawiki.org/xml/sitelist-1.0/',
  53. ];
  54. fwrite( $this->sink, Xml::openElement( 'sites', $attributes ) . "\n" );
  55. foreach ( $sites as $site ) {
  56. $this->exportSite( $site );
  57. }
  58. fwrite( $this->sink, Xml::closeElement( 'sites' ) . "\n" );
  59. fflush( $this->sink );
  60. }
  61. /**
  62. * Writes a <site> tag representing the given Site object.
  63. *
  64. * @param Site $site
  65. */
  66. private function exportSite( Site $site ) {
  67. if ( $site->getType() !== Site::TYPE_UNKNOWN ) {
  68. $siteAttr = [ 'type' => $site->getType() ];
  69. } else {
  70. $siteAttr = null;
  71. }
  72. fwrite( $this->sink, "\t" . Xml::openElement( 'site', $siteAttr ) . "\n" );
  73. fwrite( $this->sink, "\t\t" . Xml::element( 'globalid', null, $site->getGlobalId() ) . "\n" );
  74. if ( $site->getGroup() !== Site::GROUP_NONE ) {
  75. fwrite( $this->sink, "\t\t" . Xml::element( 'group', null, $site->getGroup() ) . "\n" );
  76. }
  77. if ( $site->getSource() !== Site::SOURCE_LOCAL ) {
  78. fwrite( $this->sink, "\t\t" . Xml::element( 'source', null, $site->getSource() ) . "\n" );
  79. }
  80. if ( $site->shouldForward() ) {
  81. fwrite( $this->sink, "\t\t" . Xml::element( 'forward', null, '' ) . "\n" );
  82. }
  83. foreach ( $site->getAllPaths() as $type => $path ) {
  84. fwrite( $this->sink, "\t\t" . Xml::element( 'path', [ 'type' => $type ], $path ) . "\n" );
  85. }
  86. foreach ( $site->getLocalIds() as $type => $ids ) {
  87. foreach ( $ids as $id ) {
  88. fwrite( $this->sink, "\t\t" . Xml::element( 'localid', [ 'type' => $type ], $id ) . "\n" );
  89. }
  90. }
  91. // @todo: export <data>
  92. // @todo: export <config>
  93. fwrite( $this->sink, "\t" . Xml::closeElement( 'site' ) . "\n" );
  94. }
  95. }