xref: /plugin/structpublish/helper/publish.php (revision b476f81d46066c318f7a5153cbfadaf4262a2998)
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        $revision = new Revision($ID, $INFO['currentrev']);
43
44        if ($action === Constants::ACTION_PUBLISH) {
45            $revision->setVersion($newversion);
46        }
47        $revision->setUser($_SERVER['REMOTE_USER']);
48        $revision->setStatus(Constants::transitionBy($action));
49        $revision->setTimestamp(time());
50        $revision->save();
51
52        if ($action === Constants::ACTION_PUBLISH) {
53            $this->updateSchemaData();
54        }
55
56        return $revision;
57    }
58
59    /**
60     * Set "published" status in all assigned schemas
61     *
62     * @return void
63     */
64    protected function updateSchemaData()
65    {
66        global $ID;
67        global $INFO;
68
69        $schemaAssignments = Assignments::getInstance();
70        $tables = $schemaAssignments->getPageAssignments($ID);
71
72        if (empty($tables)) {
73            return;
74        }
75
76        $sqlite = $this->dbHelper->getDB();
77
78        foreach ($tables as $table) {
79            // unpublish earlier revisions
80            $sqlite->query("UPDATE data_$table SET published = 0 WHERE pid = ?", [$ID]);
81            $sqlite->query("UPDATE multi_$table SET published = 0 WHERE pid = ?", [$ID]);
82
83            // publish the current revision
84            $sqlite->query("UPDATE data_$table SET published = 1 WHERE pid = ? AND rev = ?",
85                [$ID, $INFO['currentrev']]);
86            $sqlite->query("UPDATE multi_$table SET published = 1 WHERE pid = ? AND rev = ?",
87                [$ID, $INFO['currentrev']]);
88        }
89    }
90}
91