Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / action / WorkerProxyAction.class.php
CommitLineData
532f1173 1<?php
a9229942 2
532f1173 3namespace wcf\acp\action;
a9229942 4
532f1173 5use wcf\action\AbstractSecureAction;
2cea2b5b 6use wcf\action\AJAXInvokeAction;
7b9ff46b 7use wcf\system\exception\ImplementationException;
532f1173
AE
8use wcf\system\exception\SystemException;
9use wcf\system\WCF;
a9229942 10use wcf\system\worker\IWorker;
532f1173
AE
11use wcf\util\JSON;
12
13/**
14 * Handles worker actions.
a9229942
TD
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Acp\Action
532f1173 20 */
a9229942
TD
21class WorkerProxyAction extends AJAXInvokeAction
22{
23 /**
24 * @inheritDoc
25 */
26 const DO_NOT_LOG = true;
27
28 /**
29 * loop counter
30 * @var int
31 */
32 protected $loopCount = -1;
33
34 /**
35 * parameters for worker action
36 * @var array
37 */
38 protected $parameters = [];
39
40 /**
41 * worker object
42 * @var IWorker
43 */
44 protected $worker;
45
46 public static $allowInvoke = [];
47
48 /**
49 * @inheritDoc
50 */
51 public function readParameters()
52 {
53 AbstractSecureAction::readParameters();
54
55 if (isset($_POST['className'])) {
56 $this->className = $_POST['className'];
57 }
58 if (isset($_POST['loopCount'])) {
59 $this->loopCount = \intval($_POST['loopCount']);
60 }
61 if (isset($_POST['parameters']) && \is_array($_POST['parameters'])) {
62 $this->parameters = $_POST['parameters'];
63 }
64
65 $this->validate();
66 }
67
68 /**
69 * Validates class name.
70 */
71 protected function validate()
72 {
73 if (empty($this->className)) {
74 throw new SystemException("class name cannot be empty.");
75 }
76
77 if (!\is_subclass_of($this->className, IWorker::class)) {
78 throw new ImplementationException($this->className, IWorker::class);
79 }
80 }
81
82 /**
83 * @inheritDoc
84 */
85 public function execute()
86 {
87 AbstractSecureAction::execute();
88
89 if ($this->loopCount == -1) {
90 $this->sendResponse();
91 }
92
93 // init worker
94 $this->worker = new $this->className($this->parameters);
95 $this->worker->setLoopCount($this->loopCount);
96
97 // validate worker parameters
98 $this->worker->validate();
99
100 // calculate progress, triggers countObjects()
101 $progress = $this->worker->getProgress();
102
103 // execute worker
104 $this->worker->execute();
105
106 $this->worker->finalize();
107
108 // send current state
109 $this->sendResponse($progress, $this->worker->getParameters(), $this->worker->getProceedURL());
110 }
111
112 /**
113 * Sends a JSON-encoded response.
114 *
115 * @param int $progress
116 * @param array $parameters
117 * @param string $proceedURL
118 */
119 protected function sendResponse($progress = 0, ?array $parameters = null, $proceedURL = '')
120 {
121 if ($parameters === null) {
122 $parameters = $this->parameters;
123 }
124
125 // build return values
126 $returnValues = [
127 'className' => $this->className,
128 'loopCount' => $this->loopCount + 1,
129 'parameters' => $parameters,
130 'proceedURL' => $proceedURL,
131 'progress' => $progress,
132 ];
133
134 // include template on startup
135 if ($this->loopCount == -1) {
136 $returnValues['template'] = WCF::getTPL()->fetch('worker');
137 }
138
139 // send JSON-encoded response
140 \header('Content-type: application/json');
141 echo JSON::encode($returnValues);
142
143 exit;
144 }
532f1173 145}