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