XPressME Integration Kit

Trac

source: trunk/include/xpress_block_render.php @ 135

Last change on this file since 135 was 135, checked in by toemon, 15 years ago

ブロックキャッシュの削除、ブロックキャッシュ更新タイミングの処理追加・変更 #65 #73
同じモジュール名が付くブロックキャッシュから、現在有効になっているブロックを除いたものを削除
ブログホーム閲覧時、ブロックのレンダリングの内容とキャッシュの内容のチェックサム(MD5)を取り比較、異なっていればキャッシュをリフレッシュする。
(ファイル書き込み回数を減らすため)

File size: 8.4 KB
Line 
1<?php
2        require_once dirname( __FILE__ ) .'/xml.php' ;
3        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
4        global $xoops_config;
5       
6        if (!is_object($xoops_config)){ // is call other modules
7                require_once dirname(dirname( __FILE__ )) .'/class/config_from_xoops.class.php' ;
8                $xoops_config = new ConfigFromXoops;
9        }
10       
11        function xpress_block_cache_write($mydirname,$block_name,$block)
12        {
13                        $xml = xpress_XML_serialize($block);
14                        $xml_name = $block_name . '.xml';
15                        xpress_cache_write($mydirname,$xml_name,$xml);
16        }
17        function xpress_block_cache_read($mydirname,$block_name)
18        {
19                        $xml_name = $block_name . '.xml';
20                        $xml_data = xpress_cache_read($mydirname,$xml_name);
21                        return @xpress_XML_unserialize($xml_data);
22        }
23       
24        function get_block_id($mydirname,$func_file,$options)
25        {
26                $options_string = '';
27                foreach ($options as $val){
28                        if (!empty($options_string)) $options_string .='|';
29                        $options_string .= $val;
30                }
31                        $xoopsDB =& Database::getInstance();
32                        $block_tbl = $xoopsDB->prefix('newblocks');     
33                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
34
35                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
36                        $result =  $xoopsDB->query($sql, 0, 0);
37                        if ($xoopsDB->getRowsNum($result)  > 0){
38                                $row = $xoopsDB->fetchArray($result);
39                                $block_id = $row['bid'];
40                        }
41                        return $block_id;
42        }
43
44        function get_xpress_theme_name($mydirname)
45        {
46                global $wpdb;
47               
48                if (is_null($wpdb)){
49                        $xoopsDB =& Database::getInstance();
50                        $wp_prefix = $mydirname;
51                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
52
53                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
54                        $theme_name = '';       
55
56                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
57                        $result =  $xoopsDB->query($sql, 0, 0);
58                        if ($xoopsDB->getRowsNum($result)  > 0){
59                                $row = $xoopsDB->fetchArray($result);
60                                $theme_name = $row['option_value'];
61                        }
62                } else {
63                        $theme_name = get_option('template');
64                }
65                return $theme_name;
66        }
67
68        function get_block_stylesheet_url($mydirname)
69        {
70                global $xoops_config;
71                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
72                $select_theme = get_xpress_theme_name($mydirname);
73                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
74                if (file_exists($style_file))
75                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
76                else   
77                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/block_style.css';
78        }
79
80        function xpress_block_css_set($mydirname = '')
81        {
82                $style_url =  get_block_stylesheet_url($mydirname);
83                 
84                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
85                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
86                        if(array_key_exists('xoops_block_header', $tplVars)) {
87                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
88                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
89                                }
90                        } else {
91                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
92                        }
93        }
94       
95        function get_block_file_path($mydirname,$file_name)
96        {
97                global $xoops_config;
98                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
99                $select_theme = get_xpress_theme_name($mydirname);
100                $block_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/' . $file_name;
101
102                if (!file_exists($block_file))
103                        $block_file =  $xoops_config->xoops_root_path . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/' . $file_name;
104                return $block_file;
105        }
106       
107    function xpress_block_cache_found($mydirname,$block_name)
108    {
109        global $xoops_config;
110        if(defined('XOOPS_ROOT_PATH')){
111                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
112        } else {
113                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
114        }
115        $xml_name = $block_name . '.xml';
116
117        $filename = $cache_dir .$mydirname . '_' . $xml_name;
118                $cache_time = 0;
119//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
120        if (file_exists($filename)) {
121            return true;
122       } else {
123                        return false;
124                }
125    }
126       
127        function xpress_block_render($mydirname,$block_function_name,$options)
128        {
129                global $wpdb;
130                $func_file = $block_function_name;
131                $call_theme_function_name = str_replace(".php", "", $block_function_name);
132                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
133                $cache_title = str_replace(".php", "", $block_function_name);
134                $blockID =get_block_id($mydirname,$func_file,$options);         
135
136                $this_url = '/modules/'. $mydirname;
137                $call_url = $_SERVER['REQUEST_URI'];
138               
139                xpress_block_css_set($mydirname);
140                if (strstr($call_url,$this_url)){                       
141                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
142                        require_once $block_theme_file;
143                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
144                } else {
145                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
146                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
147                                $block = $xml['block'];
148                        } else {
149                                $block['err_message'] = sprintf(_MB_XPRESS_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
150                        }
151                }
152
153                $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
154                $tpl =& new XoopsTpl() ;
155                $tpl->assign( 'block' , $block ) ;
156                $ret['content'] = $tpl->fetch( $templates_file ) ;
157                return $ret ;
158        }
159       
160        function xpress_block_cache_refresh()
161        {
162                global $xoops_db;
163                $mid = get_xpress_modid();
164                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
165                $blocks = $xoops_db->get_results($sql);
166                $mydirname = get_xpress_dir_name();
167                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
168
169                foreach($blocks as $block){
170                        $func_file = $block->func_file;
171                        $call_theme_function_name = str_replace(".php", "", $func_file);
172                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
173                        $cache_title = str_replace(".php", "", $func_file);
174                        $blockID = $block->bid;
175                        $options = explode("|", $block->options);
176
177                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
178                        require_once $block_theme_file;
179                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
180                        $render_array['block'] = $render;
181                        $render_array['block']['options'] = $block->options;
182                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
183                                $render_serialize = xpress_XML_serialize($render_array);
184                                $render_md5 = md5($render_serialize);
185
186                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
187                                $cache_md5 = md5($cache_serialize);
188                               
189                                if ($render_md5 != $cache_md5){
190                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
191                                }
192                        } else {
193                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
194                        }
195                }
196        }
197       
198        function xpress_unnecessary_block_cache_delete()
199        {
200                global $xoops_db,$xoops_config;
201                $mid = get_xpress_modid();
202                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
203                $blocks = $xoops_db->get_results($sql);
204                $mydirname = get_xpress_dir_name();
205                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
206
207                $pattern ='';
208                foreach($blocks as $block){
209                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
210                        if (!empty($pattern))  $pattern .= '|';
211                        $pattern .= $cache_file_name;
212                }
213                $pattern = '(' . $pattern . ')';
214               
215                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
216                $cache_time = 0;
217        if ($dh = opendir($cache_dir)) {
218            while (($file = readdir($dh)) !== false) {
219                if (preg_match('/^' . preg_quote($mydirname) . '/', $file)) {
220                        if(! preg_match('/' . $pattern . '/', $file)) {
221                        unlink($cache_dir.$file);
222                    }
223                }
224            }
225            closedir($dh);
226        }
227    }
228       
229?>
Note: See TracBrowser for help on using the repository browser.