| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @brief maintenance, a plugin for Dotclear 2 |
|---|
| 4 | * |
|---|
| 5 | * @package Dotclear |
|---|
| 6 | * @subpackage Plugins |
|---|
| 7 | * |
|---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 9 | * @copyright GPL-2.0-only |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | @brief Maintenance plugin task class. |
|---|
| 16 | |
|---|
| 17 | Every task of maintenance must extend this class. |
|---|
| 18 | */ |
|---|
| 19 | class dcMaintenanceTask |
|---|
| 20 | { |
|---|
| 21 | protected $maintenance; |
|---|
| 22 | protected $core; |
|---|
| 23 | protected $p_url; |
|---|
| 24 | protected $code; |
|---|
| 25 | protected $ts = 0; |
|---|
| 26 | protected $expired = 0; |
|---|
| 27 | protected $ajax = false; |
|---|
| 28 | protected $blog = false; |
|---|
| 29 | protected $perm = null; |
|---|
| 30 | |
|---|
| 31 | protected $id; |
|---|
| 32 | protected $name; |
|---|
| 33 | protected $description; |
|---|
| 34 | protected $tab = 'maintenance'; |
|---|
| 35 | protected $group = 'other'; |
|---|
| 36 | |
|---|
| 37 | protected $task; |
|---|
| 38 | protected $step; |
|---|
| 39 | protected $error; |
|---|
| 40 | protected $success; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Constructor. |
|---|
| 44 | * |
|---|
| 45 | * If your task required something on construct, |
|---|
| 46 | * use method init() to do it. |
|---|
| 47 | * |
|---|
| 48 | * @param maintenance <b>dcMaintenance</b> dcMaintenance instance |
|---|
| 49 | * @param p_url <b>string</b> Maintenance plugin url |
|---|
| 50 | */ |
|---|
| 51 | public function __construct($maintenance) |
|---|
| 52 | { |
|---|
| 53 | $this->maintenance = $maintenance; |
|---|
| 54 | $this->core = $maintenance->core; |
|---|
| 55 | $this->init(); |
|---|
| 56 | $this->id = null; |
|---|
| 57 | |
|---|
| 58 | if ($this->perm() === null && !$this->core->auth->isSuperAdmin() |
|---|
| 59 | || !$this->core->auth->check($this->perm(), $this->core->blog->id)) { |
|---|
| 60 | return; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $this->p_url = $maintenance->p_url; |
|---|
| 64 | $this->id = get_class($this); |
|---|
| 65 | |
|---|
| 66 | if (!$this->name) { |
|---|
| 67 | $this->name = get_class($this); |
|---|
| 68 | } |
|---|
| 69 | if (!$this->error) { |
|---|
| 70 | $this->error = __('Failed to execute task.'); |
|---|
| 71 | } |
|---|
| 72 | if (!$this->success) { |
|---|
| 73 | $this->success = __('Task successfully executed.'); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | $this->core->blog->settings->addNamespace('maintenance'); |
|---|
| 77 | $ts = $this->core->blog->settings->maintenance->get('ts_' . $this->id); |
|---|
| 78 | |
|---|
| 79 | $this->ts = abs((integer) $ts); |
|---|
| 80 | |
|---|
| 81 | return true; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Initialize task object. |
|---|
| 86 | * |
|---|
| 87 | * Better to set translated messages here than |
|---|
| 88 | * to rewrite constructor. |
|---|
| 89 | */ |
|---|
| 90 | protected function init() |
|---|
| 91 | { |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Get task permission. |
|---|
| 97 | * |
|---|
| 98 | * Return user permission required to run this task |
|---|
| 99 | * or null for super admin. |
|---|
| 100 | * |
|---|
| 101 | * @return <b>mixed</b> Permission. |
|---|
| 102 | */ |
|---|
| 103 | public function perm() |
|---|
| 104 | { |
|---|
| 105 | return $this->perm; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * Get task scope. |
|---|
| 110 | *. |
|---|
| 111 | * Is task limited to current blog. |
|---|
| 112 | * |
|---|
| 113 | * @return <b>boolean</b> Limit to blog |
|---|
| 114 | */ |
|---|
| 115 | public function blog() |
|---|
| 116 | { |
|---|
| 117 | return $this->blog; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Set $code for task having multiple steps. |
|---|
| 122 | * |
|---|
| 123 | * @param code <b>integer</b> Code used for task execution |
|---|
| 124 | */ |
|---|
| 125 | public function code($code) |
|---|
| 126 | { |
|---|
| 127 | $this->code = (integer) $code; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Get timestamp between maintenances. |
|---|
| 132 | * |
|---|
| 133 | * @return <b>intetger</b> Timestamp |
|---|
| 134 | */ |
|---|
| 135 | public function ts() |
|---|
| 136 | { |
|---|
| 137 | return $this->ts === false ? false : abs((integer) $this->ts); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Get task expired. |
|---|
| 142 | * |
|---|
| 143 | * This return: |
|---|
| 144 | * - Timstamp of last update if it expired |
|---|
| 145 | * - False if it not expired or has no recall time |
|---|
| 146 | * - Null if it has never been executed |
|---|
| 147 | * |
|---|
| 148 | * @return <b>mixed</b> Last update |
|---|
| 149 | */ |
|---|
| 150 | public function expired() |
|---|
| 151 | { |
|---|
| 152 | if ($this->expired === 0) { |
|---|
| 153 | if (!$this->ts()) { |
|---|
| 154 | $this->expired = false; |
|---|
| 155 | } else { |
|---|
| 156 | $this->expired = null; |
|---|
| 157 | $logs = []; |
|---|
| 158 | foreach ($this->maintenance->getLogs() as $id => $log) { |
|---|
| 159 | if ($id != $this->id() || $this->blog && !$log['blog']) { |
|---|
| 160 | continue; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | $this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | return $this->expired; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * Get task ID. |
|---|
| 172 | * |
|---|
| 173 | * @return <b>string</b> Task ID (class name) |
|---|
| 174 | */ |
|---|
| 175 | public function id() |
|---|
| 176 | { |
|---|
| 177 | return $this->id; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Get task name. |
|---|
| 182 | * |
|---|
| 183 | * @return <b>string</b> Task name |
|---|
| 184 | */ |
|---|
| 185 | public function name() |
|---|
| 186 | { |
|---|
| 187 | return $this->name; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * Get task description. |
|---|
| 192 | * |
|---|
| 193 | * @return <b>string</b> Description |
|---|
| 194 | */ |
|---|
| 195 | public function description() |
|---|
| 196 | { |
|---|
| 197 | return $this->description; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * Get task tab. |
|---|
| 202 | * |
|---|
| 203 | * @return <b>mixed</b> Task tab ID or null |
|---|
| 204 | */ |
|---|
| 205 | public function tab() |
|---|
| 206 | { |
|---|
| 207 | return $this->tab; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * Get task group. |
|---|
| 212 | * |
|---|
| 213 | * If task required a full tab, |
|---|
| 214 | * this must be returned null. |
|---|
| 215 | * |
|---|
| 216 | * @return <b>mixed</b> Task group ID or null |
|---|
| 217 | */ |
|---|
| 218 | public function group() |
|---|
| 219 | { |
|---|
| 220 | return $this->group; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Use ajax |
|---|
| 225 | * |
|---|
| 226 | * Is task use maintenance ajax script |
|---|
| 227 | * for steps process. |
|---|
| 228 | * |
|---|
| 229 | * @return <b>boolean</b> Use ajax |
|---|
| 230 | */ |
|---|
| 231 | public function ajax() |
|---|
| 232 | { |
|---|
| 233 | return (boolean) $this->ajax; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * Get task message. |
|---|
| 238 | * |
|---|
| 239 | * This message is used on form button. |
|---|
| 240 | * |
|---|
| 241 | * @return <b>string</b> Message |
|---|
| 242 | */ |
|---|
| 243 | public function task() |
|---|
| 244 | { |
|---|
| 245 | return $this->task; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | /** |
|---|
| 249 | * Get step message. |
|---|
| 250 | * |
|---|
| 251 | * This message is displayed during task step execution. |
|---|
| 252 | * |
|---|
| 253 | * @return <b>mixed</b> Message or null |
|---|
| 254 | */ |
|---|
| 255 | public function step() |
|---|
| 256 | { |
|---|
| 257 | return $this->step; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * Get success message. |
|---|
| 262 | * |
|---|
| 263 | * This message is displayed when task is accomplished. |
|---|
| 264 | * |
|---|
| 265 | * @return <b>mixed</b> Message or null |
|---|
| 266 | */ |
|---|
| 267 | public function success() |
|---|
| 268 | { |
|---|
| 269 | return $this->success; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Get error message. |
|---|
| 274 | * |
|---|
| 275 | * This message is displayed on error. |
|---|
| 276 | * |
|---|
| 277 | * @return <b>mixed</b> Message or null |
|---|
| 278 | */ |
|---|
| 279 | public function error() |
|---|
| 280 | { |
|---|
| 281 | return $this->error; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | * Get header. |
|---|
| 286 | * |
|---|
| 287 | * Headers required on maintenance page. |
|---|
| 288 | * |
|---|
| 289 | * @return <b>mixed</b> Message or null |
|---|
| 290 | */ |
|---|
| 291 | public function header() |
|---|
| 292 | { |
|---|
| 293 | return; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | /** |
|---|
| 297 | * Get content. |
|---|
| 298 | * |
|---|
| 299 | * Content for full tab task. |
|---|
| 300 | * |
|---|
| 301 | * @return <b>string</b> Tab's content |
|---|
| 302 | */ |
|---|
| 303 | public function content() |
|---|
| 304 | { |
|---|
| 305 | return; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | * Execute task. |
|---|
| 310 | * |
|---|
| 311 | * @return <b>mixed</b> : |
|---|
| 312 | * - FALSE on error, |
|---|
| 313 | * - TRUE if task is finished |
|---|
| 314 | * - INTEGER if task required a next step |
|---|
| 315 | */ |
|---|
| 316 | public function execute() |
|---|
| 317 | { |
|---|
| 318 | return; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /** |
|---|
| 322 | * Log task execution. |
|---|
| 323 | * |
|---|
| 324 | * Sometimes we need to log task execution |
|---|
| 325 | * direct from task itself. |
|---|
| 326 | * |
|---|
| 327 | */ |
|---|
| 328 | protected function log() |
|---|
| 329 | { |
|---|
| 330 | $this->maintenance->setLog($this->id); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | public function help() |
|---|
| 334 | { |
|---|
| 335 | return; |
|---|
| 336 | } |
|---|
| 337 | } |
|---|