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
8 // encoded-EUC-JP.txt -> EUC-JP -> UTF-8 -> encoded-UTF-8.txt
11 if (php_sapi_name() != 'cli') die('Invalid SAPI');
12 if (! isset($argv)) die('PHP too old (Not 4.3.0 of above)');
14 $base = basename(array_shift($argv));
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";
28 //////////////////////////////////
29 // Code from PukiWiki 1.4.7
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 ...'
44 // Inversion of bin2hex()
45 function hex2bin($hex_string)
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;
52 // Remove [[ ]] (brackets)
53 function strip_bracket($str)
56 if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
62 //////////////////////////////////
63 // lib/file.php r1.68 (modified)
65 // Get a page list of this wiki
66 function get_existpages($dir = '', $ext = '.txt')
70 $pattern = '((?:[0-9A-F]{2})+)';
71 if ($ext != '') $ext = preg_quote($ext, '/');
72 $pattern = '/^' . $pattern . $ext . '$/';
74 $dp = @opendir($dir) or
75 die($dir . ' is not found or not readable.');
77 while ($file = readdir($dp))
78 if (preg_match($pattern, $file, $matches))
79 $aryret[$file] = decode($matches[1]);
84 //////////////////////////////////
86 if (empty($argv)) usage();
91 $encoding_from = 'EUC-JP';
92 $encoding_to = 'UTF-8';
93 foreach ($argv as $key => $value) {
94 if ($value != '' && $value[0] != '-') break;
96 list($value, $optarg) = explode('=', $value, 2);
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;
106 define('SOURCE_ENCODING', $encoding_from);
107 define('TARGET_ENCODING', $encoding_to);
110 if ($f_all && empty($argv)) {
111 $argv = array_keys(get_existpages('.', $suffix));
113 foreach ($argv as $arg) {
114 if (! file_exists($arg)) {
115 echo 'File not found: ' . $arg . "\n";
122 mb_internal_encoding(SOURCE_ENCODING);
123 mb_detect_order('auto');
125 foreach ($argv as $arg) {
126 if (preg_match('/^(.+)(\.[a-zA-Z0-9]+)$/', $arg, $matches)) {
128 $suffix = $matches[2];
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