xref: /plugin/struct/meta/SearchCloud.php (revision 8925ba298f12cecb837c2c249b3a592d289eabe2)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5
6/**
7 * Class SearchCloud
8 *
9 * The same as @see SearchConfig, but executed a search that is not pid-focused
10 *
11 * @package dokuwiki\plugin\struct\meta
12 */
13class SearchCloud extends SearchConfig {
14
15    protected $limit = '';
16
17
18    /**
19     * Transform the set search parameters into a statement
20     *
21     * @return array ($sql, $opts) The SQL and parameters to execute
22     */
23    public function getSQL() {
24        if(!$this->columns) throw new StructException('nocolname');
25
26        $QB = new QueryBuilder();
27        reset($this->schemas);
28        $schema = current($this->schemas);
29        $datatable = 'data_' . $schema->getTable();
30        if(!$schema->isLookup()) {
31            $QB->addTable('schema_assignments');
32            $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid");
33            $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'");
34            $QB->filters()->whereAnd("schema_assignments.assigned = 1");
35            $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0");
36            $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1");
37        }
38        $QB->addTable($datatable);
39        $QB->filters()->whereAnd("$datatable.latest = 1");
40
41        $col = $this->columns[0];
42        if($col->isMulti()) {
43            $multitable = "multi_{$col->getTable()}";
44            $MN = $QB->generateTableAlias('M');
45
46            $QB->addLeftJoin(
47                $datatable,
48                $multitable,
49                $MN,
50                "$datatable.pid = $MN.pid AND
51                     $datatable.rev = $MN.rev AND
52                     $MN.colref = {$col->getColref()}"
53            );
54
55            $col->getType()->select($QB, $MN, 'value', 'tag');
56            $colname = $MN . '.value';
57        } else {
58            $col->getType()->select($QB, $datatable, $col->getColName(), 'tag');
59            $colname = $datatable . '.' . $col->getColName();
60        }
61        $QB->addSelectStatement("COUNT($colname)", 'count');
62        $QB->addGroupByStatement('tag');
63        $QB->addOrderBy('count DESC');
64
65        list($sql, $opts) = $QB->getSQL();
66        return [$sql . $this->limit, $opts];
67    }
68
69    /**
70     * We do not have pagination in clouds, so we can work with a limit within SQL
71     *
72     * @param int $limit
73     */
74    public function setLimit($limit) {
75        $this->limit = " LIMIT $limit";
76    }
77
78    /**
79     * Execute this search and return the result
80     *
81     * The result is a two dimensional array of Value()s.
82     *
83     * @return Value[][]
84     */
85    public function execute() {
86        list($sql, $opts) = $this->getSQL();
87
88        /** @var \PDOStatement $res */
89        $res = $this->sqlite->query($sql, $opts);
90        if($res === false) throw new StructException("SQL execution failed for\n\n$sql");
91
92        $result = [];
93        $rows = $this->sqlite->res2arr($res);
94
95        foreach ($rows as $row) {
96            if (!empty($this->config['min']) && $this->config['min'] > $row['count']) {
97                break;
98            }
99
100            $row['tag'] = new Value($this->columns[0], $row['tag']);
101            $result[] = $row;
102        }
103
104        $this->sqlite->res_close($res);
105        $this->count = count($result);
106        return $result;
107    }
108}
109