Dotclear

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

Revision 2044:4a3330bc8bd5, 5.5 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@defgroup PLUGIN_MAINTENANCE Maintenance plugin for Dotclear
16*/
17
18/**
19@ingroup PLUGIN_MAINTENANCE
20@nosubgrouping
21@brief Maintenance plugin core class
22
23Main class to call everything related to maintenance.
24*/
25class dcMaintenance
26{
27     public $core;
28     public $p_url = 'plugin.php?p=maintenance';
29
30     private $tasks = array();
31     private $tabs = array();
32     private $groups = array();
33     private $logs = null;
34
35     /**
36      * Constructor.
37      *
38      * @param core <b>dcCore</b>  dcCore instance
39      */
40     public function __construct($core)
41     {
42          $this->core = $core;
43          $logs = $this->getLogs();
44          $this->init();
45     }
46
47     /**
48      * Initialize list of tabs and groups and tasks.
49      *
50      * To register a tab or group or task,
51      * use behavior dcMaintenanceInit then a method of
52      * dcMaintenance like addTab('myTab', ...).
53      */
54     protected function init()
55     {
56          # --BEHAVIOR-- dcMaintenanceInit
57          $this->core->callBehavior('dcMaintenanceInit', $this);
58     }
59
60     /// @name Tab methods
61     //@{
62     /**
63      * Add a tab.
64      *
65      * @param id        <b>string<b> Tab ID
66      * @param name <b>string<b> Tab name
67      * @param options   <b>string<b> Options
68      * @return <b>dcMaintenance</b>    Self
69      */
70     public function addTab($id, $name, $options=array())
71     {
72          $this->tabs[$id] = new dcMaintenanceDescriptor($id, $name, $options);
73
74          return $this;
75     }
76
77     /**
78      * Get a tab.
79      *
80      * @param id   <b>string</b> Tab ID
81      * @return     <b>object</b> dcMaintenanceDescriptor of a tab
82      */
83     public function getTab($id)
84     {
85          return array_key_exists($id, $this->tabs) ? $this->tabs[$id] : null;
86     }
87
88     /**
89      * Get tabs.
90      *
91      * @return     <b>array</b> Array of tabs ID and name
92      */
93     public function getTabs()
94     {
95          return $this->tabs;
96     }
97     //@}
98
99
100     /// @name Group methods
101     //@{
102     /**
103      * Add a group.
104      *
105      * @param id        <b>string<b> Group ID
106      * @param name <b>string<b> Group name
107      * @param options   <b>string<b> Options
108      * @return <b>dcMaintenance</b>    Self
109      */
110     public function addGroup($id, $name, $options=array())
111     {
112          $this->groups[$id] = new dcMaintenanceDescriptor($id, $name, $options);
113
114          return $this;
115     }
116
117     /**
118      * Get a group.
119      *
120      * @param id   <b>string</b> Group ID
121      * @return     <b>object</b> dcMaintenanceDescriptor of a group
122      */
123     public function getGroup($id)
124     {
125          return array_key_exists($id, $this->groups) ? $this->groups[$id] : null;
126     }
127
128     /**
129      * Get groups.
130      *
131      * @return     <b>array</b> Array of groups ID and descriptor
132      */
133     public function getGroups()
134     {
135          return $this->groups;
136     }
137     //@}
138
139
140     /// @name Task methods
141     //@{
142     /**
143      * Add a task.
144      *
145      * @param task <b>mixed<b> Class name or object
146      * @return     <b>boolean</b> True if it is added
147      * @return <b>dcMaintenance</b>    Self
148      */
149     public function addTask($task)
150     {
151          if (class_exists($task) && is_subclass_of($task, 'dcMaintenanceTask')) {
152               $this->tasks[$task] = new $task($this);
153          }
154
155          return $this;
156     }
157
158     /**
159      * Get a task object.
160      *
161      * @param id   <b>string</b> task ID
162      * @return     <b>mixed</b> Task object or null if not exists
163      */
164     public function getTask($id)
165     {
166          return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null;
167     }
168
169     /**
170      * Get tasks.
171      *
172      * @return     <b>array</b> Array of tasks objects
173      */
174     public function getTasks()
175     {
176          return $this->tasks;
177     }
178
179     /**
180      * Get headers for plugin maintenance admin page.
181      *
182      * @return     <b>string</b> Page headers
183      */
184     public function getHeaders()
185     {
186          $res = '';
187          foreach($this->tasks as $task)
188          {
189               $res .= $task->header();
190          }
191          return $res;
192     }
193     //@}
194
195
196     /// @name Log methods
197     //@{
198     /**
199      * Set log for a task.
200      *
201      * @param id   <b>string</b>  Task ID
202      */
203     public function setLog($id)
204     {
205          // Check if taks exists
206          if (!$this->getTask($id)) {
207               return null;
208          }
209
210          // Get logs from this task
211          $rs = $this->core->con->select (
212               'SELECT log_id '.
213               'FROM '.$this->core->prefix.'log '.
214               "WHERE log_msg = '".$this->core->con->escape($id)."' ".
215               "AND log_table = 'maintenance' "
216          );
217
218          $logs = array();
219          while ($rs->fetch()) {
220               $logs[] = $rs->log_id;
221          }
222
223          // Delete old logs
224          if (!empty($logs)) {
225               $this->core->log->delLogs($logs);
226          }
227
228          // Add new log
229          $cur = $this->core->con->openCursor($this->core->prefix.'log');
230
231          $cur->log_msg = $id;
232          $cur->log_table = 'maintenance';
233          $cur->user_id = $this->core->auth->userID();
234
235          $this->core->log->addLog($cur);
236     }
237
238     /**
239      * Delete all maintenance logs.
240      */
241     public function delLogs()
242     {
243          // Retrieve logs from this task
244          $rs = $this->core->log->getLogs(array(
245               'log_table' => 'maintenance',
246               'blog_id' => 'all'
247          ));
248
249          $logs = array();
250          while ($rs->fetch()) {
251               $logs[] = $rs->log_id;
252          }
253
254          // Delete old logs
255          if (!empty($logs)) {
256               $this->core->log->delLogs($logs);
257          }
258     }
259
260     /**
261      * Get logs
262      *
263      * Return array(
264      *        task id => array(
265      *             timestamp of last execution,
266      *             logged on current blog or not
267      *        )
268      * )
269      *
270      * @return     <b>array</b> List of logged tasks
271      */
272     public function getLogs()
273     {
274          if ($this->logs === null) {
275               $rs = $this->core->log->getLogs(array(
276                    'log_table' => 'maintenance',
277                    'blog_id' => 'all'
278               ));
279
280               $this->logs = array();
281               while ($rs->fetch()) {
282                    $this->logs[$rs->log_msg] = array(
283                         'ts' => strtotime($rs->log_dt),
284                         'blog' => $rs->blog_id == $this->core->blog->id
285                    );
286               }
287          }
288
289          return $this->logs;
290     }
291     //@}
292}
Note: See TracBrowser for help on using the repository browser.

Sites map