PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/html/modules/eguide/duplicate.php

http://xoopscube-modules.googlecode.com/
PHP | 111 lines | 90 code | 14 blank | 7 comment | 29 complexity | ab20c40b081f1227d39631bed8800af7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. #!/usr/bin/php -q
  2. <?php
  3. // only command line execute this script
  4. if (isset($_SERVER['HTTP_HOST'])) exit;
  5. define('ORIGIN_NAME', 'eguide');
  6. $prog = array_shift($argv);
  7. if (count($argv)<1) {
  8. echo "usage: $prog [-v] dirname ...\n";
  9. echo " dirname: /^[a-zA-Z0-9_]+\$/\n";
  10. exit;
  11. }
  12. # for duplicatable (not D3, old style)
  13. $mydirpath = dirname(__FILE__);
  14. $myprefix = $mydirname = basename($mydirpath);
  15. chdir($mydirpath);
  16. // make target file lists
  17. // force writable permission files
  18. $writable = array();
  19. // rewrite prefix files
  20. $modifies = array();
  21. foreach (find_file('templates', ORIGIN_NAME.'_*.(html|xml)') as $sfile) {
  22. $modifies[$sfile] = preg_replace('/\/'.preg_quote(ORIGIN_NAME).'_/', '/{prefix}_', $sfile);
  23. }
  24. $modifies['sql/mysql.sql'] = 'sql/mysql_{prefix}.sql';
  25. function duplicate($dir, $writable, $files, $verb=false) {
  26. $base = '../'.$dir;
  27. $id = preg_replace('/^\D+/', '', $dir);
  28. if (!empty($id)) $id = intval($id);
  29. if (!is_dir($base)) mkdir($base);
  30. $base .= '/';
  31. foreach (find_file() as $name) {
  32. if ($verb) echo " dup: $name\n";
  33. if (is_dir($name)) {
  34. if (!@mkdir($base.$name)) echo "mkdir error: $name\n";
  35. } elseif (!@link($name, $base.$name)) echo "link error: $name\n";
  36. }
  37. foreach ($writable as $i) {
  38. $f = $base.$i;
  39. if ($verb) echo " writable: $f\n";
  40. if (is_dir($f)) chmod($f, 0777);
  41. else chmod($f, 0666);
  42. }
  43. foreach ($files as $file=>$dest) {
  44. $newfile = $base.preg_replace('/{prefix}/', $dir, $dest);
  45. $body = file_get_contents($file);
  46. if (preg_match('/\.sql$/', $file)) {
  47. if ($verb) echo " $file -> $newfile\n";
  48. $body = preg_replace('/\\s'.preg_quote(ORIGIN_NAME).'([\s_])/', " $dir$1", $body);
  49. } elseif (preg_match('/\.html$/', $file)) {
  50. $body = preg_replace('/'.preg_quote(ORIGIN_NAME).'_/', $dir."_", $body);
  51. if ($verb) echo " $file -> $newfile\n";
  52. }
  53. file_put_contents($newfile, $body);
  54. }
  55. }
  56. function find_file($dir='.', $pat='') {
  57. $files = array();
  58. $dh = opendir($dir);
  59. $reg = preg_replace(array('/\./', '/\*/'), array('\.', '.*'), $pat);
  60. while ($file = readdir($dh)) {
  61. if ($file=='.' || $file=='..') continue;
  62. if (preg_match('/~$/', $file)) continue;
  63. $path = preg_replace('/\.\//', '', "$dir/$file");
  64. if (is_dir($path)) {
  65. if ($file == "CVS") continue;
  66. if (!$pat) $files[] = $path;
  67. $files = array_merge($files, find_file($path, $pat));
  68. } else {
  69. if ($pat && !preg_match("/^$reg$/", $file)) continue;
  70. $files[] = $path;
  71. }
  72. }
  73. closedir($dh);
  74. return $files;
  75. }
  76. if (!function_exists('file_put_contents')) {
  77. // php 4.3.11 no have?
  78. function file_put_contents($file, $text) {
  79. $fp = fopen($file, "w");
  80. fwrite($fp, $text);
  81. fclose($fp);
  82. }
  83. }
  84. $verb = false;
  85. if ($argv[0] == '-v') {
  86. array_shift($argv);
  87. $verb = true;
  88. }
  89. foreach ($argv as $dir) {
  90. if (preg_match('/^[a-zA-Z0-9_]+$/', $dir)) {
  91. echo "Duplicate: $dir\n";
  92. duplicate($dir, $writable, $modifies, $verb);
  93. } else {
  94. echo "Error dirname: $dir\n";
  95. }
  96. }
  97. ?>