OSDN Git Service

Enable to build on FreeBSD (OSDN project server environment)
[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.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
7 //
8 // encoded-EUC-JP.txt -> EUC-JP -> UTF-8 -> encoded-UTF-8.txt
9
10 // Error reporting
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
14
15 // PHP-cli only
16 if (php_sapi_name() != 'cli') die('Invalid SAPI');
17 if (! isset($argv)) die('PHP too old (Not 4.3.0 of above)');
18
19 //////////////////////////////////
20
21 $base = basename(array_shift($argv));
22 function usage(){
23         global $base;
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";
34         exit(1);
35 }
36
37 //////////////////////////////////
38 // Code from PukiWiki 1.4.7
39
40 // lib/func.php r1.72
41 // Encode page-name
42 function encode($key)
43 {
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 ...'
47 }
48 // Decode page name
49 function decode($key)
50 {
51         return hex2bin($key);
52 }
53 // Inversion of bin2hex()
54 function hex2bin($hex_string)
55 {
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;
60 }
61 // Remove [[ ]] (brackets)
62 function strip_bracket($str)
63 {
64         $match = array();
65         if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
66                 return $match[1];
67         } else {
68                 return $str;
69         }
70 }
71 //////////////////////////////////
72 // lib/file.php r1.68 (modified)
73
74 // Get a page list of this wiki
75 function get_existpages($dir = '', $ext = '.txt')
76 {
77         $aryret = array();
78
79         $pattern = '((?:[0-9A-F]{2})+)';
80         if ($ext != '') $ext = preg_quote($ext, '/');
81         $pattern = '/^' . $pattern . $ext . '$/';
82
83         $dp = @opendir($dir) or
84                 die($dir . ' is not found or not readable.');
85         $matches = array();
86         while ($file = readdir($dp))
87                 if (preg_match($pattern, $file, $matches))
88                         $aryret[$file] = decode($matches[1]);
89         closedir($dp);
90
91         return $aryret;
92 }
93 //////////////////////////////////
94
95 if (empty($argv)) usage();
96
97 // Options
98 $f_all = FALSE;
99 $suffix = '.txt';
100 $encoding_from = 'UTF-8';
101 $encoding_to   = 'EUC-JP';
102 foreach ($argv as $key => $value) {
103         if ($value != '' && $value[0] != '-') break;
104         $optarg = '';
105         list($value, $optarg) = explode('=', $value, 2);
106         switch ($value) {
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;
114         }
115         unset($argv[$key]);
116 }
117 define('SOURCE_ENCODING', $encoding_from);
118 define('TARGET_ENCODING', $encoding_to);
119
120 // Target
121 if ($f_all && empty($argv)) {
122         $argv = array_keys(get_existpages('.', $suffix));
123 } else {
124         foreach ($argv as $arg) {
125                 if (! $f_nocheck && ! file_exists($arg)) {
126                         echo 'File not found: ' . $arg . "\n";
127                         usage();
128                 }
129         }
130 }
131
132 // Do
133 mb_internal_encoding(SOURCE_ENCODING);
134 mb_detect_order('auto');
135 $matches = array();
136 foreach ($argv as $arg) {
137         if (preg_match('/^(.+)(\.[a-zA-Z0-9]+)$/', $arg, $matches)) {
138                 $name   = $matches[1];
139                 $suffix = $matches[2];
140         } else {
141                 $name   = $arg;
142                 $suffix = '';
143         }
144         //echo $name . $suffix . "\n";          // As-is
145         if ($f_decode) {
146                 // Decord
147                 echo decode($name) . $suffix . "\n";
148         } else {
149                 // Decord -> convert -> encode
150                 echo encode(mb_convert_encoding(decode($name),
151                         TARGET_ENCODING, SOURCE_ENCODING)) .
152                         $suffix . "\n";
153         }
154         //echo "\n";
155 }
156 ?>