DumpPipeOutput.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Stream outputter to send data to a file via some filter program.
  4. * Even if compression is available in a library, using a separate
  5. * program can allow us to make use of a multi-processor system.
  6. *
  7. * Copyright © 2003, 2005, 2006 Brion Vibber <brion@pobox.com>
  8. * https://www.mediawiki.org/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. * http://www.gnu.org/copyleft/gpl.html
  24. *
  25. * @file
  26. */
  27. /**
  28. * @ingroup Dump
  29. */
  30. class DumpPipeOutput extends DumpFileOutput {
  31. protected $command, $filename;
  32. protected $procOpenResource = false;
  33. /**
  34. * @param string $command
  35. * @param string $file
  36. */
  37. function __construct( $command, $file = null ) {
  38. if ( !is_null( $file ) ) {
  39. $command .= " > " . wfEscapeShellArg( $file );
  40. }
  41. $this->startCommand( $command );
  42. $this->command = $command;
  43. $this->filename = $file;
  44. }
  45. /**
  46. * @param string $string
  47. */
  48. function writeCloseStream( $string ) {
  49. parent::writeCloseStream( $string );
  50. if ( $this->procOpenResource ) {
  51. proc_close( $this->procOpenResource );
  52. $this->procOpenResource = false;
  53. }
  54. }
  55. /**
  56. * @param string $command
  57. */
  58. function startCommand( $command ) {
  59. $spec = [
  60. 0 => [ "pipe", "r" ],
  61. ];
  62. $pipes = [];
  63. $this->procOpenResource = proc_open( $command, $spec, $pipes );
  64. $this->handle = $pipes[0];
  65. }
  66. /**
  67. * @param string $newname
  68. */
  69. function closeRenameAndReopen( $newname ) {
  70. $this->closeAndRename( $newname, true );
  71. }
  72. /**
  73. * @param string $newname
  74. * @param bool $open
  75. */
  76. function closeAndRename( $newname, $open = false ) {
  77. $newname = $this->checkRenameArgCount( $newname );
  78. if ( $newname ) {
  79. if ( $this->handle ) {
  80. fclose( $this->handle );
  81. $this->handle = false;
  82. }
  83. if ( $this->procOpenResource ) {
  84. proc_close( $this->procOpenResource );
  85. $this->procOpenResource = false;
  86. }
  87. $this->renameOrException( $newname );
  88. if ( $open ) {
  89. $command = $this->command;
  90. $command .= " > " . wfEscapeShellArg( $this->filename );
  91. $this->startCommand( $command );
  92. }
  93. }
  94. }
  95. }