3 // PukiWiki - Yet another WikiWikiWeb clone.
4 // $Id: encls.php,v 1.4 2009/03/22 15:38:02 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 error_reporting(0); // Nothing
12 //error_reporting(E_ERROR | E_PARSE); // Avoid E_WARNING, E_NOTICE, etc
13 //error_reporting(E_ALL); // Debug purpose
16 if (php_sapi_name() != 'cli') die('Invalid SAPI');
17 if (! isset($argv)) die('PHP too old (Not 4.3.0 of above)');
19 //////////////////////////////////
21 $base = basename(array_shift($argv));
24 echo 'Usage: ' . "\n";
25 echo ' ' . $base . ' [options] file [file ...]' . "\n";
26 echo ' ' . $base . ' [options] --all' . "\n";
27 echo ' Options:' . "\n";
28 echo ' --all -- Check all of this directory' . "\n";
29 echo ' --suffix -- Specify suffix (default: .txt)' . "\n";
30 echo ' --encoding_from -- Specify encoding (default: EUC-JP)' . "\n";
31 echo ' --encoding_to -- Specify encoding (default: UTF-8)' . "\n";
32 echo ' --nocheck -- Suppress check if the argument is exists as a file' . "\n";
33 echo ' --decode -- Just decode() it' . "\n";
37 //////////////////////////////////
38 // Code from PukiWiki 1.4.7
44 return ($key == '') ? '' : strtoupper(bin2hex($key));
45 // Equal to strtoupper(join('', unpack('H*0', $key)));
46 // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
53 // Inversion of bin2hex()
54 function hex2bin($hex_string)
56 // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
57 // (string) : Always treat as string (not int etc). See BugTrack2/31
58 return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
59 pack('H*', (string)$hex_string) : $hex_string;
61 // Remove [[ ]] (brackets)
62 function strip_bracket($str)
65 if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
71 //////////////////////////////////
72 // lib/file.php r1.68 (modified)
74 // Get a page list of this wiki
75 function get_existpages($dir = '', $ext = '.txt')
79 $pattern = '((?:[0-9A-F]{2})+)';
80 if ($ext != '') $ext = preg_quote($ext, '/');
81 $pattern = '/^' . $pattern . $ext . '$/';
83 $dp = @opendir($dir) or
84 die($dir . ' is not found or not readable.');
86 while ($file = readdir($dp))
87 if (preg_match($pattern, $file, $matches))
88 $aryret[$file] = decode($matches[1]);
93 //////////////////////////////////
95 if (empty($argv)) usage();
100 $encoding_from = 'UTF-8';
101 $encoding_to = 'EUC-JP';
102 foreach ($argv as $key => $value) {
103 if ($value != '' && $value[0] != '-') break;
105 list($value, $optarg) = explode('=', $value, 2);
107 case '--all' : $f_all = TRUE; break;
108 case '--decode' : $f_decode = TRUE; break;
109 case '--nocheck' : $f_nocheck = TRUE; break;
110 case '--suffix' : $suffix = $optarg; break;
111 case '--encoding_from': $encoding_from = $optarg; break;
112 case '--encoding_to' : $encoding_to = $optarg; break;
113 case '--encoding' : $encoding_to = $optarg; break;
117 define('SOURCE_ENCODING', $encoding_from);
118 define('TARGET_ENCODING', $encoding_to);
121 if ($f_all && empty($argv)) {
122 $argv = array_keys(get_existpages('.', $suffix));
124 foreach ($argv as $arg) {
125 if (! $f_nocheck && ! file_exists($arg)) {
126 echo 'File not found: ' . $arg . "\n";
133 mb_internal_encoding(SOURCE_ENCODING);
134 mb_detect_order('auto');
136 foreach ($argv as $arg) {
137 if (preg_match('/^(.+)(\.[a-zA-Z0-9]+)$/', $arg, $matches)) {
139 $suffix = $matches[2];
144 //echo $name . $suffix . "\n"; // As-is
147 echo decode($name) . $suffix . "\n";
149 // Decord -> convert -> encode
150 echo encode(mb_convert_encoding(decode($name),
151 TARGET_ENCODING, SOURCE_ENCODING)) .