1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Extension\Engine; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTreeExtension; |
6
|
|
|
use SilverStripe\Core\Environment; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger; |
9
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; |
10
|
|
|
use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob; |
12
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This extension couples to the StaticallyPublishable and StaticPublishingTrigger implementations |
16
|
|
|
* on the SiteTree objects and makes sure the actual change to SiteTree is triggered/enqueued. |
17
|
|
|
* |
18
|
|
|
* Provides the following information as a context to StaticPublishingTrigger: |
19
|
|
|
* * action - name of the executed action: publish or unpublish |
20
|
|
|
* |
21
|
|
|
* @see PublishableSiteTree |
22
|
|
|
*/ |
23
|
|
|
class SiteTreePublishingEngine extends SiteTreeExtension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Queues the urls to be flushed into the queue. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $toUpdate = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Queues the urls to be deleted as part of a next flush operation. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $toDelete = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getToUpdate() |
43
|
|
|
{ |
44
|
|
|
return $this->toUpdate; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getToDelete() |
51
|
|
|
{ |
52
|
|
|
return $this->toDelete; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $toUpdate |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function setToUpdate($toUpdate) |
60
|
|
|
{ |
61
|
|
|
$this->toUpdate = $toUpdate; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $toDelete |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setToDelete($toDelete) |
70
|
|
|
{ |
71
|
|
|
$this->toDelete = $toDelete; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \SilverStripe\CMS\Model\SiteTree|null $original |
77
|
|
|
*/ |
78
|
|
|
public function onAfterPublishRecursive(&$original) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
return; |
81
|
|
|
// if the site tree has been "reorganised" (ie: the parentID has changed) |
82
|
|
|
// then this is eht equivalent of an unpublish and publish as far as the |
83
|
|
|
// static publisher is concerned |
84
|
|
|
if ($original && ( |
|
|
|
|
85
|
|
|
$original->ParentID !== $this->getOwner()->ParentID |
86
|
|
|
|| $original->URLSegment !== $this->getOwner()->URLSegment |
87
|
|
|
) |
88
|
|
|
) { |
89
|
|
|
$context = [ |
90
|
|
|
'action' => 'unpublish', |
91
|
|
|
]; |
92
|
|
|
$original->collectChanges($context); |
93
|
|
|
$original->flushChanges(); |
94
|
|
|
} |
95
|
|
|
$context = [ |
96
|
|
|
'action' => 'publish', |
97
|
|
|
]; |
98
|
|
|
$this->collectChanges($context); |
99
|
|
|
$this->flushChanges(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function onBeforeUnpublish() |
103
|
|
|
{ |
104
|
|
|
$context = [ |
105
|
|
|
'action' => 'unpublish', |
106
|
|
|
]; |
107
|
|
|
$this->collectChanges($context); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function onAfterUnpublish() |
111
|
|
|
{ |
112
|
|
|
return; |
113
|
|
|
$this->flushChanges(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Collect all changes for the given context. |
118
|
|
|
* |
119
|
|
|
* @param array $context |
120
|
|
|
*/ |
121
|
|
|
public function collectChanges($context) |
122
|
|
|
{ |
123
|
|
|
Environment::increaseMemoryLimitTo(); |
124
|
|
|
Environment::increaseTimeLimitTo(); |
125
|
|
|
|
126
|
|
|
if ($this->getOwner()->hasExtension(PublishableSiteTree::class) |
127
|
|
|
|| $this->getOwner() instanceof StaticPublishingTrigger |
128
|
|
|
) { |
129
|
|
|
$toUpdate = $this->getOwner()->objectsToUpdate($context); |
130
|
|
|
$this->setToUpdate($toUpdate); |
131
|
|
|
|
132
|
|
|
$toDelete = $this->getOwner()->objectsToDelete($context); |
133
|
|
|
$this->setToDelete($toDelete); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Execute URL deletions, enqueue URL updates. |
139
|
|
|
*/ |
140
|
|
|
public function flushChanges() |
141
|
|
|
{ |
142
|
|
|
$queue = QueuedJobService::singleton(); |
143
|
|
|
echo get_class($queue); |
144
|
|
|
if (!empty($this->toUpdate)) { |
145
|
|
|
foreach ($this->toUpdate as $queueItem) { |
146
|
|
|
$job = Injector::inst()->create(GenerateStaticCacheJob::class); |
147
|
|
|
|
148
|
|
|
$jobData = new \stdClass(); |
149
|
|
|
$urls = $queueItem->urlsToCache(); |
150
|
|
|
ksort($urls); |
151
|
|
|
$jobData->URLsToProcess = $urls; |
152
|
|
|
|
153
|
|
|
$job->setJobData(0, 0, false, $jobData, [ |
154
|
|
|
'Building URLs: ' . var_export(array_keys($jobData->URLsToProcess), true), |
155
|
|
|
]); |
156
|
|
|
|
157
|
|
|
$queue->queueJob($job); |
158
|
|
|
} |
159
|
|
|
$this->toUpdate = []; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!empty($this->toDelete)) { |
163
|
|
|
foreach ($this->toDelete as $queueItem) { |
164
|
|
|
$job = Injector::inst()->create(DeleteStaticCacheJob::class); |
165
|
|
|
|
166
|
|
|
$jobData = new \stdClass(); |
167
|
|
|
$urls = $queueItem->urlsToCache(); |
168
|
|
|
ksort($urls); |
169
|
|
|
$jobData->URLsToProcess = $urls; |
170
|
|
|
|
171
|
|
|
$job->setJobData(0, 0, false, $jobData, [ |
172
|
|
|
'Purging URLs: ' . var_export(array_keys($jobData->URLsToProcess), true), |
173
|
|
|
]); |
174
|
|
|
|
175
|
|
|
$queue->queueJob($job); |
176
|
|
|
} |
177
|
|
|
$this->toDelete = []; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.