dcCore dcCore instance
*/
public function __construct($core)
{
$this->core = $core;
$tasks = new ArrayObject();
$tabs = new ArrayObject();
$groups = new ArrayObject();
$logs = $this->getLogs();
# --BEHAVIOR-- dcMaintenanceRegister
$core->callBehavior('dcMaintenanceRegister', $core, $tasks, $groups, $tabs);
$this->init($tasks, $groups, $tabs);
}
/**
* Initialize list of groups and tasks.
*
* @param tasks arrayObject Array of task to register
* @param groups arrayObject Array of groups to add
* @param tabs arrayObject Array of tabs to add
*/
public function init($tasks, $groups, $tabs)
{
$this->tasks = $this->groups = array();
foreach($tasks as $task)
{
if (!class_exists($task)) {
continue;
}
$r = new ReflectionClass($task);
$p = $r->getParentClass();
if (!$p || $p->name != 'dcMaintenanceTask') {
continue;
}
if (($t = new $task($this, 'plugin.php?p=maintenance')) === null
|| $t->perm() === null && !$this->core->auth->isSuperAdmin()
|| !$this->core->auth->check($t->perm(), $this->core->blog->id)) {
continue;
}
$this->tasks[$task] = $t;
}
foreach($groups as $id => $name)
{
$this->groups[(string) $id] = (string) $name;
}
foreach($tabs as $id => $name)
{
$this->tabs[(string) $id] = (string) $name;
}
}
/**
* Get a tab name.
*
* @param id string Tab ID
* @return mixed tab name or null if not exists
*/
public function getTab($id)
{
return array_key_exists($id, $this->tabs) ? $this->tabs[$id] : null;
}
/**
* Get tabs.
*
* @return array Array of tabs ID and name
*/
public function getTabs()
{
return $this->tabs;
}
/**
* Get a group name.
*
* @param id string Group ID
* @return mixed Group name or null if not exists
*/
public function getGroup($id)
{
return array_key_exists($id, $this->groups) ? $this->groups[$id] : null;
}
/**
* Get groups.
*
* @return array Array of groups ID and name
*/
public function getGroups()
{
return $this->groups;
}
/**
* Get a task object.
*
* @param id string task ID
* @return mixed Task object or null if not exists
*/
public function getTask($id)
{
return array_key_exists($id, $this->tasks) ? $this->tasks[$id] : null;
}
/**
* Get tasks.
*
* @return array Array of tasks objects
*/
public function getTasks()
{
return $this->tasks;
}
/**
* Get headers for plugin maintenance admin page.
*
* @return string Page headers
*/
public function getHeaders()
{
$res = '';
foreach($this->tasks as $task)
{
$res .= $task->header();
}
return $res;
}
/**
* Set log for a task.
*
* @param id string Task ID
*/
public function setLog($id)
{
// Check if taks exists
if (!$this->getTask($id)) {
return null;
}
// Get logs from this task
$rs = $this->core->con->select (
'SELECT log_id '.
'FROM '.$this->core->prefix.'log '.
"WHERE log_msg = '".$this->core->con->escape($id)."' ".
"AND log_table = 'maintenance' "
);
$logs = array();
while ($rs->fetch()) {
$logs[] = $rs->log_id;
}
// Delete old logs
if (!empty($logs)) {
$this->core->log->delLogs($logs);
}
// Add new log
$cur = $this->core->con->openCursor($this->core->prefix.'log');
$cur->log_msg = $id;
$cur->log_table = 'maintenance';
$cur->user_id = $this->core->auth->userID();
$this->core->log->addLog($cur);
}
/**
* Delete all maintenance logs.
*/
public function delLogs()
{
// Retrieve logs from this task
$rs = $this->core->log->getLogs(array(
'log_table' => 'maintenance',
'blog_id' => 'all'
));
$logs = array();
while ($rs->fetch()) {
$logs[] = $rs->log_id;
}
// Delete old logs
if (!empty($logs)) {
$this->core->log->delLogs($logs);
}
}
/**
* Get logs
*
* Return array(
* task id => array(
* timestamp of last execution,
* logged on current blog or not
* )
* )
*
* @return array List of logged tasks
*/
public function getLogs()
{
if ($this->logs === null) {
$rs = $this->core->log->getLogs(array(
'log_table' => 'maintenance',
'blog_id' => 'all'
));
$this->logs = array();
while ($rs->fetch()) {
$this->logs[$rs->log_msg] = array(
'ts' => strtotime($rs->log_dt),
'blog' => $rs->blog_id == $this->core->blog->id
);
}
}
return $this->logs;
}
}