Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.task.php @ 1989:b234959829da

Revision 1989:b234959829da, 5.7 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Revamp plugin maintenance, step 5, now open to admin user and various fixes, addresses #999

Line 
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup PLUGIN_MAINTENANCE
16@nosubgrouping
17@brief Maintenance plugin task class.
18
19Every task of maintenance must extend this class.
20*/
21class dcMaintenanceTask
22{
23     protected $maintenance;
24     protected $core;
25     protected $p_url;
26     protected $code;
27     protected $ts = 0;
28     protected $expired = 0;
29     protected $ajax = false;
30     protected $blog = false;
31     protected $perm = null;
32
33     protected $id;
34     protected $name;
35     protected $tab = 'maintenance';
36     protected $group = 'other';
37
38     protected $task;
39     protected $step;
40     protected $error;
41     protected $success;
42
43     /**
44      * Constructor.
45      *
46      * If your task required something on construct,
47      * use method init() to do it.
48      *
49      * @param maintenance    <b>dcMaintenance</b>     dcMaintenance instance
50      * @param p_url     <b>string</b>  Maintenance plugin url
51      */
52     public function __construct($maintenance, $p_url)
53     {
54          $this->maintenance = $maintenance;
55          $this->core = $maintenance->core;
56          $this->init();
57
58          if ($this->perm() === null && !$this->core->auth->isSuperAdmin()
59          || !$this->core->auth->check($this->perm(), $this->core->blog->id)) {
60               return null;
61          }
62
63          $this->p_url = $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 null;
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               }
156               else {
157                    $this->expired = null;
158                    $logs = array();
159                    foreach($this->maintenance->getLogs() as $id => $log)
160                    {
161                         if ($id != $this->id() || $this->blog && !$log['blog']) {
162                              continue;
163                         }
164
165                         $this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false;
166                    }
167               }
168          }
169          return $this->expired;
170     }
171
172     /**
173      * Get task ID.
174      *
175      * @return     <b>string</b>  Task ID (class name)
176      */
177     public function id()
178     {
179          return $this->id;
180     }
181
182     /**
183      * Get task name.
184      *
185      * @return     <b>string</b>  Task name
186      */
187     public function name()
188     {
189          return $this->name;
190     }
191
192     /**
193      * Get task tab.
194      *
195      * @return     <b>mixed</b>   Task tab ID or null
196      */
197     public function tab()
198     {
199          return $this->tab;
200     }
201
202     /**
203      * Get task group.
204      *
205      * If task required a full tab,
206      * this must be returned null.
207      *
208      * @return     <b>mixed</b>   Task group ID or null
209      */
210     public function group()
211     {
212          return $this->group;
213     }
214
215     /**
216      * Use ajax
217      *
218      * Is task use maintenance ajax script
219      * for steps process.
220      *
221      * @return     <b>boolean</b> Use ajax
222      */
223     public function ajax()
224     {
225          return (boolean) $this->ajax;
226     }
227
228     /**
229      * Get task message.
230      *
231      * This message is used on form button.
232      *
233      * @return     <b>string</b>  Message
234      */
235     public function task()
236     {
237          return $this->task;
238     }
239
240     /**
241      * Get step message.
242      *
243      * This message is displayed during task step execution.
244      *
245      * @return     <b>mixed</b>   Message or null
246      */
247     public function step()
248     {
249          return $this->step;
250     }
251
252     /**
253      * Get success message.
254      *
255      * This message is displayed when task is accomplished.
256      *
257      * @return     <b>mixed</b>   Message or null
258      */
259     public function success()
260     {
261          return $this->success;
262     }
263
264     /**
265      * Get error message.
266      *
267      * This message is displayed on error.
268      *
269      * @return     <b>mixed</b>   Message or null
270      */
271     public function error()
272     {
273          return $this->error;
274     }
275
276     /**
277      * Get header.
278      *
279      * Headers required on maintenance page.
280      *
281      * @return     <b>mixed</b>   Message or null
282      */
283     public function header()
284     {
285          return null;
286     }
287
288     /**
289      * Get content.
290      *
291      * Content for full tab task.
292      *
293      * @return     <b>string</b>  Tab's content
294      */
295     public function content()
296     {
297          return null;
298     }
299
300     /**
301      * Execute task.
302      *
303      * @return     <b>mixed</b>   :
304      *   - FALSE on error,
305      *   - TRUE if task is finished
306      *   - INTEGER if task required a next step
307      */
308     public function execute()
309     {
310          return null;
311     }
312
313     /**
314      * Log task execution.
315      *
316      * Sometimes we need to log task execution
317      * direct from task itself.
318      *
319      */
320     protected function log()
321     {
322          $this->maintenance->setLog($this->id);
323     }
324}
Note: See TracBrowser for help on using the repository browser.

Sites map