Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.