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
7 // Update entities plugin - Update XHTML entities from DTD
11 define('W3C_XHTML_DTD_LOCATION', 'http://www.w3.org/TR/xhtml1/DTD/');
14 function plugin_update_entities_init()
17 '_entities_messages'=>array(
18 'title_update' => 'キャッシュ更新',
19 'msg_adminpass' => '管理者パスワード',
21 'msg_done' => 'キャッシュの更新が完了しました。',
25 :文字実体参照にマッチする正規表現パターンのキャッシュを更新|
26 PHPの持つテーブルおよびW3CのDTDをスキャンして、キャッシュに記録します。
29 「COLOR(red){not found.}」と表示されたファイルは処理されません。
33 管理者パスワードを入力して、[実行]ボタンをクリックしてください。
36 set_plugin_messages($messages);
39 function plugin_update_entities_action()
41 global $script, $vars;
42 global $_entities_messages;
44 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
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)));
52 <form method="post" action="$script">
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']}" />
62 } else if ($vars['action'] == 'update') {
63 plugin_update_entities_create(TRUE);
64 $msg = & $_entities_messages['title_update'];
65 $body = & $_entities_messages['msg_done' ];
67 $msg = & $_entities_messages['title_update'];
68 $body = & $_entities_messages['err_invalid' ];
70 return array('msg'=>$msg, 'body'=>$body);
73 // Remove & => amp
74 function plugin_update_entities_strtr($entity){
75 return strtr($entity, array('&'=>'', ';'=>''));
78 function plugin_update_entities_create($do = FALSE)
80 $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
82 $entities = array_map('plugin_update_entities_strtr',
83 array_values(get_html_translation_table(HTML_ENTITIES)));
84 $items = array('php:html_translation_table');
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.';
93 $items[] = 'w3c:' . $file;
94 if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
95 join('', $source), $matches, PREG_PATTERN_ORDER))
97 $entities = array_merge($entities, $matches[1]);
100 if (! $do) return $items;
102 $entities = array_unique($entities);
103 sort($entities, SORT_STRING);
106 foreach ($entities as $entity) {
107 $len = strlen($entity);
108 $max = max($max, $len);
109 $min = min($min, $len);
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);