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