xref: /dokuwiki/inc/File/PageResolver.php (revision 42411fe91871199e19a856156edc4b3187205715)
12cd6cc0aSAndreas Gohr<?php
22cd6cc0aSAndreas Gohr
32cd6cc0aSAndreas Gohrnamespace dokuwiki\File;
42cd6cc0aSAndreas Gohr
52cd6cc0aSAndreas Gohr/**
62cd6cc0aSAndreas Gohr * Creates an absolute page ID from a relative one
72cd6cc0aSAndreas Gohr */
82cd6cc0aSAndreas Gohrclass PageResolver extends Resolver
92cd6cc0aSAndreas Gohr{
102cd6cc0aSAndreas Gohr    /**
112cd6cc0aSAndreas Gohr     * Resolves a given ID to be absolute
122cd6cc0aSAndreas Gohr     *
132cd6cc0aSAndreas Gohr     * This handles all kinds of relative shortcuts, startpages and autoplurals
142cd6cc0aSAndreas Gohr     * @inheritDoc
152cd6cc0aSAndreas Gohr     */
162cd6cc0aSAndreas Gohr    public function resolveId($id, $rev = '', $isDateAt = false)
172cd6cc0aSAndreas Gohr    {
182cd6cc0aSAndreas Gohr        global $conf;
196d4b5a0aSasivery        $id = (string) $id;
202cd6cc0aSAndreas Gohr
212cd6cc0aSAndreas Gohr        // pages may have a hash attached, we separate it on resolving
222cd6cc0aSAndreas Gohr        if (strpos($id, '#') !== false) {
23445164b2SAndreas Gohr            [$id, $hash] = sexplode('#', $id, 2);
242cd6cc0aSAndreas Gohr            $hash = cleanID($hash);
252cd6cc0aSAndreas Gohr        } else {
262cd6cc0aSAndreas Gohr            $hash = '';
272cd6cc0aSAndreas Gohr        }
282cd6cc0aSAndreas Gohr
296d4b5a0aSasivery        if ($id !== '') {
302cd6cc0aSAndreas Gohr            $id = parent::resolveId($id, $rev, $isDateAt);
312cd6cc0aSAndreas Gohr            $id = $this->resolveStartPage($id, $rev, $isDateAt);
322cd6cc0aSAndreas Gohr            if ($conf['autoplural']) {
332cd6cc0aSAndreas Gohr                $id = $this->resolveAutoPlural($id, $rev, $isDateAt);
342cd6cc0aSAndreas Gohr            }
352cd6cc0aSAndreas Gohr        } else {
362cd6cc0aSAndreas Gohr            $id = $this->contextID;
372cd6cc0aSAndreas Gohr        }
382cd6cc0aSAndreas Gohr
392cd6cc0aSAndreas Gohr        $id = cleanID($id); // FIXME always? or support parameter
402cd6cc0aSAndreas Gohr        // readd hash if any
412cd6cc0aSAndreas Gohr        if ($hash !== '') $id .= "#$hash";
422cd6cc0aSAndreas Gohr        return $id;
432cd6cc0aSAndreas Gohr    }
442cd6cc0aSAndreas Gohr
452cd6cc0aSAndreas Gohr    /**
462cd6cc0aSAndreas Gohr     * IDs ending in :
472cd6cc0aSAndreas Gohr     *
482cd6cc0aSAndreas Gohr     * @param string $id
492cd6cc0aSAndreas Gohr     * @param string|int|false $rev
502cd6cc0aSAndreas Gohr     * @param bool $isDateAt
512cd6cc0aSAndreas Gohr     * @return string
522cd6cc0aSAndreas Gohr     */
532cd6cc0aSAndreas Gohr    protected function resolveStartPage($id, $rev, $isDateAt)
542cd6cc0aSAndreas Gohr    {
552cd6cc0aSAndreas Gohr        global $conf;
562cd6cc0aSAndreas Gohr
57*42411fe9SAndreas Gohr        if ($id === '' || $id[-1] !== ':') return $id;
582cd6cc0aSAndreas Gohr
592cd6cc0aSAndreas Gohr        if (page_exists($id . $conf['start'], $rev, true, $isDateAt)) {
602cd6cc0aSAndreas Gohr            // start page inside namespace
612cd6cc0aSAndreas Gohr            return $id . $conf['start'];
622cd6cc0aSAndreas Gohr        } elseif (page_exists($id . noNS(cleanID($id)), $rev, true, $isDateAt)) {
632cd6cc0aSAndreas Gohr            // page named like the NS inside the NS
642cd6cc0aSAndreas Gohr            return $id . noNS(cleanID($id));
652cd6cc0aSAndreas Gohr        } elseif (page_exists(substr($id, 0, -1), $rev, true, $isDateAt)) {
662cd6cc0aSAndreas Gohr            // page named like the NS outside the NS
672cd6cc0aSAndreas Gohr            return substr($id, 0, -1);
682cd6cc0aSAndreas Gohr        }
692cd6cc0aSAndreas Gohr
702cd6cc0aSAndreas Gohr        // fall back to default start page
712cd6cc0aSAndreas Gohr        return $id . $conf['start'];
722cd6cc0aSAndreas Gohr    }
732cd6cc0aSAndreas Gohr
742cd6cc0aSAndreas Gohr    /**
752cd6cc0aSAndreas Gohr     * Try alternative plural/singular form
762cd6cc0aSAndreas Gohr     *
772cd6cc0aSAndreas Gohr     * @param string $id
782cd6cc0aSAndreas Gohr     * @param int $rev
792cd6cc0aSAndreas Gohr     * @param bool $isDateAt
802cd6cc0aSAndreas Gohr     * @return string
812cd6cc0aSAndreas Gohr     */
822cd6cc0aSAndreas Gohr    protected function resolveAutoPlural($id, $rev, $isDateAt)
832cd6cc0aSAndreas Gohr    {
842cd6cc0aSAndreas Gohr        if (page_exists($id, $rev, $isDateAt)) return $id;
852cd6cc0aSAndreas Gohr
862cd6cc0aSAndreas Gohr        if ($id[-1] === 's') {
872cd6cc0aSAndreas Gohr            $try = substr($id, 0, -1);
882cd6cc0aSAndreas Gohr        } else {
892cd6cc0aSAndreas Gohr            $try = $id . 's';
902cd6cc0aSAndreas Gohr        }
912cd6cc0aSAndreas Gohr
922cd6cc0aSAndreas Gohr        if (page_exists($try, $rev, true, $isDateAt)) {
932cd6cc0aSAndreas Gohr            return $try;
942cd6cc0aSAndreas Gohr        }
952cd6cc0aSAndreas Gohr        return $id;
962cd6cc0aSAndreas Gohr    }
972cd6cc0aSAndreas Gohr}
98