rename-config.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/php
  2. <?php
  3. chdir(dirname(__FILE__));
  4. require_once 'common.php';
  5. require_once '../library/HTMLPurifier.auto.php';
  6. assertCli();
  7. /**
  8. * @file
  9. * Renames a configuration directive. This involves renaming the file,
  10. * adding an alias, and then regenerating the cache. You still have to
  11. * manually go through and fix any calls to the directive.
  12. * @warning This script doesn't handle multi-stringhash files.
  13. */
  14. $argv = $_SERVER['argv'];
  15. if (count($argv) < 3) {
  16. echo "Usage: {$argv[0]} OldName NewName\n";
  17. exit(1);
  18. }
  19. chdir('../library/HTMLPurifier/ConfigSchema/schema');
  20. $old = $argv[1];
  21. $new = $argv[2];
  22. if (!file_exists("$old.txt")) {
  23. echo "Cannot move undefined configuration directive $old\n";
  24. exit(1);
  25. }
  26. if ($old === $new) {
  27. echo "Attempting to move to self, aborting\n";
  28. exit(1);
  29. }
  30. if (file_exists("$new.txt")) {
  31. echo "Cannot move to already defined directive $new\n";
  32. exit(1);
  33. }
  34. $file = "$old.txt";
  35. $builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder();
  36. $interchange = new HTMLPurifier_ConfigSchema_Interchange();
  37. $builder->buildFile($interchange, $file);
  38. $contents = file_get_contents($file);
  39. if (strpos($contents, "\r\n") !== false) {
  40. $nl = "\r\n";
  41. } elseif (strpos($contents, "\r") !== false) {
  42. $nl = "\r";
  43. } else {
  44. $nl = "\n";
  45. }
  46. // replace name with new name
  47. $contents = str_replace($old, $new, $contents);
  48. if ($interchange->directives[$old]->aliases) {
  49. $pos_alias = strpos($contents, 'ALIASES:');
  50. $pos_ins = strpos($contents, $nl, $pos_alias);
  51. if ($pos_ins === false) $pos_ins = strlen($contents);
  52. $contents =
  53. substr($contents, 0, $pos_ins) . ", $old" . substr($contents, $pos_ins);
  54. file_put_contents($file, $contents);
  55. } else {
  56. $lines = explode($nl, $contents);
  57. $insert = false;
  58. foreach ($lines as $n => $line) {
  59. if (strncmp($line, '--', 2) === 0) {
  60. $insert = $n;
  61. break;
  62. }
  63. }
  64. if (!$insert) {
  65. $lines[] = "ALIASES: $old";
  66. } else {
  67. array_splice($lines, $insert, 0, "ALIASES: $old");
  68. }
  69. file_put_contents($file, implode($nl, $lines));
  70. }
  71. rename("$old.txt", "$new.txt") || exit(1);