OSDN Git Service

BugTrack2/361 Simplify URL. Keep slash / and colon : in page name URL
[pukiwiki/pukiwiki.git] / plugin / update_entities.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: update_entities.inc.php,v 1.15 2007/04/08 10:29:24 henoheno Exp $
4 // Copyright (C) 2003-2007 PukiWiki Developers Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 // Update entities plugin - Update XHTML entities from DTD
8 // (for admin)
9
10 // DTDの場所
11 define('W3C_XHTML_DTD_LOCATION', 'http://www.w3.org/TR/xhtml1/DTD/');
12
13 // メッセージ設定
14 function plugin_update_entities_init()
15 {
16         $messages = array(
17                 '_entities_messages'=>array(
18                         'title_update'  => 'キャッシュ更新',
19                         'msg_adminpass' => '管理者パスワード',
20                         'btn_submit'    => '実行',
21                         'msg_done'      => 'キャッシュの更新が完了しました。',
22                         'msg_usage'     => '
23 * 処理内容
24
25 :文字実体参照にマッチする正規表現パターンのキャッシュを更新|
26 PHPの持つテーブルおよびW3CのDTDをスキャンして、キャッシュに記録します。
27
28 * 処理対象
29 「COLOR(red){not found.}」と表示されたファイルは処理されません。
30 -%s
31
32 * 実行
33 管理者パスワードを入力して、[実行]ボタンをクリックしてください。
34 '
35                 ));
36         set_plugin_messages($messages);
37 }
38
39 function plugin_update_entities_action()
40 {
41         global $script, $vars;
42         global $_entities_messages;
43
44         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
45
46         $msg = $body = '';
47         if (empty($vars['action']) || empty($vars['adminpass']) || ! pkwk_login($vars['adminpass'])) {
48                 $msg   = & $_entities_messages['title_update'];
49                 $items = plugin_update_entities_create();
50                 $body  = convert_html(sprintf($_entities_messages['msg_usage'], join("\n" . '-', $items)));
51                 $body .= <<<EOD
52 <form method="post" action="$script">
53  <div>
54   <input type="hidden" name="plugin" value="update_entities" />
55   <input type="hidden" name="action" value="update" />
56   <label for="_p_update_entities_adminpass">{$_entities_messages['msg_adminpass']}</label>
57   <input type="password" name="adminpass" id="_p_update_entities_adminpass" size="20" value="" />
58   <input type="submit" value="{$_entities_messages['btn_submit']}" />
59  </div>
60 </form>
61 EOD;
62         } else if ($vars['action'] == 'update') {
63                 plugin_update_entities_create(TRUE);
64                 $msg  = & $_entities_messages['title_update'];
65                 $body = & $_entities_messages['msg_done'    ];
66         } else {
67                 $msg  = & $_entities_messages['title_update'];
68                 $body = & $_entities_messages['err_invalid' ];
69         }
70         return array('msg'=>$msg, 'body'=>$body);
71 }
72
73 // Remove &amp; => amp
74 function plugin_update_entities_strtr($entity){
75         return strtr($entity, array('&'=>'', ';'=>''));
76 }
77
78 function plugin_update_entities_create($do = FALSE)
79 {
80         $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
81         
82         $entities = array_map('plugin_update_entities_strtr',
83                 array_values(get_html_translation_table(HTML_ENTITIES)));
84         $items   = array('php:html_translation_table');
85         $matches = array();
86         foreach ($files as $file) {
87                 $source = file(W3C_XHTML_DTD_LOCATION . $file);
88 //                      or die_message('cannot receive ' . W3C_XHTML_DTD_LOCATION . $file . '.');
89                 if (! is_array($source)) {
90                         $items[] = 'w3c:' . $file . ' COLOR(red):not found.';
91                         continue;
92                 }
93                 $items[] = 'w3c:' . $file;
94                 if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
95                         join('', $source), $matches, PREG_PATTERN_ORDER))
96                 {
97                         $entities = array_merge($entities, $matches[1]);
98                 }
99         }
100         if (! $do) return $items;
101
102         $entities = array_unique($entities);
103         sort($entities, SORT_STRING);
104         $min = 999;
105         $max = 0;
106         foreach ($entities as $entity) {
107                 $len = strlen($entity);
108                 $max = max($max, $len);
109                 $min = min($min, $len);
110         }
111
112         $pattern = "(?=[a-zA-Z0-9]\{$min,$max})" .
113                 get_autolink_pattern_sub($entities, 0, count($entities), 0);
114         $fp = fopen(CACHE_DIR  .'entities.dat', 'w')
115                 or die_message('cannot write file ' . CACHE_DIR . 'entities.dat<br />' . "\n" .
116                         'maybe permission is not writable or filename is too long');
117         fwrite($fp, $pattern);
118         fclose($fp);
119
120         return $items;
121 }
122 ?>