Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.task.php @ 2044:4a3330bc8bd5

Revision 2044:4a3330bc8bd5, 5.9 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Revamp plugin maintenance, final setp (perhaps still some typo), addresses #1484, fixes #1208, fixes #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 $description;
36     protected $tab = 'maintenance';
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      *
50      * @param maintenance    <b>dcMaintenance</b>     dcMaintenance instance
51      * @param p_url     <b>string</b>  Maintenance plugin url
52      */
53     public function __construct($maintenance)
54     {
55          $this->maintenance = $maintenance;
56          $this->core = $maintenance->core;
57          $this->init();
58
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
64          $this->p_url = $maintenance->p_url;
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          }
76
77          $this->core->blog->settings->addNamespace('maintenance');
78          $ts = $this->core->blog->settings->maintenance->get('ts_'.$this->id);
79
80          $this->ts = abs((integer) $ts);
81
82          return true;
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     /**
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     /**
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     /**
132      * Get timestamp between maintenances.
133      *
134      * @return     <b>intetger</b>     Timestamp
135      */
136     public function ts()
137     {
138          return $this->ts === false ? false : abs((integer) $this->ts);
139     }
140
141     /**
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     /**
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     /**
194      * Get task description.
195      *
196      * @return     <b>string</b>  Description
197      */
198     public function description()
199     {
200          return $this->description;
201     }
202
203     /**
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     /**
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     /**
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     /**
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     }
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     {
333          $this->maintenance->setLog($this->id);
334     }
335
336     public function help()
337     {
338          return null;
339     }
340}
Note: See TracBrowser for help on using the repository browser.

Sites map