1<?php
2/**
3 * NFTR - Not for the Robots!
4 *
5 * version 0.2
6 *
7 * DokuWiki got a built-in method to hide pages. Those pages have a 'noindex'
8 * their metadata and they are hidden in feeds and search results but this
9 * behaviour is not inherited to sub pages in the same namespace.
10 *
11 * This plugin provides the possibility to show pages to the user (feeds, search
12 * results) but but provide metadata to prevent the indexing of pages and
13 * namespaces by search engines. In the HTML header the plugin set the meta
14 * element for robots to 'noindex' (<meta name="robots" content="index,follow" />),
15 * additionally it sends 'X-Robots-Tag: noindex' via HTTP header.
16 *
17 * This does not mean that this content is not indexed by any search engines,
18 * spiders and robots could ignore all the information. But in generally, their
19 * spiders respect it (Google, Yahoo, ... won't index your content if you ask
20 * not to do).
21 *
22 * METADATA
23 *
24 * @author    Michael Haschke @ eye48.com
25 * @copyright 2009 Michael Haschke
26 * @license   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License 2.0 (GPLv2)
27 * @version   0.2
28 *
29 * WEBSITES
30 *
31 * @link      http://eye48.com/go/nftr Plugin Website and Overview
32 *
33 * LICENCE
34 *
35 * This program is free software: you can redistribute it and/or modify it under
36 * the terms of the GNU General Public License as published by the Free Software
37 * Foundation, version 2 of the License.
38 *
39 * This program is distributed in the hope that it will be useful, but WITHOUT
40 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
41 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
42 *
43 * @link      http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License 2.0 (GPLv2)
44 *
45 * CHANGELOG
46 *
47 * 0.2
48 * - exchange licence b/c CC-BY-SA was incompatible with GPL
49 * 0.1
50 * - first release under CC-BY-SA
51 **/
52
53
54if(!defined('DOKU_INC')) die();
55if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
56require_once(DOKU_PLUGIN.'action.php');
57
58class action_plugin_nftr extends DokuWiki_Action_Plugin {
59
60    private $isHidden = null;
61
62    /* -- Methods to manage plugin ------------------------------------------ */
63
64    /**
65    * return some info
66    */
67    function getInfo(){
68        return array(
69	         'author' => 'Michael Haschke',
70	         'email'  => 'haschek@eye48.com',
71	         'date'   => '2009-07-06',
72	         'name'   => 'NFTR - Not for the Robots!',
73	         'desc'   => 'Used to hide some namespaces and wikipages from the search engines
74	                      (set robots to "noindex,follow" and send HTTP header
75	                      "X-Robots-Tag: noindex") but not from the wiki/user itself
76	                      (like the built in hidden function works).',
77	         'url'    => 'http://eye48.com/go/nftr'
78	         );
79    }
80
81    /**
82    * Register its handlers with the DokuWiki's event controller
83    */
84    function register(&$controller)
85    {
86        // alter html/http header for meta data
87        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',  $this, 'setNoindex');
88        // alter http header for meta data
89        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE',  $this, 'setNoindex');
90    }
91
92    /* -- Event handlers ---------------------------------------------------- */
93
94    function setNoindex(&$event)
95    {
96
97        if ($this->isHidden === null) $this->isHidden();
98        if ($this->isHidden === false) return;
99
100        if ($event->name == 'TPL_METAHEADER_OUTPUT')
101        {
102            $key = array_search(array( 'name'=>'robots', 'content'=>'index,follow'), $event->data['meta']);
103
104            if ($key !== false)
105                $event->data['meta'][$key] = array( 'name'=>'robots', 'content'=>'noindex,follow');
106        }
107
108        if ($event->name == 'ACTION_HEADERS_SEND')
109        {
110            $event->data[] = 'X-Robots-Tag: noindex';
111        }
112
113    }
114
115    /* -- Helper methods ---------------------------------------------------- */
116
117    function isHidden()
118    {
119        global $INFO;
120
121        $ns = $INFO['namespace'];
122        $id = $INFO['id'];
123
124        $hidePages = explode(' ', $this->getConf('pages'));
125        $hideSpaces = explode(' ', $this->getConf('spaces'));
126
127        // wikisite should be hidden
128        if (array_search($id, $hidePages) !== false)
129        {
130            $this->isHidden = true;
131            return true;
132        }
133
134        // namespace should be hidden
135        if (array_search($ns, $hideSpaces) !== false)
136        {
137            $this->isHidden = true;
138            return true;
139        }
140
141        // wikisite is top element of hidden namespace
142        if (array_search($id, $hideSpaces) !== false)
143        {
144            $this->isHidden = true;
145            return true;
146        }
147
148        // namespace or wikisite is subpart of a hidden namespace
149        foreach($hideSpaces as $hiddenpart)
150        {
151            if (strpos($id, $hiddenpart.':') === 0) // subsite
152            {
153                $this->isHidden = true;
154                return true;
155            }
156
157            if (strpos($ns, $hiddenpart.':') === 0) // subspace
158            {
159                $this->isHidden = true;
160                return true;
161            }
162        }
163
164        // is not hidden
165        $this->isHidden = false;
166        return false;
167
168    }
169}
170
171
172