Commit c91bd080 authored by Charles Bourdot's avatar Charles Bourdot
Browse files

initial commit

parent 2b5c4d02
Loading
Loading
Loading
Loading
+12 −35
Original line number Diff line number Diff line
<?php

use APP\pages\article\ArticleHandler;
use APP\core\Services;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use PKP\config\Config;
use PKP\file\FileManager;
use PKP\plugins\Hook;
import('pages.article.ArticleHandler');


class ArticleImageDataHandler extends ArticleHandler
{

    private $fs;

    public function __construct()
    {
        parent::__construct();
        $this->fs = ImageUtils::buildFileSystem($this);
    }

    /**
     * Image from zip archive galley file
     * @param array $args
     * @param APP\core\Request $request
     * @param Request $request
     * @return mixed
     */
    public function zimage(array $args, APP\core\Request $request)
    public function zimage(array $args, Request $request)
    {

        $galleyId = (int)$args[1];
        $galleyId = $args[1];
        $imgName = $args[2];
        $galley = NULL;
         foreach ($this->publication->getData('galleys') as $g) {
             if($galleyId == $g->getId()){
                 $galley = $g;
             }
         }
         if($galley != NULL){
        $galley = Services::get('galley')->get($galleyId);
        $file = $galley->getFile();
        $filename = Services::get('file')
                ->formatFilename($file->getData('path'), $file->getLocalizedData('path'));
        $pathInfo = pathinfo($filename);
        $imagePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . DIRECTORY_SEPARATOR . $imgName;
             ImageUtils::downloadImage($this->fs, $imagePath, $imgName);
         }
        return NULL;

        return Services::get('file')->download($imagePath, $imgName);
    }


+48 −25
Original line number Diff line number Diff line
<?php

use APP\core\Application;
use APP\facades\Repo;
use APP\pages\catalog\CatalogBookHandler;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use PKP\config\Config;
use PKP\file\FileManager;
use PKP\plugins\Hook;
import('pages.catalog.CatalogBookHandler');


class BookImageDataHandler extends CatalogBookHandler
{

    private $fs;

    public function __construct()
    {
        parent::__construct();
        $this->fs = ImageUtils::buildFileSystem($this);
    }
//    /**
//     * @param array $args
//     * @param Request $request
//     * @return mixed
//     */
//    public function image(array $args, Request $request)
//    {
//        $fileId = $args[1];
//        $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
//        import('lib.pkp.classes.submission.SubmissionFile');
//        $dependentFilesIterator = Services::get('submissionFile')->getMany([
//            'assocTypes' => [ASSOC_TYPE_SUBMISSION_FILE],
//            'submissionIds' => [$submission->getId()],
//            'fileStages' => [SUBMISSION_FILE_DEPENDENT],
//            'includeDependentFiles' => true,
//        ]);
//
//        $submissionFile = null;
//        foreach ($dependentFilesIterator as $dependentFile) {
//            if ($fileId == $dependentFile->getData('fileId')) {
//                $submissionFile = $dependentFile;
//                break;
//            }
//        }
//
//        // Download file if exists
//        if (!Services::get('file')->fs->has($submissionFile->getData('path'))) {
//            $request->getDispatcher()->handle404();
//        }
//
//        $filename = Services::get('file')
//            ->formatFilename($submissionFile->getData('path'), $submissionFile->getLocalizedData('name'));
//
//        return Services::get('file')->download($submissionFile->getData('path'), $filename);
//    }

    /**
     * Image from zip archive submission file
@@ -27,18 +49,16 @@ class BookImageDataHandler extends CatalogBookHandler
     * @param Request $request
     * @return mixed
     */
    public function zimage(array $args, APP\core\Request $request)
    public function zimage(array $args, Request $request)
    {
        $pubId= $args[1];
        $imgName = $args[2];
        $submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);
        $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);


