forked from himiklab/yii2-sitemap-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sitemap.php
144 lines (118 loc) · 4.05 KB
/
Sitemap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* @link https://github.com/himiklab/yii2-sitemap-module
* @copyright Copyright (c) 2014 HimikLab
* @license http://opensource.org/licenses/MIT MIT
*/
namespace katech91\sitemap;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Module;
use yii\caching\Cache;
/**
* Yii2 module for automatically generating XML Sitemap.
*
* @author HimikLab
* @package katech91\sitemap
*/
class Sitemap extends Module
{
//About limit - https://support.google.com/webmasters/answer/75712
const URLS_ON_PAGE = 1000;
public $controllerNamespace = 'katech91\sitemap\controllers';
/** @var int */
public $cacheExpire = 86400;
/** @var Cache|string */
public $cacheProvider = 'cache';
/** @var string */
public $cacheKey = 'sitemap';
/** @var boolean Use php's gzip compressing. */
public $enableGzip = false;
/** @var array */
public $models = [];
/** @var array */
public $urls = [];
public function init()
{
parent::init();
if (is_string($this->cacheProvider)) {
$this->cacheProvider = Yii::$app->{$this->cacheProvider};
}
if (!$this->cacheProvider instanceof Cache) {
throw new InvalidConfigException('Invalid `cacheKey` parameter was specified.');
}
}
/**
* Build and cache a site map.
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function buildSitemap($name = '', $page = null)
{
/** @var behaviors\SitemapBehavior $model */
if (is_array($this->models)) {
$model = new $this->models['class'];
if (isset($this->models['behaviors'])) {
$model->attachBehaviors($this->models['behaviors']);
}
} else {
$model = new $this->models;
}
$sitemapData = $this->generateSitemap($model, $name, $page);
$this->cacheProvider->set($this->cacheKey . $name . $page ? : '', $sitemapData, $this->cacheExpire);
return $sitemapData;
}
public function buildSitemapIndex()
{
$maps = [];
if (!empty($this->urls)) {
$maps[] = 'urls';
}
$maps += array_merge($maps, array_keys($this->models));
foreach ($maps as $item) {
$urls[]['loc'] = '/sitemap_' . $item . '.xml';
}
$sitemapData = $this->createControllerByID('default')->renderPartial('index', [
'urls' => $urls
]);
$this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire);
return $sitemapData;
}
public function buildSitemapUrl($name)
{
$urls = $this->urls;
$sitemapData = $this->createControllerByID('default')->renderPartial('sitemap', [
'urls' => $urls
]);
$this->cacheProvider->set($this->cacheKey, $sitemapData, $this->cacheExpire);
return $sitemapData;
}
private function generateSitemap($model, $name = '', $page = null)
{
$countUrls = $model::find()->count();
if ($countUrls > self::URLS_ON_PAGE) {
if (is_numeric($page) && $page >= 0) {
//Create a page of sitemaps
$urls = $model->generateSiteMap(self::URLS_ON_PAGE, $page);
$sitemapData = $this->createControllerByID('default')->renderPartial('sitemap', [
'urls' => $urls
]);
} else {
//Create common sitemap
$pages = floor($countUrls / self::URLS_ON_PAGE);
foreach (range(0, $pages) as $index) {
$urls[]['loc'] = '/sitemap_' . $name . '_' . $index . '.xml';
}
$sitemapData = $this->createControllerByID('default')->renderPartial('index', [
'urls' => $urls
]);
}
} else {
$urls = $model->generateSiteMap();
$sitemapData = $this->createControllerByID('default')->renderPartial('sitemap', [
'urls' => $urls
]);
}
return $sitemapData;
}
}