aalfiann/sitemap-manager

一个用于动态管理静态网站的 sitemap 的 PHP 类。

1.3.1 2019-04-27 08:36 UTC

This package is auto-updated.

Last update: 2024-09-27 21:09:34 UTC


README

Version Total Downloads License

一个用于动态管理静态网站的 sitemap 的 PHP 类。

背景

此 SitemapManager 类允许您在静态 sitemap 中进行 CRUD 操作,如插入和更新 URL。我创建此类是因为我需要管理小型网站的 sitemap,这些网站是静态的、基于文件的或未使用任何数据库引擎。

安装

通过 Composer 安装此包。

composer require "aalfiann/sitemap-manager:^1.0"

示例用法

sitemap 索引

创建空白自定义 sitemap 索引

在使用此类之前需要 sitemap 文件,因此如果您服务器上没有现有的 sitemap 文件,您必须创建一个。

require 'vendor/autoload.php';
use \SitemapManager\SitemapIndex;

$sm = new SitemapIndex;

// Create blank custom sitemap index
$sm->path = 'sitemap-index.xml';
$sm->create();

自动将 sitemap 插入到 sitemap 索引中

// Set the first sitemap file
$sm->path = 'sitemap-index.xml';
// Set the last sitemap file (will increment automatically)
$sm->setLastFile();

// Check the url in all sitemap files
$url = 'http://yourdomain.com/sitemap-test.xml';
if(!$sm->find($url)){
    $sm->addBlock($url)
        ->addLastMod(date('Y-m-d'))
        ->save();
}

直接将多个 sitemap 添加到 sitemap 索引中

注意

  • 请确保您知道 sitemap 的限制
  • 达到限制时,入队将停止
$sm->path = 'sitemap-index.xml';
for ($i=1;$i<10;$i++){
    $sm->addBlock('http://yourdomain.com/sitemap-test-'.$i.'.xml')
        ->addLastMod(date('Y-m-d'))
        ->enqueue();
}
$sm->save();

动态更新 sitemap 索引中的 sitemap

$url = 'http://yourdomain.com/sitemap-test-5.xml';
// Set the default path sitemap (required for finding range sitemap files)
$sm->path = 'sitemap-index.xml';
// Find the sitemap files if you not sure where is url located
$path = $sm->find($url,false);
if(!empty($path)){
    $sm->path = $path;
    $sm->setBlock($url)
        ->unsetLastMod()
        ->update();
}

直接更新 sitemap 索引中的 sitemap

注意:请确保您已经知道 sitemap 文件中 sitemap URL 的位置。

$url = 'http://yourdomain.com/sitemap-test-4.xml';
// Set the path sitemap
$sm->path = 'sitemap-index.xml';
$sm->setBlock($url)
    ->unsetLastMod()
    ->update();

从 sitemap 索引中删除 sitemap

$sm->path = 'sitemap-index.xml';
$sm->delete('http://yourdomain.com/sitemap-test-3.xml');

从 sitemap 索引中直接删除多个 sitemap

$sm->path = 'sitemap-index.xml';
for($i=5;$i<10;$i++){
    $sm->prepareDelete('http://yourdomain.com/sitemap-test-'.$i.'.xml')->enqueue();
}
$sm->save();

删除 sitemap 文件

$sm->path = 'sitemap-index.xml';
$sm->deleteFile();

生成 Sitemap.xml

// Generate All sitemap index into file sitemap.xml
// Note: You need a cronjob to make this refreshed automatically
$sm->generate('https://yourdomain.com');

// Generate All sitemap include sub directories (path without trailing slash)
$sm->generate('https://yourdomain.com',['sitemap-folder-1','sitemap/folder-2']);

// Generate All sitemap index into string
echo $sm->generate('https://yourdomain.com','',false);

Sitemap Urlset

创建空白自定义 sitemap urlset

在使用此类之前需要 sitemap 文件,因此如果您服务器上没有现有的 sitemap 文件,您必须创建一个。

require 'vendor/autoload.php';
use \SitemapManager\Sitemap;

$sm = new Sitemap;

// Create blank custom sitemap urlset
$sm->path = 'sitemap-post.xml';
$sm->create();

自动将 URL 插入到 sitemap urlset 中

// Set the first sitemap file
$sm->path = 'sitemap-post.xml';
// Set the last sitemap file (will increment automatically)
$sm->setLastFile();
// Check the url in all sitemap files
$url = 'http://yourdomain.com/test-suka-suka-aja-13';
if(!$sm->find($url)){
    $sm->addBlock($url)
        ->addChangeFreq('monthly')
        ->addLastMod(date('Y-m-d'))
        ->addPriority(0.9)
        ->save();
}

直接将多个 URL 添加到 sitemap urlset 中

注意

  • 请确保您知道 sitemap 的限制
  • 达到限制时,入队将停止
$sm->path = 'sitemap-post.xml';
for ($i=0;$i<10;$i++){
    $sm->addBlock('http://yourdomain.com/test-suka-suka-aja-'.$i)
        ->addChangeFreq('monthly')
        ->addLastMod(date('Y-m-d'))
        ->addPriority(0.9)
        ->enqueue();
}
$sm->save();

动态更新 sitemap urlset 中的 URL

$url = 'http://yourdomain.com/test-suka-suka-aja-7';
// Set the default path sitemap (required for finding range sitemap files)
$sm->path = 'sitemap-post.xml';
// Find the sitemap files if you not sure where is url located
$path = $sm->find($url,false);
if(!empty($path)){
    $sm->path = $path;
    $sm->setBlock($url)
        ->setChangeFreq('daily')
        ->setLastMod(date('Y-m-d'))
        ->setPriority(0.5)
        ->update();
}

直接更新 sitemap urlset 中的 URL

注意:请确保您已经知道 sitemap 文件中 sitemap URL 的位置。

$url = 'http://yourdomain.com/test-suka-suka-aja-7';
// Set the path sitemap
$sm->path = 'sitemap-post.xml';
$sm->setBlock($url)
    ->setChangeFreq('monthly')
    ->setLastMod(date('Y-m-d'))
    ->setPriority(0.9)
    ->update();

从 sitemap urlset 中删除 URL

$sm->path = 'sitemap-post.xml';
$sm->delete('http://yourdomain.com/test-suka-suka-aja-7');

从 sitemap urlset 中直接删除多个 URL

$sm->path = 'sitemap-post.xml';
for($i=0;$i<10;$i++){
    $sm->prepareDelete('http://yourdomain.com/test-suka-suka-aja-'.$i)->enqueue();
}
$sm->save();

如何贡献

拉取请求

  1. sitemap-manager 仓库分叉
  2. 为每个功能或改进创建一个新分支
  3. 从每个功能分支向 develop 分支发送拉取请求