This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic SQL implementation for tasks persistence layer
- Loading branch information
Showing
7 changed files
with
399 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE IF NOT EXISTS `ajxp_tasks` ( | ||
`uid` varchar(255) NOT NULL, | ||
`userId` varchar(255) NOT NULL, | ||
`wsId` varchar(32) NOT NULL, | ||
`status` int(11) NOT NULL, | ||
`schedule` text NOT NULL, | ||
`action` text NOT NULL, | ||
`parameters` mediumtext NOT NULL, | ||
`nodes` text NOT NULL, | ||
PRIMARY KEY (`uid`), | ||
UNIQUE KEY `wsId` (`wsId`), | ||
KEY `userId` (`userId`,`status`), | ||
FULLTEXT KEY `nodes` (`nodes`) | ||
) DEFAULT CHARSET=utf8 COMMENT='Task persistence layer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
core/src/plugins/core.tasks/src/Providers/SqlTasksProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
/* | ||
* Copyright 2007-2015 Abstrium <contact (at) pydio.com> | ||
* This file is part of Pydio. | ||
* | ||
* Pydio is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Pydio is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Pydio. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* The latest code can be found at <http://pyd.io/>. | ||
*/ | ||
namespace Pydio\Tasks\Providers; | ||
|
||
use Pydio\Access\Core\Model\AJXP_Node; | ||
use Pydio\Access\Core\Model\Repository; | ||
use Pydio\Conf\Core\AbstractAjxpUser; | ||
use Pydio\Tasks\ITasksProvider; | ||
use Pydio\Tasks\Schedule; | ||
use Pydio\Tasks\Task; | ||
|
||
defined('AJXP_EXEC') or die('Access not allowed'); | ||
|
||
|
||
|
||
|
||
class SqlTasksProvider implements ITasksProvider | ||
{ | ||
|
||
protected function taskToDBValues(Task $task, $removeId = false){ | ||
$values = [ | ||
"userId" => $task->getUserId(), | ||
"wsId" => $task->getWsId(), | ||
"status" => $task->getStatus(), | ||
"schedule" => json_encode($task->getSchedule()), | ||
"action" => $task->getAction(), | ||
"parameters"=> json_encode($task->getParameters()), | ||
"nodes" => "|||".implode("|||", $task->nodes)."|||" | ||
]; | ||
if(!$removeId){ | ||
$values = array_merge(["uid" => $task->getId()], $values); | ||
} | ||
return $values; | ||
} | ||
|
||
/** | ||
* @param \DibiRow $values | ||
* @return Task | ||
*/ | ||
protected function taskFromDBValues(\DibiRow $values){ | ||
$task = new Task(); | ||
$task->setId($values["uid"]); | ||
$task->setUserId($values["userId"]); | ||
$task->setWsId($values["wsId"]); | ||
$task->setStatus($values["status"]); | ||
$task->setSchedule(Schedule::fromJson($values["schedule"])); | ||
$task->setAction($values["action"]); | ||
$task->setParameters(json_decode($values["parameters"], true)); | ||
$nodes = explode("|||", trim($values["nodes"], "|||")); | ||
foreach ($nodes as $node) $task->attachToNode($node); | ||
return $task; | ||
} | ||
|
||
/** | ||
* @param Task $task | ||
* @param Schedule $when | ||
* @return Task | ||
*/ | ||
public function createTask(Task $task, Schedule $when) | ||
{ | ||
\dibi::query("INSERT INTO [ajxp_tasks] ", $this->taskToDBValues($task)); | ||
} | ||
|
||
/** | ||
* @param string $taskId | ||
* @return Task | ||
*/ | ||
public function getTaskById($taskId) | ||
{ | ||
$res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE [uid]=%s', $taskId); | ||
foreach ($res->fetchAll() as $row) { | ||
return $this->taskFromDBValues($row); | ||
} | ||
} | ||
|
||
/** | ||
* @param Task $task | ||
* @return Task | ||
*/ | ||
public function updateTask(Task $task) | ||
{ | ||
try{ | ||
\dibi::query("UPDATE [ajxp_tasks] SET ", $this->taskToDBValues($task, true), " WHERE [uid] =%s", $task->getId()); | ||
}catch (\DibiException $ex){ | ||
$sql = $ex->getSql(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $taskId | ||
* @param int $status | ||
* @return Task | ||
*/ | ||
public function updateTaskStatus($taskId, $status) | ||
{ | ||
$task = $this->getTaskById($taskId); | ||
$task->setStatus($status); | ||
\dibi::query("UPDATE [ajxp_tasks] SET ", $this->taskToDBValues($task, true), " WHERE [uid] =%s", $taskId); | ||
} | ||
|
||
/** | ||
* @param string $taskId | ||
* @return bool | ||
*/ | ||
public function deleteTask($taskId) | ||
{ | ||
\dibi::query("DELETE FROM [ajxp_tasks] WHERE uid=%s", $taskId); | ||
} | ||
|
||
/** | ||
* @return Task[] | ||
*/ | ||
public function getPendingTasks() | ||
{ | ||
return $this->getTasks(null, null, Task::STATUS_PENDING); | ||
} | ||
|
||
/** | ||
* @param AJXP_Node $node | ||
* @return Task[] | ||
*/ | ||
public function getTasksForNode(AJXP_Node $node) | ||
{ | ||
$tasks = []; | ||
try{ | ||
$res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE [nodes] LIKE %s', "%|||".$node->getUrl()."|||%"); | ||
foreach ($res->fetchAll() as $row) { | ||
$tasks[] = $this->taskFromDBValues($row); | ||
} | ||
}catch(\DibiException $e){ | ||
$sql = $e->getSql(); | ||
} | ||
return $tasks; | ||
} | ||
|
||
/** | ||
* @param AbstractAjxpUser $user | ||
* @param Repository $repository | ||
* @param int $status | ||
* @return Task[] | ||
*/ | ||
public function getTasks($user = null, $repository = null, $status = -1) | ||
{ | ||
$tasks = []; | ||
$where = []; | ||
if($user !== null){ | ||
$where[] = array("[userId] = %s", $user->getId()); | ||
} | ||
if($repository !== null){ | ||
$where[] = array("[wsId] = %s", $repository->getId()); | ||
} | ||
if($status !== -1){ | ||
$where[] = array("[status] = %i", $status); | ||
} | ||
$res = \dibi::query('SELECT * FROM [ajxp_tasks] WHERE %and', $where); | ||
foreach ($res->fetchAll() as $row) { | ||
$tasks[] = $this->taskFromDBValues($row); | ||
} | ||
return $tasks; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.