xref: /plugin/description/action.php (revision e689d2932c90c4a915785840f66fe487918d7d88)
1<?php
2/*
3 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
4 * @noinspection AutoloadingIssuesInspection
5 */
6
7/**
8 *  Description action plugin.
9 *
10 * @license      GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author       Ikuo Obataya <I.Obataya@gmail.com>
12 * @author       Matthias Schulte <dokuwiki@lupo49.de>.
13 * @author       Mark C. Prins <mprins@users.sf.net>
14 *
15 */
16
17use dokuwiki\Extension\ActionPlugin;
18use dokuwiki\Extension\Event;
19use dokuwiki\Extension\EventHandler;
20
21const KEYWORD_SOURCE_ABSTRACT = 'abstract';
22const KEYWORD_SOURCE_GLOBAL = 'global';
23const KEYWORD_SOURCE_SYNTAX = 'syntax';
24
25class action_plugin_description extends ActionPlugin
26{
27
28    final public function register(EventHandler $controller): void
29    {
30        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description', array());
31    }
32
33    /**
34     * Add an abstract, global value or a specified string to meta header
35     */
36    final public function description(Event $event, $param): void
37    {
38        if (empty($event->data) || empty($event->data['meta'])) {
39            return;
40        }
41
42        global $ID;
43        $source = $this->getConf('keyword_source');
44        if (empty($source)) {
45            $source = 'abstract';
46        }
47
48        if ($source === KEYWORD_SOURCE_ABSTRACT) {
49            if (auth_quickaclcheck($ID) < AUTH_READ) {
50                // don't add meta header when user has no read permissions
51                return;
52            }
53
54            $d = p_get_metadata($ID, 'description');
55            if (empty($d)) {
56                return;
57            }
58
59            $a = str_replace("\n", " ", $d['abstract']);
60            if (empty($a)) {
61                return;
62            }
63        }
64
65        if ($source === KEYWORD_SOURCE_GLOBAL) {
66            $a = $this->getConf('global_description');
67            if (empty($a)) {
68                return;
69            }
70        }
71
72        if ($source === KEYWORD_SOURCE_SYNTAX) {
73            if (auth_quickaclcheck($ID) < AUTH_READ) {
74                // don't add meta header when user has no read permissions
75                return;
76            }
77            $metadata = p_get_metadata($ID);
78            $a = $metadata['plugin_description']['keywords'];
79            if (empty($a)) {
80                return;
81            }
82        }
83
84        $m = array("name" => "description", "content" => $a);
85        $event->data['meta'][] = $m;
86    }
87}
88