7zip.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 7z stream wrapper
  4. *
  5. * Copyright © 2005 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. * @ingroup Maintenance
  25. */
  26. /**
  27. * Stream wrapper around 7za filter program.
  28. * Required since we can't pass an open file resource to XMLReader->open()
  29. * which is used for the text prefetch.
  30. *
  31. * @ingroup Maintenance
  32. */
  33. class SevenZipStream {
  34. protected $stream;
  35. private function stripPath( $path ) {
  36. $prefix = 'mediawiki.compress.7z://';
  37. return substr( $path, strlen( $prefix ) );
  38. }
  39. function stream_open( $path, $mode, $options, &$opened_path ) {
  40. if ( $mode[0] == 'r' ) {
  41. $options = 'e -bd -so';
  42. } elseif ( $mode[0] == 'w' ) {
  43. $options = 'a -bd -si';
  44. } else {
  45. return false;
  46. }
  47. $arg = wfEscapeShellArg( $this->stripPath( $path ) );
  48. $command = "7za $options $arg";
  49. if ( !wfIsWindows() ) {
  50. // Suppress the stupid messages on stderr
  51. $command .= ' 2>/dev/null';
  52. }
  53. $this->stream = popen( $command, $mode[0] ); // popen() doesn't like two-letter modes
  54. return ( $this->stream !== false );
  55. }
  56. function url_stat( $path, $flags ) {
  57. return stat( $this->stripPath( $path ) );
  58. }
  59. // This is all so lame; there should be a default class we can extend
  60. function stream_close() {
  61. return fclose( $this->stream );
  62. }
  63. function stream_flush() {
  64. return fflush( $this->stream );
  65. }
  66. function stream_read( $count ) {
  67. return fread( $this->stream, $count );
  68. }
  69. function stream_write( $data ) {
  70. return fwrite( $this->stream, $data );
  71. }
  72. function stream_tell() {
  73. return ftell( $this->stream );
  74. }
  75. function stream_eof() {
  76. return feof( $this->stream );
  77. }
  78. function stream_seek( $offset, $whence ) {
  79. return fseek( $this->stream, $offset, $whence );
  80. }
  81. }
  82. stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' );