xref: /plugin/combo/action/backlinkmenuitem.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3use ComboStrap\BacklinkMenuItem;
4use ComboStrap\Event;
5use ComboStrap\ExceptionNotFound;
6use ComboStrap\ExecutionContext;
7use ComboStrap\FetcherMarkup;
8use ComboStrap\FetcherPage;
9use ComboStrap\FileSystems;
10use ComboStrap\HttpResponseStatus;
11use ComboStrap\Identity;
12use ComboStrap\LinkMarkup;
13use ComboStrap\LogUtility;
14use ComboStrap\Meta\Store\MetadataDokuWikiStore;
15use ComboStrap\Mime;
16use ComboStrap\MarkupPath;
17use ComboStrap\PagePath;
18use ComboStrap\PluginUtility;
19use ComboStrap\Reference;
20use ComboStrap\References;
21use ComboStrap\Tag\RelatedTag;
22use ComboStrap\WikiPath;
23use dokuwiki\Menu\Item\Backlink;
24
25
26require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
27
28/**
29 * Handle the backlink menu item
30 */
31class action_plugin_combo_backlinkmenuitem extends DokuWiki_Action_Plugin
32{
33
34
35    const CALL_ID = "combo-backlink";
36    const CANONICAL = "backlink";
37    const WHREF = "whref";
38
39    public function register(Doku_Event_Handler $controller)
40    {
41
42
43        /**
44         * Add a icon in the page tools menu
45         * https://www.dokuwiki.org/devel:event:menu_items_assembly
46         */
47        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItem');
48
49
50        /**
51         * The ajax api to return data
52         */
53        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxCall');
54
55
56    }
57
58    function addMenuItem(Doku_Event $event, $param)
59    {
60
61
62        /**
63         * The `view` property defines the menu that is currently built
64         * https://www.dokuwiki.org/devel:menus
65         * If this is not the page menu, return
66         */
67        if ($event->data['view'] != 'page') return;
68
69
70        $menuItems = &$event->data["items"];
71        foreach ($menuItems as $key => $menuItem) {
72            if ($menuItem instanceof Backlink) {
73                $menuItems[$key] = new BacklinkMenuItem();
74                break;
75            }
76        }
77        /**
78         * Add the link to build the link to the backlinks actions
79         */
80        try {
81            $requestedContextPage = ExecutionContext::getActualOrCreateFromEnv()->getRequestedPath();
82        } catch (ExceptionNotFound $e) {
83            // admin
84            return;
85        }
86        global $JSINFO;
87        $JSINFO[self::WHREF] = FetcherPage::createPageFetcherFromPath($requestedContextPage)->getFetchUrl()->toString();
88
89    }
90
91    /**
92     * Main function; dispatches the visual comment actions
93     * @param   $event Doku_Event
94     */
95    function ajaxCall(&$event, $param): void
96    {
97        $call = $event->data;
98        if ($call != self::CALL_ID) {
99            return;
100        }
101        //no other ajax call handlers needed
102        $event->stopPropagation();
103        $event->preventDefault();
104
105        /**
106         * Shared check between post and get HTTP method
107         */
108        $id = $_GET["id"];
109        if ($id === null) {
110            /**
111             * With {@link TestRequest}
112             * for instance
113             */
114            $id = $_REQUEST["id"];
115        }
116        $executionContext = ExecutionContext::getActualOrCreateFromEnv();
117        if (empty($id)) {
118            $executionContext->response()
119                ->setStatus(HttpResponseStatus::BAD_REQUEST)
120                ->setEvent($event)
121                ->setCanonical(self::CANONICAL)
122                ->setBody("The page id should not be empty", Mime::getHtml())
123                ->end();
124            return;
125        }
126
127
128        $backlinkPages = MarkupPath::createMarkupFromId($id);
129        $html = RelatedTag::renderForPage($backlinkPages);
130
131
132        $executionContext
133            ->response()
134            ->setStatus(HttpResponseStatus::ALL_GOOD)
135            ->setEvent($event)
136            ->setCanonical(self::CANONICAL)
137            ->setBody($html, Mime::getHtml())
138            ->end();
139
140    }
141
142
143}
144
145
146
147