Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.task.php @ 1984:0b0cb9cd7da7

Revision 1984:0b0cb9cd7da7, 5.1 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Revamp plugin maintenance, step 4 bis, better place for settings according to boss and some typo, 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
32     protected $id;
33     protected $name;
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, $p_url)
52     {
53          $this->maintenance = $maintenance;
54          $this->core = $maintenance->core;
55          $this->init();
56
57          $this->p_url = $p_url;
58          $this->id = get_class($this);
59
60          if (!$this->name) {
61               $this->name = get_class($this);
62          }
63          if (!$this->error) {
64               $this->error = __('Failed to execute task.');
65          }
66          if (!$this->success) {
67               $this->success = __('Task successfully executed.');
68          }
69
70          $this->core->blog->settings->addNamespace('maintenance');
71          $ts = $this->core->blog->settings->maintenance->get('ts_'.$this->id);
72
73          $this->ts = abs((integer) $ts);
74     }
75
76     /**
77      * Initialize task object.
78      *
79      * Better to set translated messages here than
80      * to rewrite constructor.
81      */
82     protected function init()
83     {
84          return null;
85     }
86
87     /**
88      * Set $code for task having multiple steps.
89      *
90      * @param code <b>integer</b> Code used for task execution
91      */
92     public function code($code)
93     {
94          $this->code = (integer) $code;
95     }
96
97     /**
98      * Get timestamp between maintenances.
99      *
100      * @return     <b>intetger</b>     Timestamp
101      */
102     public function ts()
103     {
104          return $this->ts === false ? false : abs((integer) $this->ts);
105     }
106
107     /**
108      * Get task expired.
109      *
110      * This return:
111      * - Timstamp of last update if it expired
112      * - False if it not expired or has no recall time
113      * - Null if it has never been executed
114      *
115      * @return     <b>mixed</b>   Last update
116      */
117     public function expired()
118     {
119          if ($this->expired === 0) {
120               if (!$this->ts()) {
121                    $this->expired = false;
122               }
123               else {
124                    $this->expired = null;
125                    $logs = array();
126                    foreach($this->maintenance->getLogs() as $id => $log)
127                    {
128                         if ($id != $this->id() || $this->blog && !$log['blog']) {
129                              continue;
130                         }
131
132                         $this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false;
133                    }
134               }
135          }
136          return $this->expired;
137     }
138
139     /**
140      * Get task ID.
141      *
142      * @return     <b>string</b>  Task ID (class name)
143      */
144     public function id()
145     {
146          return $this->id;
147     }
148
149     /**
150      * Get task name.
151      *
152      * @return     <b>string</b>  Task name
153      */
154     public function name()
155     {
156          return $this->name;
157     }
158
159     /**
160      * Get task tab.
161      *
162      * @return     <b>mixed</b>   Task tab ID or null
163      */
164     public function tab()
165     {
166          return $this->tab;
167     }
168
169     /**
170      * Get task group.
171      *
172      * If task required a full tab,
173      * this must be returned null.
174      *
175      * @return     <b>mixed</b>   Task group ID or null
176      */
177     public function group()
178     {
179          return $this->group;
180     }
181
182     /**
183      * Use ajax
184      *
185      * Is task use maintenance ajax script
186      * for steps process.
187      *
188      * @return     <b>boolean</b> Use ajax
189      */
190     public function ajax()
191     {
192          return (boolean) $this->ajax;
193     }
194
195     /**
196      * Get task message.
197      *
198      * This message is used on form button.
199      *
200      * @return     <b>string</b>  Message
201      */
202     public function task()
203     {
204          return $this->task;
205     }
206
207     /**
208      * Get step message.
209      *
210      * This message is displayed during task step execution.
211      *
212      * @return     <b>mixed</b>   Message or null
213      */
214     public function step()
215     {
216          return $this->step;
217     }
218
219     /**
220      * Get success message.
221      *
222      * This message is displayed when task is accomplished.
223      *
224      * @return     <b>mixed</b>   Message or null
225      */
226     public function success()
227     {
228          return $this->success;
229     }
230
231     /**
232      * Get error message.
233      *
234      * This message is displayed on error.
235      *
236      * @return     <b>mixed</b>   Message or null
237      */
238     public function error()
239     {
240          return $this->error;
241     }
242
243     /**
244      * Get header.
245      *
246      * Headers required on maintenance page.
247      *
248      * @return     <b>mixed</b>   Message or null
249      */
250     public function header()
251     {
252          return null;
253     }
254
255     /**
256      * Get content.
257      *
258      * Content for full tab task.
259      *
260      * @return     <b>string</b>  Tab's content
261      */
262     public function content()
263     {
264          return null;
265     }
266
267     /**
268      * Execute task.
269      *
270      * @return     <b>mixed</b>   :
271      *   - FALSE on error,
272      *   - TRUE if task is finished
273      *   - INTEGER if task required a next step
274      */
275     public function execute()
276     {
277          return null;
278     }
279
280     /**
281      * Log task execution.
282      *
283      * Sometimes we need to log task execution
284      * direct from task itself.
285      *
286      */
287     protected function log()
288     {
289          $this->maintenance->setLog($this->id);
290     }
291}
Note: See TracBrowser for help on using the repository browser.

Sites map