Skip to content

Commit

Permalink
fix(duplication): various fixes from @seballot code review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Jul 31, 2024
1 parent d4fdb73 commit 6715402
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
23 changes: 11 additions & 12 deletions handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Throwable;
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ListManager;
Expand Down Expand Up @@ -51,15 +50,15 @@ public function run()
$data = $this->duplicationManager->checkPostData($_POST);
$this->duplicationManager->duplicateLocally($data);
if ($data['duplicate-action'] == 'edit') {
$this->wiki->Redirect($this->wiki->href('edit', $data['pageTag']));
$this->wiki->Redirect($this->wiki->href('edit', $data['newTag']));

return;
} elseif ($data['duplicate-action'] == 'return') {
$this->wiki->Redirect($this->wiki->href());

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

return;
} catch (Throwable $th) {
Expand All @@ -80,23 +79,23 @@ public function run()
$pageTitle = '';
if ($isEntry) {
$title = _t('TEMPLATE_DUPLICATE_ENTRY') . ' ' . $this->wiki->GetPageTag();
$pageContent = $this->getService(EntryManager::class)->getOne($this->wiki->GetPageTag());
$originalContent = $this->getService(EntryManager::class)->getOne($this->wiki->GetPageTag());
if ($toExternalWiki) {
$pageTitle = $pageContent['bf_titre'];
$pageTitle = $originalContent['bf_titre'];
$proposedTag = $this->wiki->GetPageTag();
$pageContent = $this->wiki->page['body'];
$originalContent = $this->wiki->page['body'];
} else {
$pageTitle = $pageContent['bf_titre'] . ' (' . _t('DUPLICATE') . ')';
$pageTitle = $originalContent['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());
$originalContent = $this->getService(ListManager::class)->getOne($this->wiki->GetPageTag());
if ($toExternalWiki) {
$pageTitle = $pageContent['titre_liste'];
$pageTitle = $originalContent['titre_liste'];
$proposedTag = $this->wiki->GetPageTag();
} else {
$pageTitle = $pageContent['titre_liste'] . ' (' . _t('DUPLICATE') . ')';
$pageTitle = $originalContent['titre_liste'] . ' (' . _t('DUPLICATE') . ')';
$proposedTag = genere_nom_wiki('Liste ' . $pageTitle);
}
} else { // page
Expand All @@ -106,7 +105,7 @@ public function run()
} else {
$proposedTag = genere_nom_wiki($this->wiki->GetPageTag() . ' ' . _t('DUPLICATE'));
}
$pageContent = $this->wiki->page['body'];
$originalContent = $this->wiki->page['body'];
}
$attachments = $this->duplicationManager->findFiles($this->wiki->page['tag']);
$totalSize = 0;
Expand All @@ -131,7 +130,7 @@ public function run()
'proposedTag' => $proposedTag ?? '',
'attachments' => $attachments ?? [],
'pageTitle' => $pageTitle ?? '',
'pageContent' => $pageContent ?? '',
'originalContent' => $originalContent ?? '',
'totalSize' => $this->duplicationManager->humanFilesize($totalSize ?? 0),
'type' => $type ?? '',
'baseUrl' => preg_replace('/\?$/Ui', '', $this->wiki->config['base_url']),
Expand Down
32 changes: 16 additions & 16 deletions includes/services/DuplicationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private function findFilesInWikiText($tag, $wikiText)
}

/**
* Get file attachements from pageTag.
* Get file attachements from newTag.
*
* @param string $tag page id
*
Expand Down Expand Up @@ -209,21 +209,21 @@ public function duplicateFiles($fromTag, $toTag)

public function checkPostData($data)
{
if (empty($data['type']) || !in_array($data['type'], ['page', 'list', 'entry'])) {
if (empty($data['type']) || !in_array($data['type'], ['form', 'page', 'list', 'entry'])) {
throw new \Exception(_t('NO_VALID_DATA_TYPE'));
}
if (empty($data['pageTag'])) {
if (empty($data['newTag'])) {
throw new \Exception(_t('EMPTY_PAGE_TAG'));
}
if ($data['type'] != 'page' && empty($data['pageTitle'])) {
if ($data['type'] != 'page' && empty($data['newTitle'])) {
throw new \Exception(_t('EMPTY_PAGE_TITLE'));
}
if (!$this->wiki->UserIsAdmin()) {
throw new \Exception(_t('ONLY_ADMINS_CAN_DUPLICATE') . '.');
}
$page = $this->wiki->services->get(PageManager::class)->getOne($data['pageTag']);
$page = $this->wiki->services->get(PageManager::class)->getOne($data['newTag']);
if ($page) {
throw new \Exception($data['pageTag'] . ' ' . _t('ALREADY_EXISTING'));
throw new \Exception($data['newTag'] . ' ' . _t('ALREADY_EXISTING'));
}
if (empty($data['duplicate-action']) || !in_array($data['duplicate-action'], ['open', 'edit', 'return'])) {
throw new \Exception(_t('NO_DUPLICATE_ACTION') . '.');
Expand All @@ -240,32 +240,32 @@ public function duplicateLocally($data)
switch ($data['type']) {
case 'list':
$list = $this->wiki->services->get(ListManager::class)->getOne($data['originalTag']);
$this->wiki->services->get(ListManager::class)->create($data['pageTitle'], $list['label'], $data['pageTag']);
$this->wiki->services->get(ListManager::class)->create($data['newTitle'], $list['label'], $data['newTag']);
break;

case 'entry':
$files = $this->duplicateFiles($data['originalTag'], $data['pageTag']);
$files = $this->duplicateFiles($data['originalTag'], $data['newTag']);
$entry = $this->wiki->services->get(EntryManager::class)->getOne($this->wiki->getPageTag());
$fields = $this->getUploadFieldsFromEntry($this->wiki->GetPageTag());
foreach ($fields as $f) {
foreach ($files as $fi) {
$entry[$f->getPropertyName()] = str_replace($fi['originalFile'], $fi['duplicatedFile'], $entry[$f->getPropertyName()]);
}
}
$entry['id_fiche'] = $data['pageTag'];
$entry['bf_titre'] = $data['pageTitle'];
$entry['id_fiche'] = $data['newTag'];
$entry['bf_titre'] = $data['newTitle'];
$entry['antispam'] = 1;
$this->wiki->services->get(EntryManager::class)->create($entry['id_typeannonce'], $entry);
break;

default:
case 'page':
$newBody = $this->wiki->page['body'];
$files = $this->duplicateFiles($data['originalTag'], $data['pageTag']);
$files = $this->duplicateFiles($data['originalTag'], $data['newTag']);
foreach ($files as $f) {
$newBody = str_replace($f['originalFile'], $f['duplicatedFile'], $newBody);
}
$this->wiki->services->get(PageManager::class)->save($data['pageTag'], $newBody);
$this->wiki->services->get(PageManager::class)->save($data['newTag'], $newBody);
break;
}

Expand All @@ -277,7 +277,7 @@ public function duplicateLocally($data)
);

$this->wiki->services->get(AclService::class)->save(
$data['pageTag'],
$data['newTag'],
$privilege,
$values['list']
);
Expand All @@ -291,7 +291,7 @@ public function duplicateLocally($data)
foreach ($properties as $prop) {
$values = $this->wiki->services->get(TripleStore::class)->getAll($data['originalTag'], $prop, '', '');
foreach ($values as $val) {
$this->wiki->services->get(TripleStore::class)->create($data['pageTag'], $prop, $val['value'], '', '');
$this->wiki->services->get(TripleStore::class)->create($data['newTag'], $prop, $val['value'], '', '');
}
}
}
Expand All @@ -302,7 +302,7 @@ public function importDistantContent($tag, $request)
throw new Exception(_t('ACEDITOR_LINK_PAGE_ALREADY_EXISTS'));
}
$req = $request->request->all();
foreach (['pageContent', 'sourceUrl', 'originalTag', 'type'] as $key) {
foreach (['originalContent', 'sourceUrl', 'originalTag', 'type'] as $key) {
if (empty($req[$key])) {
throw new Exception(_t('NOT_FOUND_IN_REQUEST', $key));
}
Expand All @@ -312,7 +312,7 @@ public function importDistantContent($tag, $request)
}

$newUrl = explode('/?', $this->wiki->config['base_url'])[0];
$newBody = str_replace($req['sourceUrl'], $newUrl, $req['pageContent']);
$newBody = str_replace($req['sourceUrl'], $newUrl, $req['originalContent']);
if ($req['type'] === 'page') {
$this->wiki->services->get(PageManager::class)->save($tag, $newBody);
} elseif ($req['type'] === 'entry') {
Expand Down
22 changes: 11 additions & 11 deletions javascripts/handlers/duplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ document.addEventListener('DOMContentLoaded', () => {
e.stopPropagation()
return false
})
$('#urlWiki').on('change', () => {
$('#url-wiki').on('change', () => {
$('.login-fields, .duplication-fields').addClass('hide')
$('#login-message').html('')
})
Expand Down Expand Up @@ -89,14 +89,14 @@ document.addEventListener('DOMContentLoaded', () => {
method: 'POST',
url: `${shortUrl}/?api/pages/${$('#pageTag').val()}/duplicate`,
data: $('#form-duplication').serialize()
}).done((data) => {
if (btnAction == 'open') {
location = `${shortUrl}/?${data.pageTag}`
} else if (btnAction == 'edit') {
location = `${shortUrl}/?${data.pageTag}/edit`
}).done((d) => {
if (btnAction === 'open') {
document.location = `${shortUrl}/?${d.pageTag}`

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
} else if (btnAction === 'edit') {
document.location = `${shortUrl}/?${d.pageTag}/edit`

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
} else {
const url = location.href.replace(/\/duplicate.*/, '')
location = url
const url = document.location.href.replace(/\/duplicate.*/, '')
document.location = url
}
}).fail((jqXHR) => {
toastMessage(`${_t('ERROR')} ${jqXHR.status}`, 3000, 'alert alert-danger')
Expand All @@ -109,7 +109,7 @@ document.addEventListener('DOMContentLoaded', () => {
})

$('.btn-verify-wiki').on('click', () => {
let url = $('#urlWiki').val()
let url = $('#url-wiki').val()

if (isValidUrl(url)) {
let taburl = []
Expand All @@ -119,7 +119,7 @@ document.addEventListener('DOMContentLoaded', () => {
taburl = url.split('?')
}
shortUrl = taburl[0].replace(/\/+$/g, '')
$('#baseUrl').text(`${shortUrl}/?`)
$('#base-url').text(`${shortUrl}/?`)
url = `${shortUrl}/?api/auth/me`
$.ajax({
method: 'GET',
Expand All @@ -138,4 +138,4 @@ document.addEventListener('DOMContentLoaded', () => {
toastMessage(_t('NOT_VALID_URL', { url }), 3000, 'alert alert-danger')
}
})
})
})
16 changes: 8 additions & 8 deletions templates/handlers/duplicate.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<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>
<label for="url-wiki" 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="" />
<input required type="url" class="form-control" id="url-wiki" name="url-wiki" placeholder="{{ _t('WIKI_URL') }}" value="" />
<span class="input-group-btn">
<button class="btn-verify-wiki btn btn-primary" type="submit">{{ _t('VERIFY_WIKI') }}</button>
</span>
Expand Down Expand Up @@ -41,17 +41,17 @@
<form id="form-duplication" method="post" action="{{ url({handler: 'duplicate'})}}">
{% if pageTitle %}
<div class="form-group">
<label for="pageTitle" class="control-label">{{ _t('PAGE_TITLE_TO_DUPLICATE') }}</label>
<label for="newTitle" class="control-label">{{ _t('PAGE_TITLE_TO_DUPLICATE') }}</label>
<div class="input-group">
<input required type="text" class="form-control" id="pageTitle" name="pageTitle" placeholder="{{ _t('PAGE_TITLE_TO_DUPLICATE') }}" value="{{pageTitle}}" />
<input required type="text" class="form-control" id="newTitle" name="newTitle" placeholder="{{ _t('PAGE_TITLE_TO_DUPLICATE') }}" value="{{pageTitle}}" />
</div>
</div>
{% endif %}
<div class="form-group">
<label for="pageTag" class="control-label">{{ _t('PAGE_TAG_TO_DUPLICATE') }}</label>
<label for="newTag" class="control-label">{{ _t('PAGE_TAG_TO_DUPLICATE') }}</label>
<div class="input-group">
<span class="input-group-addon" id="baseUrl">{{ baseUrl }}</span>
<input required type="text" class="form-control" id="pageTag" name="pageTag" placeholder="{{ _t('PAGE_TAG_TO_DUPLICATE') }}" value="{{proposedTag}}" />
<span class="input-group-addon" id="base-url">{{ baseUrl }}</span>
<input required type="text" class="form-control" id="newTag" name="newTag" placeholder="{{ _t('PAGE_TAG_TO_DUPLICATE') }}" value="{{proposedTag}}" />
{% if toExternalWiki %}
<span class="input-group-btn">
<button class="btn-verify-tag btn btn-primary" type="submit">{{ _t('VERIFY_PAGE_AVAILABILITY') }}</button>
Expand All @@ -69,7 +69,7 @@
{% endfor %}
</ol>
{% endif %}
{% if toExternalWiki %}<input type="hidden" name="pageContent" value="{{ pageContent }}" />{% endif %}
{% if toExternalWiki %}<input type="hidden" name="originalContent" value="{{ originalContent }}" />{% endif %}

<input type="hidden" name="sourceUrl" value="{{ sourceUrl }}" />
<input type="hidden" name="originalTag" value="{{ originalTag }}" />
Expand Down

0 comments on commit 6715402

Please sign in to comment.