File: class.dc.maintenance.task.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (341 lines) | stat: -rw-r--r-- 6,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

/**
@ingroup PLUGIN_MAINTENANCE
@nosubgrouping
@brief Maintenance plugin task class.

Every task of maintenance must extend this class.
*/
class dcMaintenanceTask
{
	protected $maintenance;
	protected $core;
	protected $p_url;
	protected $code;
	protected $ts = 0;
	protected $expired = 0;
	protected $ajax = false;
	protected $blog = false;
	protected $perm = null;

	protected $id;
	protected $name;
	protected $description;
	protected $tab = 'maintenance';
	protected $group = 'other';

	protected $task;
	protected $step;
	protected $error;
	protected $success;

	/**
	 * Constructor.
	 *
	 * If your task required something on construct,
	 * use method init() to do it.
	 *
	 * @param	maintenance	<b>dcMaintenance</b>	dcMaintenance instance
	 * @param	p_url	<b>string</b>	Maintenance plugin url
	 */
	public function __construct($maintenance)
	{
		$this->maintenance = $maintenance;
		$this->core = $maintenance->core;
		$this->init();
		$this->id = null;

		if ($this->perm() === null && !$this->core->auth->isSuperAdmin()
		|| !$this->core->auth->check($this->perm(), $this->core->blog->id)) {
			return null;
		}

		$this->p_url = $maintenance->p_url;
		$this->id = get_class($this);

		if (!$this->name) {
			$this->name = get_class($this);
		}
		if (!$this->error) {
			$this->error = __('Failed to execute task.');
		}
		if (!$this->success) {
			$this->success = __('Task successfully executed.');
		}

		$this->core->blog->settings->addNamespace('maintenance');
		$ts = $this->core->blog->settings->maintenance->get('ts_'.$this->id);

		$this->ts = abs((integer) $ts);

		return true;
	}

	/**
	 * Initialize task object.
	 *
	 * Better to set translated messages here than
	 * to rewrite constructor.
	 */
	protected function init()
	{
		return null;
	}

	/**
	 * Get task permission.
	 *
	 * Return user permission required to run this task
	 * or null for super admin.
	 *
	 * @return <b>mixed</b> Permission.
	 */
	public function perm()
	{
		return $this->perm;
	}

	/**
	 * Get task scope.
	 *.
	 * Is task limited to current blog.
	 *
	 * @return <b>boolean</b> Limit to blog
	 */
	public function blog()
	{
		return $this->blog;
	}

	/**
	 * Set $code for task having multiple steps.
	 *
	 * @param	code	<b>integer</b>	Code used for task execution
	 */
	public function code($code)
	{
		$this->code = (integer) $code;
	}

	/**
	 * Get timestamp between maintenances.
	 *
	 * @return	<b>intetger</b>	Timestamp
	 */
	public function ts()
	{
		return $this->ts === false ? false : abs((integer) $this->ts);
	}

	/**
	 * Get task expired.
	 *
	 * This return:
	 * - Timstamp of last update if it expired
	 * - False if it not expired or has no recall time
	 * - Null if it has never been executed
	 *
	 * @return	<b>mixed</b>	Last update
	 */
	public function expired()
	{
		if ($this->expired === 0) {
			if (!$this->ts()) {
				$this->expired = false;
			}
			else {
				$this->expired = null;
				$logs = array();
				foreach($this->maintenance->getLogs() as $id => $log)
				{
					if ($id != $this->id() || $this->blog && !$log['blog']) {
						continue;
					}

					$this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false;
				}
			}
		}
		return $this->expired;
	}

	/**
	 * Get task ID.
	 *
	 * @return	<b>string</b>	Task ID (class name)
	 */
	public function id()
	{
		return $this->id;
	}

	/**
	 * Get task name.
	 *
	 * @return	<b>string</b>	Task name
	 */
	public function name()
	{
		return $this->name;
	}

	/**
	 * Get task description.
	 *
	 * @return	<b>string</b>	Description
	 */
	public function description()
	{
		return $this->description;
	}

	/**
	 * Get task tab.
	 *
	 * @return	<b>mixed</b>	Task tab ID or null
	 */
	public function tab()
	{
		return $this->tab;
	}

	/**
	 * Get task group.
	 *
	 * If task required a full tab,
	 * this must be returned null.
	 *
	 * @return	<b>mixed</b>	Task group ID or null
	 */
	public function group()
	{
		return $this->group;
	}

	/**
	 * Use ajax
	 *
	 * Is task use maintenance ajax script
	 * for steps process.
	 *
	 * @return	<b>boolean</b>	Use ajax
	 */
	public function ajax()
	{
		return (boolean) $this->ajax;
	}

	/**
	 * Get task message.
	 *
	 * This message is used on form button.
	 *
	 * @return	<b>string</b>	Message
	 */
	public function task()
	{
		return $this->task;
	}

	/**
	 * Get step message.
	 *
	 * This message is displayed during task step execution.
	 *
	 * @return	<b>mixed</b>	Message or null
	 */
	public function step()
	{
		return $this->step;
	}

	/**
	 * Get success message.
	 *
	 * This message is displayed when task is accomplished.
	 *
	 * @return	<b>mixed</b>	Message or null
	 */
	public function success()
	{
		return $this->success;
	}

	/**
	 * Get error message.
	 *
	 * This message is displayed on error.
	 *
	 * @return	<b>mixed</b>	Message or null
	 */
	public function error()
	{
		return $this->error;
	}

	/**
	 * Get header.
	 *
	 * Headers required on maintenance page.
	 *
	 * @return 	<b>mixed</b>	Message or null
	 */
	public function header()
	{
		return null;
	}

	/**
	 * Get content.
	 *
	 * Content for full tab task.
	 *
	 * @return	<b>string</b>	Tab's content
	 */
	public function content()
	{
		return null;
	}

	/**
	 * Execute task.
	 *
	 * @return	<b>mixed</b>	:
	 *	- FALSE on error,
	 *	- TRUE if task is finished
	 *	- INTEGER if task required a next step
	 */
	public function execute()
	{
		return null;
	}

	/**
	 * Log task execution.
	 *
	 * Sometimes we need to log task execution
	 * direct from task itself.
	 *
	 */
	protected function log()
	{
		$this->maintenance->setLog($this->id);
	}

	public function help()
	{
		return null;
	}
}