1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class SearchResult
7 *
8 * Search is executed only once per request.
9 */
10class SearchResult
11{
12    /** @var Value[][] */
13    protected $rows = [];
14    /** @var array */
15    protected $pids = [];
16    protected $rids = [];
17    /** @var array */
18    protected $revs = [];
19    /** @var int */
20    protected $count = -1;
21
22    /**
23     * Construct SearchResult
24     *
25     * @param \PDOStatement $res PDO statement containing the search result
26     * @param int $rangeBegin Begin of requested result range
27     * @param int $rangeEnd End of requested result range
28     * @param Column[] $columns Search columns
29     * @param bool $pageidAndRevOnly
30     */
31    public function __construct($res, $rangeBegin, $rangeEnd, $columns, $pageidAndRevOnly)
32    {
33        while ($row = $res->fetch(\PDO::FETCH_ASSOC)) {
34            $this->increaseCount();
35            if ($this->getCount() < $rangeBegin) continue;
36            if ($rangeEnd && $this->getCount() >= $rangeEnd) continue;
37
38            $C = 0;
39            $resrow = [];
40            $isempty = true;
41            foreach ($columns as $col) {
42                $val = $row["C$C"];
43                if ($col->isMulti()) {
44                    $val = explode(Search::CONCAT_SEPARATOR, $val);
45                }
46                $value = new Value($col, $val);
47                $isempty &= $this->isEmptyValue($value);
48                $resrow[] = $value;
49                $C++;
50            }
51
52            // skip empty rows
53            if ($isempty && !$pageidAndRevOnly) {
54                $this->decreaseCount();
55                continue;
56            }
57
58            $this->pids[] = $row['PID'];
59            $this->rids[] = $row['rid'];
60            $this->revs[] = $row['rev'];
61            $this->rows[] = $resrow;
62        }
63
64        $this->increaseCount();
65    }
66
67    /**
68     * @return array
69     */
70    public function getPids(): array
71    {
72        return $this->pids;
73    }
74
75    /**
76     * @return Value[][]
77     */
78    public function getRows()
79    {
80        return $this->rows;
81    }
82
83    /**
84     * @return array
85     */
86    public function getRids(): array
87    {
88        return $this->rids;
89    }
90
91    /**
92     * @return int
93     */
94    public function getCount(): int
95    {
96        return $this->count;
97    }
98
99    /**
100     * @return array
101     */
102    public function getRevs(): array
103    {
104        return $this->revs;
105    }
106
107    /**
108     * @return void
109     */
110    public function increaseCount()
111    {
112        $this->count++;
113    }
114/**
115     * @return void
116     */
117    public function decreaseCount()
118    {
119        $this->count--;
120    }
121
122    /**
123     * Check if the given row is empty or references our own row
124     *
125     * @param Value $value
126     * @return bool
127     */
128    protected function isEmptyValue(Value $value)
129    {
130        if ($value->isEmpty()) return true;
131        if ($value->getColumn()->getTid() == 0) return true;
132        return false;
133    }
134}
135