Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.php @ 1989:b234959829da

Revision 1989:b234959829da, 5.2 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Revamp plugin maintenance, step 5, now open to admin user and various fixes, 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               if (($t = new $task($this, 'plugin.php?p=maintenance')) === null
80               || $t->perm() === null && !$this->core->auth->isSuperAdmin()
81               || !$this->core->auth->check($t->perm(), $this->core->blog->id)) {
82                    continue;
83               }
84
85               $this->tasks[$task] = $t;
86          }
87
88          foreach($groups as $id => $name)
89          {
90               $this->groups[(string) $id] = (string) $name;
91          }
92
93          foreach($tabs as $id => $name)
94          {
95               $this->tabs[(string) $id] = (string) $name;
96          }
97     }
98
99     /**
100      * Get a tab name.
101      *
102      * @param id   <b>string</b> Tab ID
103      * @return     <b>mixed</b> tab name or null if not exists
104      */
105     public function getTab($id)
106     {
107          return array_key_exists($id, $this->tabs) ? $this->tabs[$id] : null;
108     }
109
110     /**
111      * Get tabs.
112      *
113      * @return     <b>array</b> Array of tabs ID and name
114      */
115     public function getTabs()
116     {
117          return $this->tabs;
118     }
119
120     /**
121      * Get a group name.
122      *
123      * @param id   <b>string</b> Group ID
124      * @return     <b>mixed</b> Group name or null if not exists
125      */
126     public function getGroup($id)
127     {
128          return array_key_exists($id, $this->groups) ? $this->groups[$id] : null;
129     }
130
131     /**
132      * Get groups.
133      *
134      * @return     <b>array</b> Array of groups ID and name
135      */
136     public function getGroups()
137     {
138          return $this->groups;
139     }
140
141     /**
142      * Get a task object.
143      *
144      * @param id   <b>string</b> task ID
145      * @return     <b>mixed</b> Task object or null if not exists
146      */
147     public function getTask($id)
148     {
149          return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null;
150     }
151
152     /**
153      * Get tasks.
154      *
155      * @return     <b>array</b> Array of tasks objects
156      */
157     public function getTasks()
158     {
159          return $this->tasks;
160     }
161
162     /**
163      * Get headers for plugin maintenance admin page.
164      *
165      * @return     <b>string</b> Page headers
166      */
167     public function getHeaders()
168     {
169          $res = '';
170          foreach($this->tasks as $task)
171          {
172               $res .= $task->header();
173          }
174          return $res;
175     }
176
177     /**
178      * Set log for a task.
179      *
180      * @param id   <b>string</b>  Task ID
181      */
182     public function setLog($id)
183     {
184          // Check if taks exists
185          if (!$this->getTask($id)) {
186               return null;
187          }
188
189          // Get logs from this task
190          $rs = $this->core->con->select (
191               'SELECT log_id '.
192               'FROM '.$this->core->prefix.'log '.
193               "WHERE log_msg = '".$this->core->con->escape($id)."' ".
194               "AND log_table = 'maintenance' "
195          );
196
197          $logs = array();
198          while ($rs->fetch()) {
199               $logs[] = $rs->log_id;
200          }
201
202          // Delete old logs
203          if (!empty($logs)) {
204               $this->core->log->delLogs($logs);
205          }
206
207          // Add new log
208          $cur = $this->core->con->openCursor($this->core->prefix.'log');
209
210          $cur->log_msg = $id;
211          $cur->log_table = 'maintenance';
212          $cur->user_id = $this->core->auth->userID();
213
214          $this->core->log->addLog($cur);
215     }
216
217     /**
218      * Delete all maintenance logs.
219      */
220     public function delLogs()
221     {
222          // Retrieve logs from this task
223          $rs = $this->core->log->getLogs(array(
224               'log_table' => 'maintenance',
225               'blog_id' => 'all'
226          ));
227
228          $logs = array();
229          while ($rs->fetch()) {
230               $logs[] = $rs->log_id;
231          }
232
233          // Delete old logs
234          if (!empty($logs)) {
235               $this->core->log->delLogs($logs);
236          }
237     }
238
239     /**
240      * Get logs
241      *
242      * Return array(
243      *        task id => array(
244      *             timestamp of last execution,
245      *             logged on current blog or not
246      *        )
247      * )
248      *
249      * @return     <b>array</b> List of logged tasks
250      */
251     public function getLogs()
252     {
253          if ($this->logs === null) {
254               $rs = $this->core->log->getLogs(array(
255                    'log_table' => 'maintenance',
256                    'blog_id' => 'all'
257               ));
258
259               $this->logs = array();
260               while ($rs->fetch()) {
261                    $this->logs[$rs->log_msg] = array(
262                         'ts' => strtotime($rs->log_dt),
263                         'blog' => $rs->blog_id == $this->core->blog->id
264                    );
265               }
266          }
267
268          return $this->logs;
269     }
270}
Note: See TracBrowser for help on using the repository browser.

Sites map