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