xref: /plugin/structpublish/action/show.php (revision 98487b7550fc2f486c5f71bc5be8cd19181af4b1)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Constants;
4use dokuwiki\plugin\structpublish\meta\Revision;
5
6class action_plugin_structpublish_show extends DokuWiki_Action_Plugin
7{
8    /** @var int */
9    static protected $latestPublishedRev;
10
11    /** @inheritDoc */
12    public function register(Doku_Event_Handler $controller)
13    {
14        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleShow');
15        $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, 'handleShowrev');
16    }
17
18    /**
19     * Decide which revision to show based on role assignments
20     *
21     * @param Doku_Event $event
22     * @return void
23     */
24    public function handleShow(Doku_Event $event)
25    {
26        if ($event->data != 'show') {
27            return;
28        }
29
30        global $ID;
31        global $REV;
32        global $INFO;
33
34        /** @var helper_plugin_structpublish_db $dbHelper */
35        $dbHelper = plugin_load('helper', 'structpublish_db');
36
37        if (
38            !$dbHelper->isPublishable() ||
39            (auth_isadmin() && !$this->getConf('restrict_admin'))
40        ) {
41            return;
42        }
43
44        $currentRevision = new Revision($dbHelper->getDB(), $ID, $REV ?: $INFO['currentrev']);
45        $isPublished = $currentRevision->getStatus() === Constants::STATUS_PUBLISHED;
46
47        /** @var action_plugin_structpublish_sqlitefunction $functions */
48        $functions = plugin_load('action', 'structpublish_sqlitefunction');
49        if (!$functions->IS_PUBLISHER($ID)) {
50            $latestPublished = $currentRevision->getLatestPublishedRevision();
51            // there is no published revision, show nothing
52            if (!$isPublished && is_null($latestPublished)) {
53                $event->data = 'denied';
54                // FIXME we could add our own action to display a custom message instead of standard denied action
55                return;
56            }
57
58            self::$latestPublishedRev = $latestPublished->getRev();
59
60            // show either the explicitly requested or the latest published revision
61            if (!$isPublished) {
62                $REV = self::$latestPublishedRev;
63                $INFO['rev'] = self::$latestPublishedRev;
64            }
65        }
66    }
67
68    /**
69     * Suppress message about viewing an old revision if it is the latest one
70     * that the current user is allowed to see.
71     *
72     * @param Doku_Event $event
73     * @return void
74     */
75    public function handleShowrev(Doku_Event $event)
76    {
77        /** @var helper_plugin_structpublish_db $dbHelper */
78        $dbHelper = plugin_load('helper', 'structpublish_db');
79
80        if (
81            !$dbHelper->isPublishable() ||
82            (auth_isadmin() && !$this->getConf('restrict_admin'))
83        ) {
84            return;
85        }
86
87        global $INFO;
88
89        if (self::$latestPublishedRev && self::$latestPublishedRev == $INFO['rev']) {
90            $event->preventDefault();
91        }
92    }
93}
94