serialize.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. if ( !defined( 'MEDIAWIKI' ) ) {
  3. $wgNoDBParam = true;
  4. $optionsWithArgs = array( 'o' );
  5. require_once( dirname(__FILE__).'/../maintenance/commandLine.inc' );
  6. $stderr = fopen( 'php://stderr', 'w' );
  7. if ( !isset( $args[0] ) ) {
  8. fwrite( $stderr, "No input file specified\n" );
  9. exit( 1 );
  10. }
  11. if ( wfIsWindows() ) {
  12. $files = array();
  13. foreach ( $args as $arg ) {
  14. $files = array_merge( $files, glob( $arg ) );
  15. }
  16. if ( !$files ) {
  17. fwrite( $stderr, "No files found\n" );
  18. }
  19. } else {
  20. $files = $args;
  21. }
  22. if ( isset( $options['o'] ) ) {
  23. $out = fopen( $options['o'], 'wb' );
  24. if ( !$out ) {
  25. fwrite( $stderr, "Unable to open file \"{$options['o']}\" for output\n" );
  26. exit( 1 );
  27. }
  28. } else {
  29. $out = fopen( 'php://stdout', 'wb' );
  30. }
  31. $vars = array();
  32. foreach ( $files as $inputFile ) {
  33. $vars = array_merge( $vars, getVars( $inputFile ) );
  34. }
  35. fwrite( $out, serialize( $vars ) );
  36. fclose( $out );
  37. exit( 0 );
  38. }
  39. //----------------------------------------------------------------------------
  40. function getVars( $_gv_filename ) {
  41. require( $_gv_filename );
  42. $vars = get_defined_vars();
  43. unset( $vars['_gv_filename'] );
  44. # Clean up line endings
  45. if ( wfIsWindows() ) {
  46. $vars = unixLineEndings( $vars );
  47. }
  48. return $vars;
  49. }
  50. function unixLineEndings( $var ) {
  51. static $recursionLevel = 0;
  52. if ( $recursionLevel > 50 ) {
  53. global $stderr;
  54. fwrite( $stderr, "Error: Recursion limit exceeded. Possible circular reference in array variable.\n" );
  55. exit( 2 );
  56. }
  57. if ( is_array( $var ) ) {
  58. ++$recursionLevel;
  59. $var = array_map( 'unixLineEndings', $var );
  60. --$recursionLevel;
  61. } elseif ( is_string( $var ) ) {
  62. $var = str_replace( "\r\n", "\n", $var );
  63. }
  64. return $var;
  65. }