OSDN Git Service

BugTrack/2436 ESLint - Linting utility for JavaScript
[pukiwiki/pukiwiki.git] / plugin / update_entities.inc.php
1 <?php
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
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 $vars;
42         global $_entities_messages;
43
44         $script = get_base_uri();
45         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
46
47         $msg = $body = '';
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)));
52                 $body .= <<<EOD
53 <form method="post" action="$script">
54  <div>
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']}" />
60  </div>
61 </form>
62 EOD;
63         } else if ($vars['action'] == 'update') {
64                 plugin_update_entities_create(TRUE);
65                 $msg  = & $_entities_messages['title_update'];
66                 $body = & $_entities_messages['msg_done'    ];
67         } else {
68                 $msg  = & $_entities_messages['title_update'];
69                 $body = & $_entities_messages['err_invalid' ];
70         }
71         return array('msg'=>$msg, 'body'=>$body);
72 }
73
74 // Remove &amp; => amp
75 function plugin_update_entities_strtr($entity){
76         return strtr($entity, array('&'=>'', ';'=>''));
77 }
78
79 function plugin_update_entities_create($do = FALSE)
80 {
81         $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
82         
83         $entities = array_map('plugin_update_entities_strtr',
84                 array_values(get_html_translation_table(HTML_ENTITIES)));
85         $items   = array('php:html_translation_table');
86         $matches = array();
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.';
92                         continue;
93                 }
94                 $items[] = 'w3c:' . $file;
95                 if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
96                         join('', $source), $matches, PREG_PATTERN_ORDER))
97                 {
98                         $entities = array_merge($entities, $matches[1]);
99                 }
100         }
101         if (! $do) return $items;
102
103         $entities = array_unique($entities);
104         sort($entities, SORT_STRING);
105         $min = 999;
106         $max = 0;
107         foreach ($entities as $entity) {
108                 $len = strlen($entity);
109                 $max = max($max, $len);
110                 $min = min($min, $len);
111         }
112
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);
119         fclose($fp);
120
121         return $items;
122 }