Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/duplication #1180

Draft
wants to merge 19 commits into
base: doryphore-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
wiki: 'writable',
Vue: 'readable',
_t: 'readable',
ace: 'writable'
ace: 'writable',
toastMessage: 'readable'
},
extends: [
'airbnb-base'
Expand Down
140 changes: 140 additions & 0 deletions handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Core\Controller\AuthController;
use YesWiki\Core\Service\AclService;
use YesWiki\Core\Service\DuplicationManager;
use YesWiki\Core\Service\PageManager;
use YesWiki\Core\YesWikiHandler;

class DuplicateHandler extends YesWikiHandler
{
protected $authController;
protected $entryController;
protected $duplicationManager;

public function run()
{
$this->authController = $this->getService(AuthController::class);
$this->entryController = $this->getService(EntryController::class);
$this->duplicationManager = $this->getService(DuplicationManager::class);
$title = $error = '';
$toExternalWiki = isset($_GET['toUrl']) && $_GET['toUrl'] == '1';
if (!$this->wiki->page) {
$error .= $this->render('@templates\alert-message.twig', [
'type' => 'warning',
'message' => str_replace(
['{beginLink}', '{endLink}'],
["<a href=\"{$this->wiki->href('')}\">", '</a>'],
_t('NOT_FOUND_PAGE')
),
]);
} elseif (!$this->getService(AclService::class)->hasAccess('read', $this->wiki->GetPageTag())) {
// if no read access to the page
if ($contenu = $this->getService(PageManager::class)->getOne('PageLogin')) {
// si une page PageLogin existe, on l'affiche
$error .= $this->wiki->Format($contenu['body']);
} else {
// sinon on affiche le formulaire d'identification minimal
$error .= '<div class="vertical-center white-bg">' . "\n"
. '<div class="alert alert-danger alert-error">' . "\n"
. _t('LOGIN_NOT_AUTORIZED') . '. ' . _t('LOGIN_PLEASE_REGISTER') . '.' . "\n"
. '</div>' . "\n"
. $this->wiki->Format('{{login signupurl="0"}}' . "\n\n")
. '</div><!-- end .vertical-center -->' . "\n";
}
} elseif (!empty($_POST)) {
try {
$data = $this->duplicationManager->checkPostData($_POST);
$this->duplicationManager->duplicateLocally($data);
if ($data['duplicate-action'] == 'edit') {
$this->wiki->Redirect($this->wiki->href('edit', $data['pageTag']));

return;
seballot marked this conversation as resolved.
Show resolved Hide resolved
} elseif ($data['duplicate-action'] == 'return') {
$this->wiki->Redirect($this->wiki->href());

return;
}
$this->wiki->Redirect($this->wiki->href('', $data['pageTag']));

return;
} catch (\Throwable $th) {
$error .= $this->render('@templates\alert-message-with-back.twig', [
'type' => 'warning',
'message' => $th->getMessage(),
]);
}
} elseif (!$toExternalWiki && !$this->wiki->UserIsAdmin()) {
$error .= $this->render('@templates\alert-message-with-back.twig', [
'type' => 'warning',
'message' => _t('ONLY_ADMINS_CAN_DUPLICATE') . '.',
]);
} elseif ($this->getService(AclService::class)->hasAccess('read', $this->wiki->GetPageTag())) {
$isEntry = $this->getService(EntryManager::class)->isEntry($this->wiki->GetPageTag());
$isList = $this->getService(ListManager::class)->isList($this->wiki->GetPageTag());
$type = $isEntry ? 'entry' : ($isList ? 'list' : 'page');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pour info, c'est pas trop recommendé d'utiliser des "magic string" comme ça, mieux vaut utiliser des enum ou des constantes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tu parles de l'opérateur ternaire?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non, de créer une variable qui a plusieurs valeurs potentielle (entry, list, page) et de la passer de methode en methode, sans savoir quelles sont les valeurs potentielles, par example avec une enum

enum PageType: string {
    case ENTRY = 'entry';
    case LIST = 'list';
    case PAGE = 'page';
}

$type = $isEntry ? PageType::ENTRY : ($isList ? PageType::LIST : PageType::PAGE);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Les enums c'est pour php>=8.1, trop récent pour yeswiki

$pageTitle = '';
if ($isEntry) {
$title = _t('TEMPLATE_DUPLICATE_ENTRY') . ' ' . $this->wiki->GetPageTag();
$pageContent = $this->getService(EntryManager::class)->getOne($this->wiki->GetPageTag());
if ($toExternalWiki) {
$pageTitle = $pageContent['bf_titre'];
$proposedTag = $this->wiki->GetPageTag();
$pageContent = $this->wiki->page['body'];
} else {
$pageTitle = $pageContent['bf_titre'] . ' (' . _t('DUPLICATE') . ')';
$proposedTag = genere_nom_wiki($pageTitle);
}
} elseif ($isList) {
$title = _t('TEMPLATE_DUPLICATE_LIST') . ' ' . $this->wiki->GetPageTag();
$pageContent = $this->getService(ListManager::class)->getOne($this->wiki->GetPageTag());
if ($toExternalWiki) {
$pageTitle = $pageContent['titre_liste'];
$proposedTag = $this->wiki->GetPageTag();
} else {
$pageTitle = $pageContent['titre_liste'] . ' (' . _t('DUPLICATE') . ')';
$proposedTag = genere_nom_wiki('Liste ' . $pageTitle);
}
} else { // page
$title = _t('TEMPLATE_DUPLICATE_PAGE') . ' ' . $this->wiki->GetPageTag();
if ($toExternalWiki) {
$proposedTag = $this->wiki->GetPageTag();
} else {
$proposedTag = genere_nom_wiki($this->wiki->GetPageTag() . ' ' . _t('DUPLICATE'));
}
$pageContent = $this->wiki->page['body'];
}
$attachments = $this->duplicationManager->findFiles($this->wiki->page['tag']);
$totalSize = 0;
foreach ($attachments as $a) {
$totalSize = $totalSize + $a['size'];
}
}

if ($toExternalWiki) {
$title .= ' ' . _t('TO_ANOTHER_YESWIKI');
}
// in ajax request for modal, no title
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$title = '';
}

return $this->renderInSquelette('@core/handlers/duplicate.twig', [
'title' => $title,
'originalTag' => $this->wiki->GetPageTag(),
'error' => $error,
'sourceUrl' => $this->wiki->href(),
'proposedTag' => $proposedTag ?? '',
'attachments' => $attachments ?? [],
'pageTitle' => $pageTitle ?? '',
'pageContent' => $pageContent ?? '',
'totalSize' => $this->duplicationManager->humanFilesize($totalSize ?? 0),
'type' => $type ?? '',
'baseUrl' => preg_replace('/\?$/Ui', '', $this->wiki->config['base_url']),
'toExternalWiki' => $toExternalWiki,
]);
}
}
3 changes: 2 additions & 1 deletion includes/YesWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function __construct($config = [])

