Skip to content

Commit

Permalink
Merge branch 'feat/duplication' into feat/entry-with-extra-data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Dec 10, 2024
2 parents 41b7d84 + f03ec43 commit fd01348
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 137 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ indent_size = 4

[*.{html,js,json}]
indent_style = space
indent_size = 2
indent_size = 2
1 change: 1 addition & 0 deletions .phpactor.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"$schema": "/phpactor.schema.json",
"language_server_php_cs_fixer.enabled": true
}
2 changes: 1 addition & 1 deletion docs/actions/lang/actionsbuilder_en.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
'AB_buttons_action_button_pull_right' => 'Right',
'AB_attach_link_label' => 'Associated link',
'AB_attach_caption_label' => 'Text displayed on hovering',
'AB_attach_nofullimagelink_label' => 'Add a link to only display the full image',
'AB_attach_nofullimagelink_label' => 'Enable to click on the image to display the full image',
'AB_attach_class_position_left' => 'Left',
'AB_attach_class_position_center' => 'Center',
'AB_attach_class_position_right' => 'Right',
Expand Down
31 changes: 17 additions & 14 deletions handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Core\Controller\AuthController;
use YesWiki\Core\Service\AclService;
Expand Down Expand Up @@ -33,9 +34,9 @@ public function run()
]);
} 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')) {
if ($content = $this->getService(PageManager::class)->getOne('PageLogin')) {
// si une page PageLogin existe, on l'affiche
$error .= $this->wiki->Format($contenu['body']);
$error .= $this->wiki->Format($content['body']);
} else {
// sinon on affiche le formulaire d'identification minimal
$error .= '<div class="vertical-center white-bg">' . "\n"
Expand All @@ -50,18 +51,18 @@ 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) {
} catch (Throwable $th) {
$error .= $this->render('@templates\alert-message-with-back.twig', [
'type' => 'warning',
'message' => $th->getMessage(),
Expand All @@ -79,23 +80,24 @@ 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'];
$form = $this->getService(FormManager::class)->getOne($this->getService(EntryManager::class)->getOne($proposedTag)['id_typeannonce']);
} 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 @@ -105,7 +107,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 @@ -130,9 +132,10 @@ public function run()
'proposedTag' => $proposedTag ?? '',
'attachments' => $attachments ?? [],
'pageTitle' => $pageTitle ?? '',
'pageContent' => $pageContent ?? '',
'originalContent' => $originalContent ?? '',
'totalSize' => $this->duplicationManager->humanFilesize($totalSize ?? 0),
'type' => $type ?? '',
'form' => $form ?? '',
'baseUrl' => preg_replace('/\?$/Ui', '', $this->wiki->config['base_url']),
'toExternalWiki' => $toExternalWiki,
]);
Expand Down
65 changes: 29 additions & 36 deletions includes/services/DuplicationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function getLocalFileUploadPath()
*
* @return array keys of fields that may contain attachments to import
*/
public function getUploadFieldsFromEntry($id)
private function getUploadFieldsFromEntry($id)
{
$fields = [];
$entry = $this->wiki->services->get(EntryManager::class)->getOne($id);
Expand All @@ -65,19 +65,16 @@ public function getUploadFieldsFromEntry($id)
$form = $formManager->getOne($entry['id_typeannonce']);
// find fields that are textareas
foreach ($form['prepared'] as $field) {
if ($field instanceof TextareaField or $field instanceof ImageField or $field instanceof FileField) {
$fields[] = [
'id' => $field->getPropertyName(),
'type' => $field->getType(),
];
if ($field instanceof TextareaField || $field instanceof ImageField || $field instanceof FileField) {
$fields[] = $field;
}
}
}

return $fields;
}

public function findFilesInUploadField($fieldValue)
private function findFilesInUploadField($fieldValue)
{
$f = $this->uploadPath . '/' . $fieldValue;
if ($f !== $this->uploadPath . '/' && file_exists($f)) {
Expand All @@ -98,7 +95,7 @@ public function findFilesInUploadField($fieldValue)
*
* @return array files
*/
public function findFilesInWikiText($tag, $wikiText)
private function findFilesInWikiText($tag, $wikiText)
{
$filesMatched = [];
$regex = '#\{\{attach.*file="(.*)".*\}\}#Ui';
Expand Down Expand Up @@ -149,7 +146,7 @@ public function findFilesInWikiText($tag, $wikiText)
}

/**
* Get file attachements from pageTag.
* Get file attachements from newTag.
*
* @param string $tag page id
*
Expand All @@ -166,12 +163,12 @@ public function findFiles($tag = '')
$fields = $this->getUploadFieldsFromEntry($tag);
$entry = $this->wiki->services->get(EntryManager::class)->getOne($tag);
foreach ($fields as $f) {
if ($f['type'] == 'image' || $f['type'] == 'fichier') {
if (!empty($fi = $this->findFilesInUploadField($entry[$f['id']]))) {
if ($f instanceof ImageField || $f instanceof FileField) {
if (!empty($fi = $this->findFilesInUploadField($entry[$f->getPropertyName()]))) {
$files[] = $fi;
}
} elseif ($f['type'] == 'textelong') {
if (!empty($fi = $this->findFilesInWikiText($tag, $entry[$f['id']]))) {
} elseif ($f instanceof TextareaField) {
if (!empty($fi = $this->findFilesInWikiText($tag, $entry[$f->getPropertyName()]))) {
$files = array_merge($files, $fi);
}
}
Expand Down Expand Up @@ -212,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 @@ -242,33 +239,33 @@ public function duplicateLocally($data)
}
switch ($data['type']) {
case 'list':
$list = $this->wiki->services->get(ListManager::class)->getOne($this->wiki->getPageTag());
$this->wiki->services->get(ListManager::class)->create($data['pageTitle'], $list['label'], $data['pageTag']);
$list = $this->wiki->services->get(ListManager::class)->getOne($data['originalTag']);
$this->wiki->services->get(ListManager::class)->create($data['newTitle'], $list['label'], $data['newTag']);
break;

case 'entry':
$files = $this->duplicateFiles($this->wiki->getPageTag(), $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['id']] = str_replace($fi['originalFile'], $fi['duplicatedFile'], $entry[$f['id']]);
$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($this->wiki->getPageTag(), $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 @@ -280,7 +277,7 @@ public function duplicateLocally($data)
);

$this->wiki->services->get(AclService::class)->save(
$data['pageTag'],
$data['newTag'],
$privilege,
$values['list']
);
Expand All @@ -292,9 +289,9 @@ public function duplicateLocally($data)
'http://outils-reseaux.org/_vocabulary/tag',
];
foreach ($properties as $prop) {
$values = $this->wiki->services->get(TripleStore::class)->getAll($this->wiki->GetPageTag(), $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 @@ -303,30 +300,26 @@ 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) {
foreach (['originalContent', '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);
}

$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') {
$entry = json_decode($newBody, true);
$entry['id_fiche'] = $tag;
$entry['antispam'] = 1;
$this->wiki->services->get(EntryManager::class)->create($entry['id_typeannonce'], $entry);
$this->wiki->services->get(EntryManager::class)->create($entry['id_typeannonce'], $entry, false, $req['sourceUrl']);
}
}

Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*
* @category Wiki
*
* @license GNU/GPL version 3
* @license GNU/GPL version 3
*
* @see https://yeswiki.net
* @see https://yeswiki.net
*/

use YesWiki\Core\YesWikiLoader;
Expand Down
Loading

0 comments on commit fd01348

Please sign in to comment.