1<?php
2/**
3 * DokuWiki Plugin htmlmetatags (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Heiko Heinz <heiko.heinz@soft2c.de>
7 * @author  Eric Maeker <eric@maeker.fr>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13class action_plugin_htmlmetatags extends DokuWiki_Action_Plugin {
14
15    /**
16     * Registers a callback function for a given event
17     *
18     * @param Doku_Event_Handler $controller DokuWiki's event controller object
19     * @return void
20     */
21    public function register(Doku_Event_Handler $controller) {
22
23       // $controller->register_hook('htmlmetatags', 'FIXME', $this, 'handle_htmlmetatags');
24       $controller->register_hook('TPL_METAHEADER_OUTPUT','BEFORE',$this,'handle_htmlmetatags',array());
25
26    }
27/*
28    public function searchname($namematch, $meta){
29    	for ($i=0;$i<sizeof($meta);$i++) {
30    		$a = $meta[$i];
31    		if($namematch == $a['name']){
32    			return $a;
33    		}
34    	}
35
36    	return null;
37    }
38  */
39
40    function replaceMeta(&$pageArray, $name, $value) {
41      dbg($name .' - '. $value, TRUE); // string representation
42    // Override dokuwiki default meta tags
43      $found = False;
44      foreach($pageArray['meta'] as $k => $v) {
45        if ($v["name"] == $name) {
46          $v["content"] = $value;
47          $pageArray['meta'][$k] = $v;
48          $found = True;
49        }
50      }
51      // If meta not set, add it as name or property
52      if (!$found) {
53        if (strpos($name, ':') !== false)
54          $pageArray['meta'][] = array("property" => $name, "content" => $value);
55        else
56          $pageArray['meta'][] = array("name" => $name, "content" => $value);
57      }
58    }
59
60
61    /**
62     * [Custom event handler which performs action]
63     * Prints keywords to the meta header
64     *
65     * @param Doku_Event $event  event object by reference
66     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
67     *                           handler was registered]
68     * @return void
69     */
70    public function handle_htmlmetatags(Doku_Event &$event, $param) {
71
72      global $ID;
73      if (empty($event->data)) return; // nothing to do for us
74
75      $metadata = p_get_metadata($ID);
76
77      $a = $metadata["htmlmetatags"] ?? '';
78      if(empty($a)) return;
79
80      foreach(array_keys($a) as $cur_key) {
81        $this->replaceMeta($event->data, $cur_key, $a[$cur_key]);
82      }
83    }
84
85}
86
87// vim:ts=4:sw=4:et:
88