xref: /plugin/struct/types/Page.php (revision 08e7b568f1c5230d7a69f10078bf85611fd6c0a4)
1<?php
2namespace 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 plugin\struct\types
10 */
11class Page extends AbstractMultiBaseType {
12
13    protected $config = array(
14        'namespace' => '',
15        'postfix' => '',
16        'autocomplete' => array(
17            'mininput' => 2,
18            'maxresult' => 5,
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        $link = cleanID($this->config['namespace'] .':'. $value . $this->config['postfix']);
32        if(!$link) return true;
33
34        $R->internallink(":$link");
35        return true;
36    }
37
38    /**
39     * Autocompletion support for pages
40     *
41     * @return array
42     */
43    public function handleAjax() {
44        global $INPUT;
45
46        // check minimum length
47        $lookup = trim($INPUT->str('search'));
48        if(utf8_strlen($lookup) < $this->config['autocomplete']['mininput']) return array();
49
50        // results wanted?
51        $max = $this->config['autocomplete']['maxresult'];
52        if($max <= 0) return array();
53
54        // lookup with namespace and postfix applied
55        $namespace = cleanID($this->config['namespace']);
56        $postfix = $this->config['postfix'];
57        if($namespace) $lookup .= ' @'.$namespace;
58
59        $data = ft_pageLookup($lookup, true, useHeading('navigation'));
60        if(!count($data)) return array();
61
62        // this basically duplicates what we do in ajax_qsearch()
63        $result = array();
64        $counter = 0;
65        foreach($data as $id => $title){
66            if (useHeading('navigation')) {
67                $name = $title;
68            } else {
69                $ns = getNS($id);
70                if($ns){
71                    $name = noNS($id).' ('.$ns.')';
72                }else{
73                    $name = $id;
74                }
75            }
76
77            // check suffix and remove
78            if($postfix) {
79                if(substr($id, -1 * strlen($postfix)) == $postfix) {
80                    $id = substr($id, 0, -1 * strlen($postfix));
81                } else {
82                    continue; // page does not end in postfix, don't suggest it
83                }
84            }
85
86            // remove namespace again
87            if($namespace) {
88                if(substr($id, 0, strlen($namespace) + 1) == "$namespace:") {
89                    $id = substr($id, strlen($namespace) + 1);
90                } else {
91                    continue; // this should usually not happen
92                }
93            }
94
95            $result[] = array(
96                'label' => $name,
97                'value' => $id
98            );
99
100            $counter ++;
101            if($counter > $max) break;
102        }
103
104        return $result;
105    }
106
107}
108