1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Jobs; |
4
|
|
|
|
5
|
|
|
use Page; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
8
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An example queued job |
12
|
|
|
* |
13
|
|
|
* Use this as an example of how you can write your own jobs |
14
|
|
|
* |
15
|
|
|
* @author Marcus Nyeholt <[email protected]> |
16
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
17
|
|
|
*/ |
18
|
|
|
class PublishItemsJob extends AbstractQueuedJob implements QueuedJob |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param DataObject $rootNodeID |
22
|
|
|
*/ |
23
|
|
|
public function __construct($rootNodeID = null) |
24
|
|
|
{ |
25
|
|
|
// this value is automatically persisted between processing requests for |
26
|
|
|
// this job |
27
|
|
|
if ($rootNodeID) { |
28
|
|
|
$this->rootID = $rootNodeID; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function getRoot() |
33
|
|
|
{ |
34
|
|
|
return DataObject::get_by_id(Page::class, $this->rootID); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Defines the title of the job |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getTitle() |
43
|
|
|
{ |
44
|
|
|
$title = 'Unknown'; |
45
|
|
|
|
46
|
|
|
if ($root = $this->getRoot()) { |
47
|
|
|
$title = $root->Title; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return _t( |
51
|
|
|
__CLASS__ . '.Title', |
52
|
|
|
"Publish items beneath {title}", |
53
|
|
|
['title' => $title] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Indicate to the system which queue we think we should be in based |
59
|
|
|
* on how many objects we're going to touch on while processing. |
60
|
|
|
* |
61
|
|
|
* We want to make sure we also set how many steps we think we might need to take to |
62
|
|
|
* process everything - note that this does not need to be 100% accurate, but it's nice |
63
|
|
|
* to give a reasonable approximation |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
public function getJobType() |
68
|
|
|
{ |
69
|
|
|
$this->totalSteps = 'Lots'; |
|
|
|
|
70
|
|
|
return QueuedJob::QUEUED; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This is called immediately before a job begins - it gives you a chance |
75
|
|
|
* to initialise job data and make sure everything's good to go |
76
|
|
|
* |
77
|
|
|
* What we're doing in our case is to queue up the list of items we know we need to |
78
|
|
|
* process still (it's not everything - just the ones we know at the moment) |
79
|
|
|
* |
80
|
|
|
* When we go through, we'll constantly add and remove from this queue, meaning |
81
|
|
|
* we never overload it with content |
82
|
|
|
*/ |
83
|
|
|
public function setup() |
84
|
|
|
{ |
85
|
|
|
if (!$this->getRoot()) { |
86
|
|
|
// we're missing for some reason! |
87
|
|
|
$this->isComplete = true; |
88
|
|
|
$this->remainingChildren = array(); |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
$remainingChildren = array(); |
92
|
|
|
$remainingChildren[] = $this->getRoot()->ID; |
93
|
|
|
$this->remainingChildren = $remainingChildren; |
94
|
|
|
|
95
|
|
|
// we reset this to 1; this is because we only know for sure about 1 item remaining |
96
|
|
|
// as time goes by, this will increase as we discover more items that need processing |
97
|
|
|
$this->totalSteps = 1; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Lets process a single node, and publish it if necessary |
102
|
|
|
*/ |
103
|
|
|
public function process() |
104
|
|
|
{ |
105
|
|
|
$remainingChildren = $this->remainingChildren; |
106
|
|
|
|
107
|
|
|
// if there's no more, we're done! |
108
|
|
|
if (!count($remainingChildren)) { |
109
|
|
|
$this->isComplete = true; |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// we need to always increment! This is important, because if we don't then our container |
114
|
|
|
// that executes around us thinks that the job has died, and will stop it running. |
115
|
|
|
$this->currentStep++; |
116
|
|
|
|
117
|
|
|
// lets process our first item - note that we take it off the list of things left to do |
118
|
|
|
$ID = array_shift($remainingChildren); |
119
|
|
|
|
120
|
|
|
// get the page |
121
|
|
|
$page = DataObject::get_by_id(Page::class, $ID); |
122
|
|
|
if ($page) { |
123
|
|
|
// publish it |
124
|
|
|
$page->doPublish(); |
125
|
|
|
|
126
|
|
|
// and add its children to the list to be published |
127
|
|
|
foreach ($page->Children() as $child) { |
128
|
|
|
$remainingChildren[] = $child->ID; |
129
|
|
|
// we increase how many steps we need to do - this means our total steps constantly rises, |
130
|
|
|
// but it gives users an idea of exactly how many more we know about |
131
|
|
|
$this->totalSteps++; |
132
|
|
|
} |
133
|
|
|
$page->destroy(); |
134
|
|
|
unset($page); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// and now we store the new list of remaining children |
138
|
|
|
$this->remainingChildren = $remainingChildren; |
139
|
|
|
|
140
|
|
|
if (!count($remainingChildren)) { |
141
|
|
|
$this->isComplete = true; |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.