静态化新思路 |
||||
---|---|---|---|---|
网站管理员
注册日期:
2005/2/24 15:17 所属群组:
网站管理员 注册会员 技术文档组 等级: 24; EXP: 37
HP: 0 / 584 MP: 241 / 11148 |
/** * 实现静态化处理 * * @author nightcat <[email protected]> * @copyright ver 1.0 2009-12-20 * * 在include/common.php增加set_magic_quotes_runtime(0)下面 * global $html; * $html = new MakeHTML(); * $html->analyzeURL(); * * 在footer.php增加 * // 静态处理输出 * $GLOBALS['html']->toHtml(); * * * @todo 实现插件模式 * * */ class MakeHTML { public $root_path = ""; public $url_path = "" ; public static $file = array() ; public function __construct() { // 判断是否有跟目录,没有则增加 $path = XOOPS_ROOT_PATH . '/html'; $this->root_path = $path ; if ( !is_dir($path) ) { mkdir($path, 0777); } $this->url_path = XOOPS_URL .'/html'; return true; } public function makeFolder($folder) { $path = $this->root_path . '/' . $folder; if(mkdir($path, 0777)){ return true; } else { return false; } } public function analyzeURL() { $this->getRule($_SERVER['REQUEST_URI']); $file = $this->file; // 是否作静态化 if ( $file['html'] ) { $file_path = $file['path'].'/'.$file['name']; // echo var_dump($file); // exit(); if ( $this->checkExpire($file_path) && $this->checkFile($file_path)){ header("Location:".$file['url']); exit(); } // 启动内容捕取 ob_start(); } return true; } public function getRule($uri) { //TODO 利用插件方式来得到不同的匹配规则 // 首页 $uri_arr = explode('/', trim($uri, '/')); // if( (count($uri_arr) <= 1 && empty($uri_arr[0])) || // (count($uri_arr) <= 1 && !empty($uri_arr[0]) && $uri_arr[0] == 'index.php' ) ){ // $file['html'] = 1 ; // $file['path'] = $this->root_path; // $file['name'] = 'index.html'; // $file['url'] = XOOPS_URL . '/html/' . $file['name']; // } // 文章页面 if ( $uri_arr[1] = "article" ) { // 文章首页 if( $uri_arr[2] == "index.home.php" ) { $path = $this->root_path . '/' . $uri_arr[1]; if ( !is_dir($path) ) { $this->makeFolder($uri_arr[1]); } $file['html'] = 1 ; $file['path'] = $path; $file['name'] = 'index.html'; if ( $uri_arr[1] ) { $file['url'] = $this->url_path .'/'. $uri_arr[1] . "/" . $file['name']; } } // 文章内容页 if( $uri_arr[2] == "view.article.php" ) { $path = $this->root_path . '/' . $uri_arr[1]; if ( !is_dir($path) ) { $this->makeFolder($uri_arr[1]); } $article_id = $uri_arr[count($uri_arr)-1]; $file['html'] = 1 ; $file['path'] = $path; $file['name'] = $article_id.'.html'; if ( $uri_arr[1] ) { $file['url'] = $this->url_path .'/'. $uri_arr[1] . "/" . $file['name']; } } } $this->file = $file ; return $file ; } public function initial() { } public function toHtml() { $file = $this->file ; if ( $file['html'] ) { // 取得页面内容 $html = ob_get_contents(); ob_end_clean(); // 写入文件 $file_path = $file['path'].'/'.$file['name']; if ( !$this->checkFile($file_path) || !$this->checkExpire($file_path) ){ // 处理页面内容 $html = $this->handleHTML($html); $this->writeToFile($file_path, $html) ; } // 显示 echo $html ; } } public function handleHTML($html) { // 去掉头部的登陆信息 $html = preg_replace("/( ]*id=\"top\"[^>]*>)(([^<]+|<(?!\/?div[^>]*>)| ]*>(?>(?3))*<\/div>)*)(<\/div>)/si", "", $html);
return $html ; } public function checkExpire() { return true; } public function checkFile($path) { return file_exists($path); } public function writeToFile($path, $html) { file_put_contents($path, $html); return true; } } ?> 简单实现article模块的静态化。这个类实现的功能还比较单一,可以扩展成插件模式,针对不同模块和URL生成不同的静态页面URL。 linux系统,在根目录下建立html目录,并赋予权限可写 例子: http://www.zucai310.com/modules/article/index.home.php 生成 http://www.zucai310.com/html/article/index.html http://www.zucai310.com/modules/article/view.article.php/14679 生成 http://www.zucai310.com/html/article/14679.html
2009/12/23 19:01
|
|||
_________________
nightcat 小站 不同时刻的访问,有不同的惊喜 小站已经改版,欢迎有事没事来踩踩,内容丰富不少,希望你有喜欢的, Gtalk:[email protected] MSN:[email protected] QQ:1043931 |
||||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
资深会员
注册日期:
2007/3/16 22:40 所属群组:
注册会员
帖子:
314
等级: 16; EXP: 56
HP: 0 / 389 MP: 104 / 5090 |
学习了
2009/12/23 21:53
|
|||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
Support Team
注册日期:
2007/7/16 17:06 来自 彭城/上海
所属群组:
网站管理员 注册会员 Dev+Hack 资料整理组 等级: 20; EXP: 62
HP: 0 / 490 MP: 166 / 5937 |
zucai的这个网站,好强啊
2009/12/24 9:17
|
|||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
Support Team
注册日期:
2006/11/30 20:48 来自 beijing
所属群组:
网站管理员 注册会员 Dev+Hack 资料整理组 等级: 30; EXP: 23
HP: 146 / 730 MP: 401 / 10179 |
链接中的url怎么办,虽然生成了html,但是没有办法应用啊。
2009/12/24 15:43
|
|||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
Support Team
注册日期:
2007/7/16 17:06 来自 彭城/上海
所属群组:
网站管理员 注册会员 Dev+Hack 资料整理组 等级: 20; EXP: 62
HP: 0 / 490 MP: 166 / 5937 |
直接写个smarty模板的插件,把URL处理成静态之后的URL,
或者 apache的rewrite重写到html地址里 这两盅方法,应该可行
2009/12/24 16:39
|
|||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
高级会员
注册日期:
2009/2/11 16:36 来自 北京
所属群组:
注册会员
帖子:
88
等级: 8; EXP: 35
HP: 0 / 183 MP: 29 / 1377 |
学习了,支持一下!
2009/12/25 4:35
|
|||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
网站管理员
注册日期:
2005/2/24 15:17 所属群组:
网站管理员 注册会员 技术文档组 等级: 24; EXP: 37
HP: 0 / 584 MP: 241 / 11148 |
URL 优化可以利用重写
我这个不需要了,现在需要先访问再自动生成静态页面,再次访问就自动指向静态页面。 新改了一下,现在支持插件模式,文章部分采用日期作子分类 http://www.zucai310.com/html/article/200912/14808.html
2009/12/25 20:38
|
|||
_________________
nightcat 小站 不同时刻的访问,有不同的惊喜 小站已经改版,欢迎有事没事来踩踩,内容丰富不少,希望你有喜欢的, Gtalk:[email protected] MSN:[email protected] QQ:1043931 |
||||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
网站管理员
注册日期:
2005/2/24 15:17 所属群组:
网站管理员 注册会员 技术文档组 等级: 24; EXP: 37
HP: 0 / 584 MP: 241 / 11148 |
<?php
/** * 实现静态化处理 * * @author nightcat <[email protected]> * @copyright ver 1.0 2009-12-20 * * 在include/common.php增加set_magic_quotes_runtime(0)下面 * global $html; * $html = new MakeHTML(); * $html->analyzeURL(); * * 在footer.php增加 * // 静态处理输出 * $GLOBALS['html']->toHtml(); * * * @todo 实现插件模式 * * */ class MakeHTML { public $root_path = ""; public $url_path = "" ; public static $file = array() ; /** * 构造函数 */ public function __construct() { // 判断是否有跟目录,没有则增加 $path = XOOPS_ROOT_PATH . '/html'; $this->root_path = $path ; if ( !is_dir($path) ) { mkdir($path, 0777); } $this->url_path = XOOPS_URL .'/html'; $this->modules = array('article'); $this->url_arr = explode('/', trim($_SERVER['REQUEST_URI'], '/')); return true; } /** * 生成目录 */ public function makeFolder($folder) { $path = $this->root_path . '/' . $folder; if(mkdir($path, 0777)){ return true; } else { return false; } } /** * 解析URL地址,判断是否需要静态化,生成静态页面,作静态页面跳转 */ public function analyzeURL() { $this->getRule(); $file = $this->file; // 是否作静态化 if ( $file['html'] ) { $file_path = $file['path'].'/'.$file['name']; if ( $this->checkExpire($file_path) && $this->checkFile($file_path)){ header("Location:".$file['url']); exit(); } // 启动内容捕取 ob_start(); } return true; } /** * 静态规则,插件模式,在不同的模块class下生成html.php文件 * 继承HtmlRuel类,写自己的实现方式 */ public function getRule() { //TODO 利用插件方式来得到不同的匹配规则 //访问模块下class目录下的html.php文件 $uri_arr = $this->url_arr; $modules = $this->modules; foreach ( $modules as $module ) { if ( $module == $uri_arr[1] ) { $class_path = XOOPS_ROOT_PATH . '/modules/' . $module . '/class/html.php'; if ( is_file($class_path) ) { require_once $class_path; $class = ucfirst($module)."Html"; $obj = new $class; $file = $obj->getRule($uri_arr); } } } $this->file = $file ; return $file ; } public function initial() { } public function toHtml() { $file = $this->file ; if ( $file['html'] ) { // 取得页面内容 $html = ob_get_contents(); ob_end_clean(); // 写入文件 $file_path = $file['path'].'/'.$file['name']; if ( !$this->checkFile($file_path) || !$this->checkExpire($file_path) ){ // 处理页面内容 $html = $this->handleHTML($html); $this->writeToFile($file_path, $html) ; } // 显示 echo $html ; } } public function handleHTML($html) { $file = $this->file; $uri_arr = $this->url_arr; $modules = $this->modules; $html = self::handlerCommonHTML($html); foreach ( $modules as $module ) { $class_path = XOOPS_ROOT_PATH . '/modules/' . $module . '/class/html.php'; if ( $module == $uri_arr[1] && is_file($class_path) ) { require_once $class_path; $class = ucfirst($module)."Html"; $obj = new $class; $html = $obj->handleHTML($file, $html); } } return $html ; } public function handlerCommonHTML($html) { // 去掉头部的登陆信息 $html = preg_replace("/(<div[^>]*id=\"top\"[^>]*>)(([^<]+|<(?!\/?div[^>]*>)|<div[^>]*>(?>(?3))*<\/div>)*)(<\/div>)/si", "", $html); return $html ; } public function checkExpire() { return true; } public function checkFile($path) { return file_exists($path); } public function writeToFile($path, $html) { file_put_contents($path, $html); return true; } } /** * 静态规则类,供不同模块继承 * @author ncnynl <[email protected]> * */ class HtmlRule { /** * 静态根目录 */ public static $root_path; /** * URL目录 */ public static $url_path; /** * 构造函数 */ public function __construct() { // 判断是否有跟目录,没有则增加 $path = XOOPS_ROOT_PATH . '/html'; self::$root_path = $path ; if ( !is_dir($path) ) { mkdir($path, 0777); } self::$url_path = XOOPS_URL .'/html'; return true; } /** * 生成目录 */ public function makeFolder($folder) { $path = self::$root_path . '/' . $folder; if(mkdir($path, 0777)){ return true; } else { return false; } } /** * 需要重写,实现静态规则 */ public function getRule(); /** * 需要重写,实现删除静态页面 */ public function deleteHtml($id); /** * 需要重写,实现对静态内容过滤 */ public function handleHTML($html); } ?> <?php /** * 文章模块的静态化规则 * * @author ncnynl <[email protected]> * * * 保存路径如: * html/article/200912/1235.html * * 根据文章的发布日期来保存 * */ class ArticleHtml extends HtmlRule { /** * 取得生成静态页面的规则 */ public function getRule($uri_arr) { $file = ""; // 文章内容页 switch ($uri_arr[2]){ case "view.article.php": // 模块目录 $path = parent::$root_path . '/' . $uri_arr[1]; if ( !is_dir($path) ) { parent::makeFolder($uri_arr[1]); } // 取得文章ID $article_id = $uri_arr[count($uri_arr)-1]; if(substr($article_id,0,1) == 'p' || substr($article_id,0,1) == 'c'){ $article_id = $uri_arr[count($uri_arr)-2]; } elseif ( substr($article_id,0,7) == 'article' ) { $article_id = substr($article_id,8); } // 子目录 $datestr = self::getArticle($article_id); $subpath = $path . "/" . $datestr; if ( !is_dir($subpath) ) { parent::makeFolder($uri_arr[1]."/".$datestr); } // 组合信息 $file['html'] = 1 ; $file['path'] = $subpath; $file['name'] = $article_id.'.html'; $file['id'] = $article_id; if ( $uri_arr[1] ) { $file['url'] = parent::$url_path .'/'. $uri_arr[1] . "/" .$datestr . "/" . $file['name']; } break; } return $file; } public function getArticle($id) { $pdo = XoopsPDO::getInstance(); $sql = "SELECT `art_time_publish` as time FROM " . XoopsPDO::prefix('art_article') . " WHERE art_id={$id}"; $re = $pdo->getResult($sql); return date('Ym', $re[0]->time); } public function deleteHtml($id) { $datestr = self::getArticle($id); $file = parent::$root_path."/article/{$datestr}/{$id}.html"; if(is_file($file)){ unlink($file); } return true; } public function handleHTML($file, $html) { // 增加点击统计 $clickstr = '<div><img src="'.XOOPS_URL.'/modules/article/counter.php?article='.$file['id'].'" width="1px" height="1px" /></div>'; if ( !preg_match('/counter.php/', $html) ) { $html = preg_replace("/<!-- Test Comment Service Begin -->/", $clickstr, $html); } // 增加管理接口 $managestr = '<div>API:<a href="'.XOOPS_URL.'/modules/article/edit.article.php?article='.$file['id'].'"><strong>管理</strong></a></div>'; if ( !preg_match('/API:/', $html) ) { $html = preg_replace("/<!-- Test Comment Service End -->/", $managestr, $html); } return $html; } } ?>
2009/12/25 20:49
|
|||
_________________
nightcat 小站 不同时刻的访问,有不同的惊喜 小站已经改版,欢迎有事没事来踩踩,内容丰富不少,希望你有喜欢的, Gtalk:[email protected] MSN:[email protected] QQ:1043931 |
||||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
网站管理员
注册日期:
2005/2/24 15:17 所属群组:
网站管理员 注册会员 技术文档组 等级: 24; EXP: 37
HP: 0 / 584 MP: 241 / 11148 |
这个静态化的类,现基本功能都实了,有兴趣自己试试
效果点www.zucai310.com里面的文章部分看看
2009/12/25 20:50
|
|||
_________________
nightcat 小站 不同时刻的访问,有不同的惊喜 小站已经改版,欢迎有事没事来踩踩,内容丰富不少,希望你有喜欢的, Gtalk:[email protected] MSN:[email protected] QQ:1043931 |
||||
|
回复: 静态化新思路 |
||||
---|---|---|---|---|
网站管理员
注册日期:
2005/2/24 15:17 所属群组:
网站管理员 注册会员 技术文档组 等级: 24; EXP: 37
HP: 0 / 584 MP: 241 / 11148 |
引用:
怎么没作用? 你第二次访问,就自动指向静态页面了,不需要更改原来的链接。
2009/12/25 20:53
|
|||
_________________
nightcat 小站 不同时刻的访问,有不同的惊喜 小站已经改版,欢迎有事没事来踩踩,内容丰富不少,希望你有喜欢的, Gtalk:[email protected] MSN:[email protected] QQ:1043931 |
||||
|