xref: /plugin/struct/action/title.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
11c9ef013SAndreas Gohr<?php
2*d6d97f60SAnna 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
101c9ef013SAndreas Gohr// must be run within Dokuwiki
117cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
123aad9612SMichael Grosseuse dokuwiki\plugin\struct\meta\PageMeta;
131c9ef013SAndreas Gohr
141c9ef013SAndreas Gohrif (!defined('DOKU_INC')) die();
151c9ef013SAndreas Gohr
161c9ef013SAndreas Gohr/**
171c9ef013SAndreas Gohr * Class action_plugin_struct_title
181c9ef013SAndreas Gohr *
191c9ef013SAndreas Gohr * Saves the page title when meta data is saved
201c9ef013SAndreas Gohr */
21*d6d97f60SAnna Dabrowskaclass action_plugin_struct_title extends DokuWiki_Action_Plugin
22*d6d97f60SAnna Dabrowska{
231c9ef013SAndreas Gohr
241c9ef013SAndreas Gohr    /**
251c9ef013SAndreas Gohr     * Registers a callback function for a given event
261c9ef013SAndreas Gohr     *
271c9ef013SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
281c9ef013SAndreas Gohr     * @return void
291c9ef013SAndreas Gohr     */
30*d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
31*d6d97f60SAnna Dabrowska    {
321c9ef013SAndreas Gohr        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_meta');
331c9ef013SAndreas Gohr    }
341c9ef013SAndreas Gohr
351c9ef013SAndreas Gohr    /**
361c9ef013SAndreas Gohr     * Store the page's title
371c9ef013SAndreas Gohr     *
381c9ef013SAndreas Gohr     * @param Doku_Event $event
391c9ef013SAndreas Gohr     * @param $param
401c9ef013SAndreas Gohr     */
41*d6d97f60SAnna Dabrowska    public function handle_meta(Doku_Event $event, $param)
42*d6d97f60SAnna Dabrowska    {
43a7cffaf2SAndreas Gohr        $id = $event->data['page'];
44a7cffaf2SAndreas Gohr
457cbcfbdbSAndreas Gohr        try {
463aad9612SMichael Grosse            $page = new PageMeta($id);
471c9ef013SAndreas Gohr
481c9ef013SAndreas Gohr            if (!blank($event->data['current']['title'])) {
49109edca8SMichael Grosse                $page->setTitle($event->data['current']['title']);
501c9ef013SAndreas Gohr            } else {
51109edca8SMichael Grosse                $page->setTitle(null);
52109edca8SMichael Grosse            }
53109edca8SMichael Grosse
54109edca8SMichael Grosse            if (!blank($event->data['current']['last_change']['date'])) {
55109edca8SMichael Grosse                $page->setLastRevision($event->data['current']['last_change']['date']);
56109edca8SMichael Grosse            } else {
57109edca8SMichael Grosse                $page->setLastRevision(null);
58109edca8SMichael Grosse            }
59109edca8SMichael Grosse
60109edca8SMichael Grosse            if (!blank($event->data['current']['last_change']['user'])) {
61109edca8SMichael Grosse                $page->setLastEditor($event->data['current']['last_change']['user']);
62109edca8SMichael Grosse            } elseif (!blank($event->data['current']['last_change']['ip'])) {
639abde7b5SMichael Grosse                $page->setLastEditor($event->data['current']['last_change']['ip']);
64109edca8SMichael Grosse            } else {
65109edca8SMichael Grosse                $page->setLastEditor(null);
661c9ef013SAndreas Gohr            }
6788b58a21SSzymon Olewniczak
6888b58a21SSzymon Olewniczak            if (!blank($event->data['current']['last_change']['sum'])) {
6988b58a21SSzymon Olewniczak                $page->setLastSummary($event->data['current']['last_change']['sum']);
7088b58a21SSzymon Olewniczak            } else {
7188b58a21SSzymon Olewniczak                $page->setLastSummary(null);
7288b58a21SSzymon Olewniczak            }
7388b58a21SSzymon Olewniczak
748d1ed4ceSMichael Grosse            $page->savePageData();
757cbcfbdbSAndreas Gohr        } catch (StructException $e) {
767cbcfbdbSAndreas Gohr            msg($e->getMessage(), -1);
777cbcfbdbSAndreas Gohr        }
781c9ef013SAndreas Gohr    }
791c9ef013SAndreas Gohr}
80