        $pubFormatFiles = Repo::submissionFile()
            ->getCollector()
            ->filterBySubmissionIds([$submission->getId()])
            ->filterByAssoc(Application::ASSOC_TYPE_PUBLICATION_FORMAT)
            ->getMany();
        $pubFormatFiles = Services::get('submissionFile')->getMany([
			'submissionIds' => [$submission->getId()],
			'assocTypes' => [ASSOC_TYPE_PUBLICATION_FORMAT],
		]);

        $publicationFile = NULL;
        foreach ($pubFormatFiles as $file){
@@ -46,10 +66,13 @@ class BookImageDataHandler extends CatalogBookHandler
                $publicationFile = $file;
            }
        }

//        $filename = Services::get('file')
//            ->formatFilename($publicationFile->getData('path'), $publicationFile->getLocalizedData('path'));
//        var_dump($filename);
        $pathInfo = pathinfo($publicationFile->getData('path'));
        $imagePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . DIRECTORY_SEPARATOR . $imgName;
        ImageUtils::downloadImage($this->fs, $imagePath, $imgName);
        return Services::get('file')->download($imagePath, $imgName);


    }

+10 −3
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@ Plugin OJS/OMP de chargement de flux XML développé dans le cadre du projet [FN

**Licence** : CeCILL-B (voir [licence](LICENCE)).

**Exemple** : https://puc-editorial.unicaen.fr/ojs-3.4/index.php/Caen/article/view/2/14
**Exemple** : 

- OJS 3.3 : https://puc-editorial.unicaen.fr/ojs-3.3/index.php/Caen/article/view/21/50
- OMP 3.3 : https://puc-editorial.unicaen.fr/omp-3.3/index.php/acp/catalog/view/9/26/158 



@@ -71,8 +74,8 @@ Deux transformations sont fournies dans le plugin :

Plugin testé sur les versions suivantes : 

- OJS 3.4 (3.4.0-4)
- OMP 3.4 (3.4.0-4)
- OJS 3.3 (3.3.0-16)
- OMP 3.3 (3.3.0-16))



