Changeset 2104

Show
Ignore:
Timestamp:
04/25/08 12:34:53 (7 months ago)
Author:
takayama
Message:

Implemented BugId:71811, BugId:79438
* Added SSI support into the Dynamic Publishing
* Added cache option support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-36/php/lib/function.mtinclude.php

    r1554 r2104  
    2424    } 
    2525 
    26     static $_include_cache = array(); 
    2726    $blog_id = $args['blog_id']; 
    2827    $blog_id or $blog_id = $ctx->stash('blog_id'); 
    29     $cache_id = ''; 
    30  
    31     # Try to read from cache 
    32     global $mt; 
    33  
    34     $cache_enable = false; 
    35     $cacje_key = ''; 
    36     $cache_ttl = 0; 
    3728    $blog = $ctx->mt->db->fetch_blog($blog_id); 
    3829 
    39     if ((isset($args['module']) || isset($args['widget']) || isset($args['identifier'])) 
    40         && $blog['blog_include_cache'] == 1 
    41         && ((isset($args['cache']) && $args['cache'] == '1') || isset($args['key']) || isset($args['ttl'])) ) 
    42     { 
    43         $tmpl_name = $args['module']; 
    44         $tmpl_name or $tmpl_name = $args['widget']; 
    45         $tmpl_name or $tmpl_name = $args['identifier']; 
    46         $type = $args['Widget'] ? 'widget' : 'custom'; 
    47         if ($type == 'custom' && preg_match('/^Widget:/', $tmpl_name)) 
    48             $type = 'widget'; 
    49  
    50  
    51         $cache_enable = true; 
    52         $cache_key = isset($args['key']) 
    53             ? $args['key'] 
    54             : 'blog::' . $blog_id . '::template_' . $type  . '::' . $tmpl_name; 
    55         $cache_ttl = isset($args['ttl']) ? $args['ttl'] : 60 * 60; # default 60 min. 
    56  
    57  
    58         $cache_driver = $mt->cache_driver($cache_ttl); 
    59         $cached_val = $cache_driver->get($cache_key, $cache_ttl); 
    60         if (!empty($cached_val)) 
    61             return $cached_val; 
    62     } 
    63  
     30    // When the module name starts by 'Widget', it converts to 'Widget' from 'Module'. 
    6431    if (isset($args['module']) && ($args['module'])) { 
    6532        $module = $args['module']; 
     
    6936        } 
    7037    } 
    71     if (isset($args['module']) && ($args['module'])) { 
    72         $module = $args['module']; 
    73         $cache_id = 'module::' . $blog_id . '::' . $module; 
     38 
     39    // Fetch template meta data 
     40    $load_type = null; 
     41    $load_name = null; 
     42    if (isset($args['module'])) { 
     43        $load_type = 'custom'; 
     44        $load_name = $args['module']; 
     45    } elseif (isset($args['widget'])) { 
     46        $load_type = 'widget'; 
     47        $load_name = $args['widget']; 
     48    } elseif (isset($args['identifier'])) { 
     49        $load_type = 'identifier'; 
     50        $load_name = $args['identifier']; 
     51    } 
     52 
     53    $tmpl_meta = array(); 
     54    if (!empty($load_type)) { 
     55        $is_global = isset($args['global']) && $args['global'] ? 1 : 0; 
     56        $tmpl_meta = $ctx->mt->db->fetch_template_meta($load_type, $load_name, $blog_id, $is_global); 
     57    } 
     58 
     59    # Convert to phrase of PHP Include 
     60    $ssi_enable = false; 
     61    $include_file = ''; 
     62    if (!empty($load_type) && 
     63        isset($blog) && $blog['blog_include_system'] == 'php' && 
     64        ((isset($args['ssi']) && $args['ssi']) || (isset($tmpl_meta['include_with_ssi']) && $tmpl_meta['include_with_ssi']))) { 
     65 
     66        $ssi_enable = true; 
     67 
     68        // Generates include path using Key 
     69        $base_path = ''; 
     70        if (isset($args['key'])) { 
     71            $base_path = $args['key']; 
     72        } elseif(isset($args['cache_key'])) { 
     73            $base_path or $base_path = $args['cache_key']; 
     74        } 
     75        $include_path_array = _include_path($base_path); 
     76 
     77        require_once('MTUtil.php'); 
     78        $filename = dirify($tmpl_meta['template_name']); 
     79        $filename or $filename = 'template_' . $tmpl_meta['template_id']; 
     80        $filename .= '.'.$blog['blog_file_extension']; 
     81 
     82        $include_path = $blog['blog_site_path']; 
     83        if (substr($include_path, strlen($include_path) - 1, 1) != DIRECTORY_SEPARATOR) 
     84            $include_path .= DIRECTORY_SEPARATOR; 
     85        foreach ($include_path_array as $p) { 
     86            $include_path .= $p . DIRECTORY_SEPARATOR; 
     87        } 
     88        $include_file = $include_path . $filename; 
     89    } 
     90 
     91    # Try to read from cache 
     92    $cache_enable = false; 
     93    $cache_id = ''; 
     94    $cacje_key = ''; 
     95    $cache_ttl = 0; 
     96    if (!empty($load_type) && 
     97        isset($blog) && $blog['blog_include_cache'] == 1 && 
     98        ((isset($tmpl_meta['cache_expire_type']) && ($tmpl_meta['cache_expire_type'] == '1' || $tmpl_meta['cache_expire_type'] == '2')) || 
     99         ((isset($args['cache']) && $args['cache'] == '1') || isset($args['key']) || isset($args['cache_key']) || isset($args['ttl'])))) 
     100    { 
     101        global $mt; 
     102        $cache_enable = true; 
     103        $cache_key = isset($args['key']) 
     104            ? $args['key'] 
     105            : isset($args['cache_key']) 
     106                ? $args['cache_key'] 
     107                : 'blog::' . $blog_id . '::template_' . $load_type  . '::' . $load_name; 
     108 
     109        if (isset($args['ttl'])) 
     110            $cache_ttl = $args['ttl']; 
     111        elseif (isset($tmpl_meta['cache_expire_type']) && $tmpl_meta['cache_expire_type'] == '1') 
     112            $cache_ttl = $tmpl_meta['cache_expire_interval']; 
     113        else 
     114            $cache_ttl = 60 * 60; # default 60 min. 
     115 
     116        if (isset($tmpl_meta['cache_expire_type']) && $tmpl_meta['cache_expire_type'] == '2') { 
     117            $expire_types = preg_split('/,/', $tmpl_meta['cache_expire_event'], -1, PREG_SPLIT_NO_EMPTY); 
     118            if (!empty($expire_types)) { 
     119                $latest = $ctx->mt->db->get_latest_touch($blog_id, $expire_types); 
     120                if ($latest) { 
     121                    if ($ssi_enable) { 
     122                        $file_stat = stat($include_file); 
     123                        if ($file_stat) { 
     124                            $file_stamp = gmdate("Y-m-d H:i:s", $file_stat[9]); 
     125                            if (strtotime($latest) > strtotime($file_stamp)) 
     126                                $cache_ttl = 1; 
     127                        } 
     128                    } else { 
     129                      $cache_ttl = time() - strtotime($latest); 
     130                    } 
     131                } 
     132            } 
     133        } 
     134 
     135        if ($cache_ttl == 0 || (time() - strtotime($tmpl_meta['template_modified_on']) < $cache_ttl)) { 
     136            $cache_ttl = time() - strtotime($tmpl_meta['template_modified_on']); 
     137        } 
     138 
     139        $cache_driver = $mt->cache_driver($cache_ttl); 
     140        $cached_val = $cache_driver->get($cache_key, $cache_ttl); 
     141        if (!empty($cached_val)) { 
     142            _clear_vars($ctx, $ext_args); 
     143            if ($ssi_enable) { 
     144                if (file_exists($include_file) && is_readable($include_file)) { 
     145                  $content = file_get_contents($include_file); 
     146                  if ($content) 
     147                      return $content; 
     148                } 
     149            } else { 
     150                return $cached_val; 
     151            } 
     152        } 
     153    } 
     154 
     155    # Compile template 
     156    static $_include_cache = array(); 
     157    $_var_compiled = ''; 
     158 
     159    if (!empty($load_type)) { 
     160        $cache_id = $load_type . '::' . $blog_id . '::' . $load_name; 
    74161        if (isset($_include_cache[$cache_id])) { 
    75162            $_var_compiled = $_include_cache[$cache_id]; 
    76163        } else { 
    77             $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, 'custom', $args['global']); 
    78             if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    79                 $_include_cache[$cache_id] = $_var_compiled; 
    80             } else { 
     164            $tmpl = $ctx->mt->db->get_template_text($ctx, $load_name, $blog_id, $load_type, $args['global']); 
     165            if (!$ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    81166                _clear_vars($ctx, $ext_args); 
    82167                return $ctx->error("Error compiling template module '$module'"); 
    83168            } 
    84         } 
    85     } elseif (isset($args['widget']) && ($args['widget'])) { 
    86         $module = $args['widget']; 
    87         $cache_id = 'widget::' . $blog_id . '::' . $module; 
    88         if (isset($_include_cache[$cache_id])) { 
    89             $_var_compiled = $_include_cache[$cache_id]; 
    90         } else { 
    91             $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, 'widget', $args['global']); 
    92             if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    93                 $_include_cache[$cache_id] = $_var_compiled; 
    94             } else { 
    95                 _clear_vars($ctx, $ext_args); 
    96                 return $ctx->error("Error compiling template module '$module'"); 
    97             } 
    98         } 
    99     } elseif (isset($args['identifier']) && ($args['identifier'])) { 
    100         $module = $args['identifier']; 
    101         $cache_id = 'identifier::' . $blog_id . '::' . $module; 
    102         if (isset($_include_cache[$cache_id])) { 
    103             $_var_compiled = $_include_cache[$cache_id]; 
    104         } else { 
    105             $tmpl = $ctx->mt->db->get_template_text($ctx, $module, $blog_id, '', $args['global']); 
    106             if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    107                 $_include_cache[$cache_id] = $_var_compiled; 
    108             } else { 
    109                 _clear_vars($ctx, $ext_args); 
    110                 return $ctx->error("Error compiling template module '$module'"); 
    111             } 
     169            $_include_cache[$cache_id] = $_var_compiled; 
    112170        } 
    113171    } elseif (isset($args['file']) && ($args['file'])) { 
    114172        $file = $args['file']; 
    115         $base_filename = basename($file); 
    116         global $restricted_include_filenames; 
    117         if (array_key_exists(strtolower($base_filename), $restricted_include_filenames)) { 
    118             _clear_vars($ctx, $ext_args); 
    119             return ""; 
    120         } 
    121173        $cache_id = 'file::' . $blog_id . '::' . $file; 
    122174        if (isset($_include_cache[$cache_id])) { 
    123175            $_var_compiled = $_include_cache[$cache_id]; 
    124176        } else { 
    125             if (is_file($file) && is_readable($file)) { 
    126                 $contents = @file($file); 
    127                 $tmpl = implode('', $contents); 
    128             } else { 
    129                 $blog = $ctx->stash('blog'); 
    130                 if ($blog['blog_id'] != $blog_id) { 
    131                     $blog = $ctx->mt->db->fetch_blog($blog_id); 
    132                 } 
    133                 $path = $blog['blog_site_path']; 
    134                 if (!preg_match('!/$!', $path)) 
    135                     $path .= '/'; 
    136                 $path .= $file; 
    137                 if (is_file($path) && is_readable($path)) { 
    138                     $contents = @file($path); 
    139                     $tmpl = implode('', $contents); 
    140                 } else { 
    141                     _clear_vars($ctx, $ext_args); 
    142                     return $ctx->error("Could not open file '$file'"); 
    143                 } 
    144             } 
    145             if ($ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    146                 $_include_cache[$cache_id] = $_var_compiled; 
    147             } else { 
     177            $tmpl = _get_template_from_file($ctx, $file, $blog_id); 
     178            if (!$ctx->_compile_source('evaluated template', $tmpl, $_var_compiled)) { 
    148179                _clear_vars($ctx, $ext_args); 
    149180                return $ctx->error("Error compiling template file '$file'"); 
    150181            } 
     182            $_include_cache[$cache_id] = $_var_compiled; 
    151183        } 
    152184    } elseif (isset($args['type']) && ($args['type'])) { 
     
    188220    } 
    189221 
     222    if ($ssi_enable) { 
     223        $include_dir = dirname($include_file); 
     224        if (!file_exists($include_dir) && !is_dir($include_dir)) { 
     225            mkdir($include_dir, 0777, true); 
     226        } 
     227        if (is_writable($include_dir)) { 
     228            if ($h_file = fopen($include_file, 'w')) { 
     229                fwrite($h_file, $_contents); 
     230                fclose($h_file); 
     231            } 
     232        } 
     233    } 
     234 
    190235    return $_contents; 
     236} 
     237 
     238function _get_template_from_file ($ctx, $file, $blog_id) { 
     239    $base_filename = basename($file); 
     240    global $restricted_include_filenames; 
     241    if (array_key_exists(strtolower($base_filename), $restricted_include_filenames)) { 
     242        return ''; 
     243    } 
     244    if (is_file($file) && is_readable($file)) { 
     245        $contents = @file($file); 
     246        $tmpl = implode('', $contents); 
     247    } else { 
     248        $blog = $ctx->stash('blog'); 
     249        if ($blog['blog_id'] != $blog_id) { 
     250            $blog = $ctx->mt->db->fetch_blog($blog_id); 
     251        } 
     252        $path = $blog['blog_site_path']; 
     253        if (!preg_match('!/$!', $path)) 
     254            $path .= '/'; 
     255        $path .= $file; 
     256        if (is_file($path) && is_readable($path)) { 
     257            $contents = @file($path); 
     258            $tmpl = implode('', $contents); 
     259        } else { 
     260            return false; 
     261        } 
     262    } 
     263 
     264    return $tmpl; 
    191265} 
    192266 
     
    199273    $ctx->__stash['vars'] =& $vars; 
    200274} 
     275 
     276function _include_path($path) { 
     277    $path_array = array(); 
     278    if (preg_match('/^\//', $path)) { 
     279        $path_array = preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY); 
     280    } else { 
     281        $path_array = preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY); 
     282        global $mt; 
     283        array_unshift($path_array, $mt->config('IncludesDir')); 
     284    } 
     285    return $path_array; 
     286} 
    201287?> 
  • branches/release-36/php/lib/mtdb_base.php

    r2103 r2104  
    152152        $result = $this->get_results($sql, ARRAY_A); 
    153153        return $result; 
     154    } 
     155 
     156    function fetch_template_meta($type, $name, $blog_id, $global) { 
     157        if ($type === 'identifier') { 
     158            $col = 'template_identifier'; 
     159            $type_filter = ""; 
     160        } else { 
     161            $col = 'template_name'; 
     162            $type_filter = "and template_type='$type'"; 
     163        } 
     164        if (!isset($global)) { 
     165            $blog_filter = "and template_blog_id in (".$this->escape($blog_id).",0)"; 
     166        } elseif ($global) { 
     167            $blog_filter = "and template_blog_id=0"; 
     168        } else { 
     169            $blog_filter = "and template_blog_id=".$this->escape($blog_id); 
     170        } 
     171 
     172        $tmpl_name = $this->escape($name); 
     173 
     174        $sql = " 
     175            select 
     176                template_id, template_name, template_modified_on 
     177            from 
     178                mt_template 
     179            where 
     180                $col = '$tmpl_name' 
     181                $blog_filter 
     182                $type_filter 
     183            order by 
     184                template_blog_id desc"; 
     185        $row = $this->get_row($sql, ARRAY_A); 
     186        if (!$row) return ''; 
     187 
     188        $data = $this->get_meta('template', $row['template_id']); 
     189        return array_merge($row, $data); 
    154190    } 
    155191 
     
    30973133        $this->query($sql); 
    30983134    } 
     3135 
     3136    function get_latest_touch($blog_id, $types) { 
     3137        $type_user = false; 
     3138        if (is_array($types)) { 
     3139            $array = preg_grep('/author/', $types); 
     3140            if (!empty($array)) $type_user = true; 
     3141        } else { 
     3142            $type_user = $types == 'author'; 
     3143        } 
     3144 
     3145        $blog_filter = ''; 
     3146        if (!empty($blog_id)) { 
     3147            if ($type_user) 
     3148                $blog_filter = 'and touch_blog_id = 0'; 
     3149            else 
     3150                $blog_filter = 'and touch_blog_id = ' . $blog_id; 
     3151        } 
     3152 
     3153        $type_filter = ''; 
     3154        if (!empty($types)) { 
     3155            if ($type_user) { 
     3156                $type_filter = 'and touch_object_type ="author"'; 
     3157            } else { 
     3158                if (is_array($types)) { 
     3159                    foreach ($types as $type) { 
     3160                        if ($type_filter != '') $type_filter .= ','; 
     3161                        $type_filter .= "'$type'"; 
     3162                    } 
     3163                    $type_filter = 'and touch_object_type in (' . $type_filter . ')'; 
     3164                } else { 
     3165                    $type_filter = 'and touch_object_type ="' . $type_filter . '"'; 
     3166                } 
     3167            } 
     3168        } 
     3169 
     3170        $sql = " 
     3171            select 
     3172                touch_modified_on 
     3173            from 
     3174                mt_touch 
     3175            where 
     3176                1 = 1 
     3177                $blog_filter 
     3178                $type_filter 
     3179            order by 
     3180                touch_modified_on desc 
     3181            <LIMIT>"; 
     3182 
     3183        $sql = $this->apply_limit_sql($sql, 1); 
     3184        $result = $this->get_row($sql, ARRAY_N); 
     3185 
     3186        if (!empty($result)) 
     3187            return $result[0]; 
     3188 
     3189        return false; 
     3190    } 
     3191 
    30993192} 
  • branches/release-36/php/mt.php.pre

    r2029 r2104  
    363363        isset($cfg['timeoffset']) or 
    364364            $cfg['timeoffset'] = '__DEFAULT_TIMEZONE__'; 
     365        isset($cfg['includesdir']) or 
     366            $cfg['includesdir'] = 'includes_c'; 
    365367    } 
    366368