Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.php @ 1955:b0bef03695c0

Revision 1955:b0bef03695c0, 5.0 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

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

Sites map