@@ -81,6 +84,10 @@ Plugin testé sur les versions suivantes :
- Template de page : `HTMLGalleyView.tpl` ([lien](https://git.unicaen.fr/fnso/fairest/xml2html-pkp-plugin/-/blob/master/templates/HTMLGalleyView.tpl?ref_type=heads))
- Javascript : `resources/javascript/xml_2html.js` ([lien](https://git.unicaen.fr/fnso/fairest/xml2html-pkp-plugin/-/blob/master/resources/javascript/xml_2_html.js?ref_type=heads))
- CSS : `resources/styles/xml_2html.css` ([lien](https://git.unicaen.fr/fnso/fairest/xml2html-pkp-plugin/-/blob/master/resources/styles/xml_2_html.css?ref_type=heads))
- XSL : 
  - `xsl/tei/03-tei2html.xsl`, feuille de transformation XSL qui surcharge `xsl/tei/03-tei2html_core.xsl`
  - ou via l'ajout d'une ou de plusieurs XSLTs `N-<nomdufichier>.xsl` (où *N* > 3 et *<nomdufichier>* est le nom de la feuille de transformation choisi par l'utilisateur)




+2 −2
Original line number Diff line number Diff line
@@ -70,8 +70,8 @@ Two conversions are embedded in the plugin:

Plugin tested on :

- OJS 3.4 (3.4.0-4)
- OMP 3.4 (3.4.0-4)
- OJS 3.3 (3.3.0-21)
- OMP 3.3 (3.3.0-21)



+195 −25
Original line number Diff line number Diff line
@@ -6,22 +6,31 @@
 * Time: 17:36
 */


use PKP\core\JSONMessage;
use PKP\plugins\GenericPlugin;
use PKP\file\PrivateFileManager;
use PKP\facades\Locale;
import('lib.pkp.classes.plugins.GenericPlugin');
import('lib.pkp.classes.file.PrivateFileManager');

class XML2HTMLPlugin extends GenericPlugin
{
    static $META_XSL_SUFFIX = "-metas2html.xsl";

    public function register($category, $path, $mainContextId = NULL)
    {

        // Register the plugin even when it is not enabled
        $success = parent::register($category, $path);
        $context = Application::get()->getRequest()->getContext();
        $landingEnabled = false;
            if ($context) {
                $landingEnabled = $this->getSetting($context->getId(), 'landingpage');
            }

        if ($success && $this->getEnabled()) {
            HookRegistry::register('ArticleHandler::view::galley', array($this, 'articleHandler'));

            if ($landingEnabled) {
                HookRegistry::register('ArticleHandler::view', array($this, 'landingPageHandler'));
            }

            //HookRegistry::register('ArticleHandler::download', array($this, 'articleDownloadCallback'), HOOK_SEQUENCE_LATE);
            HookRegistry::register('LoadHandler', array($this, 'loadImageHandler'));
            HookRegistry::register('CatalogBookHandler::view', array($this, 'catalogHandler'));
@@ -38,12 +47,80 @@ class XML2HTMLPlugin extends GenericPlugin
    {
        $galley =& $params[2];
        $submission =& $params[3];
        //handle only on zip galleys - supposed to be an xml archive (source + images)
        $zipMode = $galley && $galley->getFileType() === 'application/zip';
        if ($zipMode) {
            $this->archiveView($galley->getFile()->getData('path'), $submission->getId(), $galley->getId(), 'article');

        if (!$galley || $galley->getFileType() !== 'application/zip') {
            return false;
        }

        $request = Application::get()->getRequest();
        $isAjax = (bool) $request->getUserVar('galley_ajax');

        // Génère ou récupère le HTML transformé
        $html = $this->archiveView(
            $galley->getFile()->getData('path'),
            $submission->getId(),
            $galley->getId(),
            'article'
        );

        if ($isAjax) {
            header('Content-Type: text/html; charset=utf-8');
            echo $html;
            return true;
        } else return false;
        }

        $templateMgr = TemplateManager::getManager($request);
        $context = $request->getContext();

        $dcMetas = XML2HTMLPlugin::transformMetas(
            XML2HTMLPlugin::getHTMLPathFromZipPath($galley->getFile()->getData('path')),
            $this->getSetting($context->getId(), 'format') ?: 'JATS'
        );

        $templateMgr->assign('assetsPath', $this->getPluginAssetsPath($request));
        $templateMgr->assign('dcMetas', $dcMetas);
        $templateMgr->assign('text', $html);

        // On réutilise ton template standard pour le galley complet
        $templateMgr->display($this->getTemplateResource('HTMLGalleyView.tpl'));

        return true;
    }



    public function landingPageHandler($hookName, $params): bool 
    {
        $request =& $params[0];
        $submission =& $params[2];

        try {
            // Get the galleys for this submission
            $galleys = $submission->getGalleys();
        
            foreach ($galleys as $galley) {
                if ($galley->getFileType() === 'application/zip') {
                    $file = $galley->getFile();
                    if ($file) {
                        $html = file_get_contents($this->landingPageView(
                            $file->getData('path'), 
                            $submission->getId(), 
                            $galley->getId(), 
                            'article'
                        ));
                        return true;
                    }
                }
            }
            
            $html = 'No .zip file available among galleys';
            return true;
            
        } catch (\Exception $e) {
            error_log('XML2HTMLPlugin error: ' . $e->getMessage());
            $html = 'Error processing submission';
            return true;
        }
    }

    public function catalogHandler($hookName, $params): bool
@@ -77,6 +154,51 @@ class XML2HTMLPlugin extends GenericPlugin
                'format'
            );

            if ($format == NULL || $format == '')
                $format = 'JATS';//default value is JATS

            $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($filePath);
            $html = $this->resolveZipImagePaths(
                XML2HTMLPlugin::transform($xmlFilePath, $format),
                $submissionId,
                $galleyId,
                $xmlFilePath, $this->getRequest(),
                $op);

            //metas transfo
            $dcMetas = XML2HTMLPlugin::transformMetas($xmlFilePath, $format);

            //store render result
            $templateMgr = TemplateManager::getManager($this->getRequest());
            $templateMgr->assign('assetsPath', $this->getPluginAssetsPath($this->getRequest()));
            $templateMgr->assign('dcMetas', $dcMetas);
            $templateMgr->assign('text', $html);
            $html = $templateMgr->display($this->getTemplateResource('LandingPage.tpl'));
            file_put_contents($htmlFile, $html, LOCK_EX);
        }
        return $html;

    }

        private function landingPageView(string $filePath, int $submissionId, int $galleyId, string $op)
    {
        $htmlFile = XML2HTMLPlugin::getHTMLPathFromZipPath($filePath);
        $html = file_get_contents($htmlFile);

        $context = Application::get()
            ->getRequest()
            ->getContext();

        if (!$html) {
            $context = Application::get()
                ->getRequest()
                ->getContext();

            $format = $this->getSetting(
                $context->getId(),
                'format'
            );

            if ($format == NULL || $format == '')
                $format = 'JATS';//default value is JATS

@@ -93,7 +215,9 @@ class XML2HTMLPlugin extends GenericPlugin
        $templateMgr = TemplateManager::getManager($this->getRequest());
        $templateMgr->assign('assetsPath', $this->getPluginAssetsPath($this->getRequest()));
        $templateMgr->assign('text', $html);
        $templateMgr->display($this->getTemplateResource('HTMLGalleyView.tpl'));
        $templateMgr->display($this->getTemplateResource('LandingPage.tpl'));
        return false;

    }

    /**
@@ -207,13 +331,25 @@ class XML2HTMLPlugin extends GenericPlugin
    )
    {

        $xsltDir = dirname(__FILE__)
            . DIRECTORY_SEPARATOR
            . 'xsl'
            . DIRECTORY_SEPARATOR
            . strtolower($format)
            . DIRECTORY_SEPARATOR;

        $xslFileName = strtolower($format) . '2html.xsl';
        ////// xslt transfo
        $xsltPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $xslFileName;
        // Load the XML source
        $result = NULL;
        foreach (glob($xsltDir . "*.xsl") as $xsltPath) {
            if(basename($xsltPath) ==  strtolower($format) . XML2HTMLPlugin::$META_XSL_SUFFIX)
                continue;//do not apply metas xsl
            $xml = new DOMDocument;
            if ($result == NULL) {
                // Load the XML source
                $xml->load($xmlFilePath);
            } else {
                //Load previous xsl result
                $xml->loadXML($result);
            }

            $xsl = new DOMDocument;
            $xsl->load($xsltPath, LIBXML_DTDATTR);
@@ -221,10 +357,44 @@ class XML2HTMLPlugin extends GenericPlugin
            // Configure the transformer
            $proc = new XSLTProcessor;
            $proc->importStyleSheet($xsl); // attach the xsl rules
        $locale = Locale::getLocale();

            //lang param
            $locale = AppLocale::getLocale();
            $proc->setParameter('', 'lang', $locale);
        return $proc->transformToXML($xml);

            try {
                $result = $proc->transformToXML($xml);
            } catch (Exception $e) {
                $result = NULL;
                var_dump($e);
            }

        }
        return $result;

    }


    static function transformMetas(
        string $xmlFilePath,
        string $format
    )
    {
        $xslt = dirname(__FILE__)
        . DIRECTORY_SEPARATOR
            . 'xsl'
            . DIRECTORY_SEPARATOR
            . strtolower($format)
            . DIRECTORY_SEPARATOR
            . strtolower($format)
            . XML2HTMLPlugin::$META_XSL_SUFFIX;
        $xml = new DOMDocument;
        $xml->load($xmlFilePath);
        $xsl = new DOMDocument;
        $xsl->load($xslt, LIBXML_DTDATTR);
        $proc = new XSLTProcessor;
        $proc->importStyleSheet($xsl);
        return $proc->transformToXML($xml);
    }


@@ -302,7 +472,7 @@ class XML2HTMLPlugin extends GenericPlugin
        int     $submissionId,
        int     $galleyId,
        string  $xmlFilePath,
        APP\core\Request $request,
        Request $request,
        string  $op = 'article')
    {
        $pathInfo = pathinfo($xmlFilePath);
Loading