Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.php @ 1940:0fb7e85337bf

Revision 1940:0fb7e85337bf, 4.4 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

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

Sites map