1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Gina Haeussge <gina@foosel.net>
5 */
6
7/**
8 * Class action_plugin_blogtng_pagedata
9 */
10class action_plugin_blogtng_pagedata extends DokuWiki_Action_Plugin{
11
12    /** @var helper_plugin_blogtng_entry */
13    private $entryhelper;
14
15    public function __construct() {
16        $this->entryhelper = plugin_load('helper', 'blogtng_entry');
17    }
18
19    /**
20     * Registers a callback function for a given event
21     *
22     * @param Doku_Event_Handler $controller
23     */
24    public function register(Doku_Event_Handler $controller) {
25        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'updateMetadataBlog', array());
26    }
27
28    /**
29     * Updates the metadata in the blogtng database.
30     *
31     * @param Doku_Event $event
32     * @param $params
33     */
34    public function updateMetadataBlog(Doku_Event $event, $params) {
35        global $ID, $INPUT;
36        /** @var DokuWiki_Auth_Plugin $auth */
37        global $auth;
38
39        $data = $event->result; //newly rendered data is here.
40
41        $pid = md5($ID);
42        $this->entryhelper->load_by_pid($pid);
43
44        //only refreshing for blog entries
45        if($this->entryhelper->entry['blog']) {
46
47            // fetch author info
48            $login = $this->entryhelper->entry['login'];
49            if(!$login) $login = $data['current']['user'];
50            if(!$login) $login = $INPUT->server->str('REMOTE_USER');
51
52            $userdata = false;
53            if($login){
54                if ($auth != null){
55                    $userdata = $auth->getUserData($login);
56                }
57            }
58
59
60            // fetch dates
61            $date_created = $data['current']['date']['created'];
62            $date_modified = $data['current']['date']['modified'];
63
64            // prepare entry ...
65            $entry = array(
66                'page' => $ID,
67                'title' => $data['current']['title'],
68                'image' => $data['current']['relation']['firstimage'],
69                'created' => $date_created,
70                'lastmod' => (!$date_modified) ? $date_created : $date_modified,
71                'login' => $login,
72                'author' => $userdata ? $userdata['name'] : $login,
73                'mail' => $userdata ? $userdata['mail'] : '',
74            );
75            $this->entryhelper->set($entry);
76
77            // ... and save it
78            $this->entryhelper->save();
79        }
80
81        // unset old persistent tag data
82        if (isset($data['persistent']['subject'])) {
83            // persistent metadata is copied to the current metadata, clean current metadata
84            // if it hasn't been changed in the renderer
85            if ($data['persistent']['subject'] == $data['current']['subject'])
86                $event->result['current']['subject'] = [];
87            unset($event->result['persistent']['subject']);
88        }
89
90        // save blogtng tags to the metadata of the page
91        $taghelper = $this->entryhelper->getTagHelper();
92        if (isset($data['current']['subject'])) {
93            $event->result['current']['subject'] = array_unique(array_merge((array)$data['current']['subject'], $taghelper->getTags()));
94        } else {
95            $event->result['current']['subject'] = $taghelper->getTags();
96        }
97    }
98
99}
100