OSDN Git Service

encls.php 57696B69456E67696E6573.rel
[pukiwiki/pukiwiki_devel.git] / encls.php
1 #!/usr/local/bin/php
2 <?php
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 // $Id: encls.php,v 1.1 2006/05/14 09:24:22 henoheno Exp $
5 // Copyright (C) 2006 PukiWiki Developers Team
6 // License: GPL v2 or (at your option) any later version
7 //
8 // encoded-EUC-JP.txt -> EUC-JP -> UTF-8 -> encoded-UTF-8.txt
9
10 // PHP-cli only
11 if (php_sapi_name() != 'cli') die('Invalid SAPI');
12 if (! isset($argv)) die('PHP too old (Not 4.3.0 of above)');
13
14 $base = basename(array_shift($argv));
15 function usage(){
16         global $base;
17         echo 'Usage: ' . "\n";
18         echo '    ' . $base . ' [options] file [file ...]' . "\n";
19         echo '    ' . $base . ' [options] --all' . "\n";
20         echo '    Options:' . "\n";
21         echo '        --all            -- Check all of this directory' . "\n";
22         echo '        --suffix         -- Specify suffix (default: .txt)' . "\n";
23         echo '        --encoding_from  -- Specify encoding (default: EUC-JP)' . "\n";
24         echo '        --encoding_to    -- Specify encoding (default: UTF-8)' . "\n";
25         exit(1);
26 }
27
28 //////////////////////////////////
29 // Code from PukiWiki 1.4.7
30
31 // lib/func.php r1.72
32 // Encode page-name
33 function encode($key)
34 {
35         return ($key == '') ? '' : strtoupper(bin2hex($key));
36         // Equal to strtoupper(join('', unpack('H*0', $key)));
37         // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
38 }
39 // Decode page name
40 function decode($key)
41 {
42         return hex2bin($key);
43 }
44 // Inversion of bin2hex()
45 function hex2bin($hex_string)
46 {
47         // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
48         // (string)   : Always treat as string (not int etc). See BugTrack2/31
49         return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
50         pack('H*', (string)$hex_string) : $hex_string;
51 }
52 // Remove [[ ]] (brackets)
53 function strip_bracket($str)
54 {
55         $match = array();
56         if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
57                 return $match[1];
58         } else {
59                 return $str;
60         }
61 }
62 //////////////////////////////////
63 // lib/file.php r1.68 (modified)
64
65 // Get a page list of this wiki
66 function get_existpages($dir = '', $ext = '.txt')
67 {
68         $aryret = array();
69
70         $pattern = '((?:[0-9A-F]{2})+)';
71         if ($ext != '') $ext = preg_quote($ext, '/');
72         $pattern = '/^' . $pattern . $ext . '$/';
73
74         $dp = @opendir($dir) or
75                 die($dir . ' is not found or not readable.');
76         $matches = array();
77         while ($file = readdir($dp))
78                 if (preg_match($pattern, $file, $matches))
79                         $aryret[$file] = decode($matches[1]);
80         closedir($dp);
81
82         return $aryret;
83 }
84 //////////////////////////////////
85
86 if (empty($argv)) usage();
87
88 // Options
89 $f_all = FALSE;
90 $suffix = '.txt';
91 $encoding_from = 'EUC-JP';
92 $encoding_to   = 'UTF-8';
93 foreach ($argv as $key => $value) {
94         if ($value != '' && $value[0] != '-') break;
95         $optarg = '';
96         list($value, $optarg) = explode('=', $value, 2);
97         switch ($value) {
98                 case '--all'          : $f_all         = TRUE;    break;
99                 case '--suffix'       : $suffix        = $optarg; break;
100                 case '--encoding_from': $encoding_from = $optarg; break;
101                 case '--encoding_to'  : $encoding_to   = $optarg; break;
102                 case '--encoding'     : $encoding_to   = $optarg; break;
103         }
104         unset($argv[$key]);
105 }
106 define('SOURCE_ENCODING', $encoding_from);
107 define('TARGET_ENCODING', $encoding_to);
108
109 // Target
110 if ($f_all && empty($argv)) {
111         $argv = array_keys(get_existpages('.', $suffix));
112 } else {
113         foreach ($argv as $arg) {
114                 if (! file_exists($arg)) {
115                         echo 'File not found: ' . $arg . "\n";
116                         usage();
117                 }
118         }
119 }
120
121 // Do
122 mb_internal_encoding(SOURCE_ENCODING);
123 mb_detect_order('auto');
124 $matches = array();
125 foreach ($argv as $arg) {
126         if (preg_match('/^(.+)(\.[a-zA-Z0-9]+)$/', $arg, $matches)) {
127                 $name   = $matches[1];
128                 $suffix = $matches[2];
129         } else {
130                 $name   = $arg;
131                 $suffix = '';
132         }
133         //echo $name . $suffix . "\n";          // As-is
134         //echo decode($name) . $suffix . "\n";  // Decorded
135         echo encode(mb_convert_encoding(decode($name),
136                 TARGET_ENCODING, SOURCE_ENCODING)) .
137                 $suffix . "\n"; // Decord -> convert -> encode
138         //echo "\n";
139 }
140 ?>