OSDN Git Service

Caching existed plugins and entities. Thank you Nekyo and machu!
[pukiwiki/pukiwiki.git] / lib / plugin.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: plugin.php,v 1.5 2004/11/04 12:11:18 henoheno Exp $
6 //
7
8 // ¥×¥é¥°¥¤¥óÍѤË̤ÄêµÁ¤Î¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤òÀßÄê
9 function set_plugin_messages($messages)
10 {
11         foreach ($messages as $name=>$val) {
12                 if (! isset($GLOBALS[$name])) $GLOBALS[$name] = $val;
13         }
14 }
15
16 //¥×¥é¥°¥¤¥ó¤¬Â¸ºß¤¹¤ë¤«
17 function exist_plugin($name)
18 {
19         static $exists = array();
20
21         $name = strtolower($name);      // Âçʸ»ú¤È¾®Ê¸»ú¤ò¶èÊ̤·¤Ê¤¤¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥àÂкö
22         if(isset($exists[$name])) return $exists[$name];
23
24         if (preg_match('/^\w{1,64}$/', $name) &&
25             file_exists(PLUGIN_DIR . $name . '.inc.php')) {
26                 $exists[$name] = TRUE;
27                 require_once(PLUGIN_DIR . $name . '.inc.php');
28                 return TRUE;
29         } else {
30                 $exists[$name] = FALSE;
31                 return FALSE;
32         }
33 }
34
35 //¥×¥é¥°¥¤¥ó´Ø¿ô(action)¤¬Â¸ºß¤¹¤ë¤«
36 function exist_plugin_action($name) {
37         return  function_exists('plugin_' . $name . '_action') ? TRUE : exist_plugin($name) ?
38                 function_exists('plugin_' . $name . '_action') : FALSE;
39 }
40
41 //¥×¥é¥°¥¤¥ó´Ø¿ô(convert)¤¬Â¸ºß¤¹¤ë¤«
42 function exist_plugin_convert($name) {
43         return  function_exists('plugin_' . $name . '_convert') ? TRUE : exist_plugin($name) ?
44                 function_exists('plugin_' . $name . '_convert') : FALSE;
45 }
46
47 //¥×¥é¥°¥¤¥ó´Ø¿ô(inline)¤¬Â¸ºß¤¹¤ë¤«
48 function exist_plugin_inline($name) {
49         return  function_exists('plugin_' . $name . '_inline') ? TRUE : exist_plugin($name) ?
50                 function_exists('plugin_' . $name . '_inline') : FALSE;
51 }
52
53 //¥×¥é¥°¥¤¥ó¤Î½é´ü²½¤ò¼Â¹Ô
54 function do_plugin_init($name)
55 {
56         static $checked = array();
57
58         if (! isset($checked[$name])) {
59                 $func = 'plugin_' . $name . '_init';
60                 if (function_exists($func)) {
61                         // TRUE or FALSE or NULL (return nothing)
62                         $checked[$name] = call_user_func($func);
63                 } else {
64                         // Not exists
65                         $checked[$name] = null;
66                 }
67         }
68         return $checked[$name];
69 }
70
71 //¥×¥é¥°¥¤¥ó(action)¤ò¼Â¹Ô
72 function do_plugin_action($name)
73 {
74         if (! exist_plugin_action($name)) return array();
75
76         if(do_plugin_init($name) === FALSE)
77                 die_message("Plugin init failed: $name");
78
79         $retvar = call_user_func('plugin_' . $name . '_action');
80
81         // Insert a hidden field, supports idenrtifying text enconding
82         if (PKWK_ENCODING_HINT != '')
83                 $retvar =  preg_replace('/(<form[^>]*>)/', "$1\n" .
84                         '<div><input type="hidden" name="encode_hint" value="' . PKWK_ENCODING_HINT . '" /></div>',
85                         $retvar);
86
87         return $retvar;
88 }
89
90 //¥×¥é¥°¥¤¥ó(convert)¤ò¼Â¹Ô
91 function do_plugin_convert($name, $args = '')
92 {
93         global $digest;
94
95         if(do_plugin_init($name) === FALSE)
96                 return "[Plugin init failed: $name]";
97
98         if ($args !== '') {
99                 $aryargs = csv_explode(',', $args);
100         } else {
101                 $aryargs = array();
102         }
103
104         $_digest = $digest;  // ÂàÈò
105         $retvar  = call_user_func_array('plugin_' . $name . '_convert', $aryargs);
106         $digest  = $_digest; // Éü¸µ
107
108         if ($retvar === FALSE) {
109                 $retvar =  htmlspecialchars('#' . $name . ($args != '' ? "($args)" : ''));
110         } else if (PKWK_ENCODING_HINT != '') {
111                 // Insert a hidden field, supports idenrtifying text enconding
112                 $retvar =  preg_replace('/(<form[^>]*>)/', "$1\n" .
113                         '<div><input type="hidden" name="encode_hint" value="' . PKWK_ENCODING_HINT . '" /></div>',
114                         $retvar);
115
116         }
117
118         return $retvar;
119 }
120
121 //¥×¥é¥°¥¤¥ó(inline)¤ò¼Â¹Ô
122 function do_plugin_inline($name, $args, & $body)
123 {
124         global $digest;
125
126         if(do_plugin_init($name) === FALSE)
127                 return "[Plugin init failed: $name]";
128
129         if ($args !== '') {
130                 $aryargs = csv_explode(',', $args);
131         } else {
132                 $aryargs = array();
133         }
134         $aryargs[] = & $body; // Added reference of $body
135
136         $_digest = $digest;  // ÂàÈò
137         $retvar  = call_user_func_array('plugin_' . $name . '_inline', $aryargs);
138         $digest  = $_digest; // Éü¸µ
139
140         if($retvar === FALSE) {
141                 return htmlspecialchars("&${name}" . ($args ? "($args)" : '') . ';');
142         } else {
143                 return $retvar;
144         }
145 }
146 ?>