xref: /dokuwiki/inc/File/PageResolver.php (revision 2cd6cc0aee287817853b56fa39a11b9eecd6da90)
1*2cd6cc0aSAndreas Gohr<?php
2*2cd6cc0aSAndreas Gohr
3*2cd6cc0aSAndreas Gohrnamespace dokuwiki\File;
4*2cd6cc0aSAndreas Gohr
5*2cd6cc0aSAndreas Gohr/**
6*2cd6cc0aSAndreas Gohr * Creates an absolute page ID from a relative one
7*2cd6cc0aSAndreas Gohr */
8*2cd6cc0aSAndreas Gohrclass PageResolver extends Resolver
9*2cd6cc0aSAndreas Gohr{
10*2cd6cc0aSAndreas Gohr
11*2cd6cc0aSAndreas Gohr    /**
12*2cd6cc0aSAndreas Gohr     * Resolves a given ID to be absolute
13*2cd6cc0aSAndreas Gohr     *
14*2cd6cc0aSAndreas Gohr     * This handles all kinds of relative shortcuts, startpages and autoplurals
15*2cd6cc0aSAndreas Gohr     * @inheritDoc
16*2cd6cc0aSAndreas Gohr     */
17*2cd6cc0aSAndreas Gohr    public function resolveId($id, $rev = '', $isDateAt = false)
18*2cd6cc0aSAndreas Gohr    {
19*2cd6cc0aSAndreas Gohr        global $conf;
20*2cd6cc0aSAndreas Gohr
21*2cd6cc0aSAndreas Gohr        // pages may have a hash attached, we separate it on resolving
22*2cd6cc0aSAndreas Gohr        if (strpos($id, '#') !== false) {
23*2cd6cc0aSAndreas Gohr            list($id, $hash) = explode('#', $id, 2);
24*2cd6cc0aSAndreas Gohr            $hash = cleanID($hash);
25*2cd6cc0aSAndreas Gohr        } else {
26*2cd6cc0aSAndreas Gohr            $hash = '';
27*2cd6cc0aSAndreas Gohr        }
28*2cd6cc0aSAndreas Gohr
29*2cd6cc0aSAndreas Gohr        if ($id !== '') {
30*2cd6cc0aSAndreas Gohr            $id = parent::resolveId($id, $rev, $isDateAt);
31*2cd6cc0aSAndreas Gohr            $id = $this->resolveStartPage($id, $rev, $isDateAt);
32*2cd6cc0aSAndreas Gohr            if ($conf['autoplural']) {
33*2cd6cc0aSAndreas Gohr                $id = $this->resolveAutoPlural($id, $rev, $isDateAt);
34*2cd6cc0aSAndreas Gohr            }
35*2cd6cc0aSAndreas Gohr        } else {
36*2cd6cc0aSAndreas Gohr            $id = $this->contextID;
37*2cd6cc0aSAndreas Gohr        }
38*2cd6cc0aSAndreas Gohr
39*2cd6cc0aSAndreas Gohr        $id = cleanID($id); // FIXME always? or support parameter
40*2cd6cc0aSAndreas Gohr        // readd hash if any
41*2cd6cc0aSAndreas Gohr        if ($hash !== '') $id .= "#$hash";
42*2cd6cc0aSAndreas Gohr        return $id;
43*2cd6cc0aSAndreas Gohr    }
44*2cd6cc0aSAndreas Gohr
45*2cd6cc0aSAndreas Gohr    /**
46*2cd6cc0aSAndreas Gohr     * IDs ending in :
47*2cd6cc0aSAndreas Gohr     *
48*2cd6cc0aSAndreas Gohr     * @param string $id
49*2cd6cc0aSAndreas Gohr     * @param string|int|false $rev
50*2cd6cc0aSAndreas Gohr     * @param bool $isDateAt
51*2cd6cc0aSAndreas Gohr     * @return string
52*2cd6cc0aSAndreas Gohr     */
53*2cd6cc0aSAndreas Gohr    protected function resolveStartPage($id, $rev, $isDateAt)
54*2cd6cc0aSAndreas Gohr    {
55*2cd6cc0aSAndreas Gohr        global $conf;
56*2cd6cc0aSAndreas Gohr
57*2cd6cc0aSAndreas Gohr        if ($id[-1] !== ':') return $id;
58*2cd6cc0aSAndreas Gohr
59*2cd6cc0aSAndreas Gohr        if (page_exists($id . $conf['start'], $rev, true, $isDateAt)) {
60*2cd6cc0aSAndreas Gohr            // start page inside namespace
61*2cd6cc0aSAndreas Gohr            return $id . $conf['start'];
62*2cd6cc0aSAndreas Gohr        } elseif (page_exists($id . noNS(cleanID($id)), $rev, true, $isDateAt)) {
63*2cd6cc0aSAndreas Gohr            // page named like the NS inside the NS
64*2cd6cc0aSAndreas Gohr            return $id . noNS(cleanID($id));
65*2cd6cc0aSAndreas Gohr        } elseif (page_exists(substr($id, 0, -1), $rev, true, $isDateAt)) {
66*2cd6cc0aSAndreas Gohr            // page named like the NS outside the NS
67*2cd6cc0aSAndreas Gohr            return substr($id, 0, -1);
68*2cd6cc0aSAndreas Gohr        }
69*2cd6cc0aSAndreas Gohr
70*2cd6cc0aSAndreas Gohr        // fall back to default start page
71*2cd6cc0aSAndreas Gohr        return $id . $conf['start'];
72*2cd6cc0aSAndreas Gohr    }
73*2cd6cc0aSAndreas Gohr
74*2cd6cc0aSAndreas Gohr    /**
75*2cd6cc0aSAndreas Gohr     * Try alternative plural/singular form
76*2cd6cc0aSAndreas Gohr     *
77*2cd6cc0aSAndreas Gohr     * @param string $id
78*2cd6cc0aSAndreas Gohr     * @param int $rev
79*2cd6cc0aSAndreas Gohr     * @param bool $isDateAt
80*2cd6cc0aSAndreas Gohr     * @return string
81*2cd6cc0aSAndreas Gohr     */
82*2cd6cc0aSAndreas Gohr    protected function resolveAutoPlural($id, $rev, $isDateAt)
83*2cd6cc0aSAndreas Gohr    {
84*2cd6cc0aSAndreas Gohr        if (page_exists($id, $rev, $isDateAt)) return $id;
85*2cd6cc0aSAndreas Gohr
86*2cd6cc0aSAndreas Gohr        if ($id[-1] === 's') {
87*2cd6cc0aSAndreas Gohr            $try = substr($id, 0, -1);
88*2cd6cc0aSAndreas Gohr        } else {
89*2cd6cc0aSAndreas Gohr            $try = $id . 's';
90*2cd6cc0aSAndreas Gohr        }
91*2cd6cc0aSAndreas Gohr
92*2cd6cc0aSAndreas Gohr        if (page_exists($try, $rev, true, $isDateAt)) {
93*2cd6cc0aSAndreas Gohr            return $try;
94*2cd6cc0aSAndreas Gohr        }
95*2cd6cc0aSAndreas Gohr        return $id;
96*2cd6cc0aSAndreas Gohr    }
97*2cd6cc0aSAndreas Gohr
98*2cd6cc0aSAndreas Gohr}
99