1#!/usr/local/bin/php
2<?php
3
4chdir(dirname(__FILE__));
5require_once 'common.php';
6assertCli();
7
8/**
9 * @file
10 * Adds vimline to files
11 */
12
13chdir(dirname(__FILE__) . '/..');
14$FS = new FSTools();
15
16$vimline = 'vim: et sw=4 sts=4';
17
18$files = $FS->globr('.', '*');
19foreach ($files as $file) {
20    if (
21        !is_file($file) ||
22        prefix_is('./docs/doxygen', $file) ||
23        prefix_is('./library/standalone', $file) ||
24        prefix_is('./docs/specimens', $file) ||
25        postfix_is('.ser', $file) ||
26        postfix_is('.tgz', $file) ||
27        postfix_is('.patch', $file) ||
28        postfix_is('.dtd', $file) ||
29        postfix_is('.ent', $file) ||
30        postfix_is('.png', $file) ||
31        postfix_is('.ico', $file) ||
32        // wontfix
33        postfix_is('.vtest', $file) ||
34        postfix_is('.svg', $file) ||
35        postfix_is('.phpt', $file) ||
36        postfix_is('VERSION', $file) ||
37        postfix_is('WHATSNEW', $file) ||
38        postfix_is('configdoc/usage.xml', $file) ||
39        postfix_is('library/HTMLPurifier.includes.php', $file) ||
40        postfix_is('library/HTMLPurifier.safe-includes.php', $file) ||
41        postfix_is('smoketests/xssAttacks.xml', $file) ||
42        // phpt files
43        postfix_is('.diff', $file) ||
44        postfix_is('.exp', $file) ||
45        postfix_is('.log', $file) ||
46        postfix_is('.out', $file) ||
47
48        $file == './library/HTMLPurifier/Lexer/PH5P.php' ||
49        $file == './maintenance/PH5P.php'
50    ) continue;
51    $ext = strrchr($file, '.');
52    if (
53        postfix_is('README', $file) ||
54        postfix_is('LICENSE', $file) ||
55        postfix_is('CREDITS', $file) ||
56        postfix_is('INSTALL', $file) ||
57        postfix_is('NEWS', $file) ||
58        postfix_is('TODO', $file) ||
59        postfix_is('WYSIWYG', $file) ||
60        postfix_is('Changelog', $file)
61    ) $ext = '.txt';
62    if (postfix_is('Doxyfile', $file)) $ext = 'Doxyfile';
63    if (postfix_is('.php.in', $file)) $ext = '.php';
64    $no_nl = false;
65    switch ($ext) {
66        case '.php':
67        case '.inc':
68        case '.js':
69            $line = '// %s';
70            break;
71        case '.html':
72        case '.xsl':
73        case '.xml':
74        case '.htc':
75            $line = "<!-- %s\n-->";
76            break;
77        case '.htmlt':
78            $no_nl = true;
79            $line = '--# %s';
80            break;
81        case '.ini':
82            $line = '; %s';
83            break;
84        case '.css':
85            $line = '/* %s */';
86            break;
87        case '.bat':
88            $line = 'rem %s';
89            break;
90        case '.txt':
91        case '.utf8':
92            if (
93                prefix_is('./library/HTMLPurifier/ConfigSchema', $file) ||
94                prefix_is('./smoketests/test-schema', $file) ||
95                prefix_is('./tests/HTMLPurifier/StringHashParser', $file)
96            ) {
97                $no_nl = true;
98                $line = '--# %s';
99            } else {
100                $line = '    %s';
101            }
102            break;
103        case 'Doxyfile':
104            $line = '# %s';
105            break;
106        default:
107            throw new Exception('Unknown file: ' . $file);
108    }
109
110    echo "$file\n";
111    $contents = file_get_contents($file);
112
113    $regex = '~' . str_replace('%s', 'vim: .+', preg_quote($line, '~')) .  '~m';
114    $contents = preg_replace($regex, '', $contents);
115
116    $contents = rtrim($contents);
117
118    if (strpos($contents, "\r\n") !== false) $nl = "\r\n";
119    elseif (strpos($contents, "\n") !== false) $nl = "\n";
120    elseif (strpos($contents, "\r") !== false) $nl = "\r";
121    else $nl = PHP_EOL;
122
123    if (!$no_nl) $contents .= $nl;
124    $contents .= $nl . str_replace('%s', $vimline, $line) . $nl;
125
126    file_put_contents($file, $contents);
127
128}
129
130// vim: et sw=4 sts=4
131