xref: /plugin/struct/action/title.php (revision 58cb2b195be4f170c094cdbf3cc5cb0ee0d1e9f0)
11c9ef013SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
31c9ef013SAndreas Gohr/**
41c9ef013SAndreas Gohr * DokuWiki Plugin struct (Action Component)
51c9ef013SAndreas Gohr *
61c9ef013SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
71c9ef013SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
81c9ef013SAndreas Gohr */
91c9ef013SAndreas Gohr
107cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
113aad9612SMichael Grosseuse dokuwiki\plugin\struct\meta\PageMeta;
121c9ef013SAndreas Gohr
131c9ef013SAndreas Gohr/**
141c9ef013SAndreas Gohr * Class action_plugin_struct_title
151c9ef013SAndreas Gohr *
161c9ef013SAndreas Gohr * Saves the page title when meta data is saved
171c9ef013SAndreas Gohr */
18d6d97f60SAnna Dabrowskaclass action_plugin_struct_title extends DokuWiki_Action_Plugin
19d6d97f60SAnna Dabrowska{
201c9ef013SAndreas Gohr
211c9ef013SAndreas Gohr    /**
221c9ef013SAndreas Gohr     * Registers a callback function for a given event
231c9ef013SAndreas Gohr     *
241c9ef013SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
251c9ef013SAndreas Gohr     * @return void
261c9ef013SAndreas Gohr     */
27d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
28d6d97f60SAnna Dabrowska    {
29748e747fSAnna Dabrowska        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handleMeta');
301c9ef013SAndreas Gohr    }
311c9ef013SAndreas Gohr
321c9ef013SAndreas Gohr    /**
331c9ef013SAndreas Gohr     * Store the page's title
341c9ef013SAndreas Gohr     *
351c9ef013SAndreas Gohr     * @param Doku_Event $event
361c9ef013SAndreas Gohr     * @param $param
371c9ef013SAndreas Gohr     */
38748e747fSAnna Dabrowska    public function handleMeta(Doku_Event $event, $param)
39d6d97f60SAnna Dabrowska    {
40a7cffaf2SAndreas Gohr        $id = $event->data['page'];
41a7cffaf2SAndreas Gohr
427cbcfbdbSAndreas Gohr        try {
433aad9612SMichael Grosse            $page = new PageMeta($id);
441c9ef013SAndreas Gohr
45*58cb2b19SAnna Dabrowska            // check if we already have data for the latest revision, or we risk redundant db writes
46*58cb2b19SAnna Dabrowska            $latest = $page->getPageData();
47*58cb2b19SAnna Dabrowska            if (!$latest || (int) $latest['lastrev'] === $event->data['current']['last_change']['date']) {
48*58cb2b19SAnna Dabrowska                return;
49*58cb2b19SAnna Dabrowska            }
50*58cb2b19SAnna Dabrowska
511c9ef013SAndreas Gohr            if (!blank($event->data['current']['title'])) {
52109edca8SMichael Grosse                $page->setTitle($event->data['current']['title']);
531c9ef013SAndreas Gohr            } else {
54109edca8SMichael Grosse                $page->setTitle(null);
55109edca8SMichael Grosse            }
56109edca8SMichael Grosse
57109edca8SMichael Grosse            if (!blank($event->data['current']['last_change']['date'])) {
58109edca8SMichael Grosse                $page->setLastRevision($event->data['current']['last_change']['date']);
59109edca8SMichael Grosse            } else {
60109edca8SMichael Grosse                $page->setLastRevision(null);
61109edca8SMichael Grosse            }
62109edca8SMichael Grosse
63109edca8SMichael Grosse            if (!blank($event->data['current']['last_change']['user'])) {
64109edca8SMichael Grosse                $page->setLastEditor($event->data['current']['last_change']['user']);
65109edca8SMichael Grosse            } elseif (!blank($event->data['current']['last_change']['ip'])) {
669abde7b5SMichael Grosse                $page->setLastEditor($event->data['current']['last_change']['ip']);
67109edca8SMichael Grosse            } else {
68109edca8SMichael Grosse                $page->setLastEditor(null);
691c9ef013SAndreas Gohr            }
7088b58a21SSzymon Olewniczak
7188b58a21SSzymon Olewniczak            if (!blank($event->data['current']['last_change']['sum'])) {
7288b58a21SSzymon Olewniczak                $page->setLastSummary($event->data['current']['last_change']['sum']);
7388b58a21SSzymon Olewniczak            } else {
7488b58a21SSzymon Olewniczak                $page->setLastSummary(null);
7588b58a21SSzymon Olewniczak            }
7688b58a21SSzymon Olewniczak
778d1ed4ceSMichael Grosse            $page->savePageData();
787cbcfbdbSAndreas Gohr        } catch (StructException $e) {
797cbcfbdbSAndreas Gohr            msg($e->getMessage(), -1);
807cbcfbdbSAndreas Gohr        }
811c9ef013SAndreas Gohr    }
821c9ef013SAndreas Gohr}
83