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