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