Failed Conditions
Push — refactorResolving ( 1356e5 )
by Andreas
03:03
created

PageResolver::resolveAutoPlural()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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