Dotclear

source: plugins/maintenance/inc/class.dc.maintenance.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 6.3 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

short notation for array (array() → [])

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

Sites map