Dump7ZipOutput.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Sends dump output via the p7zip compressor.
  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 Dump7ZipOutput extends DumpPipeOutput {
  29. /**
  30. * @var int
  31. */
  32. protected $compressionLevel;
  33. /**
  34. * @param string $file
  35. * @param int $cmpLevel Compression level passed to 7za command's -mx
  36. */
  37. function __construct( $file, $cmpLevel = 4 ) {
  38. $this->compressionLevel = $cmpLevel;
  39. $command = $this->setup7zCommand( $file );
  40. parent::__construct( $command );
  41. $this->filename = $file;
  42. }
  43. /**
  44. * @param string $file
  45. * @return string
  46. */
  47. function setup7zCommand( $file ) {
  48. $command = "7za a -bd -si -mx=";
  49. $command .= wfEscapeShellArg( $this->compressionLevel ) . ' ';
  50. $command .= wfEscapeShellArg( $file );
  51. // Suppress annoying useless crap from p7zip
  52. // Unfortunately this could suppress real error messages too
  53. $command .= ' >' . wfGetNull() . ' 2>&1';
  54. return $command;
  55. }
  56. /**
  57. * @param string $newname
  58. * @param bool $open
  59. */
  60. function closeAndRename( $newname, $open = false ) {
  61. $newname = $this->checkRenameArgCount( $newname );
  62. if ( $newname ) {
  63. fclose( $this->handle );
  64. proc_close( $this->procOpenResource );
  65. $this->renameOrException( $newname );
  66. if ( $open ) {
  67. $command = $this->setup7zCommand( $this->filename );
  68. $this->startCommand( $command );
  69. }
  70. }
  71. }
  72. }