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