Service to manage wiki almost ready
This commit is contained in:
parent
a30a4f13ec
commit
9c6114cf86
@ -24,7 +24,7 @@
|
|||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
public function test() {
|
public function test() {
|
||||||
$x = $this->service->test($this->userId);
|
$x = $this->service->test();
|
||||||
return new DataResponse(print_r($x,true));
|
return new DataResponse(print_r($x,true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,34 +1,194 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace OCA\MyWiki\Helper;
|
namespace OCA\MyWiki\Helper;
|
||||||
|
|
||||||
use OCP\Files\IRootFolder;
|
use OCP\Files\Folder;
|
||||||
|
use OC\Files\Node\File;
|
||||||
|
/*
|
||||||
|
use OCA\MyWiki\Helper\WikiTree;
|
||||||
|
use OCA\MyWiki\Helper\WikiTreePage;
|
||||||
|
*/
|
||||||
|
|
||||||
class WikiHelper {
|
class WikiHelper {
|
||||||
public static function isFolder(IRootFolder $storage, int $folderId) :bool {
|
private const WIKI_FILE = 'wiki.json';
|
||||||
$nodes = $storage->getById($folderId);
|
private const WIKI_FILE_CONTENT = 'Readme.md';
|
||||||
if ( count($nodes)>0 ) {
|
private Folder $userFolder;
|
||||||
return $nodes[0]->getType() == \OCP\Files\Node::TYPE_FOLDER;
|
private ?Folder $wikiFolder;
|
||||||
}
|
|
||||||
return false;
|
private function getWikiFolder(Folder $folder, int $folderId): ?Folder {
|
||||||
|
$nodes = $folder->getById($folderId);
|
||||||
|
return count($nodes)>0?$nodes[0]:null;
|
||||||
}
|
}
|
||||||
public static function isWiki(IRootFolder $storage, int $folderId) :string {
|
private function getFolderById(int $id): Folder {
|
||||||
$nodes = $storage->getById($folderId);
|
return $this->wikiFolder->getById($id)[0];
|
||||||
if ( count($nodes)>0 ) {
|
|
||||||
$nodeStorage = $nodes[0]->getStorage();
|
|
||||||
return $nodeStorage->file_get_contents('/wiki.yaml');
|
|
||||||
// getPath()
|
|
||||||
// getStorage()
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
public static function initWiki(int $folderId, string $title) :bool {
|
private function getFileById(int $id): File {
|
||||||
// ToDo
|
return $this->wikiFolder->getById($id)[0];
|
||||||
// create file ".wiki"
|
}
|
||||||
// title: $title
|
private function getFileByName(string $name): File {
|
||||||
// pages:
|
return $this->wikiFolder->get($name);
|
||||||
|
}
|
||||||
|
private function sanitize_file_name(string $nameFile): string {
|
||||||
|
return preg_replace("([^\w\s\d\-_~,;\[\]\(\)])", "", $nameFile);
|
||||||
|
}
|
||||||
|
private function scanFolder(Folder $folder, WikiTree $wikiTree, int $parentId=0) {
|
||||||
|
$nodes = $folder->getDirectoryListing();
|
||||||
|
foreach($nodes as $node) {
|
||||||
|
if ($node->getType() == \OCP\Files\Node::TYPE_FOLDER) {
|
||||||
|
$wikiPage = new WikiTreePage();
|
||||||
|
$wikiPage->id = $node->getId();
|
||||||
|
$wikiPage->pid = $parentId;
|
||||||
|
$wikiPage->title = $node->getName();
|
||||||
|
$wikiTree->set($wikiPage);
|
||||||
|
$this->scanFolder($node, $wikiTree, $wikiPage->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private function rebuildWikiTree(): array {
|
||||||
|
$wikiTree = new WikiTree(null);
|
||||||
|
$this->scanFolder($this->userFolder, $wikiTree);
|
||||||
|
return $wikiTree->getWikiPages();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(Folder $folder) {
|
||||||
|
$this->userFolder = $folder;
|
||||||
|
$this->wikiFolder = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFolderId(int $folderId): WikiHelper {
|
||||||
|
$this->wikiFolder = $this->getWikiFolder($this->userFolder, $folderId);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isWiki() :bool {
|
||||||
|
return $this->wikiFolder && $this->wikiFolder->getType() == \OCP\Files\Node::TYPE_FOLDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reloadWikiTree(): array {
|
||||||
|
$wiki = $this->getWikiData();
|
||||||
|
$wiki['pages'] = $this->rebuildWikiTree();
|
||||||
|
$this->setWikiData($wiki);
|
||||||
|
return $wiki;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getWikiData(): ?array {
|
||||||
|
try {
|
||||||
|
$data = $this->getFileByName(self::WIKI_FILE)->getContent();
|
||||||
|
} catch(\OCP\Files\NotFoundException $ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return json_decode($data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setWikiData(array $wiki): bool {
|
||||||
|
try {
|
||||||
|
$data = json_encode($wiki);
|
||||||
|
$path = $this->wikiFolder->getInternalPath().'/'.self::WIKI_FILE;
|
||||||
|
if ( $this->wikiFolder->nodeExists($path) ) {
|
||||||
|
$this->getFileByName(self::WIKI_FILE)->putContent($data);
|
||||||
|
} else {
|
||||||
|
$this->wikiFolder
|
||||||
|
->newFile(self::WIKI_FILE, $data);
|
||||||
|
}
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public static function removePage(int $folderId, bool $includeSubfolders) {
|
|
||||||
// ToDo :: Remove this folder and all subfolders
|
public function initWiki(string $title) :bool {
|
||||||
|
$wiki = [
|
||||||
|
"title"=>$title,
|
||||||
|
"folderId"=>$this->wikiFolder->getId(),
|
||||||
|
"pages"=>$this->rebuildWikiTree()
|
||||||
|
];
|
||||||
|
if ( $this->getWikiData() === null ) {
|
||||||
|
return $this->setWikiData($wiki);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function add(int $parentId, string $title) {
|
||||||
|
// $folder = ...newFolder($path)
|
||||||
|
|
||||||
|
$wikiData = $this->getWikiData();
|
||||||
|
if ($wikiData) {
|
||||||
|
$wikiTree = new WikiTree($wikiData['pages']);
|
||||||
|
$wikiPage = new WikiTreePage();
|
||||||
|
$wikiPage->id = $id;
|
||||||
|
$wikiPage->pid = $parentId;
|
||||||
|
$wikiPage->title = $title;
|
||||||
|
$wikiTree->set($wikiPage);
|
||||||
|
$wikiData['pages'] = $wikiTree->getWikiPages();
|
||||||
|
$this->setWikiData($wikiData);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $id, string $content) {
|
||||||
|
try {
|
||||||
|
$path = $this->wikiFolder->getInternalPath().'/'.self::WIKI_FILE_CONTENT;
|
||||||
|
if ( $this->wikiFolder->nodeExists($path) ) {
|
||||||
|
$this->getFileByName(self::WIKI_FILE_CONTENT)->putContent($content);
|
||||||
|
} else {
|
||||||
|
$this->wikiFolder
|
||||||
|
->newFile(self::WIKI_FILE_CONTENT, $content);
|
||||||
|
}
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rename(int $id, string $title) {
|
||||||
|
try {
|
||||||
|
$folder = $this->getFolderById($id);
|
||||||
|
$to = $folder->getParent()->getFullPath($this->sanitize_file_name($title));
|
||||||
|
try {
|
||||||
|
if ( !$folder->move($to) ) return false;
|
||||||
|
|
||||||
|
$wikiData = $this->getWikiData();
|
||||||
|
if ($wikiData) {
|
||||||
|
$wikiTree = new WikiTree($wikiData['pages']);
|
||||||
|
$wikiPage = $wikiTree->get($id);
|
||||||
|
if ($wikiPage) {
|
||||||
|
$wikiPage->title = $title;
|
||||||
|
$wikiTree->set($wikiPage);
|
||||||
|
$wikiData['pages'] = $wikiTree->getWikiPages();
|
||||||
|
$this->setWikiData($wikiData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch(\OCP\Lock\LockedException $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(int $id=null) {
|
||||||
|
if ($id!==null) {
|
||||||
|
$folder = $this->getFolderById($id);
|
||||||
|
|
||||||
|
$wikiData = $this->getWikiData();
|
||||||
|
if ($wikiData) {
|
||||||
|
$wikiTree = new WikiTree($wikiData['pages']);
|
||||||
|
$wikiTree->del($id);
|
||||||
|
$wikiData['pages'] = $wikiTree->getWikiPages();
|
||||||
|
$this->setWikiData($wikiData);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$folder = $this->wikiFolder;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$folder->delete();
|
||||||
|
} catch(\Exception $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -5,33 +5,35 @@ use Exception;
|
|||||||
|
|
||||||
use OCP\AppFramework\Db\DoesNotExistException;
|
use OCP\AppFramework\Db\DoesNotExistException;
|
||||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||||
|
use OCP\Files\IRootFolder;
|
||||||
use OCA\MyWiki\Db\Wiki;
|
use OCA\MyWiki\Db\Wiki;
|
||||||
use OCA\MyWiki\Db\WikiMapper;
|
use OCA\MyWiki\Db\WikiMapper;
|
||||||
use OCA\MyWiki\Helper\WikiHelper;
|
use OCA\MyWiki\Helper\WikiHelper;
|
||||||
|
|
||||||
use \OCP\Files\Storage;
|
|
||||||
use \OCP\Files\IRootFolder;
|
|
||||||
|
|
||||||
use \OCP\IUserSession;
|
|
||||||
|
|
||||||
class WikiService {
|
class WikiService {
|
||||||
|
|
||||||
private $mapper;
|
private $mapper;
|
||||||
private $storage;
|
private $userId;
|
||||||
private $userSession;
|
private $wikiHelper;
|
||||||
|
|
||||||
public function __construct(WikiMapper $mapper, IRootFolder $storage) {
|
public function __construct(WikiMapper $mapper, IRootFolder $storage, $UserId) {
|
||||||
// , IUserSession $userSession ) {
|
|
||||||
$this->mapper = $mapper;
|
$this->mapper = $mapper;
|
||||||
// $this->userSession = $userSession;
|
$this->userId = $UserId;
|
||||||
$this->storage = $storage;
|
|
||||||
|
$userFolder = $storage->getUserFolder($this->userId);
|
||||||
// , IUserSession $userSession
|
$this->wikiHelper = new WikiHelper($userFolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test(string $userId) {
|
public function test() {
|
||||||
return WikiHelper::isWiki($this->storage, 208);
|
$folderId = 381;
|
||||||
|
return $this->wikiHelper->setFolderId($folderId)->reloadWikiTree();
|
||||||
|
return $this->wikiHelper->setFolderId($folderId)->rename(707, 'UnoMasUno');
|
||||||
|
return $this->wikiHelper->setFolderId($folderId)->rename(647, 'RenameTest3');
|
||||||
|
|
||||||
|
return $this->wikiHelper->setFolderId(395)->delete(395) ? 'Yes' : 'No';
|
||||||
|
$this->wikiHelper->setFolderId($folderId)->initWiki("First Wiki");
|
||||||
|
return print_r($this->wikiHelper->setFolderId($folderId)->getWikiTree(), true);
|
||||||
|
return $this->wikiHelper->setFolderId($folderId)->isWiki() ? 'Yes' : 'No';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findAll(string $userId) {
|
public function findAll(string $userId) {
|
||||||
@ -50,22 +52,15 @@ class WikiService {
|
|||||||
public function find(int $id, string $userId) {
|
public function find(int $id, string $userId) {
|
||||||
try {
|
try {
|
||||||
return $this->mapper->find($id, $userId);
|
return $this->mapper->find($id, $userId);
|
||||||
|
|
||||||
// in order to be able to plug in different storage backends like files
|
|
||||||
// for instance it is a good idea to turn storage related exceptions
|
|
||||||
// into service related exceptions so controllers and service users
|
|
||||||
// have to deal with only one type of exception
|
|
||||||
} catch(Exception $e) {
|
} catch(Exception $e) {
|
||||||
$this->handleException($e);
|
$this->handleException($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(string $title, int $fileId, string $userId) {
|
public function create(string $title, int $fileId, string $userId) {
|
||||||
if ( !WikiHelper::isFolder($fileId) ) {
|
$this->wikiHelper->setFolderId($fileId);
|
||||||
throw new ReadOnlyException('The folder is not valid');
|
if ( $this->wikiHelper->isWiki() ) {
|
||||||
}
|
if ( !$this->wikiHelper->initWiki($title) ) {
|
||||||
if ( !WikiHelper::isWiki($fileId) ) {
|
|
||||||
if ( !WikiHelper::initWiki($fileId, $title) ) {
|
|
||||||
throw new ReadOnlyException('Error creating wiki');
|
throw new ReadOnlyException('Error creating wiki');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,7 +88,7 @@ class WikiService {
|
|||||||
if ($removeFiles) {
|
if ($removeFiles) {
|
||||||
$fileId = $wiki->getFileId();
|
$fileId = $wiki->getFileId();
|
||||||
$this->mapper->usersCount($fileId);
|
$this->mapper->usersCount($fileId);
|
||||||
WikiHelper::removePage($fileId, true);
|
$this->wikiHelper->setFolderId($fileId)->delete();
|
||||||
}
|
}
|
||||||
$this->mapper->delete($wiki);
|
$this->mapper->delete($wiki);
|
||||||
return $wiki;
|
return $wiki;
|
||||||
|
@ -37,12 +37,6 @@ class MyWikiIntegrationTest extends TestCase {
|
|||||||
|
|
||||||
public function e($x) { echo "\n>>>$x<<<"; }
|
public function e($x) { echo "\n>>>$x<<<"; }
|
||||||
|
|
||||||
public function testJDG() {
|
|
||||||
|
|
||||||
$x = \OC\Files\Filesystem::getLocalFolder('\\');
|
|
||||||
|
|
||||||
$this->e(print_r($x,true));
|
|
||||||
}
|
|
||||||
/*
|
/*
|
||||||
public function testUpdate() {
|
public function testUpdate() {
|
||||||
// create a new note that should be updated
|
// create a new note that should be updated
|
||||||
|
4
vendor/composer/installed.php
vendored
4
vendor/composer/installed.php
vendored
@ -5,7 +5,7 @@
|
|||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
'reference' => '1984e55837df92230d61b2bce1fcf2e08e2ec4a9',
|
'reference' => 'a30a4f13ec699c6e80f32148dc3aa017511c5f5a',
|
||||||
'name' => 'jdg/mywiki',
|
'name' => 'jdg/mywiki',
|
||||||
'dev' => true,
|
'dev' => true,
|
||||||
),
|
),
|
||||||
@ -25,7 +25,7 @@
|
|||||||
'type' => 'project',
|
'type' => 'project',
|
||||||
'install_path' => __DIR__ . '/../../',
|
'install_path' => __DIR__ . '/../../',
|
||||||
'aliases' => array(),
|
'aliases' => array(),
|
||||||
'reference' => '1984e55837df92230d61b2bce1fcf2e08e2ec4a9',
|
'reference' => 'a30a4f13ec699c6e80f32148dc3aa017511c5f5a',
|
||||||
'dev_requirement' => false,
|
'dev_requirement' => false,
|
||||||
),
|
),
|
||||||
'myclabs/deep-copy' => array(
|
'myclabs/deep-copy' => array(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user