Changeset 3730:5c45a5df9a59 for plugins/maintenance/inc
- Timestamp:
- 03/08/18 17:58:39 (8 years ago)
- Branch:
- default
- Location:
- plugins/maintenance/inc
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/maintenance/inc/class.dc.maintenance.descriptor.php
r2566 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 /** … … 19 19 At this time this class is used in same way an arrayObject 20 20 but in futur it could be completed with advance methods. 21 */21 */ 22 22 class dcMaintenanceDescriptor 23 23 { 24 25 26 24 protected $id; 25 protected $name; 26 protected $options; 27 27 28 29 30 31 * @param id<b>string<b> Tab ID32 * @param name<b>string<b> Tab name33 * @param options<b>string<b> Options34 35 public function __construct($id, $name, $options=array())36 37 $this->id= (string) $id;38 $this->name= (string) $name;39 40 28 /** 29 * Constructor (really ?!). 30 * 31 * @param id <b>string<b> Tab ID 32 * @param name <b>string<b> Tab name 33 * @param options <b>string<b> Options 34 */ 35 public function __construct($id, $name, $options = array()) 36 { 37 $this->id = (string) $id; 38 $this->name = (string) $name; 39 $this->options = (array) $options; 40 } 41 41 42 43 44 45 * @return <b>string</b>ID46 47 48 49 50 42 /** 43 * Get ID. 44 * 45 * @return <b>string</b> ID 46 */ 47 public function id() 48 { 49 return $this->id; 50 } 51 51 52 53 54 55 * @return <b>string</b>Name56 57 58 59 60 52 /** 53 * Get name. 54 * 55 * @return <b>string</b> Name 56 */ 57 public function name() 58 { 59 return $this->name; 60 } 61 61 62 63 64 65 66 67 * @param key<b>string<b> Option key68 * @return <b>string</b>Option value69 70 71 72 73 62 /** 63 * Get option. 64 * 65 * Option called "summary" and "description" are used. 66 * 67 * @param key <b>string<b> Option key 68 * @return <b>string</b> Option value 69 */ 70 public function option($key) 71 { 72 return isset($this->options[$key]) ? $this->options[$key] : null; 73 } 74 74 75 76 77 78 79 75 /* @ignore */ 76 public function __get($key) 77 { 78 return $this->option($key); 79 } 80 80 81 82 83 84 85 81 /* @ignore */ 82 public function __isset($key) 83 { 84 return isset($this->options[$key]); 85 } 86 86 } -
plugins/maintenance/inc/class.dc.maintenance.php
r2825 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 /** 15 15 @defgroup PLUGIN_MAINTENANCE Maintenance plugin for Dotclear 16 */16 */ 17 17 18 18 /** … … 22 22 23 23 Main class to call everything related to maintenance. 24 */24 */ 25 25 class dcMaintenance 26 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 //@} 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 /// @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 } 116 117 /** 118 * Get a group. 119 * 120 * @param id <b>string</b> Group ID 121 * @return <b>object</b> dcMaintenanceDescriptor of a group 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 * 131 * @return <b>array</b> Array of groups ID and descriptor 132 */ 133 public function getGroups() 134 { 135 return $this->groups; 136 } 137 //@} 138 139 /// @name Task methods 140 //@{ 141 /** 142 * Add a task. 143 * 144 * @param task <b>mixed<b> Class name or object 145 * @return <b>boolean</b> True if it is added 146 * @return <b>dcMaintenance</b> Self 147 */ 148 public function addTask($task) 149 { 150 if (class_exists($task) && is_subclass_of($task, 'dcMaintenanceTask')) { 151 $this->tasks[$task] = new $task($this); 152 } 153 154 return $this; 155 } 156 157 /** 158 * Get a task object. 159 * 160 * @param id <b>string</b> task ID 161 * @return <b>mixed</b> Task object or null if not exists 162 */ 163 public function getTask($id) 164 { 165 return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null; 166 } 167 168 /** 169 * Get tasks. 170 * 171 * @return <b>array</b> Array of tasks objects 172 */ 173 public function getTasks() 174 { 175 return $this->tasks; 176 } 177 178 /** 179 * Get headers for plugin maintenance admin page. 180 * 181 * @return <b>string</b> Page headers 182 */ 183 public function getHeaders() 184 { 185 $res = ''; 186 foreach ($this->tasks as $task) { 187 $res .= $task->header(); 188 } 189 return $res; 190 } 191 //@} 192 193 /// @name Log methods 194 //@{ 195 /** 196 * Set log for a task. 197 * 198 * @param id <b>string</b> Task ID 199 */ 200 public function setLog($id) 201 { 202 // Check if taks exists 203 if (!$this->getTask($id)) { 204 return; 205 } 206 207 // Get logs from this task 208 $rs = $this->core->con->select( 209 'SELECT log_id ' . 210 'FROM ' . $this->core->prefix . 'log ' . 211 "WHERE log_msg = '" . $this->core->con->escape($id) . "' " . 212 "AND log_table = 'maintenance' " 213 ); 214 215 $logs = array(); 216 while ($rs->fetch()) { 217 $logs[] = $rs->log_id; 218 } 219 220 // Delete old logs 221 if (!empty($logs)) { 222 $this->core->log->delLogs($logs); 223 } 224 225 // Add new log 226 $cur = $this->core->con->openCursor($this->core->prefix . 'log'); 227 228 $cur->log_msg = $id; 229 $cur->log_table = 'maintenance'; 230 $cur->user_id = $this->core->auth->userID(); 231 232 $this->core->log->addLog($cur); 233 } 234 235 /** 236 * Delete all maintenance logs. 237 */ 238 public function delLogs() 239 { 240 // Retrieve logs from this task 241 $rs = $this->core->log->getLogs(array( 242 'log_table' => 'maintenance', 243 'blog_id' => 'all' 244 )); 245 246 $logs = array(); 247 while ($rs->fetch()) { 248 $logs[] = $rs->log_id; 249 } 250 251 // Delete old logs 252 if (!empty($logs)) { 253 $this->core->log->delLogs($logs); 254 } 255 } 256 257 /** 258 * Get logs 259 * 260 * Return array( 261 * task id => array( 262 * timestamp of last execution, 263 * logged on current blog or not 264 * ) 265 * ) 266 * 267 * @return <b>array</b> List of logged tasks 268 */ 269 public function getLogs() 270 { 271 if ($this->logs === null) { 272 $rs = $this->core->log->getLogs(array( 273 'log_table' => 'maintenance', 274 'blog_id' => 'all' 275 )); 276 277 $this->logs = array(); 278 while ($rs->fetch()) { 279 $this->logs[$rs->log_msg] = array( 280 'ts' => strtotime($rs->log_dt), 281 'blog' => $rs->blog_id == $this->core->blog->id 282 ); 283 } 284 } 285 286 return $this->logs; 287 } 288 //@} 293 289 } -
plugins/maintenance/inc/class.dc.maintenance.task.php
r2566 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 /** … … 18 18 19 19 Every task of maintenance must extend this class. 20 */20 */ 21 21 class dcMaintenanceTask 22 22 { 23 protected $maintenance; 24 protected $core; 25 protected $p_url; 26 protected $code; 27 protected $ts = 0; 28 protected $expired = 0; 29 protected $ajax = false; 30 protected $blog = false; 31 protected $perm = null; 32 33 protected $id; 34 protected $name; 35 protected $description; 36 protected $tab = 'maintenance'; 37 protected $group = 'other'; 38 39 protected $task; 40 protected $step; 41 protected $error; 42 protected $success; 43 44 /** 45 * Constructor. 46 * 47 * If your task required something on construct, 48 * use method init() to do it. 49 * 50 * @param maintenance <b>dcMaintenance</b> dcMaintenance instance 51 * @param p_url <b>string</b> Maintenance plugin url 52 */ 53 public function __construct($maintenance) 54 { 55 $this->maintenance = $maintenance; 56 $this->core = $maintenance->core; 57 $this->init(); 58 $this->id = null; 59 60 if ($this->perm() === null && !$this->core->auth->isSuperAdmin() 61 || !$this->core->auth->check($this->perm(), $this->core->blog->id)) { 62 return null; 63 } 64 65 $this->p_url = $maintenance->p_url; 66 $this->id = get_class($this); 67 68 if (!$this->name) { 69 $this->name = get_class($this); 70 } 71 if (!$this->error) { 72 $this->error = __('Failed to execute task.'); 73 } 74 if (!$this->success) { 75 $this->success = __('Task successfully executed.'); 76 } 77 78 $this->core->blog->settings->addNamespace('maintenance'); 79 $ts = $this->core->blog->settings->maintenance->get('ts_'.$this->id); 80 81 $this->ts = abs((integer) $ts); 82 83 return true; 84 } 85 86 /** 87 * Initialize task object. 88 * 89 * Better to set translated messages here than 90 * to rewrite constructor. 91 */ 92 protected function init() 93 { 94 return null; 95 } 96 97 /** 98 * Get task permission. 99 * 100 * Return user permission required to run this task 101 * or null for super admin. 102 * 103 * @return <b>mixed</b> Permission. 104 */ 105 public function perm() 106 { 107 return $this->perm; 108 } 109 110 /** 111 * Get task scope. 112 *. 113 * Is task limited to current blog. 114 * 115 * @return <b>boolean</b> Limit to blog 116 */ 117 public function blog() 118 { 119 return $this->blog; 120 } 121 122 /** 123 * Set $code for task having multiple steps. 124 * 125 * @param code <b>integer</b> Code used for task execution 126 */ 127 public function code($code) 128 { 129 $this->code = (integer) $code; 130 } 131 132 /** 133 * Get timestamp between maintenances. 134 * 135 * @return <b>intetger</b> Timestamp 136 */ 137 public function ts() 138 { 139 return $this->ts === false ? false : abs((integer) $this->ts); 140 } 141 142 /** 143 * Get task expired. 144 * 145 * This return: 146 * - Timstamp of last update if it expired 147 * - False if it not expired or has no recall time 148 * - Null if it has never been executed 149 * 150 * @return <b>mixed</b> Last update 151 */ 152 public function expired() 153 { 154 if ($this->expired === 0) { 155 if (!$this->ts()) { 156 $this->expired = false; 157 } 158 else { 159 $this->expired = null; 160 $logs = array(); 161 foreach($this->maintenance->getLogs() as $id => $log) 162 { 163 if ($id != $this->id() || $this->blog && !$log['blog']) { 164 continue; 165 } 166 167 $this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false; 168 } 169 } 170 } 171 return $this->expired; 172 } 173 174 /** 175 * Get task ID. 176 * 177 * @return <b>string</b> Task ID (class name) 178 */ 179 public function id() 180 { 181 return $this->id; 182 } 183 184 /** 185 * Get task name. 186 * 187 * @return <b>string</b> Task name 188 */ 189 public function name() 190 { 191 return $this->name; 192 } 193 194 /** 195 * Get task description. 196 * 197 * @return <b>string</b> Description 198 */ 199 public function description() 200 { 201 return $this->description; 202 } 203 204 /** 205 * Get task tab. 206 * 207 * @return <b>mixed</b> Task tab ID or null 208 */ 209 public function tab() 210 { 211 return $this->tab; 212 } 213 214 /** 215 * Get task group. 216 * 217 * If task required a full tab, 218 * this must be returned null. 219 * 220 * @return <b>mixed</b> Task group ID or null 221 */ 222 public function group() 223 { 224 return $this->group; 225 } 226 227 /** 228 * Use ajax 229 * 230 * Is task use maintenance ajax script 231 * for steps process. 232 * 233 * @return <b>boolean</b> Use ajax 234 */ 235 public function ajax() 236 { 237 return (boolean) $this->ajax; 238 } 239 240 /** 241 * Get task message. 242 * 243 * This message is used on form button. 244 * 245 * @return <b>string</b> Message 246 */ 247 public function task() 248 { 249 return $this->task; 250 } 251 252 /** 253 * Get step message. 254 * 255 * This message is displayed during task step execution. 256 * 257 * @return <b>mixed</b> Message or null 258 */ 259 public function step() 260 { 261 return $this->step; 262 } 263 264 /** 265 * Get success message. 266 * 267 * This message is displayed when task is accomplished. 268 * 269 * @return <b>mixed</b> Message or null 270 */ 271 public function success() 272 { 273 return $this->success; 274 } 275 276 /** 277 * Get error message. 278 * 279 * This message is displayed on error. 280 * 281 * @return <b>mixed</b> Message or null 282 */ 283 public function error() 284 { 285 return $this->error; 286 } 287 288 /** 289 * Get header. 290 * 291 * Headers required on maintenance page. 292 * 293 * @return <b>mixed</b> Message or null 294 */ 295 public function header() 296 { 297 return null; 298 } 299 300 /** 301 * Get content. 302 * 303 * Content for full tab task. 304 * 305 * @return <b>string</b> Tab's content 306 */ 307 public function content() 308 { 309 return null; 310 } 311 312 /** 313 * Execute task. 314 * 315 * @return <b>mixed</b> : 316 * - FALSE on error, 317 * - TRUE if task is finished 318 * - INTEGER if task required a next step 319 */ 320 public function execute() 321 { 322 return null; 323 } 324 325 /** 326 * Log task execution. 327 * 328 * Sometimes we need to log task execution 329 * direct from task itself. 330 * 331 */ 332 protected function log() 333 { 334 $this->maintenance->setLog($this->id); 335 } 336 337 public function help() 338 { 339 return null; 340 } 23 protected $maintenance; 24 protected $core; 25 protected $p_url; 26 protected $code; 27 protected $ts = 0; 28 protected $expired = 0; 29 protected $ajax = false; 30 protected $blog = false; 31 protected $perm = null; 32 33 protected $id; 34 protected $name; 35 protected $description; 36 protected $tab = 'maintenance'; 37 protected $group = 'other'; 38 39 protected $task; 40 protected $step; 41 protected $error; 42 protected $success; 43 44 /** 45 * Constructor. 46 * 47 * If your task required something on construct, 48 * use method init() to do it. 49 * 50 * @param maintenance <b>dcMaintenance</b> dcMaintenance instance 51 * @param p_url <b>string</b> Maintenance plugin url 52 */ 53 public function __construct($maintenance) 54 { 55 $this->maintenance = $maintenance; 56 $this->core = $maintenance->core; 57 $this->init(); 58 $this->id = null; 59 60 if ($this->perm() === null && !$this->core->auth->isSuperAdmin() 61 || !$this->core->auth->check($this->perm(), $this->core->blog->id)) { 62 return; 63 } 64 65 $this->p_url = $maintenance->p_url; 66 $this->id = get_class($this); 67 68 if (!$this->name) { 69 $this->name = get_class($this); 70 } 71 if (!$this->error) { 72 $this->error = __('Failed to execute task.'); 73 } 74 if (!$this->success) { 75 $this->success = __('Task successfully executed.'); 76 } 77 78 $this->core->blog->settings->addNamespace('maintenance'); 79 $ts = $this->core->blog->settings->maintenance->get('ts_' . $this->id); 80 81 $this->ts = abs((integer) $ts); 82 83 return true; 84 } 85 86 /** 87 * Initialize task object. 88 * 89 * Better to set translated messages here than 90 * to rewrite constructor. 91 */ 92 protected function init() 93 { 94 return; 95 } 96 97 /** 98 * Get task permission. 99 * 100 * Return user permission required to run this task 101 * or null for super admin. 102 * 103 * @return <b>mixed</b> Permission. 104 */ 105 public function perm() 106 { 107 return $this->perm; 108 } 109 110 /** 111 * Get task scope. 112 *. 113 * Is task limited to current blog. 114 * 115 * @return <b>boolean</b> Limit to blog 116 */ 117 public function blog() 118 { 119 return $this->blog; 120 } 121 122 /** 123 * Set $code for task having multiple steps. 124 * 125 * @param code <b>integer</b> Code used for task execution 126 */ 127 public function code($code) 128 { 129 $this->code = (integer) $code; 130 } 131 132 /** 133 * Get timestamp between maintenances. 134 * 135 * @return <b>intetger</b> Timestamp 136 */ 137 public function ts() 138 { 139 return $this->ts === false ? false : abs((integer) $this->ts); 140 } 141 142 /** 143 * Get task expired. 144 * 145 * This return: 146 * - Timstamp of last update if it expired 147 * - False if it not expired or has no recall time 148 * - Null if it has never been executed 149 * 150 * @return <b>mixed</b> Last update 151 */ 152 public function expired() 153 { 154 if ($this->expired === 0) { 155 if (!$this->ts()) { 156 $this->expired = false; 157 } else { 158 $this->expired = null; 159 $logs = array(); 160 foreach ($this->maintenance->getLogs() as $id => $log) { 161 if ($id != $this->id() || $this->blog && !$log['blog']) { 162 continue; 163 } 164 165 $this->expired = $log['ts'] + $this->ts() < time() ? $log['ts'] : false; 166 } 167 } 168 } 169 return $this->expired; 170 } 171 172 /** 173 * Get task ID. 174 * 175 * @return <b>string</b> Task ID (class name) 176 */ 177 public function id() 178 { 179 return $this->id; 180 } 181 182 /** 183 * Get task name. 184 * 185 * @return <b>string</b> Task name 186 */ 187 public function name() 188 { 189 return $this->name; 190 } 191 192 /** 193 * Get task description. 194 * 195 * @return <b>string</b> Description 196 */ 197 public function description() 198 { 199 return $this->description; 200 } 201 202 /** 203 * Get task tab. 204 * 205 * @return <b>mixed</b> Task tab ID or null 206 */ 207 public function tab() 208 { 209 return $this->tab; 210 } 211 212 /** 213 * Get task group. 214 * 215 * If task required a full tab, 216 * this must be returned null. 217 * 218 * @return <b>mixed</b> Task group ID or null 219 */ 220 public function group() 221 { 222 return $this->group; 223 } 224 225 /** 226 * Use ajax 227 * 228 * Is task use maintenance ajax script 229 * for steps process. 230 * 231 * @return <b>boolean</b> Use ajax 232 */ 233 public function ajax() 234 { 235 return (boolean) $this->ajax; 236 } 237 238 /** 239 * Get task message. 240 * 241 * This message is used on form button. 242 * 243 * @return <b>string</b> Message 244 */ 245 public function task() 246 { 247 return $this->task; 248 } 249 250 /** 251 * Get step message. 252 * 253 * This message is displayed during task step execution. 254 * 255 * @return <b>mixed</b> Message or null 256 */ 257 public function step() 258 { 259 return $this->step; 260 } 261 262 /** 263 * Get success message. 264 * 265 * This message is displayed when task is accomplished. 266 * 267 * @return <b>mixed</b> Message or null 268 */ 269 public function success() 270 { 271 return $this->success; 272 } 273 274 /** 275 * Get error message. 276 * 277 * This message is displayed on error. 278 * 279 * @return <b>mixed</b> Message or null 280 */ 281 public function error() 282 { 283 return $this->error; 284 } 285 286 /** 287 * Get header. 288 * 289 * Headers required on maintenance page. 290 * 291 * @return <b>mixed</b> Message or null 292 */ 293 public function header() 294 { 295 return; 296 } 297 298 /** 299 * Get content. 300 * 301 * Content for full tab task. 302 * 303 * @return <b>string</b> Tab's content 304 */ 305 public function content() 306 { 307 return; 308 } 309 310 /** 311 * Execute task. 312 * 313 * @return <b>mixed</b> : 314 * - FALSE on error, 315 * - TRUE if task is finished 316 * - INTEGER if task required a next step 317 */ 318 public function execute() 319 { 320 return; 321 } 322 323 /** 324 * Log task execution. 325 * 326 * Sometimes we need to log task execution 327 * direct from task itself. 328 * 329 */ 330 protected function log() 331 { 332 $this->maintenance->setLog($this->id); 333 } 334 335 public function help() 336 { 337 return; 338 } 341 339 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.cache.php
r2044 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceCache extends dcMaintenanceTask 15 15 { 16 16 protected $group = 'purge'; 17 17 18 19 20 $this->task= __('Empty templates cache directory');21 $this->success= __('Templates cache directory emptied.');22 $this->error= __('Failed to empty templates cache directory.');18 protected function init() 19 { 20 $this->task = __('Empty templates cache directory'); 21 $this->success = __('Templates cache directory emptied.'); 22 $this->error = __('Failed to empty templates cache directory.'); 23 23 24 25 24 $this->description = __("It may be useful to empty this cache when modifying a theme's .html or .css files (or when updating a theme or plugin). Notice : with some hosters, the templates cache cannot be emptied with this plugin. You may then have to delete the directory <strong>/cbtpl/</strong> directly on the server with your FTP software."); 25 } 26 26 27 28 29 27 public function execute() 28 { 29 $this->core->emptyTemplatesCache(); 30 30 31 32 31 return true; 32 } 33 33 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.countcomments.php
r2044 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceCountcomments extends dcMaintenanceTask 15 15 { 16 16 protected $group = 'index'; 17 17 18 19 20 $this->task= __('Count again comments and trackbacks');21 $this->success= __('Comments and trackback counted.');22 $this->error= __('Failed to count comments and trackbacks.');18 protected function init() 19 { 20 $this->task = __('Count again comments and trackbacks'); 21 $this->success = __('Comments and trackback counted.'); 22 $this->error = __('Failed to count comments and trackbacks.'); 23 23 24 25 24 $this->description = __('Count again comments and trackbacks allows to check their exact numbers. This operation can be useful when importing from another blog platform (or when migrating from dotclear 1 to dotclear 2).'); 25 } 26 26 27 28 29 27 public function execute() 28 { 29 $this->core->countAllComments(); 30 30 31 32 31 return true; 32 } 33 33 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.csp.php
r3477 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceCSP extends dcMaintenanceTask 15 15 { 16 16 protected $group = 'purge'; 17 17 18 19 20 $this->task= __('Delete the Content-Security-Policy report file');21 $this->success= __('Content-Security-Policy report file has been deleted.');22 $this->error= __('Failed to delete the Content-Security-Policy report file.');18 protected function init() 19 { 20 $this->task = __('Delete the Content-Security-Policy report file'); 21 $this->success = __('Content-Security-Policy report file has been deleted.'); 22 $this->error = __('Failed to delete the Content-Security-Policy report file.'); 23 23 24 25 24 $this->description = __("Remove the Content-Security-Policy report file."); 25 } 26 26 27 28 29 $csp_file = path::real(DC_VAR).'/csp/csp_report.json';30 31 32 27 public function execute() 28 { 29 $csp_file = path::real(DC_VAR) . '/csp/csp_report.json'; 30 if (file_exists($csp_file)) { 31 unlink($csp_file); 32 } 33 33 34 35 34 return true; 35 } 36 36 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.indexcomments.php
r3340 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceIndexcomments extends dcMaintenanceTask 15 15 { 16 protected $ajax= true;17 18 19 16 protected $ajax = true; 17 protected $group = 'index'; 18 protected $limit = 500; 19 protected $step_task; 20 20 21 22 23 $this->name= __('Search engine index');24 $this->task= __('Index all comments for search engine');25 $this->step_task= __('Next');26 $this->step= __('Indexing comment %d to %d.');27 $this->success= __('Comments index done.');28 $this->error= __('Failed to index comments.');21 protected function init() 22 { 23 $this->name = __('Search engine index'); 24 $this->task = __('Index all comments for search engine'); 25 $this->step_task = __('Next'); 26 $this->step = __('Indexing comment %d to %d.'); 27 $this->success = __('Comments index done.'); 28 $this->error = __('Failed to index comments.'); 29 29 30 31 30 $this->description = __('Index all comments and trackbacks in search engine index. This operation is necessary, after importing content in your blog, to use internal search engine, on public and private pages.'); 31 } 32 32 33 34 35 33 public function execute() 34 { 35 $this->code = $this->core->indexAllComments($this->code, $this->limit); 36 36 37 38 37 return $this->code ?: true; 38 } 39 39 40 41 42 43 40 public function task() 41 { 42 return $this->code ? $this->step_task : $this->task; 43 } 44 44 45 46 47 48 45 public function step() 46 { 47 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : null; 48 } 49 49 50 51 52 53 50 public function success() 51 { 52 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : $this->success; 53 } 54 54 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.indexposts.php
r3340 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceIndexposts extends dcMaintenanceTask 15 15 { 16 protected $ajax= true;17 18 19 16 protected $ajax = true; 17 protected $group = 'index'; 18 protected $limit = 500; 19 protected $step_task; 20 20 21 22 23 $this->name= __('Search engine index');24 $this->task= __('Index all entries for search engine');25 $this->step_task= __('Next');26 $this->step= __('Indexing entry %d to %d.');27 $this->success= __('Entries index done.');28 $this->error= __('Failed to index entries.');21 protected function init() 22 { 23 $this->name = __('Search engine index'); 24 $this->task = __('Index all entries for search engine'); 25 $this->step_task = __('Next'); 26 $this->step = __('Indexing entry %d to %d.'); 27 $this->success = __('Entries index done.'); 28 $this->error = __('Failed to index entries.'); 29 29 30 31 30 $this->description = __('Index all entries in search engine index. This operation is necessary, after importing content in your blog, to use internal search engine, on public and private pages.'); 31 } 32 32 33 34 35 33 public function execute() 34 { 35 $this->code = $this->core->indexAllPosts($this->code, $this->limit); 36 36 37 38 37 return $this->code ?: true; 38 } 39 39 40 41 42 43 40 public function task() 41 { 42 return $this->code ? $this->step_task : $this->task; 43 } 44 44 45 46 47 48 45 public function step() 46 { 47 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : null; 48 } 49 49 50 51 52 53 50 public function success() 51 { 52 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : $this->success; 53 } 54 54 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.logs.php
r2482 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceLogs extends dcMaintenanceTask 15 15 { 16 16 public static $keep_maintenance_logs = true; 17 17 18 18 protected $group = 'purge'; 19 19 20 21 22 $this->task= __('Delete all logs');23 $this->success= __('Logs deleted.');24 $this->error= __('Failed to delete logs.');20 protected function init() 21 { 22 $this->task = __('Delete all logs'); 23 $this->success = __('Logs deleted.'); 24 $this->error = __('Failed to delete logs.'); 25 25 26 27 26 $this->description = __('Logs record all activity and connection to your blog history. Unless you need to keep this history, consider deleting these logs from time to time.'); 27 } 28 28 29 public function execute() 30 { 31 if (dcMaintenanceLogs::$keep_maintenance_logs) { 32 $this->core->con->execute( 33 'DELETE FROM '.$this->core->prefix.'log '. 34 "WHERE log_table <> 'maintenance' " 35 ); 36 } 37 else { 38 $this->core->log->delLogs(null, true); 39 } 29 public function execute() 30 { 31 if (dcMaintenanceLogs::$keep_maintenance_logs) { 32 $this->core->con->execute( 33 'DELETE FROM ' . $this->core->prefix . 'log ' . 34 "WHERE log_table <> 'maintenance' " 35 ); 36 } else { 37 $this->core->log->delLogs(null, true); 38 } 40 39 41 42 40 return true; 41 } 43 42 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.synchpostsmeta.php
r3340 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceSynchpostsmeta extends dcMaintenanceTask 15 15 { 16 protected $ajax= true;17 18 19 16 protected $ajax = true; 17 protected $group = 'index'; 18 protected $limit = 100; 19 protected $step_task; 20 20 21 22 23 $this->name= __('Entries metadata');24 $this->task= __('Synchronize entries metadata');25 $this->step_task= __('Next');26 $this->step= __('Synchronize entry %d to %d.');27 $this->success= __('Entries metadata synchronize done.');28 $this->error= __('Failed to synchronize entries metadata.');21 protected function init() 22 { 23 $this->name = __('Entries metadata'); 24 $this->task = __('Synchronize entries metadata'); 25 $this->step_task = __('Next'); 26 $this->step = __('Synchronize entry %d to %d.'); 27 $this->success = __('Entries metadata synchronize done.'); 28 $this->error = __('Failed to synchronize entries metadata.'); 29 29 30 31 30 $this->description = __('Synchronize all entries metadata could be useful after importing content in your blog or do bad operation on database tables.'); 31 } 32 32 33 34 35 33 public function execute() 34 { 35 $this->code = $this->synchronizeAllPostsmeta($this->code, $this->limit); 36 36 37 38 37 return $this->code ?: true; 38 } 39 39 40 41 42 43 40 public function task() 41 { 42 return $this->code ? $this->step_task : $this->task; 43 } 44 44 45 46 47 48 45 public function step() 46 { 47 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : null; 48 } 49 49 50 51 52 53 50 public function success() 51 { 52 return $this->code ? sprintf($this->step, $this->code - $this->limit, $this->code) : $this->success; 53 } 54 54 55 protected function synchronizeAllPostsmeta($start=null,$limit=null)56 57 58 $rs = $this->core->con->select('SELECT COUNT(post_id) FROM '.$this->core->prefix.'post');59 55 protected function synchronizeAllPostsmeta($start = null, $limit = null) 56 { 57 // Get number of posts 58 $rs = $this->core->con->select('SELECT COUNT(post_id) FROM ' . $this->core->prefix . 'post'); 59 $count = $rs->f(0); 60 60 61 62 63 $rs = $this->core->con->select('SELECT post_id FROM '.$this->core->prefix.'post '.$req_limit, true);61 // Get posts ids to update 62 $req_limit = $start !== null && $limit !== null ? $this->core->con->limit($start, $limit) : ''; 63 $rs = $this->core->con->select('SELECT post_id FROM ' . $this->core->prefix . 'post ' . $req_limit, true); 64 64 65 66 67 $rs_meta = $this->core->con->select('SELECT meta_id, meta_type FROM '.$this->core->prefix.'meta WHERE post_id = '.$rs->post_id.' ');65 // Update posts meta 66 while ($rs->fetch()) { 67 $rs_meta = $this->core->con->select('SELECT meta_id, meta_type FROM ' . $this->core->prefix . 'meta WHERE post_id = ' . $rs->post_id . ' '); 68 68 69 70 71 72 69 $meta = array(); 70 while ($rs_meta->fetch()) { 71 $meta[$rs_meta->meta_type][] = $rs_meta->meta_id; 72 } 73 73 74 $cur = $this->core->con->openCursor($this->core->prefix.'post');75 76 $cur->update('WHERE post_id = '.$rs->post_id);77 78 74 $cur = $this->core->con->openCursor($this->core->prefix . 'post'); 75 $cur->post_meta = serialize($meta); 76 $cur->update('WHERE post_id = ' . $rs->post_id); 77 } 78 $this->core->blog->triggerBlog(); 79 79 80 81 82 80 // Return next step 81 return $start + $limit > $count ? null : $start + $limit; 82 } 83 83 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.vacuum.php
r2044 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceVacuum extends dcMaintenanceTask 15 15 { 16 16 protected $group = 'optimize'; 17 17 18 19 20 $this->name= __('Optimise database');21 $this->task= __('optimize tables');22 $this->success= __('Optimization successful.');23 $this->error= __('Failed to optimize tables.');18 protected function init() 19 { 20 $this->name = __('Optimise database'); 21 $this->task = __('optimize tables'); 22 $this->success = __('Optimization successful.'); 23 $this->error = __('Failed to optimize tables.'); 24 24 25 26 25 $this->description = __("After numerous delete or update operations on Dotclear's database, it gets fragmented. Optimizing will allow to defragment it. It has no incidence on your data's integrity. It is recommended to optimize before any blog export."); 26 } 27 27 28 29 30 28 public function execute() 29 { 30 $schema = dbSchema::init($this->core->con); 31 31 32 foreach ($schema->getTables() as $table) 33 { 34 if (strpos($table, $this->core->prefix) === 0) { 35 $this->core->con->vacuum($table); 36 } 37 } 32 foreach ($schema->getTables() as $table) { 33 if (strpos($table, $this->core->prefix) === 0) { 34 $this->core->con->vacuum($table); 35 } 36 } 38 37 39 40 38 return true; 39 } 41 40 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.zipmedia.php
r3034 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceZipmedia extends dcMaintenanceTask 15 15 { 16 protected $perm= 'admin';17 protected $blog= true;18 protected $tab= 'backup';19 16 protected $perm = 'admin'; 17 protected $blog = true; 18 protected $tab = 'backup'; 19 protected $group = 'zipblog'; 20 20 21 22 23 21 protected function init() 22 { 23 $this->task = __('Download media folder of current blog'); 24 24 25 26 25 $this->description = __('It may be useful to backup your media folder. This compress all content of media folder into a single zip file. Notice : with some hosters, the media folder cannot be compressed with this plugin if it is too big.'); 26 } 27 27 28 29 30 31 32 33 28 public function execute() 29 { 30 // Instance media 31 $this->core->media = new dcMedia($this->core); 32 $this->core->media->chdir(null); 33 $this->core->media->getDir(); 34 34 35 36 37 $fp= fopen('php://output', 'wb');38 39 40 $zip->addDirectory($this->core->media->root.'/', '', true);35 // Create zip 36 @set_time_limit(300); 37 $fp = fopen('php://output', 'wb'); 38 $zip = new fileZip($fp); 39 $zip->addExclusion('#(^|/).(.*?)_(m|s|sq|t).jpg$#'); 40 $zip->addDirectory($this->core->media->root . '/', '', true); 41 41 42 43 42 // Log task execution here as we sent file and stop script 43 $this->log(); 44 44 45 46 header('Content-Disposition: attachment;filename='.date('Y-m-d').'-'.$this->core->blog->id.'-'.'media.zip');47 48 49 50 51 45 // Send zip 46 header('Content-Disposition: attachment;filename=' . date('Y-m-d') . '-' . $this->core->blog->id . '-' . 'media.zip'); 47 header('Content-Type: application/x-zip'); 48 $zip->write(); 49 unset($zip); 50 exit(1); 51 } 52 52 } -
plugins/maintenance/inc/tasks/class.dc.maintenance.ziptheme.php
r2044 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcMaintenanceZiptheme extends dcMaintenanceTask 15 15 { 16 protected $perm= 'admin';17 protected $blog= true;18 protected $tab= 'backup';19 16 protected $perm = 'admin'; 17 protected $blog = true; 18 protected $tab = 'backup'; 19 protected $group = 'zipblog'; 20 20 21 22 23 21 protected function init() 22 { 23 $this->task = __('Download active theme of current blog'); 24 24 25 26 25 $this->description = __('It may be useful to backup the active theme before any change or update. This compress theme folder into a single zip file.'); 26 } 27 27 28 29 30 31 $path= $this->core->blog->themes_path;32 $theme =$this->core->blog->settings->system->theme;33 $dir = path::real($path.'/'.$theme);34 35 36 28 public function execute() 29 { 30 // Get theme path 31 $path = $this->core->blog->themes_path; 32 $theme = $this->core->blog->settings->system->theme; 33 $dir = path::real($path . '/' . $theme); 34 if (empty($path) || empty($theme) || !is_dir($dir)) { 35 return false; 36 } 37 37 38 39 40 $fp= fopen('php://output', 'wb');41 42 43 $zip->addDirectory($dir.'/', '', true);38 // Create zip 39 @set_time_limit(300); 40 $fp = fopen('php://output', 'wb'); 41 $zip = new fileZip($fp); 42 $zip->addExclusion('#(^|/).(.*?)_(m|s|sq|t).jpg$#'); 43 $zip->addDirectory($dir . '/', '', true); 44 44 45 46 45 // Log task execution here as we sent file and stop script 46 $this->log(); 47 47 48 49 header('Content-Disposition: attachment;filename=theme-'.$theme.'.zip');50 51 52 53 54 48 // Send zip 49 header('Content-Disposition: attachment;filename=theme-' . $theme . '.zip'); 50 header('Content-Type: application/x-zip'); 51 $zip->write(); 52 unset($zip); 53 exit(1); 54 } 55 55 }
Note: See TracChangeset
for help on using the changeset viewer.