xref: /dokuwiki/inc/File/Resolver.php (revision 79a2d7845d5e5e48fe3be8f192717de9294aaba5)
12cd6cc0aSAndreas Gohr<?php
22cd6cc0aSAndreas Gohr
32cd6cc0aSAndreas Gohrnamespace dokuwiki\File;
42cd6cc0aSAndreas Gohr
52cd6cc0aSAndreas Gohr/**
62cd6cc0aSAndreas Gohr * Resolving relative IDs to absolute ones
72cd6cc0aSAndreas Gohr */
82cd6cc0aSAndreas Gohrabstract class Resolver
92cd6cc0aSAndreas Gohr{
102cd6cc0aSAndreas Gohr
112cd6cc0aSAndreas Gohr    /** @var string context page ID */
122cd6cc0aSAndreas Gohr    protected $contextID;
132cd6cc0aSAndreas Gohr    /** @var string namespace of context page ID */
142cd6cc0aSAndreas Gohr    protected $contextNS;
152cd6cc0aSAndreas Gohr
162cd6cc0aSAndreas Gohr    /**
172cd6cc0aSAndreas Gohr     * @param string $contextID the current pageID that's the context to resolve relative IDs to
182cd6cc0aSAndreas Gohr     */
192cd6cc0aSAndreas Gohr    public function __construct($contextID)
202cd6cc0aSAndreas Gohr    {
212cd6cc0aSAndreas Gohr        $this->contextID = $contextID;
222cd6cc0aSAndreas Gohr        $this->contextNS = (string)getNS($contextID);
232cd6cc0aSAndreas Gohr    }
242cd6cc0aSAndreas Gohr
252cd6cc0aSAndreas Gohr    /**
262cd6cc0aSAndreas Gohr     * Resolves a given ID to be absolute
272cd6cc0aSAndreas Gohr     *
282cd6cc0aSAndreas Gohr     * @param string $id The ID to resolve
292cd6cc0aSAndreas Gohr     * @param string|int|false $rev The revision time to use when resolving
302cd6cc0aSAndreas Gohr     * @param bool $isDateAt Is the given revision only a datetime hint not an exact revision?
312cd6cc0aSAndreas Gohr     * @return string
322cd6cc0aSAndreas Gohr     */
332cd6cc0aSAndreas Gohr    public function resolveId($id, $rev = '', $isDateAt = false)
342cd6cc0aSAndreas Gohr    {
352cd6cc0aSAndreas Gohr        global $conf;
362cd6cc0aSAndreas Gohr
372cd6cc0aSAndreas Gohr        // some pre cleaning for useslash:
382cd6cc0aSAndreas Gohr        if ($conf['useslash']) $id = str_replace('/', ':', $id);
392cd6cc0aSAndreas Gohr        // on some systems, semicolons might be used instead of colons:
402cd6cc0aSAndreas Gohr        $id = str_replace(';', ':', $id);
412cd6cc0aSAndreas Gohr
422cd6cc0aSAndreas Gohr        $id = $this->resolvePrefix($id);
43*79a2d784SGerrit Uitslag        return $this->resolveRelatives($id);
442cd6cc0aSAndreas Gohr    }
452cd6cc0aSAndreas Gohr
462cd6cc0aSAndreas Gohr    /**
472cd6cc0aSAndreas Gohr     * Handle IDs starting with . or ~ and prepend the proper prefix
482cd6cc0aSAndreas Gohr     *
492cd6cc0aSAndreas Gohr     * @param string $id
502cd6cc0aSAndreas Gohr     * @return string
512cd6cc0aSAndreas Gohr     */
522cd6cc0aSAndreas Gohr    protected function resolvePrefix($id)
532cd6cc0aSAndreas Gohr    {
542cd6cc0aSAndreas Gohr        // relative to current page (makes the current page a start page)
552cd6cc0aSAndreas Gohr        if ($id[0] === '~') {
562cd6cc0aSAndreas Gohr            $id = $this->contextID . ':' . substr($id, 1);
572cd6cc0aSAndreas Gohr        }
582cd6cc0aSAndreas Gohr
592cd6cc0aSAndreas Gohr        // relative to current namespace
602cd6cc0aSAndreas Gohr        if ($id[0] === '.') {
612cd6cc0aSAndreas Gohr            // normalize initial dots without a colon
622cd6cc0aSAndreas Gohr            $id = preg_replace('/^((\.+:)*)(\.+)(?=[^:\.])/', '\1\3:', $id);
632cd6cc0aSAndreas Gohr            $id = $this->contextNS . ':' . $id;
642cd6cc0aSAndreas Gohr        }
652cd6cc0aSAndreas Gohr
662cd6cc0aSAndreas Gohr        // auto-relative, because there is a context namespace but no namespace in the ID
672cd6cc0aSAndreas Gohr        if ($this->contextID !== '' && strpos($id, ':') === false) {
682cd6cc0aSAndreas Gohr            $id = $this->contextNS . ':' . $id;
692cd6cc0aSAndreas Gohr        }
702cd6cc0aSAndreas Gohr
712cd6cc0aSAndreas Gohr        return $id;
722cd6cc0aSAndreas Gohr    }
732cd6cc0aSAndreas Gohr
742cd6cc0aSAndreas Gohr    /**
752cd6cc0aSAndreas Gohr     * Handle . and .. within IDs
762cd6cc0aSAndreas Gohr     *
772cd6cc0aSAndreas Gohr     * @param string $id
782cd6cc0aSAndreas Gohr     * @return string
792cd6cc0aSAndreas Gohr     */
802cd6cc0aSAndreas Gohr    protected function resolveRelatives($id)
812cd6cc0aSAndreas Gohr    {
822cd6cc0aSAndreas Gohr        if ($id === '') return '';
832cd6cc0aSAndreas Gohr        $trail = ($id[-1] === ':') ? ':' : ''; // keep trailing colon
842cd6cc0aSAndreas Gohr
852cd6cc0aSAndreas Gohr        $result = [];
862cd6cc0aSAndreas Gohr        $parts = explode(':', $id);
872cd6cc0aSAndreas Gohr
882cd6cc0aSAndreas Gohr        foreach ($parts as $dir) {
892cd6cc0aSAndreas Gohr            if ($dir === '.') continue;
902cd6cc0aSAndreas Gohr            if ($dir === '') continue;
912cd6cc0aSAndreas Gohr            if ($dir === '..') {
922cd6cc0aSAndreas Gohr                array_pop($result);
932cd6cc0aSAndreas Gohr                continue;
942cd6cc0aSAndreas Gohr            }
952cd6cc0aSAndreas Gohr            array_push($result, $dir);
962cd6cc0aSAndreas Gohr        }
972cd6cc0aSAndreas Gohr
982cd6cc0aSAndreas Gohr        $id = implode(':', $result);
992cd6cc0aSAndreas Gohr        $id .= $trail;
1002cd6cc0aSAndreas Gohr
1012cd6cc0aSAndreas Gohr        return $id;
1022cd6cc0aSAndreas Gohr    }
1032cd6cc0aSAndreas Gohr
1042cd6cc0aSAndreas Gohr}
105