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