Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / ezyang / htmlpurifier / maintenance / rename-config.php
1 #!/usr/bin/php
2 <?php
3
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 require_once '../library/HTMLPurifier.auto.php';
7 assertCli();
8
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.
15  */
16
17 $argv = $_SERVER['argv'];
18 if (count($argv) < 3) {
19     echo "Usage: {$argv[0]} OldName NewName\n";
20     exit(1);
21 }
22
23 chdir('../library/HTMLPurifier/ConfigSchema/schema');
24
25 $old = $argv[1];
26 $new = $argv[2];
27
28 if (!file_exists("$old.txt")) {
29     echo "Cannot move undefined configuration directive $old\n";
30     exit(1);
31 }
32
33 if ($old === $new) {
34     echo "Attempting to move to self, aborting\n";
35     exit(1);
36 }
37
38 if (file_exists("$new.txt")) {
39     echo "Cannot move to already defined directive $new\n";
40     exit(1);
41 }
42
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);
48
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";
55 }
56
57 // replace name with new name
58 $contents = str_replace($old, $new, $contents);
59
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;
74         }
75     }
76     if (!$insert) {
77         $lines[] = "ALIASES: $old";
78     } else {
79         array_splice($lines, $insert, 0, "ALIASES: $old");
80     }
81     file_put_contents($file, implode($nl, $lines));
82 }
83
84 rename("$old.txt", "$new.txt") || exit(1);