OSDN Git Service

Added error_reporting()
[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.2 2006/05/14 15:05:19 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         exit(1);
33 }
34
35 //////////////////////////////////
36 // Code from PukiWiki 1.4.7
37
38 // lib/func.php r1.72
39 // Encode page-name
40 function encode($key)
41 {
42         return ($key == '') ? '' : strtoupper(bin2hex($key));
43         // Equal to strtoupper(join('', unpack('H*0', $key)));
44         // But PHP 4.3.10 says 'Warning: unpack(): Type H: outside of string in ...'
45 }
46 // Decode page name
47 function decode($key)
48 {
49         return hex2bin($key);
50 }
51 // Inversion of bin2hex()
52 function hex2bin($hex_string)
53 {
54         // preg_match : Avoid warning : pack(): Type H: illegal hex digit ...
55         // (string)   : Always treat as string (not int etc). See BugTrack2/31
56         return preg_match('/^[0-9a-f]+$/i', $hex_string) ?
57         pack('H*', (string)$hex_string) : $hex_string;
58 }
59 // Remove [[ ]] (brackets)
60 function strip_bracket($str)
61 {
62         $match = array();
63         if (preg_match('/^\[\[(.*)\]\]$/', $str, $match)) {
64                 return $match[1];
65         } else {
66                 return $str;
67         }
68 }
69 //////////////////////////////////
70 // lib/file.php r1.68 (modified)
71
72 // Get a page list of this wiki
73 function get_existpages($dir = '', $ext = '.txt')
74 {
75         $aryret = array();
76
77         $pattern = '((?:[0-9A-F]{2})+)';
78         if ($ext != '') $ext = preg_quote($ext, '/');
79         $pattern = '/^' . $pattern . $ext . '$/';
80
81         $dp = @opendir($dir) or
82                 die($dir . ' is not found or not readable.');
83         $matches = array();
84         while ($file = readdir($dp))
85                 if (preg_match($pattern, $file, $matches))
86                         $aryret[$file] = decode($matches[1]);
87         closedir($dp);
88
89         return $aryret;
90 }
91 //////////////////////////////////
92
93 if (empty($argv)) usage();
94
95 // Options
96 $f_all = FALSE;
97 $suffix = '.txt';
98 $encoding_from = 'EUC-JP';
99 $encoding_to   = 'UTF-8';
100 foreach ($argv as $key => $value) {
101         if ($value != '' && $value[0] != '-') break;
102         $optarg = '';
103         list($value, $optarg) = explode('=', $value, 2);
104         switch ($value) {
105                 case '--all'          : $f_all         = TRUE;    break;
106                 case '--suffix'       : $suffix        = $optarg; break;
107                 case '--encoding_from': $encoding_from = $optarg; break;
108                 case '--encoding_to'  : $encoding_to   = $optarg; break;
109                 case '--encoding'     : $encoding_to   = $optarg; break;
110         }
111         unset($argv[$key]);
112 }
113 define('SOURCE_ENCODING', $encoding_from);
114 define('TARGET_ENCODING', $encoding_to);
115
116 // Target
117 if ($f_all && empty($argv)) {
118         $argv = array_keys(get_existpages('.', $suffix));
119 } else {
120         foreach ($argv as $arg) {
121                 if (! file_exists($arg)) {
122                         echo 'File not found: ' . $arg . "\n";
123                         usage();
124                 }
125         }
126 }
127
128 // Do
129 mb_internal_encoding(SOURCE_ENCODING);
130 mb_detect_order('auto');
131 $matches = array();
132 foreach ($argv as $arg) {
133         if (preg_match('/^(.+)(\.[a-zA-Z0-9]+)$/', $arg, $matches)) {
134                 $name   = $matches[1];
135                 $suffix = $matches[2];
136         } else {
137                 $name   = $arg;
138                 $suffix = '';
139         }
140         //echo $name . $suffix . "\n";          // As-is
141         //echo decode($name) . $suffix . "\n";  // Decorded
142         echo encode(mb_convert_encoding(decode($name),
143                 TARGET_ENCODING, SOURCE_ENCODING)) .
144                 $suffix . "\n"; // Decord -> convert -> encode
145         //echo "\n";
146 }
147 ?>