Skip to content

Commit

Permalink
wip distant duplication new api routes and ux
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Jun 1, 2024
1 parent e522225 commit e1c47a9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function run()
'pageContent' => $pageContent,
'totalSize' => $this->duplicationManager->humanFilesize($totalSize),
'type' => $type,
'baseUrl' => $this->wiki->config['base_url'],
'baseUrl' => preg_replace('/\?$/Ui', '', $this->wiki->config['base_url']),
'toExternalWiki' => $toExternalWiki,
]);
}
Expand Down
1 change: 1 addition & 0 deletions includes/YesWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function __construct($config = array())

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

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

Expand Down
25 changes: 10 additions & 15 deletions includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use YesWiki\Core\Service\DiffService;
use YesWiki\Core\Service\PageManager;
use YesWiki\Core\Service\UserManager;
use YesWiki\Core\Service\DuplicationManager;
use YesWiki\Core\Service\CommentService;
use YesWiki\Core\Service\ReactionManager;
use YesWiki\Core\Service\TripleStore;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function getDocumentation()
$output .= '<p><code>GET ' . $urlPages . '</code><br>Get indicated page\'s comments</p>';

$urlPages = $this->wiki->Href('', 'api/pages/{pageTag}/duplicate');
$output .= '<p><code>GET ' . $urlPages . '</code><br>Duplicate page into same YesWiki (in edit mode with <code>edit=1</code> param) or to another YesWiki (with <code>toUrl=1</code> param)</p>';
$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 Down Expand Up @@ -379,24 +380,18 @@ public function getPage(Request $request, $tag)
}

/**
* @Route("/api/pages/{tag}/duplicate",methods={"GET"},options={"acl":{"public"}})
* @Route("/api/pages/{tag}/duplicate",methods={"POST"},options={"acl":{"public","@admins"}})
*/
public function duplicatePage(Request $request, $tag)
{
$this->denyAccessUnlessGranted('read', $tag);
$pageManager = $this->getService(PageManager::class);
$page = $pageManager->getOne($tag, $request->get('time'));
if (!$page) {
return new ApiResponse(null, Response::HTTP_NOT_FOUND);
}

if (!empty($request->get('destination'))) {
return new ApiResponse('Tag de destination : ' . $request->get('destination'));
} else {
return new ApiResponse('Tag de destination ?');
$this->denyAccessUnlessAdmin();
$duplicationManager = $this->getService(DuplicationManager::class);
try {
$duplicationManager->importDistantContent($tag, $request);
} catch (\Throwable $th) {
return new ApiResponse($th->getMessage(), Response::HTTP_FORBIDDEN);
}
$pageManager = $this->getService(PageManager::class);
$entryManager = $this->getService(EntryManager::class);
return new ApiResponse($request->request->all(), Response::HTTP_OK);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions includes/services/DuplicationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace YesWiki\Core\Service;

use Exception;
use YesWiki\Bazar\Field\FileField;
use YesWiki\Bazar\Field\ImageField;
use YesWiki\Bazar\Field\TextareaField;
Expand Down Expand Up @@ -287,6 +288,45 @@ public function duplicateLocally($data)
}
}

public function importDistantContent($tag, $request)
{
if ($this->wiki->services->get(PageManager::class)->getOne($tag)) {
throw new Exception(_t('ACEDITOR_LINK_PAGE_ALREADY_EXISTS'));
return;
}
$req = $request->request->all();
foreach (['pageContent', 'sourceUrl', 'originalTag', 'type'] as $key) {
if (empty($req[$key])) {
throw new Exception(_t('NOT_FOUND_IN_REQUEST', $key));
return;
}
}
foreach ($req['files'] as $fileUrl) {
$this->downloadFile($fileUrl, $req['originalTag'], $tag);
}
if ($req['type'] === 'page') {
$newBody = str_replace('', '', $req['pageContent']);
$this->wiki->services->get(PageManager::class)->save($tag, $newBody);
}
}

public function downloadFile($sourceUrl, $fromTag, $toTag, $timeoutInSec = 10)
{
$t = explode('/', $sourceUrl);
$fileName = array_pop($t);
$destPath = 'files/' . str_replace($fromTag, $toTag, $fileName);
$fp = fopen($destPath, 'wb');
$ch = curl_init($sourceUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeoutInSec);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeoutInSec);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $destPath;
}

