xref: /plugin/structpublish/action/publish.php (revision 5c2215e890cc9516db9bc28cfa976283c0e72b1e)
1<?php
2
3use dokuwiki\plugin\structpublish\meta\Constants;
4use dokuwiki\plugin\structpublish\meta\Revision;
5
6class action_plugin_structpublish_publish extends DokuWiki_Action_Plugin
7{
8    /** @var \helper_plugin_structpublish_db */
9    protected $dbHelper;
10
11    /**
12     * @inheritDoc
13     */
14    public function register(Doku_Event_Handler $controller)
15    {
16        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleApprove');
17        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handlePublish');
18    }
19
20    public function handlePublish(Doku_Event $event)
21    {
22        if ($event->data != 'show') return;
23
24        $this->dbHelper = plugin_load('helper', 'structpublish_db');
25
26        global $INPUT;
27        $in = $INPUT->arr('structpublish');
28        if (!$in || !isset($in[Constants::ACTION_PUBLISH])) {
29            return;
30        }
31
32        if(checkSecurityToken()) {
33            $this->saveRevision(Constants::STATUS_PUBLISHED, $INPUT->str('version'));
34            $this->updateSchemaData();
35        }
36    }
37
38    public function handleApprove(Doku_Event $event)
39    {
40        if ($event->data != 'show') return;
41
42        $this->dbHelper = plugin_load('helper', 'structpublish_db');
43
44        global $INPUT;
45        $in = $INPUT->arr('structpublish');
46        if (!$in || !isset($in[Constants::ACTION_APPROVE])) {
47            return;
48        }
49
50        if(checkSecurityToken()) {
51            $this->saveRevision(Constants::STATUS_APPROVED);
52        }
53    }
54
55    /**
56     * Save publish data
57     *
58     * @param string $status
59     * @return void
60     */
61    protected function saveRevision($status, $newversion='')
62    {
63        global $ID;
64        global $INFO;
65
66        // FIXME prevent bumping an already published revision
67        $sqlite = $this->dbHelper->getDB();
68        $revision = new Revision($sqlite, $ID, $INFO['currentrev']);
69
70        if ($status === Constants::STATUS_PUBLISHED) {
71            $revision->setVersion($newversion);
72        }
73        $revision->setUser($_SERVER['REMOTE_USER']);
74        $revision->setStatus($status);
75        $revision->setDatetime(time());
76        $revision->save();
77    }
78
79    /**
80     * Set "published" status in all assigned schemas
81     *
82     * @return void
83     */
84    protected function updateSchemaData()
85    {
86        global $ID;
87        global $INFO;
88
89        $schemaAssignments = \dokuwiki\plugin\struct\meta\Assignments::getInstance();
90        $tables = $schemaAssignments->getPageAssignments($ID);
91
92        if (empty($tables)) return;
93
94        $sqlite = $this->dbHelper->getDB();
95
96        foreach ($tables as $table) {
97            // TODO unpublish earlier revisions
98            $sqlite->query( "UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]);
99            $sqlite->query( "UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?", [$ID, $INFO['currentrev']]);
100        }
101    }
102}
103