Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
seballot committed Jul 1, 2024
1 parent 84019bd commit eba365d
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 137 deletions.
26 changes: 15 additions & 11 deletions handlers/DuplicateHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use YesWiki\Core\Service\AclService;
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Core\Service\PageManager;
use YesWiki\Core\Service\DuplicationManager;
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
Expand All @@ -21,21 +21,21 @@ public function run()
$this->entryController = $this->getService(EntryController::class);
$this->duplicationManager = $this->getService(DuplicationManager::class);
$title = $error = '';
$toExternalWiki = isset($_GET['toUrl']) && $_GET['toUrl'] == "1";
$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")
['{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")) {
if ($contenu = $this->getService(PageManager::class)->getOne('PageLogin')) {
// si une page PageLogin existe, on l'affiche
$error .= $this->wiki->Format($contenu["body"]);
$error .= $this->wiki->Format($contenu['body']);
} else {
// sinon on affiche le formulaire d'identification minimal
$error .= '<div class="vertical-center white-bg">' . "\n"
Expand All @@ -51,12 +51,15 @@ public function run()
$this->duplicationManager->duplicateLocally($data);
if ($data['duplicate-action'] == 'edit') {
$this->wiki->Redirect($this->wiki->href('edit', $data['pageTag']));

return;
} else if ($data['duplicate-action'] == 'return') {
} 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', [
Expand Down Expand Up @@ -118,6 +121,7 @@ public function run()
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(),
Expand All @@ -133,4 +137,4 @@ public function run()
'toExternalWiki' => $toExternalWiki,
]);
}
}
}
2 changes: 1 addition & 1 deletion includes/YesWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -2067,4 +2067,4 @@ public function GetCookie($name)
{
return $_COOKIE[$name];
}
}
}
14 changes: 8 additions & 6 deletions includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace YesWiki\Core\Controller;

use Exception;
use Throwable;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
use Throwable;
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Core\ApiResponse;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function getDocumentation()
if (file_exists($pluginBase . 'controllers/ApiController.php')) {
$apiClassName = 'YesWiki\\' . ucfirst($extension) . '\\Controller\\ApiController';
if (!class_exists($apiClassName, false)) {
include($pluginBase . 'controllers/ApiController.php');
include $pluginBase . 'controllers/ApiController.php';
}
if (class_exists($apiClassName, false)) {
$apiController = new $apiClassName();
Expand Down Expand Up @@ -393,6 +393,7 @@ public function duplicatePage(Request $request, $tag)
} catch (\Throwable $th) {
return new ApiResponse($th->getMessage(), Response::HTTP_FORBIDDEN);
}

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

Expand Down Expand Up @@ -835,13 +836,13 @@ private function extractTriplesParams(string $method, $resource): array
);
} else {
$property = filter_input($method, 'property', FILTER_UNSAFE_RAW);
$property = in_array($property, [false, null], true) ? "" : htmlspecialchars(strip_tags($property));
$property = in_array($property, [false, null], true) ? '' : htmlspecialchars(strip_tags($property));
if (empty($property)) {
$property = null;
}

$username = filter_input($method, 'user', FILTER_UNSAFE_RAW);
$username = in_array($username, [false, null], true) ? "" : htmlspecialchars(strip_tags($username));
$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 All @@ -857,6 +858,7 @@ private function extractTriplesParams(string $method, $resource): array
);
}
}

return compact(['property', 'username', 'apiResponse']);
}

Expand All @@ -875,7 +877,7 @@ public function getArchiveStatus($uid)
{
return $this->getService(ArchiveController::class)->getArchiveStatus(
$uid,
empty($_GET['forceStarted']) ? false : in_array($_GET['forceStarted'], [1, true, "1", "true"], true)
empty($_GET['forceStarted']) ? false : in_array($_GET['forceStarted'], [1, true, '1', 'true'], true)
);
}

Expand Down Expand Up @@ -932,4 +934,4 @@ public function archivesAction()
{
return $this->getService(ArchiveController::class)->manageArchiveAction();
}
}
}
4 changes: 2 additions & 2 deletions includes/controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public function delete(string $tag): bool
}
}

