Commit 23783060 authored by Jerome Chauveau's avatar Jerome Chauveau
Browse files

Publication::publish hook

parent e6db2d80
Loading
Loading
Loading
Loading
+4 −20
Original line number Diff line number Diff line
@@ -29,28 +29,12 @@ class BookImageDataHandler extends CatalogBookHandler
     */
    public function zimage(array $args, APP\core\Request $request)
    {
        $pubId= $args[1];
        $imgName = $args[2];
        $submission = $this->getAuthorizedContextObject(Application::ASSOC_TYPE_SUBMISSION);


        $pubFormatFiles = Repo::submissionFile()
            ->getCollector()
            ->filterBySubmissionIds([$submission->getId()])
            ->filterByAssoc(Application::ASSOC_TYPE_PUBLICATION_FORMAT)
            ->getMany();

        $publicationFile = NULL;
        foreach ($pubFormatFiles as $file){
            if($file->getData('assocId') == $pubId) {
                $publicationFile = $file;
            }
        }

        $pathInfo = pathinfo($publicationFile->getData('path'));
        $submissionFileId= (int)$args[2];
        $imgName = $args[3];
        $submissionFile = Repo::submissionFile()->get($submissionFileId);
        $pathInfo = pathinfo($submissionFile->getData('path'));
        $imagePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . DIRECTORY_SEPARATOR . $imgName;
        ImageUtils::downloadImage($this->fs, $imagePath, $imgName);

    }


+38 −24
Original line number Diff line number Diff line
@@ -7,10 +7,13 @@
 */


use APP\facades\Repo;
use PKP\core\JSONMessage;
use PKP\plugins\GenericPlugin;
use PKP\file\PrivateFileManager;
use PKP\facades\Locale;
use PKP\submissionFile\SubmissionFile;
use PKP\plugins\Hook;

class XML2HTMLPlugin extends GenericPlugin
{
@@ -22,48 +25,71 @@ class XML2HTMLPlugin extends GenericPlugin

        if ($success && $this->getEnabled()) {
            HookRegistry::register('ArticleHandler::view::galley', array($this, 'articleHandler'));
            //HookRegistry::register('ArticleHandler::download', array($this, 'articleDownloadCallback'), HOOK_SEQUENCE_LATE);
            HookRegistry::register('LoadHandler', array($this, 'loadImageHandler'));
            HookRegistry::register('CatalogBookHandler::view', array($this, 'catalogHandler'));
            Hook::add('Publication::publish',array($this, 'publishHandler'));

        }

        return $success;
    }

    /**
     * when a publication is published : remove all html transformation file results
     * @param $hookName
     * @param $params
     * @return void
     */
    public function publishHandler($hookName, $params){
        $publication= &$params[1];
        $submissionId = $publication->getData('submissionId');

        $submissionFiles = Repo::submissionFile()
            ->getCollector()
            ->filterBySubmissionIds([$submissionId])
            ->getMany();

        foreach ($submissionFiles as $sf){
            $htmlFile = XML2HTMLPlugin::getHTMLPathFromZipPath($sf->getData('path'));
            if(file_exists($htmlFile)){
                unlink($htmlFile);
            }
        }
    }

    /**
     * @throws Exception
     */
    public function articleHandler($hookName, $params): bool
    {

        $galley =& $params[2];
        $submission = &$params[3];
        //handle only on zip galleys - supposed to be an xml archive (source + images)

        #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');
            $this->archiveView($galley->getFile()->getData('path'), [$submission->getId(), $galley->getId()], 'article');
            return true;
        } else return false;
    }

    public function catalogHandler($hookName, $params): bool
    {

        $submission = &$params[1];
        $galley =& $params[2];
        $submissionFile =& $params[3];
        $zipMode = $submissionFile->getData('mimetype') === "application/zip";
        if ($zipMode) {
            $this->archiveView($submissionFile->getData('path'), $submission->getId(), $galley->getId(), 'catalog');
            $this->archiveView($submissionFile->getData('path'), [$submission->getId(),$galley->getId(), $submissionFile->getId()], 'catalog');
            return true;
        } else
            return false;
    }


    private function archiveView(string $filePath, int $submissionId, int $galleyId, string $op)
    private function archiveView(string $zipPath, array $routeIds, string $op)
    {
        $htmlFile = XML2HTMLPlugin::getHTMLPathFromZipPath($filePath);
        $htmlFile = XML2HTMLPlugin::getHTMLPathFromZipPath($zipPath);
        $html = file_get_contents($htmlFile);
        //make transfo only if html does not exist
        if (!$html) {
@@ -80,11 +106,10 @@ class XML2HTMLPlugin extends GenericPlugin
            if ($format == NULL || $format == '')
                $format = 'JATS';//default value is JATS

            $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($filePath);
            $xmlFilePath = XML2HTMLPlugin::unzipXMLArchive($zipPath);
            $html = $this->resolveZipImagePaths(
                XML2HTMLPlugin::transform($xmlFilePath, $format),
                $submissionId,
                $galleyId,
                $routeIds,
                $xmlFilePath, $this->getRequest(),
                $op);
            //store transfo result
@@ -206,18 +231,14 @@ class XML2HTMLPlugin extends GenericPlugin
        string $format
    )
    {


        $xslFileName = strtolower($format) . '2html.xsl';
        ////// xslt transfo
        $xsltPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $xslFileName;
        // Load the XML source
        $xml = new DOMDocument;
        $xml->load($xmlFilePath);

        $xsl = new DOMDocument;
        $xsl->load($xsltPath, LIBXML_DTDATTR);

        // Configure the transformer
        $proc = new XSLTProcessor;
        $proc->importStyleSheet($xsl); // attach the xsl rules
@@ -286,7 +307,6 @@ class XML2HTMLPlugin extends GenericPlugin
    {
        $page = $args[0];
        $op = $args[1];

        if ($page == 'article' && (/*$op == 'image' || */ $op == 'zimage')) {
            define('HANDLER_CLASS', 'ArticleImageDataHandler');
            import('plugins.generic.xml2html.ArticleImageDataHandler');
@@ -296,11 +316,9 @@ class XML2HTMLPlugin extends GenericPlugin
        }
    }


    static function resolveZipImagePaths(
        string  $htmlString,
        int     $submissionId,
        int     $galleyId,
        array $routesIds,
        string  $xmlFilePath,
        APP\core\Request $request,
        string  $op = 'article')
@@ -317,11 +335,7 @@ class XML2HTMLPlugin extends GenericPlugin
                $filePath = $request->url(
                    null, $op,
                    'zimage',
                    array(
                        $submissionId,
                        $galleyId,
                        $file
                    )
                    array_merge($routesIds, [$file])
                );
                $pattern = preg_quote(rawurlencode($file));
                $htmlString = preg_replace(