1<?php
2
3use dokuwiki\plugin\struct\meta\Assignments;
4use dokuwiki\plugin\structpublish\meta\Constants;
5use dokuwiki\plugin\structpublish\meta\Revision;
6
7/**
8 * DokuWiki Plugin structpublish (Helper Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
12 */
13class helper_plugin_structpublish_publish extends DokuWiki_Plugin
14{
15    /** @var helper_plugin_structpublish_db  */
16    protected $dbHelper;
17
18    public function __construct()
19    {
20        $this->dbHelper = plugin_load('helper', 'structpublish_db');
21    }
22
23    /**
24     * Save publish data
25     *
26     * @param string $action
27     * @return Revision
28     * @throws Exception
29     */
30    public function saveRevision($action, $newversion = '')
31    {
32        global $ID;
33        global $INFO;
34
35        if (!$this->dbHelper->checkAccess($ID, [$action])) {
36            throw new \Exception('User may not ' . $action);
37        }
38
39        $revision = new Revision($ID, $INFO['currentrev']);
40
41        if ($action === Constants::ACTION_PUBLISH) {
42            $revision->setVersion($newversion);
43        }
44        $revision->setUser($_SERVER['REMOTE_USER']);
45        $revision->setStatus(Constants::transitionBy($action));
46        $revision->setTimestamp(time());
47        $revision->save();
48
49        if ($action === Constants::ACTION_PUBLISH) {
50            $this->updateSchemaData();
51        }
52
53        return $revision;
54    }
55
56    /**
57     * Set "published" status in all assigned schemas
58     *
59     * @return void
60     */
61    protected function updateSchemaData()
62    {
63        global $ID;
64        global $INFO;
65
66        $schemaAssignments = Assignments::getInstance();
67        $tables = $schemaAssignments->getPageAssignments($ID);
68
69        if (empty($tables)) {
70            return;
71        }
72
73        $sqlite = $this->dbHelper->getDB();
74
75        foreach ($tables as $table) {
76            // unpublish earlier revisions
77            $sqlite->query("UPDATE data_$table SET published = 0 WHERE pid = ?", [$ID]);
78            $sqlite->query("UPDATE multi_$table SET published = 0 WHERE pid = ?", [$ID]);
79
80            // publish the current revision
81            $sqlite->query(
82                "UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?",
83                [$ID, $INFO['currentrev']]
84            );
85            $sqlite->query(
86                "UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?",
87                [$ID, $INFO['currentrev']]
88            );
89        }
90    }
91}
92