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
|
|
|
// if the site tree has been "reorganised" (ie: the parentID has changed) |
81
|
|
|
// then this is eht equivalent of an unpublish and publish as far as the |
82
|
|
|
// static publisher is concerned |
83
|
|
|
if ($original && ( |
84
|
|
|
$original->ParentID !== $this->getOwner()->ParentID |
85
|
|
|
|| $original->URLSegment !== $this->getOwner()->URLSegment |
86
|
|
|
) |
87
|
|
|
) { |
88
|
|
|
$context = [ |
89
|
|
|
'action' => 'unpublish', |
90
|
|
|
]; |
91
|
|
|
$original->collectChanges($context); |
92
|
|
|
$original->flushChanges(); |
93
|
|
|
} |
94
|
|
|
$context = [ |
95
|
|
|
'action' => 'publish', |
96
|
|
|
]; |
97
|
|
|
$this->collectChanges($context); |
98
|
|
|
$this->flushChanges(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function onBeforeUnpublish() |
102
|
|
|
{ |
103
|
|
|
$context = [ |
104
|
|
|
'action' => 'unpublish', |
105
|
|
|
]; |
106
|
|
|
$this->collectChanges($context); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function onAfterUnpublish() |
110
|
|
|
{ |
111
|
|
|
$this->flushChanges(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Collect all changes for the given context. |
116
|
|
|
* |
117
|
|
|
* @param array $context |
118
|
|
|
*/ |
119
|
|
|
public function collectChanges($context) |
120
|
|
|
{ |
121
|
|
|
Environment::increaseMemoryLimitTo(); |
122
|
|
|
Environment::increaseTimeLimitTo(); |
123
|
|
|
|
124
|
|
|
if ($this->getOwner()->hasExtension(PublishableSiteTree::class) |
125
|
|
|
|| $this->getOwner() instanceof StaticPublishingTrigger |
126
|
|
|
) { |
127
|
|
|
$toUpdate = $this->getOwner()->objectsToUpdate($context); |
128
|
|
|
$this->setToUpdate($toUpdate); |
129
|
|
|
|
130
|
|
|
$toDelete = $this->getOwner()->objectsToDelete($context); |
131
|
|
|
$this->setToDelete($toDelete); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Execute URL deletions, enqueue URL updates. |
137
|
|
|
*/ |
138
|
|
|
public function flushChanges() |
139
|
|
|
{ |
140
|
|
|
$queue = QueuedJobService::singleton(); |
141
|
|
|
echo PHP_EOL . PHP_EOL . get_class($queue) . PHP_EOL . PHP_EOL; |
142
|
|
|
|
143
|
|
|
if (get_class($queue) === QueuedJobService::class) { |
144
|
|
|
try { |
145
|
|
|
throw new \Exception('test'); |
146
|
|
|
} catch (\Exception $e) { |
147
|
|
|
echo PHP_EOL . PHP_EOL . $e->getTraceAsString() . PHP_EOL . PHP_EOL; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!empty($this->toUpdate)) { |
152
|
|
|
foreach ($this->toUpdate as $queueItem) { |
153
|
|
|
$job = Injector::inst()->create(GenerateStaticCacheJob::class); |
154
|
|
|
|
155
|
|
|
$jobData = new \stdClass(); |
156
|
|
|
$urls = $queueItem->urlsToCache(); |
157
|
|
|
ksort($urls); |
158
|
|
|
$jobData->URLsToProcess = $urls; |
159
|
|
|
|
160
|
|
|
$job->setJobData(0, 0, false, $jobData, [ |
161
|
|
|
'Building URLs: ' . var_export(array_keys($jobData->URLsToProcess), true), |
162
|
|
|
]); |
163
|
|
|
|
164
|
|
|
$queue->queueJob($job); |
165
|
|
|
} |
166
|
|
|
$this->toUpdate = []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (!empty($this->toDelete)) { |
170
|
|
|
foreach ($this->toDelete as $queueItem) { |
171
|
|
|
$job = Injector::inst()->create(DeleteStaticCacheJob::class); |
172
|
|
|
|
173
|
|
|
$jobData = new \stdClass(); |
174
|
|
|
$urls = $queueItem->urlsToCache(); |
175
|
|
|
ksort($urls); |
176
|
|
|
$jobData->URLsToProcess = $urls; |
177
|
|
|
|
178
|
|
|
$job->setJobData(0, 0, false, $jobData, [ |
179
|
|
|
'Purging URLs: ' . var_export(array_keys($jobData->URLsToProcess), true), |
180
|
|
|
]); |
181
|
|
|
|
182
|
|
|
$queue->queueJob($job); |
183
|
|
|
} |
184
|
|
|
$this->toDelete = []; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|