public function duplicate(string $sourceTag, string $destinationTag = '') : bool
public function duplicate(string $sourceTag, string $destinationTag = ''): bool
{
if ($this->entryManager->isEntry($sourceTag)){
if ($this->entryManager->isEntry($sourceTag)) {
return $this->entryController->duplicate($sourceTag, $destinationTag);
} else {
return $this->pageManager->duplicate($sourceTag, $destinationTag);
Expand Down
41 changes: 26 additions & 15 deletions includes/services/DuplicationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
use YesWiki\Bazar\Field\FileField;
use YesWiki\Bazar\Field\ImageField;
use YesWiki\Bazar\Field\TextareaField;
use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\EntryManager;
use YesWiki\Bazar\Service\FormManager;
use YesWiki\Bazar\Service\ListManager;
use YesWiki\Core\Service\PageManager;
use YesWiki\Wiki;

class DuplicationManager
{
protected $uploadPath;
protected $wiki;


/**
* DuplicationManager constructor
* DuplicationManager constructor.
*
* @param Wiki $wiki the injected Wiki instance
*/
public function __construct(Wiki $wiki)
Expand All @@ -29,7 +28,7 @@ public function __construct(Wiki $wiki)
}

/**
* Get the local path to files uploads (usually "files")
* Get the local path to files uploads (usually "files").
*
* @return string local path to files uploads
*/
Expand All @@ -38,7 +37,7 @@ private function getLocalFileUploadPath()
$attachConfig = $this->wiki->config['attach_config'];

if (!is_array($attachConfig)) {
$attachConfig = array();
$attachConfig = [];
}

if (empty($attachConfig['upload_path'])) {
Expand All @@ -51,9 +50,10 @@ private function getLocalFileUploadPath()
}

/**
* Return fields that may contain attachments to import (fichier, image, or textelong fields for bazar entries)
* Return fields that may contain attachments to import (fichier, image, or textelong fields for bazar entries).
*
* @param array $id
*
* @return array keys of fields that may contain attachments to import
*/
public function getUploadFieldsFromEntry($id)
Expand All @@ -68,11 +68,12 @@ public function getUploadFieldsFromEntry($id)
if ($field instanceof TextareaField or $field instanceof ImageField or $field instanceof FileField) {
$fields[] = [
'id' => $field->getPropertyName(),
'type' => $field->getType()
'type' => $field->getType(),
];
}
}
}

return $fields;
}

Expand All @@ -82,17 +83,19 @@ public function findFilesInUploadField($fieldValue)
if ($f !== $this->uploadPath . '/' && file_exists($f)) {
$size = filesize($f);
$humanSize = $this->humanFilesize($size);

return ['path' => $f, 'size' => $size, 'humanSize' => $humanSize];
} else {
return [];
}
}

/**
* find files in wiki text
* find files in wiki text.
*
* @param string $wikiTag
* @param string $wikiText
*
* @return array files
*/
public function findFilesInWikiText($tag, $wikiText)
Expand Down Expand Up @@ -141,13 +144,15 @@ public function findFilesInWikiText($tag, $wikiText)
$filesMatched[] = ['path' => $f, 'size' => $size, 'humanSize' => $humanSize];
}
}

return $filesMatched;
}

/**
* Get file attachements from pageTag
*
* Get file attachements from pageTag.
*
* @param string $tag page id
*
* @return array attachments filenames
*/
public function findFiles($tag = '')
Expand All @@ -157,7 +162,7 @@ public function findFiles($tag = '')
$tag = $this->wiki->GetPageTag();
}
if ($this->wiki->services->get(EntryManager::class)->isEntry($tag)) {
// bazar
// bazar
$fields = $this->getUploadFieldsFromEntry($tag);
$entry = $this->wiki->services->get(EntryManager::class)->getOne($tag);
foreach ($fields as $f) {
Expand All @@ -177,6 +182,7 @@ public function findFiles($tag = '')
$files = array_merge($files, $fi);
}
}

return $files;
}

Expand All @@ -200,6 +206,7 @@ public function duplicateFiles($fromTag, $toTag)
'duplicatedFile' => str_replace($this->uploadPath . '/', '', $newPath),
];
}

return $doneFiles;
}

Expand All @@ -224,6 +231,7 @@ public function checkPostData($data)
if (empty($data['duplicate-action']) || !in_array($data['duplicate-action'], ['open', 'edit', 'return'])) {
throw new \Exception(_t('NO_DUPLICATE_ACTION') . '.');
}

return $data;
}

Expand Down Expand Up @@ -281,7 +289,7 @@ public function duplicateLocally($data)
// duplicate metadatas and tags (TODO: is there more duplicable triples?)
$properties = [
'http://outils-reseaux.org/_vocabulary/metadata',
'http://outils-reseaux.org/_vocabulary/tag'
'http://outils-reseaux.org/_vocabulary/tag',
];
foreach ($properties as $prop) {
$values = $this->wiki->services->get(TripleStore::class)->getAll($this->wiki->GetPageTag(), $prop, '', '');
Expand All @@ -295,20 +303,22 @@ 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);
}

$newUrl = explode('/?', $this->wiki->config['base_url'])[0];
$newUrl = explode('/?', $this->wiki->config['base_url'])[0];
$newBody = str_replace($req['sourceUrl'], $newUrl, $req['pageContent']);
if ($req['type'] === 'page') {
$this->wiki->services->get(PageManager::class)->save($tag, $newBody);
Expand Down Expand Up @@ -348,6 +358,7 @@ public function humanFilesize($bytes, $decimals = 2)
if ($factor > 0) {
$sz = 'KMGT';
}

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

0 comments on commit eba365d

Please sign in to comment.