1<?php
2
3/**
4 * DokuWiki Plugin struct (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\Extension\ActionPlugin;
11use dokuwiki\Extension\EventHandler;
12use dokuwiki\Extension\Event;
13use dokuwiki\plugin\struct\meta\PageMeta;
14use dokuwiki\plugin\struct\meta\StructException;
15
16/**
17 * Class action_plugin_struct_title
18 *
19 * Saves the page title when meta data is saved
20 */
21class action_plugin_struct_title extends ActionPlugin
22{
23    /**
24     * Registers a callback function for a given event
25     *
26     * @param EventHandler $controller DokuWiki's event controller object
27     * @return void
28     */
29    public function register(EventHandler $controller)
30    {
31        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handleMeta');
32    }
33
34    /**
35     * Store the page's title
36     *
37     * @param Event $event
38     * @param $param
39     */
40    public function handleMeta(Event $event, $param)
41    {
42        $id = $event->data['page'];
43
44        try {
45            $page = new PageMeta($id);
46
47            // check if we already have data for the latest revision, or we risk redundant db writes
48            $latest = $page->getPageData();
49            if (
50                $latest &&
51                // external edits do not have last change info
52                isset($event->data['current']['last_change']['date']) &&
53                (int)$latest['lastrev'] === $event->data['current']['last_change']['date']
54            ) {
55                return;
56            }
57
58            if (!blank($event->data['current']['title'])) {
59                $page->setTitle($event->data['current']['title']);
60            } else {
61                $page->setTitle(null);
62            }
63
64            if (!blank($event->data['current']['last_change']['date'])) {
65                $page->setLastRevision($event->data['current']['last_change']['date']);
66            } else {
67                $page->setLastRevision(null);
68            }
69
70            if (!blank($event->data['current']['last_change']['user'])) {
71                $page->setLastEditor($event->data['current']['last_change']['user']);
72            } elseif (!blank($event->data['current']['last_change']['ip'])) {
73                $page->setLastEditor($event->data['current']['last_change']['ip']);
74            } else {
75                $page->setLastEditor(null);
76            }
77
78            if (!blank($event->data['current']['last_change']['sum'])) {
79                $page->setLastSummary($event->data['current']['last_change']['sum']);
80            } else {
81                $page->setLastSummary(null);
82            }
83
84            $page->savePageData();
85        } catch (StructException $e) {
86            msg($e->getMessage(), -1);
87        }
88    }
89}
90