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() → [])

RevLine 
[1925]1<?php
[3731]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
[3730]12if (!defined('DC_RC_PATH')) {return;}
[1925]13
14/**
15Main class to call everything related to maintenance.
[3730]16 */
[1925]17class dcMaintenance
18{
[3730]19    public $core;
20    public $p_url;
[2044]21
[3874]22    private $tasks  = [];
23    private $tabs   = [];
24    private $groups = [];
[3730]25    private $logs   = null;
[1925]26
[3730]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    }
[1925]39
[3730]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    }
[1925]52
[3730]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     */
[3874]63    public function addTab($id, $name, $options = [])
[3730]64    {
65        $this->tabs[$id] = new dcMaintenanceDescriptor($id, $name, $options);
[1925]66
[3730]67        return $this;
68    }
[1955]69
[3730]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    }
[1955]80
[3730]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    //@}
[2044]91
[3730]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     */
[3874]102    public function addGroup($id, $name, $options = [])
[3730]103    {
104        $this->groups[$id] = new dcMaintenanceDescriptor($id, $name, $options);
[2044]105
[3730]106        return $this;
107    }
[2044]108
[3730]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    }
[1925]119
[3730]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    //@}
[1925]130
[3730]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        }
[2044]145
[3730]146        return $this;
147    }
[2044]148
[3730]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    }
[2044]159
[3730]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    }
[1925]169
[3730]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    //@}
[1925]184
[3730]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        }
[1925]198
[3730]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        );
[1940]206
[3874]207        $logs = [];
[3730]208        while ($rs->fetch()) {
209            $logs[] = $rs->log_id;
210        }
[2044]211
[3730]212        // Delete old logs
213        if (!empty($logs)) {
214            $this->core->log->delLogs($logs);
215        }
[1940]216
[3730]217        // Add new log
218        $cur = $this->core->con->openCursor($this->core->prefix . 'log');
[1940]219
[3730]220        $cur->log_msg   = $id;
221        $cur->log_table = 'maintenance';
222        $cur->user_id   = $this->core->auth->userID();
[1940]223
[3730]224        $this->core->log->addLog($cur);
225    }
[1940]226
[3730]227    /**
228     * Delete all maintenance logs.
229     */
230    public function delLogs()
231    {
232        // Retrieve logs from this task
[3874]233        $rs = $this->core->log->getLogs([
[3730]234            'log_table' => 'maintenance',
235            'blog_id'   => 'all'
[3874]236        ]);
[1940]237
[3874]238        $logs = [];
[3730]239        while ($rs->fetch()) {
240            $logs[] = $rs->log_id;
241        }
[1940]242
[3730]243        // Delete old logs
244        if (!empty($logs)) {
245            $this->core->log->delLogs($logs);
246        }
247    }
[1940]248
[3730]249    /**
250     * Get logs
251     *
[3874]252     * Return [
253     *        task id => [
[3730]254     *            timestamp of last execution,
255     *            logged on current blog or not
[3874]256     *        ]
257     * ]
[3730]258     *
259     * @return    <b>array</b> List of logged tasks
260     */
261    public function getLogs()
262    {
263        if ($this->logs === null) {
[3874]264            $rs = $this->core->log->getLogs([
[3730]265                'log_table' => 'maintenance',
266                'blog_id'   => 'all'
[3874]267            ]);
[1940]268
[3874]269            $this->logs = [];
[3730]270            while ($rs->fetch()) {
[3874]271                $this->logs[$rs->log_msg] = [
[3730]272                    'ts'   => strtotime($rs->log_dt),
273                    'blog' => $rs->blog_id == $this->core->blog->id
[3874]274                ];
[3730]275            }
276        }
[1940]277
[3730]278        return $this->logs;
279    }
280    //@}
[1925]281}
Note: See TracBrowser for help on using the repository browser.

Sites map