Yii2命令行下百度sitemap生成xml文件以及索引文件

<?php/** * * User: guo * Date: 2019-08-19 */namespace console\controllers;use backend\models\Article;use backend\models\Question;use yii\console\Controller;use yii\helpers\FileHelpe...
摘要由CSDN通过智能技术生成
<?php
/**
 *
 * User: guo 
 * Date: 2019-08-19
 */


namespace console\controllers;


use backend\models\Article;
use backend\models\Question;
use yii\console\Controller;
use yii\helpers\FileHelper;

class SitemapMobileController extends Controller
{
    //文章xml文件名
    public $article_file_name = 'baidmb_article_';
    //问答xml文件名
    public $question_file_name = 'baidmb_question_';

    //sitemap类型
    public $dataType = [
        'article',
        'question',
    ];

    //索引文件名
    public $index_file_name = 'baidmb.xml';
    //文件目录
    public $base_dir;
    //cdn地址
    public $cdn_url_prefix;
    //PC端地址
    public $pc_host = 'https://www.demo.net/';
    //M端地址
    public $m_host = 'https://m.demo.net/';
    //每页查询数据量
    public $page_size = 1000;

    //文件大小,10M
    public $max_file_size = 10485760;
    //每个文件最大链接数
    public $max_url_count = 10;

    /**
     * 初始化
     * Author: guo 
     * Date: 2019-08-19
     */
    public function init()
    {
        date_default_timezone_set('PRC');
        $this->base_dir = rtrim(\Yii::$app->params['upload_dir'], '/') . '/mobile/baidu_sitemap/';
        $this->cdn_url_prefix = rtrim(\Yii::$app->params['cdn_url_prefix'], '/');
    }

    /**
     * 初始化xml文件保存目录
     * Author: guo 
     * Date: 2019-08-19
     */
    public function initDir()
    {
        $savePath = rtrim($this->base_dir, '/');

        //创建目录
        if (!is_dir($savePath)) {
            FileHelper::createDirectory($savePath, 0777, true);
        }
        //判断目录是否可写
        if (!is_writable($savePath)) {
            $this->stderr($savePath . "目录没有写的权限");
        }
    }

    /**
     * 格式化内存使用单位
     * Author: guo 
     * Date: 2019-08-19
     * @param $size
     * @return string
     */
    private function convert($size)
    {
        $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');

        return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
    }

    /**
     * 创建站点地图
     * Author: guo 
     * Date: 2019-08-19
     * @param $dataType
     * @param $filename
     */
    private function buildXml($dataType, $filename)
    {
        $mem_start = memory_get_usage(true);
        echo "初始使用内存:" . $this->convert($mem_start) . "\n";
        $time_start = microtime(true);
        //初始化目录
        $this->initDir();

        //数据集合,如果数据量大,需要设置合理内存
        $list = [];

        //接口分页数
        $page = 1;
        //每个xml文件包含数据量计数器
        $eachFileCount = 0;
        //文件号码
        $fileNo = 1;

        do {
         
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值