| 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 | { |
|---|
| 27 | public $core; |
|---|
| 28 | public $p_url; |
|---|
| 29 | |
|---|
| 30 | private $tasks = array(); |
|---|
| 31 | private $tabs = array(); |
|---|
| 32 | private $groups = array(); |
|---|
| 33 | private $logs = null; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Constructor. |
|---|
| 37 | * |
|---|
| 38 | * @param core <b>dcCore</b> dcCore instance |
|---|
| 39 | */ |
|---|
| 40 | public function __construct($core) |
|---|
| 41 | { |
|---|
| 42 | $this->core = $core; |
|---|
| 43 | $this->p_url = $core->adminurl->get('admin.plugin.maintenance'); |
|---|
| 44 | $logs = $this->getLogs(); |
|---|
| 45 | $this->init(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Initialize list of tabs and groups and tasks. |
|---|
| 50 | * |
|---|
| 51 | * To register a tab or group or task, |
|---|
| 52 | * use behavior dcMaintenanceInit then a method of |
|---|
| 53 | * dcMaintenance like addTab('myTab', ...). |
|---|
| 54 | */ |
|---|
| 55 | protected function init() |
|---|
| 56 | { |
|---|
| 57 | # --BEHAVIOR-- dcMaintenanceInit |
|---|
| 58 | $this->core->callBehavior('dcMaintenanceInit', $this); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 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); |
|---|
| 74 | |
|---|
| 75 | return $this; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Get a tab. |
|---|
| 80 | * |
|---|
| 81 | * @param id <b>string</b> Tab ID |
|---|
| 82 | * @return <b>object</b> dcMaintenanceDescriptor of a tab |
|---|
| 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; |
|---|
| 97 | } |
|---|
| 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 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Get a group. |
|---|
| 120 | * |
|---|
| 121 | * @param id <b>string</b> Group ID |
|---|
| 122 | * @return <b>object</b> dcMaintenanceDescriptor of a group |
|---|
| 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 | * |
|---|
| 132 | * @return <b>array</b> Array of groups ID and descriptor |
|---|
| 133 | */ |
|---|
| 134 | public function getGroups() |
|---|
| 135 | { |
|---|
| 136 | return $this->groups; |
|---|
| 137 | } |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 194 | //@} |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | /// @name Log methods |
|---|
| 198 | //@{ |
|---|
| 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 | /** |
|---|
| 262 | * Get logs |
|---|
| 263 | * |
|---|
| 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 |
|---|
| 272 | */ |
|---|
| 273 | public function getLogs() |
|---|
| 274 | { |
|---|
| 275 | if ($this->logs === null) { |
|---|
| 276 | $rs = $this->core->log->getLogs(array( |
|---|
| 277 | 'log_table' => 'maintenance', |
|---|
| 278 | 'blog_id' => 'all' |
|---|
| 279 | )); |
|---|
| 280 | |
|---|
| 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 | ); |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | return $this->logs; |
|---|
| 291 | } |
|---|
| 292 | //@} |
|---|
| 293 | } |
|---|