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