xref: /plugin/description/action.php (revision d409808b399484c9e22d03017ee4ec6c27fae59b)
1<?php
2
3/*
4 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
5 * @noinspection AutoloadingIssuesInspection
6 */
7
8/**
9 *  Description action plugin.
10 *
11 * @license      GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 * @author       Ikuo Obataya <I.Obataya@gmail.com>
13 * @author       Matthias Schulte <dokuwiki@lupo49.de>.
14 * @author       Mark C. Prins <mprins@users.sf.net>
15 *
16 */
17
18use dokuwiki\Extension\ActionPlugin;
19use dokuwiki\Extension\Event;
20use dokuwiki\Extension\EventHandler;
21
22const KEYWORD_SOURCE_ABSTRACT = 'abstract';
23const KEYWORD_SOURCE_GLOBAL = 'global';
24const KEYWORD_SOURCE_SYNTAX = 'syntax';
25
26class action_plugin_description extends ActionPlugin
27{
28    final public function register(EventHandler $controller): void
29    {
30        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description', []);
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 = ["name" => "description", "content" => $a];
85        $event->data['meta'][] = $m;
86    }
87}
88