OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / update_entities.inc.php
1 <?php
2 /**
3  * update_entitiesプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2009 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: update_entities.inc.php 1601 2009-03-21 05:51:06Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 // Update entities plugin - Update XHTML entities from DTD
17 // (for admin)
18
19 // DTDの場所
20 define('W3C_XHTML_DTD_LOCATION', 'http://www.w3.org/TR/xhtml1/DTD/');
21
22 // メッセージ設定
23 function plugin_update_entities_init()
24 {
25         $messages = array(
26                 '_entities_messages'=>array(
27                         'title_update'  => 'キャッシュ更新',
28                         'msg_adminpass' => '管理者パスワード',
29                         'btn_submit'    => '実行',
30                         'msg_done'      => 'キャッシュの更新が完了しました。',
31                         'msg_usage'     => '
32 * 処理内容
33
34 :文字実体参照にマッチする正規表現パターンのキャッシュを更新|
35 PHPの持つテーブルおよびW3CのDTDをスキャンして、キャッシュに記録します。
36
37 * 処理対象
38 「COLOR(red){not found.}」と表示されたファイルは処理されません。
39 -%s
40
41 * 実行
42 管理者パスワードを入力して、[実行]ボタンをクリックしてください。
43 '
44                 ));
45         set_plugin_messages($messages);
46 }
47
48 function plugin_update_entities_action()
49 {
50         //global $script, $vars;
51         global $script;
52         global $_entities_messages;
53
54         if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
55
56         $msg = $body = '';
57         $action = WikiParam::getVar('action');
58         $pass = WikiParam::getVar('pass');
59         //if (empty($vars['action']) || empty($vars['adminpass']) || ! pkwk_login($vars['adminpass'])) {
60         if (empty($action) || empty($pass) || !pkwk_login($pass)){
61                 $msg   = $_entities_messages['title_update'];
62                 $items = plugin_update_entities_create();
63                 $body  = convert_html(sprintf($_entities_messages['msg_usage'], join("\n" . '-', $items)));
64                 $postScript = $script . WikiParam::convQuery("?");
65                 $body .= <<<EOD
66 <form action="$postScript" method="post" class="form">
67  <div>
68   <input type="hidden" name="plugin" value="update_entities" />
69   <input type="hidden" name="action" value="update" />
70   <input type="hidden" name="pass" />
71   <label for="_p_update_entities_adminpass">{$_entities_messages['msg_adminpass']}</label>
72   <input type="password" name="password" id="_p_update_entities_adminpass" size="12" />
73   <input type="submit" class="button" value="{$_entities_messages['btn_submit']}" onclick="this.form.pass.value = hex_md5(this.form.password.value);" />
74  </div>
75 </form>
76 EOD;
77
78         //} else if ($vars['action'] == 'update') {
79         } else if ($action == 'update') {
80                 plugin_update_entities_create(TRUE);
81                 $msg  = $_entities_messages['title_update'];
82                 $body = $_entities_messages['msg_done'    ];
83         } else {
84                 $msg  = $_entities_messages['title_update'];
85                 $body = $_entities_messages['err_invalid' ];
86         }
87         return array('msg'=>$msg, 'body'=>$body);
88 }
89
90 // Remove &amp; => amp
91 function plugin_update_entities_strtr($entity){
92         return strtr($entity, array('&'=>'', ';'=>''));
93 }
94
95 function plugin_update_entities_create($do = FALSE)
96 {
97         $files = array('xhtml-lat1.ent', 'xhtml-special.ent', 'xhtml-symbol.ent');
98         
99         $entities = array_map('plugin_update_entities_strtr',
100                 array_values(get_html_translation_table(HTML_ENTITIES)));
101         $items   = array('php:html_translation_table');
102         $matches = array();
103         foreach ($files as $file) {
104                 $source = file(W3C_XHTML_DTD_LOCATION . $file);
105 //                      or die_message('cannot receive ' . W3C_XHTML_DTD_LOCATION . $file . '.');
106                 if (! is_array($source)) {
107                         $items[] = 'w3c:' . $file . ' COLOR(red):not found.';
108                         continue;
109                 }
110                 $items[] = 'w3c:' . $file;
111                 if (preg_match_all('/<!ENTITY\s+([A-Za-z0-9]+)/',
112                         join('', $source), $matches, PREG_PATTERN_ORDER))
113                 {
114                         $entities = array_merge($entities, $matches[1]);
115                 }
116         }
117         if (! $do) return $items;
118
119         $entities = array_unique($entities);
120         sort($entities, SORT_STRING);
121         $min = 999;
122         $max = 0;
123         foreach ($entities as $entity) {
124                 $len = strlen($entity);
125                 $max = max($max, $len);
126                 $min = min($min, $len);
127         }
128
129         // 判定パターンを更新
130         $pattern = "(?=[a-zA-Z0-9]\{$min,$max})" . get_autolink_pattern_sub($entities, 0, count($entities), 0);
131         WikiPage::updateEntityData($pattern);
132         /*
133         $fp = fopen(CACHE_DIR  .'entities.dat', 'w')
134                 or die_message('cannot write file ' . CACHE_DIR . 'entities.dat<br />' . "\n" .
135                         'maybe permission is not writable or filename is too long');
136         fwrite($fp, $pattern);
137         fclose($fp);*/
138
139         return $items;
140 }
141 ?>