xref: /plugin/combo/action/linkmove.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3use ComboStrap\LinkUtility;
4use ComboStrap\PluginUtility;
5
6if (!defined('DOKU_INC')) die();
7require_once(__DIR__ . '/../class/PluginUtility.php');
8require_once(__DIR__ . '/../class/LinkUtility.php');
9
10/**
11 * Class action_plugin_combo_move
12 * Handle the move of a page in order to update the link
13 */
14class action_plugin_combo_linkmove extends DokuWiki_Action_Plugin
15{
16
17    /**
18     * As explained https://www.dokuwiki.org/plugin:move
19     * @param Doku_Event_Handler $controller
20     */
21    function register(Doku_Event_Handler $controller)
22    {
23        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move', array());
24    }
25
26    /**
27     * Handle the move of a page
28     * @param Doku_Event $event
29     * @param $params
30     */
31    function handle_move(Doku_Event $event, $params)
32    {
33        /**
34         * The handlers is the name of the component (ie refers to the {@link syntax_plugin_combo_link} handler)
35         * and 'rewrite_combo' to the below method
36         */
37        $event->data['handlers'][syntax_plugin_combo_link::COMPONENT] = array($this, 'rewrite_combo');
38    }
39
40    /**
41     *
42     * @param $match
43     * @param $state
44     * @param $pos
45     * @param $plugin
46     * @param helper_plugin_move_handler $handler
47     */
48    public function rewrite_combo($match, $state, $pos, $plugin, helper_plugin_move_handler $handler)
49    {
50        /**
51         * The original move method
52         * is {@link helper_plugin_move_handler::internallink()}
53         *
54         */
55        if ($state == DOKU_LEXER_ENTER) {
56            $ref = LinkUtility::parse($match)[LinkUtility::ATTRIBUTE_REF];
57            $link = new LinkUtility($ref);
58            if ($link->getType() == LinkUtility::TYPE_INTERNAL) {
59
60                $handler->internallink($match, $state, $pos);
61                $suffix = "]]";
62                $suffixStartPosition = strlen($handler->calls) - strlen($suffix);
63                if (strpos($handler->calls, $suffix) === $suffixStartPosition) {
64                    $handler->calls = substr($handler->calls ,0,$suffixStartPosition);
65                }
66
67            } else {
68
69                // Other type of links
70                $handler->calls .= $match;
71
72            }
73        } else {
74
75            // Description and ending
76            $handler->calls .= $match;
77
78        }
79    }
80
81
82}
83