2 // PukiWiki - Yet another WikiWikiWeb clone
3 // update_entities.inc.php
4 // Copyright 2003-2017 PukiWiki Development 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()
42 global $_entities_messages;
44 $script = get_base_uri();
45 if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
48 if (empty($vars['action']) || empty($vars['adminpass']) || ! pkwk_login($vars['adminpass'])) {
49 $msg = & $_entities_messages['title_update'];
50 $items = plugin_update_entities_create();
51 $body = convert_html(sprintf($_entities_messages['msg_usage'], join("\n" . '-', $items)));
53 <form method="post" action="$script">
55 <input type="hidden" name="plugin" value="update_entities" />
56 <input type="hidden" name="action" value="update" />
57 <label for="_p_update_entities_adminpass">{$_entities_messages['msg_adminpass']}</label>
58 <input type="password" name="adminpass" id="_p_update_entities_adminpass" size="20" value="" />
59 <input type="submit" value="{$_entities_messages['btn_submit']}" />
63 } else if ($vars['action'] == 'update') {
64 plugin_update_entities_create(TRUE);
65 $msg = & $_entities_messages['title_update'];
66 $body = & $_entities_messages['msg_done' ];
68 $msg = & $_entities_messages['title_update'];
69 $body = & $_entities_messages['err_invalid' ];
71 return array('msg'=>$msg, 'body'=>$body);
74 // Remove & => amp
75 function plugin_update_entities_strtr($entity){
76 return strtr($entity, array('&'=>'', ';'=>''));
79 function plugin_update_entities_create($do = FALSE)
81 $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
83 $entities = array_map('plugin_update_entities_strtr',
84 array_values(get_html_translation_table(HTML_ENTITIES)));
85 $items = array('php:html_translation_table');
87 foreach ($files as $file) {
88 $source = file(W3C_XHTML_DTD_LOCATION . $file);
89 // or die_message('cannot receive ' . W3C_XHTML_DTD_LOCATION . $file . '.');
90 if (! is_array($source)) {
91 $items[] = 'w3c:' . $file . ' COLOR(red):not found.';
94 $items[] = 'w3c:' . $file;
95 if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
96 join('', $source), $matches, PREG_PATTERN_ORDER))
98 $entities = array_merge($entities, $matches[1]);
101 if (! $do) return $items;
103 $entities = array_unique($entities);
104 sort($entities, SORT_STRING);
107 foreach ($entities as $entity) {
108 $len = strlen($entity);
109 $max = max($max, $len);
110 $min = min($min, $len);
113 $pattern = "(?=[a-zA-Z0-9]\{$min,$max})" .
114 get_autolink_pattern_sub($entities, 0, count($entities), 0);
115 $fp = fopen(CACHE_DIR .'entities.dat', 'w')
116 or die_message('cannot write file ' . CACHE_DIR . 'entities.dat<br />' . "\n" .
117 'maybe permission is not writable or filename is too long');
118 fwrite($fp, $pattern);