From 7a265031e638a6fe7be81e3ed3b26b01087c57a0 Mon Sep 17 00:00:00 2001 From: GaziYucel Date: Thu, 30 May 2024 11:00:15 +0200 Subject: [PATCH] register handler bug --- LatexConverterPlugin.php | 41 ++++++++------- assets/index.php | 0 classes/Handler/PluginHandler.php | 76 ++++++--------------------- classes/Helpers/FileSystemHelper.php | 8 ++- classes/Workflow/Convert.php | 6 +-- classes/Workflow/Extract.php | 6 +-- classes/Workflow/Links.php | 42 +++++---------- vendor/composer/autoload_classmap.php | 2 - vendor/composer/autoload_static.php | 2 - version.xml | 2 +- 10 files changed, 66 insertions(+), 119 deletions(-) create mode 100644 assets/index.php diff --git a/LatexConverterPlugin.php b/LatexConverterPlugin.php index 39bf4f6..4c77404 100755 --- a/LatexConverterPlugin.php +++ b/LatexConverterPlugin.php @@ -17,7 +17,6 @@ require_once(__DIR__ . '/vendor/autoload.php'); use APP\plugins\generic\latexConverter\classes\Constants; -use APP\plugins\generic\latexConverter\classes\Handler\PluginHandler; use APP\plugins\generic\latexConverter\classes\Settings\Actions; use APP\plugins\generic\latexConverter\classes\Settings\Manage; use APP\plugins\generic\latexConverter\classes\Settings\MimeTypes; @@ -36,10 +35,9 @@ function register($category, $path, $mainContextId = null): bool { if (parent::register($category, $path, $mainContextId)) { if ($this->getEnabled()) { - $pluginHandler = new PluginHandler(); $links = new Links($this); $mimeTypes = new MimeTypes($this); - Hook::add('LoadHandler', [$pluginHandler, 'register']); + Hook::add('LoadHandler', [$this, 'registerHandler']); Hook::add('TemplateManager::fetch', [$links, 'execute']); Hook::add('SubmissionFile::supportsDependentFiles', [$mimeTypes, 'execute']); @@ -52,6 +50,25 @@ function register($category, $path, $mainContextId = null): bool return false; } + /** Register PluginHandler */ + public function registerHandler(string $hookName, array $args): bool + { + $page = $args[0]; + $op = $args[1]; + + switch ("$page/$op") { + case "latexConverter/extractShow": + case "latexConverter/extractExecute": + case "latexConverter/convert": + define('HANDLER_CLASS', '\APP\plugins\generic\latexConverter\classes\Handler\PluginHandler'); + return true; + default: + break; + } + + return false; + } + /** @copydoc Plugin::getActions() */ public function getActions($request, $actionArgs): array { @@ -68,31 +85,19 @@ public function manage($args, $request): JSONMessage return $manage->execute($args, $request); } - /* Plugin required methods */ - - /** - * @copydoc PKPPlugin::getDisplayName - */ + /** @copydoc PKPPlugin::getDisplayName */ public function getDisplayName(): string { return __('plugins.generic.latexConverter.displayName'); } - /** - * @copydoc PKPPlugin::getDescription - */ + /** @copydoc PKPPlugin::getDescription */ public function getDescription(): string { return __('plugins.generic.latexConverter.description'); } - /** - * Overrides parent getSetting - * - * @param $contextId - * @param $name - * @return mixed|null - */ + /** @copydoc PKPPlugin::getSetting */ public function getSetting($contextId, $name): mixed { switch ($name) { diff --git a/assets/index.php b/assets/index.php new file mode 100644 index 0000000..e69de29 diff --git a/classes/Handler/PluginHandler.php b/classes/Handler/PluginHandler.php index 6330546..f364445 100755 --- a/classes/Handler/PluginHandler.php +++ b/classes/Handler/PluginHandler.php @@ -26,16 +26,10 @@ class PluginHandler extends Handler { - /** - * @var LatexConverterPlugin - */ + /** @var LatexConverterPlugin */ protected LatexConverterPlugin $plugin; - /** - * List of methods allowed - * - * @var array|string[] - */ + /** @var array|string[] List of methods allowed */ protected array $allowedMethods = ['extractShow', 'extractExecute', 'convert']; function __construct() @@ -44,56 +38,24 @@ function __construct() /* @var LatexConverterPlugin $plugin */ $plugin = PluginRegistry::getPlugin('generic', strtolower(LATEX_CONVERTER_PLUGIN_NAME)); - $this->plugin = $plugin; - - $this->addRoleAssignment( - Constants::authorizedRoles, - $this->allowedMethods - ); - } - - /** - * Register PluginHandler - * - * @param $hookName string - * @param $args array Hook arguments [&$page, &$op, &$sourceFile] - * @return bool - */ - public function register(string $hookName, array $args): bool - { - $page = $args[0]; - $op = $args[1]; + $this->plugin = &$plugin; - switch ("$page/$op") { - case "latexConverter/extractShow": - case "latexConverter/extractExecute": - case "latexConverter/convert": - define('HANDLER_CLASS', '\APP\plugins\generic\latexConverter\classes\Handler\PluginHandler'); - return true; - default: - break; - } - - return false; + $this->addRoleAssignment(Constants::authorizedRoles, $this->allowedMethods); } - /** - * Overridden method from Handler - * - * @copydoc PKPHandler::authorize() - */ + /** @copydoc PKPHandler::authorize() */ function authorize($request, &$args, $roleAssignments): bool { - $this->addPolicy( - new WorkflowStageAccessPolicy( - $request, - $args, - $roleAssignments, - 'submissionId', - (int)$request->getUserVar('stageId') - ) + $policy = new WorkflowStageAccessPolicy( + $request, + $args, + $roleAssignments, + 'submissionId', + (int)$request->getUserVar('stageId') ); + $this->addPolicy($policy); + return parent::authorize($request, $args, $roleAssignments); } @@ -106,10 +68,8 @@ function authorize($request, &$args, $roleAssignments): bool */ public function extractShow($args, $request): JSONMessage { - $action = new Extract($this->plugin, $request, $args); - + $action = new Extract($this->plugin); $action->initData(); - return new JSONMessage(true, $action->fetch($request)); } @@ -122,12 +82,9 @@ public function extractShow($args, $request): JSONMessage */ public function extractExecute($args, $request): JSONMessage { - $action = new Extract($this->plugin, $request, $args); - + $action = new Extract($this->plugin); $action->readInputData(); - $action->process(); - return $request->redirectUrlJson( $request->getDispatcher()->url( $request, @@ -153,8 +110,7 @@ public function extractExecute($args, $request): JSONMessage */ public function convert($args, $request): JSONMessage { - $action = new Convert($this->plugin, $request, $args); - + $action = new Convert($this->plugin); return $action->process(); } } diff --git a/classes/Helpers/FileSystemHelper.php b/classes/Helpers/FileSystemHelper.php index feb7ed8..3134df9 100644 --- a/classes/Helpers/FileSystemHelper.php +++ b/classes/Helpers/FileSystemHelper.php @@ -14,6 +14,10 @@ namespace APP\plugins\generic\latexConverter\classes\Helpers; +use FilesystemIterator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; + class FileSystemHelper { /** @@ -59,8 +63,8 @@ public static function getDirectoryFilesRecursively(string $path): array if (empty($path) || !file_exists($path)) return $files; - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS)); + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)); foreach ($iterator as $file) { if (!is_dir($file)) { diff --git a/classes/Workflow/Convert.php b/classes/Workflow/Convert.php index 8500989..a402591 100755 --- a/classes/Workflow/Convert.php +++ b/classes/Workflow/Convert.php @@ -127,15 +127,15 @@ class Convert */ protected string $latexExe = ''; - function __construct(LatexConverterPlugin $plugin, PKPRequest $request, $args) + function __construct(LatexConverterPlugin &$plugin) { $this->timeStamp = date('Ymd_His'); - $this->plugin = $plugin; + $this->plugin = &$plugin; $this->notificationManager = new NotificationManager(); - $this->request = $request; + $this->request = $this->plugin->getRequest(); $this->submissionFileId = (int)$this->request->getUserVar('submissionFileId'); $this->submissionFile = Repo::submissionFile()->get($this->submissionFileId); diff --git a/classes/Workflow/Extract.php b/classes/Workflow/Extract.php index c69fd56..814654d 100755 --- a/classes/Workflow/Extract.php +++ b/classes/Workflow/Extract.php @@ -101,17 +101,17 @@ class Extract extends Form */ protected string $latexConverterSelectedFilenameKey = 'latexConverter_SelectedFilename'; - function __construct(LatexConverterPlugin $plugin, PKPRequest $request, $args) + function __construct(LatexConverterPlugin &$plugin) { $this->timeStamp = date('Ymd_His'); - $this->plugin = $plugin; + $this->plugin = &$plugin; $this->fileManager = new PrivateFileManager(); $this->notificationManager = new NotificationManager(); - $this->request = $request; + $this->request = $this->plugin->getRequest(); $this->submissionFileId = (int)$this->request->getUserVar('submissionFileId'); $this->submissionFile = Repo::submissionFile()->get($this->submissionFileId); diff --git a/classes/Workflow/Links.php b/classes/Workflow/Links.php index 6bf1304..94779fd 100644 --- a/classes/Workflow/Links.php +++ b/classes/Workflow/Links.php @@ -100,22 +100,27 @@ public function execute(string $hookName, array $args): void $row->addAction( new LinkAction( 'latexconverter_extract_zip', - new AjaxModal( - $path, - __('plugins.generic.latexConverter.modal.extract.title') - ), + new AjaxModal($path, __('plugins.generic.latexConverter.modal.extract.title')), __('plugins.generic.latexConverter.button.extract'), null ) ); - } // only show link if file is tex and is not dependent file (assocId is null) elseif (strtolower($fileExtension) == Constants::texFileType && empty($submissionFile->getData('assocId'))) { - - $disableLink = true; - $linkText = $this->getConvertButton(); - if ($linkText === 'plugins.generic.latexConverter.button.convert') $disableLink = false; + $disableLink = false; + $latexExe = $this->plugin->getSetting( + $this->plugin->getRequest()->getContext()->getId(), + Constants::settingKeyPathExecutable); + + $linkText = 'plugins.generic.latexConverter.button.convert'; + if (strlen($latexExe) == 0) { + $linkText = 'plugins.generic.latexConverter.executable.notConfigured'; + $disableLink = true; + } elseif (!is_executable($latexExe)) { + $linkText = 'plugins.generic.latexConverter.executable.notFound'; + $disableLink = true; + } $path = $dispatcher->url( $request, @@ -141,23 +146,4 @@ public function execute(string $hookName, array $args): void } } } - - /** - * Converter button depending on the latex executable status - * - * @return string - */ - public function getConvertButton(): string - { - $latexExe = $this->plugin->getSetting($this->plugin->getRequest()->getContext()->getId(), - Constants::settingKeyPathExecutable); - - if (strlen($latexExe) == 0) { - return 'plugins.generic.latexConverter.executable.notConfigured'; - } elseif (!is_executable($latexExe)) { - return 'plugins.generic.latexConverter.executable.notFound'; - } - - return 'plugins.generic.latexConverter.button.convert'; - } } \ No newline at end of file diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index df12344..27df81f 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -7,10 +7,8 @@ return array( 'APP\\plugins\\generic\\latexConverter\\classes\\Constants' => $baseDir . '/classes/Constants.php', - 'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\LoadHandler' => $baseDir . '/classes/Handler/LoadHandler.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\PluginHandler' => $baseDir . '/classes/Handler/PluginHandler.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\FileSystemHelper' => $baseDir . '/classes/Helpers/FileSystemHelper.php', - 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\LogHelper' => $baseDir . '/classes/Helpers/LogHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\SubmissionFileHelper' => $baseDir . '/classes/Helpers/SubmissionFileHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\ZipHelper' => $baseDir . '/classes/Helpers/ZipHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Settings\\Actions' => $baseDir . '/classes/Settings/Actions.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 6ab8482..22d0be8 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -8,10 +8,8 @@ class ComposerStaticInit1b66bd88bb6dab52bf3af9b9a16ca7a1 { public static $classMap = array ( 'APP\\plugins\\generic\\latexConverter\\classes\\Constants' => __DIR__ . '/../..' . '/classes/Constants.php', - 'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\LoadHandler' => __DIR__ . '/../..' . '/classes/Handler/LoadHandler.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Handler\\PluginHandler' => __DIR__ . '/../..' . '/classes/Handler/PluginHandler.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\FileSystemHelper' => __DIR__ . '/../..' . '/classes/Helpers/FileSystemHelper.php', - 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\LogHelper' => __DIR__ . '/../..' . '/classes/Helpers/LogHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\SubmissionFileHelper' => __DIR__ . '/../..' . '/classes/Helpers/SubmissionFileHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Helpers\\ZipHelper' => __DIR__ . '/../..' . '/classes/Helpers/ZipHelper.php', 'APP\\plugins\\generic\\latexConverter\\classes\\Settings\\Actions' => __DIR__ . '/../..' . '/classes/Settings/Actions.php', diff --git a/version.xml b/version.xml index 38c9e60..300e6b6 100755 --- a/version.xml +++ b/version.xml @@ -16,7 +16,7 @@ latexConverter plugins.generic - 3.5.0.3 + 3.5.0.4 2024-06-01 1 LatexConverterPlugin