xref: /dokuwiki/inc/Parsing/ParserMode/Internallink.php (revision 71096e46fcbfaeaa808667aba794e77fe2780169)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode;
4be906b56SAndreas Gohr
5*71096e46SAndreas Gohruse dokuwiki\Parsing\Handler;
6*71096e46SAndreas Gohr
7be906b56SAndreas Gohrclass Internallink extends AbstractMode
8be906b56SAndreas Gohr{
9be906b56SAndreas Gohr    /** @inheritdoc */
10*71096e46SAndreas Gohr    public function getSort()
11*71096e46SAndreas Gohr    {
12*71096e46SAndreas Gohr        return 300;
13*71096e46SAndreas Gohr    }
14*71096e46SAndreas Gohr
15*71096e46SAndreas Gohr    /** @inheritdoc */
16be906b56SAndreas Gohr    public function connectTo($mode)
17be906b56SAndreas Gohr    {
18be906b56SAndreas Gohr        // Word boundaries?
19be906b56SAndreas Gohr        $this->Lexer->addSpecialPattern("\[\[.*?\]\](?!\])", $mode, 'internallink');
20be906b56SAndreas Gohr    }
21be906b56SAndreas Gohr
22be906b56SAndreas Gohr    /** @inheritdoc */
23*71096e46SAndreas Gohr    public function handle($match, $state, $pos, Handler $handler)
24be906b56SAndreas Gohr    {
25*71096e46SAndreas Gohr        // Strip the opening and closing markup
26*71096e46SAndreas Gohr        $link = preg_replace(['/^\[\[/', '/\]\]$/u'], '', $match);
27*71096e46SAndreas Gohr
28*71096e46SAndreas Gohr        // Split title from URL
29*71096e46SAndreas Gohr        $link = sexplode('|', $link, 2);
30*71096e46SAndreas Gohr        if ($link[1] !== null && preg_match('/^\{\{[^\}]+\}\}$/', $link[1])) {
31*71096e46SAndreas Gohr            // If the title is an image, convert it to an array containing the image details
32*71096e46SAndreas Gohr            $link[1] = Media::parseMedia($link[1]);
33*71096e46SAndreas Gohr        }
34*71096e46SAndreas Gohr        $link[0] = trim($link[0]);
35*71096e46SAndreas Gohr
36*71096e46SAndreas Gohr        //decide which kind of link it is
37*71096e46SAndreas Gohr
38*71096e46SAndreas Gohr        if (link_isinterwiki($link[0])) {
39*71096e46SAndreas Gohr            // Interwiki
40*71096e46SAndreas Gohr            $interwiki = sexplode('>', $link[0], 2, '');
41*71096e46SAndreas Gohr            $handler->addCall(
42*71096e46SAndreas Gohr                'interwikilink',
43*71096e46SAndreas Gohr                [$link[0], $link[1], strtolower($interwiki[0]), $interwiki[1]],
44*71096e46SAndreas Gohr                $pos
45*71096e46SAndreas Gohr            );
46*71096e46SAndreas Gohr        } elseif (preg_match('/^\\\\\\\\[^\\\\]+?\\\\/u', $link[0])) {
47*71096e46SAndreas Gohr            // Windows Share
48*71096e46SAndreas Gohr            $handler->addCall(
49*71096e46SAndreas Gohr                'windowssharelink',
50*71096e46SAndreas Gohr                [$link[0], $link[1]],
51*71096e46SAndreas Gohr                $pos
52*71096e46SAndreas Gohr            );
53*71096e46SAndreas Gohr        } elseif (preg_match('#^([a-z0-9\-\.+]+?)://#i', $link[0])) {
54*71096e46SAndreas Gohr            // external link (accepts all protocols)
55*71096e46SAndreas Gohr            $handler->addCall(
56*71096e46SAndreas Gohr                'externallink',
57*71096e46SAndreas Gohr                [$link[0], $link[1]],
58*71096e46SAndreas Gohr                $pos
59*71096e46SAndreas Gohr            );
60*71096e46SAndreas Gohr        } elseif (preg_match('<' . PREG_PATTERN_VALID_EMAIL . '>', $link[0])) {
61*71096e46SAndreas Gohr            // E-Mail (pattern above is defined in inc/mail.php)
62*71096e46SAndreas Gohr            $handler->addCall(
63*71096e46SAndreas Gohr                'emaillink',
64*71096e46SAndreas Gohr                [$link[0], $link[1]],
65*71096e46SAndreas Gohr                $pos
66*71096e46SAndreas Gohr            );
67*71096e46SAndreas Gohr        } elseif (preg_match('!^#.+!', $link[0])) {
68*71096e46SAndreas Gohr            // local link
69*71096e46SAndreas Gohr            $handler->addCall(
70*71096e46SAndreas Gohr                'locallink',
71*71096e46SAndreas Gohr                [substr($link[0], 1), $link[1]],
72*71096e46SAndreas Gohr                $pos
73*71096e46SAndreas Gohr            );
74*71096e46SAndreas Gohr        } else {
75*71096e46SAndreas Gohr            // internal link
76*71096e46SAndreas Gohr            $handler->addCall(
77*71096e46SAndreas Gohr                'internallink',
78*71096e46SAndreas Gohr                [$link[0], $link[1]],
79*71096e46SAndreas Gohr                $pos
80*71096e46SAndreas Gohr            );
81*71096e46SAndreas Gohr        }
82*71096e46SAndreas Gohr
83*71096e46SAndreas Gohr        return true;
84be906b56SAndreas Gohr    }
85be906b56SAndreas Gohr}
86