$this->services = $init->initCoreServices($this);
$this->loadExtensions();

$this->routes = $init->initRoutes($this);
}

Expand Down Expand Up @@ -1243,7 +1244,7 @@ public function Run($tag = '', $method = '')
unset($_SESSION['redirects']);
}
// do nothing except and script with message
exit($th->getMessage);
exit($th->getMessage());
}
}

Expand Down
38 changes: 31 additions & 7 deletions includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
use YesWiki\Core\Service\CommentService;
use YesWiki\Core\Service\DbService;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est une erreur d'avoir retiré cet import, c'est utilisé. J'ai fait un fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je croies que j'avais corrigé ca et le missing { mais j'ai pas du bien reprendre mon commit lors du cherry-pick

use YesWiki\Core\Service\DiffService;
use YesWiki\Core\Service\DuplicationManager;
use YesWiki\Core\Service\PageManager;
use YesWiki\Core\Service\ReactionManager;
use YesWiki\Core\Service\TripleStore;
use YesWiki\Core\Service\UserManager;
use YesWiki\Core\YesWikiController;
use YesWiki\Security\Controller\SecurityController;

class ApiController extends YesWikiController
{
Expand All @@ -44,9 +44,15 @@ public function getDocumentation()

$urlPages = $this->wiki->Href('', 'api/pages');
$output .= '<h2>' . _t('PAGES') . '</h2>' . "\n" .
'<p><code>GET ' . $urlPages . '</code></p>';
$urlPagesComments = $this->wiki->Href('', 'api/pages/{pageTag}/comments');
$output .= '<p><code>GET ' . $urlPagesComments . '</code></p>';
'<p><code>GET ' . $urlPages . '</code><br>Get all pages</p>';
$urlPages = $this->wiki->Href('', 'api/pages/{pageTag}');
$output .= '<p><code>GET ' . $urlPages . '</code><br>Get indicated page\'s informations, with raw and html contents</p>';

$urlPages = $this->wiki->Href('', 'api/pages/{pageTag}/comments');
$output .= '<p><code>GET ' . $urlPages . '</code><br>Get indicated page\'s comments</p>';

$urlPages = $this->wiki->Href('', 'api/pages/{pageTag}/duplicate');
$output .= '<p><code>POST ' . $urlPages . '</code><br>Duplicate an external page into this YesWiki pageTag</p>';

$urlComments = $this->wiki->Href('', 'api/comments');
$output .= '<h2>' . _t('COMMENTS') . '</h2>' . "\n" .
Expand All @@ -65,7 +71,6 @@ public function getDocumentation()
'<p><code>POST ' . $urlArchives . '/{id}</code></p>';

// TODO use annotations to document the API endpoints
$extensions = $this->wiki->extensions;
foreach ($this->wiki->extensions as $extension => $pluginBase) {
$response = null;
if (file_exists($pluginBase . 'controllers/ApiController.php')) {
Expand Down Expand Up @@ -376,6 +381,22 @@ public function getPage(Request $request, $tag)
return new ApiResponse($page);
}

/**
* @Route("/api/pages/{tag}/duplicate",methods={"POST"},options={"acl":{"public","@admins"}})
*/
public function duplicatePage(Request $request, $tag)
{
$this->denyAccessUnlessAdmin();
$duplicationManager = $this->getService(DuplicationManager::class);
try {
$duplicationManager->importDistantContent($tag, $request);
} catch (\Throwable $th) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pour info comme y'a un use Throwable y'a pas besoin du \

return new ApiResponse($th->getMessage(), Response::HTTP_FORBIDDEN);
}

return new ApiResponse($request->request->all(), Response::HTTP_OK);
}

/**
* @Route("/api/pages/{tag}",methods={"DELETE"},options={"acl":{"public","+"}})
*/
Expand Down Expand Up @@ -814,11 +835,14 @@ private function extractTriplesParams(string $method, $resource): array
Response::HTTP_BAD_REQUEST
);
} else {
$property = $this->getService(SecurityController::class)->filterInput($method, 'property', FILTER_DEFAULT, true);
$property = filter_input($method, 'property', FILTER_UNSAFE_RAW);
$property = in_array($property, [false, null], true) ? '' : htmlspecialchars(strip_tags($property));
if (empty($property)) {
$property = null;
}
$username = $this->getService(SecurityController::class)->filterInput($method, 'user', FILTER_DEFAULT, true);

$username = filter_input($method, 'user', FILTER_UNSAFE_RAW);
$username = in_array($username, [false, null], true) ? '' : htmlspecialchars(strip_tags($username));
if (empty($username)) {
if (!$this->wiki->UserIsAdmin()) {
$username = $this->getService(AuthController::class)->getLoggedUser()['name'];
Expand Down
6 changes: 4 additions & 2 deletions includes/controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ public function connectUser()
// connect in SESSION
$this->login($data['user'], $data['remember'] ? 1 : 0);
} catch (BadUserConnectException $th) {
if (empty($_SESSION['user']['name']) ||
if (
empty($_SESSION['user']['name']) ||
empty($data['user']['name']) ||
$data['user']['name'] != $_SESSION['user']['name'] ||
!$this->wiki->UserIsAdmin($data['user']['name'])) {
!$this->wiki->UserIsAdmin($data['user']['name'])
) {
// do not disconnect admin during update
$this->logout();
}
Expand Down
9 changes: 9 additions & 0 deletions includes/controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ public function delete(string $tag): bool
return true;
}
}

public function duplicate(string $sourceTag, string $destinationTag = ''): bool
{
if ($this->entryManager->isEntry($sourceTag)) {
return $this->entryController->duplicate($sourceTag, $destinationTag);
} else {
return $this->pageManager->duplicate($sourceTag, $destinationTag);
}
}
}
Loading
Loading