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