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