first commit
This commit is contained in:
33
lib/Controller/Errors.php
Normal file
33
lib/Controller/Errors.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use Closure;
|
||||
|
||||
use OCP\AppFramework\Http;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
|
||||
use OCA\MyWiki\Service\NotFoundException;
|
||||
use OCA\MyWiki\Service\ReadOnlyException;
|
||||
|
||||
|
||||
trait Errors {
|
||||
|
||||
protected function handleNotFound (Closure $callback) {
|
||||
try {
|
||||
return new DataResponse($callback());
|
||||
} catch(NotFoundException $e) {
|
||||
$message = ['message' => $e->getMessage()];
|
||||
return new DataResponse($message, Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
protected function handleReadOnly (Closure $callback) {
|
||||
try {
|
||||
return new DataResponse($callback());
|
||||
} catch(ReadOnlyException $e) {
|
||||
$message = ['message' => $e->getMessage()];
|
||||
return new DataResponse($message, Http::STATUS_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
31
lib/Controller/PageController.php
Normal file
31
lib/Controller/PageController.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http\TemplateResponse;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
class PageController extends Controller {
|
||||
private $userId;
|
||||
|
||||
public function __construct($AppName, IRequest $request, $UserId){
|
||||
parent::__construct($AppName, $request);
|
||||
$this->userId = $UserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* CAUTION: the @Stuff turns off security checks; for this page no admin is
|
||||
* required and no CSRF check. If you don't know what CSRF is, read
|
||||
* it up in the docs or you might create a security hole. This is
|
||||
* basically the only required method to add this exemption, don't
|
||||
* add it to any other method if you don't exactly know what it does
|
||||
*
|
||||
* @NoAdminRequired
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
public function index() {
|
||||
return new TemplateResponse('mywiki', 'index'); // templates/index.php
|
||||
}
|
||||
|
||||
}
|
81
lib/Controller/WikiController.php
Normal file
81
lib/Controller/WikiController.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Controller;
|
||||
|
||||
use OCP\IRequest;
|
||||
use OCP\AppFramework\Http\DataResponse;
|
||||
use OCP\AppFramework\Controller;
|
||||
|
||||
use OCA\MyWiki\Service\WikiService;
|
||||
|
||||
class WikiController extends Controller {
|
||||
|
||||
private $service;
|
||||
private $userId;
|
||||
|
||||
use Errors;
|
||||
|
||||
public function __construct(string $AppName, IRequest $request, WikiService $service, $UserId){
|
||||
parent::__construct($AppName, $request);
|
||||
$this->service = $service;
|
||||
$this->userId = $UserId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function index() {
|
||||
return new DataResponse($this->service->test($this->userId));
|
||||
|
||||
return new DataResponse($this->service->findAll($this->userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
*/
|
||||
public function show(int $id) {
|
||||
return $this->handleNotFound(function () use ($id) {
|
||||
return $this->service->find($id, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param string $title
|
||||
* @param string $fileId
|
||||
*/
|
||||
public function create(string $title, int $fileId) {
|
||||
return $this->handleReadOnly(function () use ($title, $fileId) {
|
||||
return $this->service->create($title, $fileId, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $title
|
||||
*/
|
||||
public function update(int $id, string $title) {
|
||||
return $this->handleNotFound(function () use ($id, $title) {
|
||||
return $this->service->update($id, $title, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
* @param int $id
|
||||
* @param bool $removeFiles
|
||||
*/
|
||||
public function destroy(int $id, bool $removeFiles) {
|
||||
return $this->handleNotFound(function () use ($id, $removeFiles) {
|
||||
return $this->service->delete($id, $removeFiles, $this->userId);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
25
lib/Db/Wiki.php
Normal file
25
lib/Db/Wiki.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
class Wiki extends Entity implements JsonSerializable {
|
||||
|
||||
protected $title;
|
||||
protected $fileId;
|
||||
protected $userId;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'file_id' => $this->file_id
|
||||
];
|
||||
}
|
||||
}
|
50
lib/Db/WikiMapper.php
Normal file
50
lib/Db/WikiMapper.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Db;
|
||||
|
||||
use OCP\IDBConnection;
|
||||
use OCP\AppFramework\Db\QBMapper;
|
||||
|
||||
class WikiMapper extends QBMapper {
|
||||
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'mywiki_wikis', Note::class);
|
||||
}
|
||||
|
||||
public function usersCount(int $folderId) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
// ToDo - get the count
|
||||
$qb->select($qb->createFunction('COUNT()'))
|
||||
->from($this->getTableName())
|
||||
->where(
|
||||
$qb->expr()->eq('folderId', $qb->createNamedParameter($folderId))
|
||||
);
|
||||
return $qb->getSQL();
|
||||
}
|
||||
|
||||
public function find(int $id, string $userId) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from($this->getTableName())
|
||||
->where(
|
||||
$qb->expr()->eq('id', $qb->createNamedParameter($id))
|
||||
)->andWhere(
|
||||
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
|
||||
);
|
||||
|
||||
return $this->findEntity($qb);
|
||||
}
|
||||
|
||||
public function findAll(string $userId) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from($this->getTableName())
|
||||
->where(
|
||||
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId))
|
||||
);
|
||||
|
||||
return $this->findEntities($qb);
|
||||
}
|
||||
|
||||
}
|
41
lib/Helper/WikiHelper.php
Normal file
41
lib/Helper/WikiHelper.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Helper;
|
||||
|
||||
use OCP\AppFramework\Files\Folder;
|
||||
|
||||
class WikiHelper {
|
||||
public static function isFolder(int $folderId) :bool {
|
||||
$mount = \OC\Files\Filesystem::getMountsForFileId($folderId);
|
||||
/*
|
||||
isReadable()
|
||||
getById($folderId)
|
||||
isCreatable()
|
||||
isUpdateable()
|
||||
lock()
|
||||
|
||||
$config = new \OC\Config('config/');
|
||||
$base_path = $config->getValue('datadirectory')
|
||||
|
||||
datadirectory is the key in the array defined in config.php that contains the base directory.
|
||||
|
||||
$basepath now contains a path like /var/www/html/nextcloud/data.
|
||||
*/
|
||||
// ToDo
|
||||
$nodes = \OC\Files\Node\Folder::getById($folderId);
|
||||
|
||||
return true;
|
||||
}
|
||||
public static function isWiki(int $folderId) :bool {
|
||||
return \OC\Files\Filesystem::nodeExists('.wiki');
|
||||
}
|
||||
public static function initWiki(int $folderId, string $title) :bool {
|
||||
// ToDo
|
||||
// create file ".wiki"
|
||||
// title: $title
|
||||
// pages:
|
||||
return true;
|
||||
}
|
||||
public static function removePage(int $folderId, bool $includeSubfolders) {
|
||||
// ToDo :: Remove this folder and all subfolders
|
||||
}
|
||||
}
|
72
lib/Migration/Version000000Date20220302210900.php
Normal file
72
lib/Migration/Version000000Date20220302210900.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace OCA\NotesTutorial\Migration;
|
||||
|
||||
use Closure;
|
||||
use OCP\DB\ISchemaWrapper;
|
||||
use OCP\Migration\SimpleMigrationStep;
|
||||
use OCP\Migration\IOutput;
|
||||
|
||||
class Version1400Date20181013124731 extends SimpleMigrationStep {
|
||||
|
||||
/**
|
||||
* @param IOutput $output
|
||||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
|
||||
* @param array $options
|
||||
* @return null|ISchemaWrapper
|
||||
*/
|
||||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
|
||||
/** @var ISchemaWrapper $schema */
|
||||
$schema = $schemaClosure();
|
||||
|
||||
if (!$schema->hasTable('mywiki')) {
|
||||
$table = $schema->createTable('mywiki');
|
||||
$table->addColumn('id', 'integer', [
|
||||
'autoincrement' => true,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('file_id', 'integer', [
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('title', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 200
|
||||
]);
|
||||
$table->addColumn('user_id', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 200,
|
||||
]);
|
||||
|
||||
$table->setPrimaryKey(['id']);
|
||||
$table->addIndex(['user_id'], 'mywiki_user_id_index');
|
||||
}
|
||||
|
||||
/*
|
||||
if (!$schema->hasTable('mywiki_pages')) {
|
||||
$table = $schema->createTable('mywiki_pages');
|
||||
$table->addColumn('id', 'integer', [
|
||||
'autoincrement' => true,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('pid', 'integer', [
|
||||
'notnull' => true
|
||||
]);
|
||||
$table->addColumn('wiki_id', 'integer', [
|
||||
'notnull' => true
|
||||
]);
|
||||
$table->addColumn('title', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 200,
|
||||
]);
|
||||
$table->addColumn('path', 'string', [
|
||||
'notnull' => true,
|
||||
'length' => 200,
|
||||
]);
|
||||
|
||||
$table->setPrimaryKey(['id']);
|
||||
$table->addIndex(['wiki_id'], 'mywiki_wiki_id_index');
|
||||
}
|
||||
*/
|
||||
return $schema;
|
||||
}
|
||||
}
|
4
lib/Service/NotFoundException.php
Normal file
4
lib/Service/NotFoundException.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Service;
|
||||
|
||||
class NotFoundException extends ServiceException {}
|
4
lib/Service/ReadOnlyException.php
Normal file
4
lib/Service/ReadOnlyException.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Service;
|
||||
|
||||
class ReadOnlyException extends ServiceException {}
|
6
lib/Service/ServiceException.php
Normal file
6
lib/Service/ServiceException.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ServiceException extends Exception {}
|
97
lib/Service/WikiService.php
Normal file
97
lib/Service/WikiService.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
namespace OCA\MyWiki\Service;
|
||||
|
||||
use Exception;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
|
||||
use OCA\MyWiki\Db\Wiki;
|
||||
use OCA\MyWiki\Db\WikiMapper;
|
||||
use OCA\MyWiki\Helper\WikiHelper;
|
||||
|
||||
class WikiService {
|
||||
|
||||
private $mapper;
|
||||
private $rootFolder;
|
||||
|
||||
public function __construct(WikiMapper $mapper,IRootFolder $rootFolder){
|
||||
$this->mapper = $mapper;
|
||||
$this->rootFolder = $rootFolder;
|
||||
|
||||
// , IUserSession $userSession
|
||||
}
|
||||
|
||||
public function test(string $userId) {
|
||||
echo 'JDG :: Test for '.$userId;
|
||||
}
|
||||
|
||||
public function findAll(string $userId) {
|
||||
return $this->mapper->findAll($userId);
|
||||
}
|
||||
|
||||
private function handleException ($e) {
|
||||
if ($e instanceof DoesNotExistException ||
|
||||
$e instanceof MultipleObjectsReturnedException) {
|
||||
throw new NotFoundException($e->getMessage());
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function find(int $id, string $userId) {
|
||||
try {
|
||||
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) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(string $title, int $fileId, string $userId) {
|
||||
if ( !WikiHelper::isFolder($fileId) ) {
|
||||
throw new ReadOnlyException('The folder is not valid');
|
||||
}
|
||||
if ( !WikiHelper::isWiki($fileId) ) {
|
||||
if ( !WikiHelper::initWiki($fileId, $title) ) {
|
||||
throw new ReadOnlyException('Error creating wiki');
|
||||
}
|
||||
}
|
||||
|
||||
$wiki = new Wiki();
|
||||
$wiki->setTitle($title);
|
||||
$wiki->setFileId($fileId);
|
||||
$wiki->setUserId($userId);
|
||||
return $this->mapper->insert($wiki);
|
||||
}
|
||||
|
||||
public function update(int $id, string $title, string $userId) {
|
||||
try {
|
||||
$wiki = $this->mapper->find($id, $userId);
|
||||
$wiki->setTitle($title);
|
||||
return $this->mapper->update($wiki);
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(int $id, bool $removeFiles, string $userId) {
|
||||
try {
|
||||
$wiki = $this->mapper->find($id, $userId);
|
||||
if ($removeFiles) {
|
||||
$fileId = $wiki->getFileId();
|
||||
$this->mapper->usersCount($fileId);
|
||||
WikiHelper::removePage($fileId, true);
|
||||
}
|
||||
$this->mapper->delete($wiki);
|
||||
return $wiki;
|
||||
} catch(Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user