xref: /plugin/struct/types/Page.php (revision 06bdb2c288b8e49547bae21cc547b56cd71ee8c4)
1<?php
2namespace dokuwiki\plugin\struct\types;
3
4/**
5 * Class Page
6 *
7 * Represents a single page in the wiki. Will be linked in output.
8 *
9 * @package dokuwiki\plugin\struct\types
10 */
11class Page extends AbstractMultiBaseType {
12
13    protected $config = array(
14        'autocomplete' => array(
15            'mininput' => 2,
16            'maxresult' => 5,
17            'namespace' => '',
18            'postfix' => '',
19        ),
20    );
21
22    /**
23     * Output the stored data
24     *
25     * @param string|int $value the value stored in the database
26     * @param \Doku_Renderer $R the renderer currently used to render the data
27     * @param string $mode The mode the output is rendered in (eg. XHTML)
28     * @return bool true if $mode could be satisfied
29     */
30    public function renderValue($value, \Doku_Renderer $R, $mode) {
31        if(!$value) return true;
32
33        $R->internallink(":$value");
34        return true;
35    }
36
37    /**
38     * Cleans the link
39     *
40     * @param string $value
41     * @return string
42     */
43    public function validate($value) {
44        return cleanID($value);
45    }
46
47    /**
48     * Autocompletion support for pages
49     *
50     * @return array
51     */
52    public function handleAjax() {
53        global $INPUT;
54
55        // check minimum length
56        $lookup = trim($INPUT->str('search'));
57        if(utf8_strlen($lookup) < $this->config['autocomplete']['mininput']) return array();
58
59        // results wanted?
60        $max = $this->config['autocomplete']['maxresult'];
61        if($max <= 0) return array();
62
63        // lookup with namespace and postfix applied
64        $namespace = $this->config['autocomplete']['namespace'];
65        if($namespace) {
66            // namespace may be relative, resolve in current context
67            $namespace .= ':foo'; // resolve expects pageID
68            resolve_pageid($INPUT->str('ns'), $namespace, $exists);
69            $namespace = getNS($namespace);
70        }
71        $postfix = $this->config['postfix'];
72        if($namespace) $lookup .= ' @' . $namespace;
73
74        $data = ft_pageLookup($lookup, true, useHeading('navigation'));
75        if(!count($data)) return array();
76
77        // this basically duplicates what we do in ajax_qsearch()
78        $result = array();
79        $counter = 0;
80        foreach($data as $id => $title) {
81            if(useHeading('navigation')) {
82                $name = $title;
83            } else {
84                $ns = getNS($id);
85                if($ns) {
86                    $name = noNS($id) . ' (' . $ns . ')';
87                } else {
88                    $name = $id;
89                }
90            }
91
92            // check suffix
93            if($postfix && !substr($id, -1 * strlen($postfix)) == $postfix) {
94                continue; // page does not end in postfix, don't suggest it
95            }
96
97            $result[] = array(
98                'label' => $name,
99                'value' => $id
100            );
101
102            $counter++;
103            if($counter > $max) break;
104        }
105
106        return $result;
107    }
108}
109