-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgen_extra_index.php
200 lines (159 loc) · 5.84 KB
/
gen_extra_index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
function FilenameInfix($filename) {
$base_filename = basename($filename);
$base_filename = preg_replace("/LCIDC01_/", "", $base_filename);
$base_filename = preg_replace("/\.html/", "", $base_filename);
return $base_filename;
}
function IndexParseKey($content) {
return $content['gazette'] . $content['book'] . sprintf("%03d", $content['seq']);
}
function Index() {
if(isset($GLOBALS['INDEX'])) return $GLOBALS['INDEX'];
$content = file_get_contents("data/index.json");
$content_st = json_decode($content, true);
$result = array();
foreach($content_st as $each_content) {
$the_key = IndexParseKey($each_content);
$result[$the_key] = $each_content;
}
$GLOBALS['INDEX'] = $result;
return $result;
}
function IndexMapping($index) {
if(isset($GLOBALS['INDEX_MAPPING'])) return $GLOBALS['INDEX_MAPPING'];
$result = array();
foreach($index as $each_index) {
if(empty($each_index['files'])) continue;
$gazette = $each_index['gazette'];
$url = $each_index['files'][0];
$filename = preg_replace("/.*\//smu", "", $url);
$filename_infix = substr($filename, 8);
$mapping = substr($filename_infix, 0, -12);
$result[$mapping] = $gazette;
}
$result['9824'] = 3710;
$result['9997'] = 3828;
$result['9999'] = 3828;
$GLOBALS['INDEX_MAPPING'] = $result;
return $result;
}
function Gazette($filename_infix, $index_mapping) {
$mapping = substr($filename_infix, 0, -8);
return $index_mapping[$mapping];
}
function Book($filename_infix) {
$result = substr($filename_infix, -8, 2);
return $result;
}
function Seq($filename_infix) {
$result = substr($filename_infix, -5, 5);
$result = (int)$result;
$result = (string)$result;
return $result;
}
function TheType($content, $filename_infix) {
$pattern_ary = array("立法院公報\s+第.*?卷\s+第.*?期\s+(.*?)<\/SPAN>",
"<TITLE>(.*?)<\/TITLE>");
$match_ary = array();
foreach($pattern_ary as $each_pattern) {
$the_pattern = "/" . $each_pattern . "/smu";
if(preg_match($the_pattern, $content, $match_ary)) {
$result = $match_ary[1];
$result = preg_replace("/\s+/u", "", $result);
break;
}
}
$special_ary = array('903004_00003' => '議事錄',
'910201_00004' => '議事錄',
'982401_00005' => '質詢事項',
'982401_00006' => '質詢事項'
);
if(isset($special_ary[$filename_infix])) $result = $special_ary[$filename_infix];
return $result;
}
function Summary($content, $the_type, $filename_infix) {
$pattern_ary = array(
"<P STYLE=\"margin-left.*?SIZE=4>(.*?)<\/FONT><\/P>",
"SIZE=4>(.*?)<\/FONT><\/P>",
"<TITLE>(.*?)<\/TITLE>");
$result = $the_type;
$match_ary = array();
$special_char_ary = array(json_decode('"\ue8f6"'), json_decode('"\ue8f4"'));
foreach($pattern_ary as $each_pattern) {
$the_pattern = "/" . $each_pattern . "/smu";
if(preg_match($the_pattern, $content, $match_ary)) {
$result = $match_ary[1];
$result = preg_replace("/\s+/u", "", $result);
$result = preg_replace("/<.*?>/smu", "", $result);
$result = preg_replace("/會議紀錄.*/smu", "會議紀錄", $result);
$result = preg_replace("/勘誤表.*/smu", "勘誤表", $result);
$result = preg_replace("/.*、/u", "", $result);
$result = preg_replace("/[ \p{C}]/u", "", $result);
if($the_type == "委員會紀錄" && $result == "報告事項") continue;
break;
}
}
if(preg_match("/補\s*?刊:?.*?<\/FONT><\/P>/smu", $content)) $result = "補刊";
if(preg_match("/補\s*<.*?>\s*刊:?.*?<\/FONT><\/P>/smu", $content)) $result = "補刊";
$result = trim($result);
if($result === "") $result = $the_type;
$special_ary = array(
'916701_00007' => '補刊',
'916801_00003' => '補刊',
'943001_00005' => '繼續開會(17時20分)',
'982401_00005' => '甲、行政院答復部分'
);
if(isset($special_ary[$filename_infix])) $result = $special_ary[$filename_infix];
return $result;
}
function Url($filename_infix) {
$year = substr($filename_infix, 0, -10);
$vol = substr($filename_infix, -10, 2);
$url_prefix = $year >= 100 ? "http://lci.ly.gov.tw/LyLCEW/communique1/work" : "http://lci.ly.gov.tw/LyLCEW/communique/work";
$filename = "LCIDC01_" . $filename_infix . ".doc";
$url = $url_prefix . "/" . $year . "/" . $vol . "/" . $filename;
return $url;
}
function TheTypeBySummary($summary, $the_type) {
if($summary == "本期委員發言紀錄索引") $the_type = "索引";
return $the_type;
}
function ProcessFile($filename) {
$content = file_get_contents($filename);
$filename_infix = FilenameInfix($filename);
$index = Index();
$index_mapping = IndexMapping($index);
$gazette = Gazette($filename_infix, $index_mapping);
$book = Book($filename_infix);
$seq = Seq($filename_infix);
$the_type = TheType($content, $filename_infix);
$summary = Summary($content, $the_type, $filename_infix);
$the_type = TheTypeBySummary($summary, $the_type);
$url = Url($filename_infix);
$files = array($url);
$result = array('gazette' => $gazette,
'book' => $book,
'seq' => $seq,
'type' => $the_type,
'summary' => $summary,
'files' => $files);
return $result;
}
function Main($argv) {
$filename_list = $argv[1];
$content = file_get_contents($filename_list);
$lines = preg_split("/\n/u", $content);
$result_ary = array();
foreach($lines as $each_filename) {
if($each_filename === "") continue;
$result_ary[] = ProcessFile($each_filename);
}
echo json_encode($result_ary, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
if(count($argv) != 2) {
echo "usage: php gen_extra_index.php [filename_list]\n";
exit;
}
Main($argv);
?>