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