Skip to content

Commit

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

use Throwable;
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ListManager;
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 @@ -61,7 +62,7 @@ public function run()
$this->wiki->Redirect($this->wiki->href('', $data['pageTag']));

return;
} catch (\Throwable $th) {
} catch (Throwable $th) {
$error .= $this->render('@templates\alert-message-with-back.twig', [
'type' => 'warning',
'message' => $th->getMessage(),
Expand Down Expand Up @@ -137,4 +138,4 @@ public function run()
'toExternalWiki' => $toExternalWiki,
]);
}
}
}
2 changes: 1 addition & 1 deletion includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,4 @@ public function archivesAction()
{
return $this->getService(ArchiveController::class)->manageArchiveAction();
}
}
}
37 changes: 15 additions & 22 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 @@ -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 @@ -242,17 +239,17 @@ public function duplicateLocally($data)
}
switch ($data['type']) {
case 'list':
$list = $this->wiki->services->get(ListManager::class)->getOne($this->wiki->getPageTag());
$list = $this->wiki->services->get(ListManager::class)->getOne($data['originalTag']);
$this->wiki->services->get(ListManager::class)->create($data['pageTitle'], $list['label'], $data['pageTag']);
break;

case 'entry':
$files = $this->duplicateFiles($this->wiki->getPageTag(), $data['pageTag']);
$files = $this->duplicateFiles($data['originalTag'], $data['pageTag']);
$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'];
Expand All @@ -264,7 +261,7 @@ public function duplicateLocally($data)
default:
case 'page':
$newBody = $this->wiki->page['body'];
$files = $this->duplicateFiles($this->wiki->getPageTag(), $data['pageTag']);
$files = $this->duplicateFiles($data['originalTag'], $data['pageTag']);
foreach ($files as $f) {
$newBody = str_replace($f['originalFile'], $f['duplicatedFile'], $newBody);
}
Expand Down Expand Up @@ -292,7 +289,7 @@ 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'], '', '');
}
Expand All @@ -303,15 +300,11 @@ 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) {
Expand Down Expand Up @@ -361,4 +354,4 @@ public function humanFilesize($bytes, $decimals = 2)

return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B';
}
}
}
Loading

0 comments on commit 66f8691

Please sign in to comment.