public function humanFilesize($bytes, $decimals = 2)
{
$factor = floor((strlen($bytes) - 1) / 3);
Expand Down
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Yeswiki start file
*
Expand Down
7 changes: 6 additions & 1 deletion javascripts/handlers/duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function handleLoginResponse(data) {
}

document.addEventListener('DOMContentLoaded', () => {
$('.duplication-wiki-form, .duplication-login-form, #form-duplication').on('submit', (e) => {
e.stopPropagation()
return false
})
$('#urlWiki').on('change', () => {
$('.login-fields, .duplication-fields').addClass('hide')
$('#login-message').html('')
Expand All @@ -76,12 +80,13 @@ document.addEventListener('DOMContentLoaded', () => {
$('.login-fields').removeClass('hide')
}
})
return false
})

$('[name="duplicate-action"]').on('click', () => {
$.ajax({
method: 'POST',
url: `${shortUrl}/?api/pages/duplicate`,
url: `${shortUrl}/?api/pages/${$('#pageTag').val()}/duplicate`,
data: $('#form-duplication').serialize()
}).done((data) => {
// handleLoginResponse(data)
Expand Down
12 changes: 7 additions & 5 deletions templates/handlers/duplicate-inner.twig
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{% if toExternalWiki %}
{{ include_javascript('javascripts/handlers/duplicate.js') }}
<form class="duplication-wiki-form">
<div class="text-tip">{{ _t('WIKI_URL_RECENT') }}.</div>
<div class="form-group">
<label for="urlWiki" class="control-label">{{ _t('WIKI_URL') }}</label>
<div class="input-group">
<input required type="url" class="form-control" id="urlWiki" name="urlWiki" placeholder="{{ _t('WIKI_URL') }}" value="" />
<span class="input-group-btn">
<button class="btn-verify-wiki btn btn-primary" type="button">{{ _t('VERIFY_WIKI') }}</button>
<button class="btn-verify-wiki btn btn-primary" type="submit">{{ _t('VERIFY_WIKI') }}</button>
</span>
</div><!-- /input-group -->
<span id="login-message" class="help-block"></span>
</div>
<div class="login-fields hide row">{# hide the field while no valid yeswiki url given #}
</form>
<form class="login-fields hide row duplication-login-form">{# hide the field while no valid yeswiki url given #}
<div class="col-lg-4">
<div class="form-group">
<label for="username" class="control-label">{{ _t('LOGIN_WIKINAME') }}</label>
Expand All @@ -29,7 +31,7 @@
<button type="submit" class="btn-distant-login btn btn-primary btn-block"><i class="fa fa-sign-in-alt"></i> {{ _t('DISTANT_LOGIN') }}</button>
</div>
</div>
</div>
</form>
<div class="duplication-fields hide">{# hide the field while not connected #}
{% endif %}
<form id="form-duplication" method="post" action="{{ url({handler: 'duplicate'})}}">
Expand All @@ -48,7 +50,7 @@
<input required type="text" class="form-control" id="pageTag" name="pageTag" placeholder="{{ _t('PAGE_TAG_TO_DUPLICATE') }}" value="{{proposedTag}}" />
{% if toExternalWiki %}
<span class="input-group-btn">
<button class="btn-verify-tag btn btn-primary" type="button">{{ _t('VERIFY_PAGE_AVAILABILITY') }}</button>
<button class="btn-verify-tag btn btn-primary" type="submit">{{ _t('VERIFY_PAGE_AVAILABILITY') }}</button>
</span>
{% endif %}
</div>
Expand All @@ -59,7 +61,7 @@
<ol class="list-files">
{% for a in attachments %}
<li>{{a.path|replace({'files/': ''})}} ({{a.humanSize}})</li>
{% if toExternalWiki %}<input type="hidden" name="files[]" value="{{ baseUrl}}/{{ a.path }}" />{% endif %}
{% if toExternalWiki %}<input type="hidden" name="files[]" value="{{ baseUrl}}{{ a.path }}" />{% endif %}
{% endfor %}
</ol>
{% endif %}
Expand Down

0 comments on commit e1c47a9

Please sign in to comment.