3 namespace wcf\acp\action
;
5 use wcf\action\AbstractSecureAction
;
6 use wcf\action\AJAXInvokeAction
;
7 use wcf\system\exception\ImplementationException
;
8 use wcf\system\exception\SystemException
;
10 use wcf\system\worker\IWorker
;
14 * Handles worker actions.
16 * @author Alexander Ebert
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 final class WorkerProxyAction
extends AJAXInvokeAction
25 const DO_NOT_LOG
= true;
31 protected $loopCount = -1;
34 * parameters for worker action
37 protected $parameters = [];
45 public static $allowInvoke = [];
50 public function readParameters()
52 AbstractSecureAction
::readParameters();
54 if (isset($_POST['className'])) {
55 $this->className
= $_POST['className'];
57 if (isset($_POST['loopCount'])) {
58 $this->loopCount
= \
intval($_POST['loopCount']);
60 if (isset($_POST['parameters']) && \
is_array($_POST['parameters'])) {
61 $this->parameters
= $_POST['parameters'];
68 * Validates class name.
70 protected function validate()
72 if (empty($this->className
)) {
73 throw new SystemException("class name cannot be empty.");
76 if (!\
is_subclass_of($this->className
, IWorker
::class)) {
77 throw new ImplementationException($this->className
, IWorker
::class);
84 public function execute()
86 AbstractSecureAction
::execute();
88 if ($this->loopCount
== -1) {
89 $this->sendResponse();
93 $this->worker
= new $this->className($this->parameters
);
94 $this->worker
->setLoopCount($this->loopCount
);
96 // validate worker parameters
97 $this->worker
->validate();
99 // calculate progress, triggers countObjects()
100 $progress = $this->worker
->getProgress();
103 $this->worker
->execute();
105 $this->worker
->finalize();
107 // send current state
108 $this->sendResponse($progress, $this->worker
->getParameters(), $this->worker
->getProceedURL());
112 * Sends a JSON-encoded response.
114 * @param int $progress
115 * @param array $parameters
116 * @param string $proceedURL
118 protected function sendResponse($progress = 0, ?
array $parameters = null, $proceedURL = '')
120 if ($parameters === null) {
121 $parameters = $this->parameters
;
124 // build return values
126 'className' => $this->className
,
127 'loopCount' => $this->loopCount +
1,
128 'parameters' => $parameters,
129 'proceedURL' => $proceedURL,
130 'progress' => $progress,
133 // include template on startup
134 if ($this->loopCount
== -1) {
135 $returnValues['template'] = WCF
::getTPL()->fetch('worker');
138 // send JSON-encoded response
139 \
header('Content-type: application/json; charset=UTF-8');
140 echo JSON
::encode($returnValues);