Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.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@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     private $tasks = array();
29     private $tabs = array();
30     private $groups = array();
31     private $logs = null;
32
33     /**
34      * Constructor.
35      *
36      * Here you register tasks and groups for tasks.
37      *
38      * @param core <b>dcCore</b>  dcCore instance
39      */
40     public function __construct($core)
41     {
42          $this->core = $core;
43
44          $tasks = new ArrayObject();
45          $tabs = new ArrayObject();
46          $groups = new ArrayObject();
47          $logs = $this->getLogs();
48
49          # --BEHAVIOR-- dcMaintenanceRegister
50          $core->callBehavior('dcMaintenanceRegister', $core, $tasks, $groups, $tabs);
51
52          $this->init($tasks, $groups, $tabs);
53     }
54
55     /**
56      * Initialize list of groups and tasks.
57      *
58      * @param tasks     <b>arrayObject</b>  Array of task to register
59      * @param groups    <b>arrayObject</b>  Array of groups to add
60      * @param tabs <b>arrayObject</b>  Array of tabs to add
61      */
62     public function init($tasks, $groups, $tabs)
63     {
64          $this->tasks = $this->groups = array();
65
66          foreach($tasks as $task)
67          {
68               if (!class_exists($task)) {
69                    continue;
70               }
71
72               $r = new ReflectionClass($task);
73               $p = $r->getParentClass();
74
75               if (!$p || $p->name != 'dcMaintenanceTask') {
76                    continue;
77               }
78
79               $this->tasks[$task] = new $task($this, 'plugin.php?p=maintenance');
80          }
81
82          foreach($groups as $id => $name)
83          {
84               $this->groups[(string) $id] = (string) $name;
85          }
86
87          foreach($tabs as $id => $name)
88          {
89               $this->tabs[(string) $id] = (string) $name;
90          }
91     }
92
93     /**
94      * Get a tab name.
95      *
96      * @param id   <b>string</b> Tab ID
97      * @return     <b>mixed</b> tab name or null if not exists
98      */
99     public function getTab($id)
100     {
101          return array_key_exists($id, $this->tabs) ? $this->tabs[$id] : null;
102     }
103
104     /**
105      * Get tabs.
106      *
107      * @return     <b>array</b> Array of tabs ID and name
108      */
109     public function getTabs()
110     {
111          return $this->tabs;
112     }
113
114     /**
115      * Get a group name.
116      *
117      * @param id   <b>string</b> Group ID
118      * @return     <b>mixed</b> Group name or null if not exists
119      */
120     public function getGroup($id)
121     {
122          return array_key_exists($id, $this->groups) ? $this->groups[$id] : null;
123     }
124
125     /**
126      * Get groups.
127      *
128      * @return     <b>array</b> Array of groups ID and name
129      */
130     public function getGroups()
131     {
132          return $this->groups;
133     }
134
135     /**
136      * Get a task object.
137      *
138      * @param id   <b>string</b> task ID
139      * @return     <b>mixed</b> Task object or null if not exists
140      */
141     public function getTask($id)
142     {
143          return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null;
144     }
145
146     /**
147      * Get tasks.
148      *
149      * @return     <b>array</b> Array of tasks objects
150      */
151     public function getTasks()
152     {
153          return $this->tasks;
154     }
155
156     /**
157      * Get headers for plugin maintenance admin page.
158      *
159      * @return     <b>string</b> Page headers
160      */
161     public function getHeaders()
162     {
163          $res = '';
164          foreach($this->tasks as $task)
165          {
166               $res .= $task->header();
167          }
168          return $res;
169     }
170
171     /**
172      * Set log for a task.
173      *
174      * @param id   <b>string</b>  Task ID
175      */
176     public function setLog($id)
177     {
178          // Check if taks exists
179          if (!$this->getTask($id)) {
180               return null;
181          }
182
183          // Get logs from this task
184          $rs = $this->core->con->select (
185               'SELECT log_id '.
186               'FROM '.$this->core->prefix.'log '.
187               "WHERE log_msg = '".$this->core->con->escape($id)."' ".
188               "AND log_table = 'maintenance' "
189          );
190
191          $logs = array();
192          while ($rs->fetch()) {
193               $logs[] = $rs->log_id;
194          }
195
196          // Delete old logs
197          if (!empty($logs)) {
198               $this->core->log->delLogs($logs);
199          }
200
201          // Add new log
202          $cur = $this->core->con->openCursor($this->core->prefix.'log');
203
204          $cur->log_msg = $id;
205          $cur->log_table = 'maintenance';
206          $cur->user_id = $this->core->auth->userID();
207
208          $this->core->log->addLog($cur);
209     }
210
211     /**
212      * Delete all maintenance logs.
213      */
214     public function delLogs()
215     {
216          // Retrieve logs from this task
217          $rs = $this->core->log->getLogs(array(
218               'log_table' => 'maintenance',
219               'blog_id' => 'all'
220          ));
221
222          $logs = array();
223          while ($rs->fetch()) {
224               $logs[] = $rs->log_id;
225          }
226
227          // Delete old logs
228          if (!empty($logs)) {
229               $this->core->log->delLogs($logs);
230          }
231     }
232
233     /**
234      * Get logs
235      *
236      * Return array(
237      *        task id => array(
238      *             timestamp of last execution,
239      *             logged on current blog or not
240      *        )
241      * )
242      *
243      * @return     <b>array</b> List of logged tasks
244      */
245     public function getLogs()
246     {
247          if ($this->logs === null) {
248               $rs = $this->core->log->getLogs(array(
249                    'log_table' => 'maintenance',
250                    'blog_id' => 'all'
251               ));
252
253               $this->logs = array();
254               while ($rs->fetch()) {
255                    $this->logs[$rs->log_msg] = array(
256                         'ts' => strtotime($rs->log_dt),
257                         'blog' => $rs->blog_id == $this->core->blog->id
258                    );
259               }
260          }
261
262          return $this->logs;
263     }
264}
Note: See TracBrowser for help on using the repository browser.

Sites map