|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\QueuedJobs\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Page; |
|
7
|
|
|
use SilverStripe\CMS\Model\ErrorPage; |
|
8
|
|
|
use SilverStripe\Control\Director; |
|
9
|
|
|
use SilverStripe\ORM\DB; |
|
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
11
|
|
|
use SilverStripe\ORM\Versioning\Versioned; |
|
12
|
|
|
use SilverStripe\QueuedJobs\Services\AbstractQueuedJob; |
|
13
|
|
|
use SilverStripe\QueuedJobs\Services\QueuedJob; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A job for generating a site's google sitemap |
|
17
|
|
|
* |
|
18
|
|
|
* If the sitemap module is installed, uses information from that to populate things |
|
19
|
|
|
* |
|
20
|
|
|
* @author [email protected] |
|
21
|
|
|
* @license http://silverstripe.org/bsd-license/ |
|
22
|
|
|
*/ |
|
23
|
|
|
class GenerateGoogleSitemapJob extends AbstractQueuedJob |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $regenerate_time = 43200; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->pagesToProcess = DB::query('SELECT ID FROM "SiteTree_Live" WHERE "ShowInSearch"=1')->column(); |
|
|
|
|
|
|
33
|
|
|
$this->currentStep = 0; |
|
34
|
|
|
$this->totalSteps = count($this->pagesToProcess); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sitemap job is going to run for a while... |
|
39
|
|
|
* |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getJobType() |
|
43
|
|
|
{ |
|
44
|
|
|
return QueuedJob::QUEUED; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getTitle() |
|
51
|
|
|
{ |
|
52
|
|
|
return _t('GenerateSitemapJob.REGENERATE', 'Regenerate Google sitemap .xml file'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return a signature for this queued job |
|
57
|
|
|
* |
|
58
|
|
|
* For the generate sitemap job, we only ever want one instance running, so just use the class name |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getSignature() |
|
63
|
|
|
{ |
|
64
|
|
|
return md5(get_class($this)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Note that this is duplicated for backwards compatibility purposes... |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setup() |
|
71
|
|
|
{ |
|
72
|
|
|
parent::setup(); |
|
73
|
|
|
increase_time_limit_to(); |
|
74
|
|
|
|
|
75
|
|
|
$restart = $this->currentStep == 0; |
|
76
|
|
|
if (!$this->tempFile || !file_exists($this->tempFile)) { |
|
|
|
|
|
|
77
|
|
|
$tmpfile = tempnam(getTempFolder(), 'sitemap'); |
|
78
|
|
|
if (file_exists($tmpfile)) { |
|
79
|
|
|
$this->tempFile = $tmpfile; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
$restart = true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($restart) { |
|
85
|
|
|
$this->pagesToProcess = DB::query('SELECT ID FROM SiteTree_Live WHERE ShowInSearch=1')->column(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* On any restart, make sure to check that our temporary file is being created still. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function prepareForRestart() |
|
93
|
|
|
{ |
|
94
|
|
|
parent::prepareForRestart(); |
|
95
|
|
|
// if the file we've been building is missing, lets fix it up |
|
96
|
|
|
if (!$this->tempFile || !file_exists($this->tempFile)) { |
|
|
|
|
|
|
97
|
|
|
$tmpfile = tempnam(getTempFolder(), 'sitemap'); |
|
98
|
|
|
if (file_exists($tmpfile)) { |
|
99
|
|
|
$this->tempFile = $tmpfile; |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
$this->currentStep = 0; |
|
102
|
|
|
$this->pagesToProcess = DB::query('SELECT ID FROM SiteTree_Live WHERE ShowInSearch=1')->column(); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function process() |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$this->tempFile) { |
|
|
|
|
|
|
109
|
|
|
throw new Exception("Temporary sitemap file has not been set"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!file_exists($this->tempFile)) { |
|
|
|
|
|
|
113
|
|
|
throw new Exception("Temporary file $this->tempFile has been deleted!"); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$remainingChildren = $this->pagesToProcess; |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
// if there's no more, we're done! |
|
119
|
|
|
if (!count($remainingChildren)) { |
|
120
|
|
|
$this->completeJob(); |
|
121
|
|
|
$this->isComplete = true; |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// lets process our first item - note that we take it off the list of things left to do |
|
126
|
|
|
$ID = array_shift($remainingChildren); |
|
127
|
|
|
|
|
128
|
|
|
// get the page |
|
129
|
|
|
$page = Versioned::get_by_stage('Page', 'Live', '"SiteTree_Live"."ID" = '.$ID); |
|
130
|
|
|
|
|
131
|
|
|
if (!$page || !$page->Count()) { |
|
132
|
|
|
$this->addMessage("Page ID #$ID could not be found, skipping"); |
|
133
|
|
|
} else { |
|
134
|
|
|
$page = $page->First(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($page && $page instanceof Page && !($page instanceof ErrorPage)) { |
|
|
|
|
|
|
138
|
|
|
if ($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) { |
|
139
|
|
|
$created = $page->dbObject('Created'); |
|
140
|
|
|
$now = new DBDatetime(); |
|
141
|
|
|
$now->value = date('Y-m-d H:i:s'); |
|
142
|
|
|
$versions = $page->Version; |
|
143
|
|
|
$timediff = $now->format('U') - $created->format('U'); |
|
144
|
|
|
|
|
145
|
|
|
// Check how many revisions have been made over the lifetime of the |
|
146
|
|
|
// Page for a rough estimate of it's changing frequency. |
|
147
|
|
|
$period = $timediff / ($versions + 1); |
|
148
|
|
|
|
|
149
|
|
|
if ($period > 60*60*24*365) { // > 1 year |
|
150
|
|
|
$page->ChangeFreq = 'yearly'; |
|
151
|
|
|
} elseif ($period > 60*60*24*30) { // > ~1 month |
|
152
|
|
|
$page->ChangeFreq = 'monthly'; |
|
153
|
|
|
} elseif ($period > 60*60*24*7) { // > 1 week |
|
154
|
|
|
$page->ChangeFreq = 'weekly'; |
|
155
|
|
|
} elseif ($period > 60*60*24) { // > 1 day |
|
156
|
|
|
$page->ChangeFreq = 'daily'; |
|
157
|
|
|
} elseif ($period > 60*60) { // > 1 hour |
|
158
|
|
|
$page->ChangeFreq = 'hourly'; |
|
159
|
|
|
} else { // < 1 hour |
|
160
|
|
|
$page->ChangeFreq = 'always'; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// do the generation of the file in a temporary location |
|
164
|
|
|
$content = $page->renderWith('SitemapEntry'); |
|
165
|
|
|
|
|
166
|
|
|
$fp = fopen($this->tempFile, "a"); |
|
|
|
|
|
|
167
|
|
|
if (!$fp) { |
|
168
|
|
|
throw new Exception("Could not open $this->tempFile for writing"); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
fputs($fp, $content, strlen($content)); |
|
171
|
|
|
fclose($fp); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// and now we store the new list of remaining children |
|
176
|
|
|
$this->pagesToProcess = $remainingChildren; |
|
|
|
|
|
|
177
|
|
|
$this->currentStep++; |
|
178
|
|
|
|
|
179
|
|
|
if (!count($remainingChildren)) { |
|
180
|
|
|
$this->completeJob(); |
|
181
|
|
|
$this->isComplete = true; |
|
182
|
|
|
return; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Outputs the completed file to the site's webroot |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function completeJob() |
|
190
|
|
|
{ |
|
191
|
|
|
$content = '<?xml version="1.0" encoding="UTF-8"?>' . |
|
192
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; |
|
193
|
|
|
$content .= file_get_contents($this->tempFile); |
|
|
|
|
|
|
194
|
|
|
$content .= '</urlset>'; |
|
195
|
|
|
|
|
196
|
|
|
$sitemap = Director::baseFolder() .'/sitemap.xml'; |
|
197
|
|
|
|
|
198
|
|
|
file_put_contents($sitemap, $content); |
|
199
|
|
|
|
|
200
|
|
|
if (file_exists($this->tempFile)) { |
|
|
|
|
|
|
201
|
|
|
unlink($this->tempFile); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$nextgeneration = new GenerateGoogleSitemapJob(); |
|
205
|
|
|
singleton('SilverStripe\\QueuedJobs\\Services\\QueuedJobService')->queueJob($nextgeneration, date('Y-m-d H:i:s', time() + self::$regenerate_time)); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.