DumpFileOutput.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Stream outputter to send data to a file.
  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 DumpFileOutput extends DumpOutput {
  29. protected $handle = false, $filename;
  30. /**
  31. * @param string $file
  32. */
  33. function __construct( $file ) {
  34. $this->handle = fopen( $file, "wt" );
  35. $this->filename = $file;
  36. }
  37. /**
  38. * @param string $string
  39. */
  40. function writeCloseStream( $string ) {
  41. parent::writeCloseStream( $string );
  42. if ( $this->handle ) {
  43. fclose( $this->handle );
  44. $this->handle = false;
  45. }
  46. }
  47. /**
  48. * @param string $string
  49. */
  50. function write( $string ) {
  51. fputs( $this->handle, $string );
  52. }
  53. /**
  54. * @param string $newname
  55. */
  56. function closeRenameAndReopen( $newname ) {
  57. $this->closeAndRename( $newname, true );
  58. }
  59. /**
  60. * @param string $newname
  61. * @throws MWException
  62. */
  63. function renameOrException( $newname ) {
  64. if ( !rename( $this->filename, $newname ) ) {
  65. throw new MWException( __METHOD__ . ": rename of file {$this->filename} to $newname failed\n" );
  66. }
  67. }
  68. /**
  69. * @param array $newname
  70. * @return string
  71. * @throws MWException
  72. */
  73. function checkRenameArgCount( $newname ) {
  74. if ( is_array( $newname ) ) {
  75. if ( count( $newname ) > 1 ) {
  76. throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
  77. } else {
  78. $newname = $newname[0];
  79. }
  80. }
  81. return $newname;
  82. }
  83. /**
  84. * @param string $newname
  85. * @param bool $open
  86. */
  87. function closeAndRename( $newname, $open = false ) {
  88. $newname = $this->checkRenameArgCount( $newname );
  89. if ( $newname ) {
  90. if ( $this->handle ) {
  91. fclose( $this->handle );
  92. $this->handle = false;
  93. }
  94. $this->renameOrException( $newname );
  95. if ( $open ) {
  96. $this->handle = fopen( $this->filename, "wt" );
  97. }
  98. }
  99. }
  100. /**
  101. * @return string|null
  102. */
  103. function getFilenames() {
  104. return $this->filename;
  105. }
  106. }