File: class.dc.maintenance.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 (292 lines) | stat: -rw-r--r-- 5,615 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
<?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; }

/**
@defgroup PLUGIN_MAINTENANCE Maintenance plugin for Dotclear
*/

/**
@ingroup PLUGIN_MAINTENANCE
@nosubgrouping
@brief Maintenance plugin core class

Main class to call everything related to maintenance.
*/
class dcMaintenance
{
	public $core;
	public $p_url = 'plugin.php?p=maintenance';

	private $tasks = array();
	private $tabs = array();
	private $groups = array();
	private $logs = null;

	/**
	 * Constructor.
	 *
	 * @param	core	<b>dcCore</b>	dcCore instance
	 */
	public function __construct($core)
	{
		$this->core = $core;
		$logs = $this->getLogs();
		$this->init();
	}

	/**
	 * Initialize list of tabs and groups and tasks.
	 *
	 * To register a tab or group or task,
	 * use behavior dcMaintenanceInit then a method of
	 * dcMaintenance like addTab('myTab', ...).
	 */
	protected function init()
	{
		# --BEHAVIOR-- dcMaintenanceInit
		$this->core->callBehavior('dcMaintenanceInit', $this);
	}

	/// @name Tab methods
	//@{
	/**
	 * Add a tab.
	 *
	 * @param	id		<b>string<b> Tab ID
	 * @param	name	<b>string<b> Tab name
	 * @param	options	<b>string<b> Options
	 * @return <b>dcMaintenance</b>	Self
	 */
	public function addTab($id, $name, $options=array())
	{
		$this->tabs[$id] = new dcMaintenanceDescriptor($id, $name, $options);

		return $this;
	}

	/**
	 * Get a tab.
	 *
	 * @param	id	<b>string</b> Tab ID
	 * @return	<b>object</b> dcMaintenanceDescriptor of a tab
	 */
	public function getTab($id)
	{
		return array_key_exists($id, $this->tabs) ? $this->tabs[$id] : null;
	}

	/**
	 * Get tabs.
	 *
	 * @return	<b>array</b> Array of tabs ID and name
	 */
	public function getTabs()
	{
		return $this->tabs;
	}
	//@}


	/// @name Group methods
	//@{
	/**
	 * Add a group.
	 *
	 * @param	id		<b>string<b> Group ID
	 * @param	name	<b>string<b> Group name
	 * @param	options	<b>string<b> Options
	 * @return <b>dcMaintenance</b>	Self
	 */
	public function addGroup($id, $name, $options=array())
	{
		$this->groups[$id] = new dcMaintenanceDescriptor($id, $name, $options);

		return $this;
	}

	/**
	 * Get a group.
	 *
	 * @param	id	<b>string</b> Group ID
	 * @return	<b>object</b> dcMaintenanceDescriptor of a group
	 */
	public function getGroup($id)
	{
		return array_key_exists($id, $this->groups) ? $this->groups[$id] : null;
	}

	/**
	 * Get groups.
	 *
	 * @return	<b>array</b> Array of groups ID and descriptor
	 */
	public function getGroups()
	{
		return $this->groups;
	}
	//@}


	/// @name Task methods
	//@{
	/**
	 * Add a task.
	 *
	 * @param	task <b>mixed<b> Class name or object
	 * @return	<b>boolean</b>	True if it is added
	 * @return <b>dcMaintenance</b>	Self
	 */
	public function addTask($task)
	{
		if (class_exists($task) && is_subclass_of($task, 'dcMaintenanceTask')) {
			$this->tasks[$task] = new $task($this);
		}

		return $this;
	}

	/**
	 * Get a task object.
	 *
	 * @param	id	<b>string</b> task ID
	 * @return	<b>mixed</b> Task object or null if not exists
	 */
	public function getTask($id)
	{
		return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null;
	}

	/**
	 * Get tasks.
	 *
	 * @return	<b>array</b> Array of tasks objects
	 */
	public function getTasks()
	{
		return $this->tasks;
	}

	/**
	 * Get headers for plugin maintenance admin page.
	 *
	 * @return	<b>string</b> Page headers
	 */
	public function getHeaders()
	{
		$res = '';
		foreach($this->tasks as $task)
		{
			$res .= $task->header();
		}
		return $res;
	}
	//@}


	/// @name Log methods
	//@{
	/**
	 * Set log for a task.
	 *
	 * @param	id	<b>string</b>	Task ID
	 */
	public function setLog($id)
	{
		// Check if taks exists
		if (!$this->getTask($id)) {
			return null;
		}

		// Get logs from this task
		$rs = $this->core->con->select (
			'SELECT log_id '.
			'FROM '.$this->core->prefix.'log '.
			"WHERE log_msg = '".$this->core->con->escape($id)."' ".
			"AND log_table = 'maintenance' "
		);

		$logs = array();
		while ($rs->fetch()) {
			$logs[] = $rs->log_id;
		}

		// Delete old logs
		if (!empty($logs)) {
			$this->core->log->delLogs($logs);
		}

		// Add new log
		$cur = $this->core->con->openCursor($this->core->prefix.'log');

		$cur->log_msg = $id;
		$cur->log_table = 'maintenance';
		$cur->user_id = $this->core->auth->userID();

		$this->core->log->addLog($cur);
	}

	/**
	 * Delete all maintenance logs.
	 */
	public function delLogs()
	{
		// Retrieve logs from this task
		$rs = $this->core->log->getLogs(array(
			'log_table' => 'maintenance',
			'blog_id' => 'all'
		));

		$logs = array();
		while ($rs->fetch()) {
			$logs[] = $rs->log_id;
		}

		// Delete old logs
		if (!empty($logs)) {
			$this->core->log->delLogs($logs);
		}
	}

	/**
	 * Get logs
	 *
	 * Return array(
	 *		task id => array(
	 *			timestamp of last execution,
	 *			logged on current blog or not
	 *		)
	 * )
	 *
	 * @return	<b>array</b> List of logged tasks
	 */
	public function getLogs()
	{
		if ($this->logs === null) {
			$rs = $this->core->log->getLogs(array(
				'log_table' => 'maintenance',
				'blog_id' => 'all'
			));

			$this->logs = array();
			while ($rs->fetch()) {
				$this->logs[$rs->log_msg] = array(
					'ts' => strtotime($rs->log_dt),
					'blog' => $rs->blog_id == $this->core->blog->id
				);
			}
		}

		return $this->logs;
	}
	//@}
}