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