File: /web/data/www.tbbprovision.com/application/admin/controllers/SitemapController.class.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/5/25
* Time: 16:02
*/
class SitemapController extends Controller
{
public function __construct($controller = '', $action = '', $module = '')
{
parent::__construct($controller, $action, $module);
$this->check_manage_login();
$this->check_permission();
$this->manage_init();
}
public function list_action()
{
try {
$save_dir = APP_PATH.'/sitemaps/';
$file_list = array();
$file_list[0]['filename'] = 'sitemap.xml';
$file_list[0]['filesize'] = f::file_size_format(filesize(APP_PATH.'/sitemap.xml'));
$file_list[0]['create_time'] = f::date(filectime(APP_PATH.'/sitemap.xml'));
$file_list[0]['last_time'] = f::date(filemtime(APP_PATH.'/sitemap.xml'));
$file_list[0]['path'] = '/sitemap.xml';
$handler = opendir($save_dir);
$i=1;
while($row = readdir($handler)){
if($row=='' || $row=='.' || $row=='..'){
continue;
}
$file_list[$i]['counts'] = preg_match_all("/<loc>([\s\S]*)<\/loc>/iU",file_get_contents($save_dir.$row));
$file_list[$i]['filename'] = $row;
$file_list[$i]['filesize'] = f::file_size_format(filesize($save_dir.$row));
$file_list[$i]['create_time'] = f::date(filectime($save_dir.$row));
$file_list[$i]['last_time'] = f::date(filemtime($save_dir.$row));
$file_list[$i]['path'] = '/sitemaps/'.$row;
$i++;
}
$this->assign('filelist',$file_list);
$this->render();
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 产品详情地图更新
*/
public function update_pro_sitemap_action()
{
try {
set_time_limit(0);
$domain = APP_URL;
$pro_row_coount = $this->model('product')->table('pro')->where('is_show=1')->get_row_count();
//每个文件10000秫记录
$page_count = 10000;
$total_page = ceil($pro_row_coount / $page_count);
for($i=0;$i<$total_page;$i++){
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.9/sitemap.xsd">';
//计算总共需要查询几次数据库
$sub_total_page = ceil($page_count / 2000);
for($j=0;$j<$sub_total_page;$j++){
$start = $i*$page_count+($j*2000);
$list = array();
$list = $this->model('product')->table('pro')->where('is_show=1')->order('proid asc')->field(array('proid','name'))->limit($start,2000)->selectAll();
if(!$list) break;
foreach((array)$list as $v){
$url = '';
$url = Route::get_url('product_detail',$v);
echo "\n".'<url><loc>' . $domain.$url . '</loc><lastmod>'.date(DATE_ATOM,WEB_TIME).'</lastmod></url>';
}
}
echo '</urlset>';
$xml = ob_get_contents();
ob_clean();
file::write_file('/sitemaps/',"sitemap_prodetail_{$i}.xml",$xml);
$xml = '';
unset($xml);
}
//更新主文件
$this->update_main_sitemap();
f::e_json("更新站点地图文件成功");
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* method name
*/
public function update_article_sitemap_action()
{
try {
set_time_limit(0);
$domain = APP_URL;
$article_list = $this->model('article')->list_json();
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.9/sitemap.xsd">';
foreach((array)$article_list as $v){
if(substr_count($v['url'],'http')>0 && substr_count($v['url'],$domain)<=0){
continue;
}
$url = $domain.Route::get_url('article_detail',$v);
echo "\n".'<url><loc>' . $url . '</loc><lastmod>'.date(DATE_ATOM,WEB_TIME).'</lastmod></url>';
}
echo '</urlset>';
$xml = ob_get_contents();
ob_clean();
file::write_file('/sitemaps/',"article.xml",$xml);
$xml = '';
unset($xml);
//更新主文件
$this->update_main_sitemap();
f::e_json("更新站点地图文件成功");
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* method name
*/
public function update_news_sitemap_action()
{
try {
set_time_limit(0);
$domain = APP_URL;
$news_list = $this->model('news')->where('audit=1')->field(array('id','title','cate_id','add_time','views','from_source','audit'))->selectAll();
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.9/sitemap.xsd">';
foreach((array)$news_list as $v){
if(substr_count($v['url'],'http')>0 && substr_count($v['url'],$domain)<=0){
continue;
}
$url = $domain.Route::get_url('info_detail',$v);
echo "\n".'<url><loc>' . $url . '</loc><lastmod>'.date(DATE_ATOM,WEB_TIME).'</lastmod></url>';
}
echo '</urlset>';
$xml = ob_get_contents();
ob_clean();
file::write_file('/sitemaps/',"info.xml",$xml);
$xml = '';
unset($xml);
//更新主文件
$this->update_main_sitemap();
f::e_json("更新站点地图文件成功");
} catch (Exception $e) {
exit($e->getMessage());
}
}
/**
* 更新主站点地图文件
*/
private function update_main_sitemap(){
$domain = APP_URL;
$save_dir = APP_PATH.'/sitemaps/';
$handler = opendir($save_dir);
ob_start();
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
while($row = readdir($handler)){
if($row=='' || $row=='.' || $row=='..'){
continue;
}
echo "\n<sitemap><loc>{$domain}/sitemaps/{$row}</loc><lastmod>".date(DATE_ATOM,WEB_TIME)."</lastmod></sitemap>";
}
echo "\n</sitemapindex>";
$xml = ob_get_contents();
ob_clean();
file::write_file('/',"sitemap.xml",$xml);
unset($xml);
}
}