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