xref: /plugin/struct/meta/SearchResult.php (revision 04c3c31f68be5f13c27465e956dba40838a1dc77)
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 \PDOStatemen $res
26     * @param int $rangeBegin
27     * @param int $rangeEnd
28     * @param array $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->addPid($row['PID']);
59            $this->addRid($row['rid']);
60            $this->addRev($row['rev']);
61            $this->addRow($resrow);
62        }
63
64        $res->closeCursor();
65        $this->increaseCount();
66    }
67
68    /**
69     * @return array
70     */
71    public function getPids(): array
72    {
73        return $this->pids;
74    }
75
76    /**
77     * @return Value[][]
78     */
79    public function getRows()
80    {
81        return $this->rows;
82    }
83
84    /**
85     * @return array
86     */
87    public function getRids(): array
88    {
89        return $this->rids;
90    }
91
92    /**
93     * @return int
94     */
95    public function getCount(): int
96    {
97        return $this->count;
98    }
99
100    /**
101     * @return array
102     */
103    public function getRevs(): array
104    {
105        return $this->revs;
106    }
107
108    /**
109     * @param string $pid
110     * @return void
111     */
112    public function addPid($pid)
113    {
114        $this->pids[] = $pid;
115    }
116
117    /**
118     * @param int $rid
119     * @return void
120     */
121    public function addRid($rid)
122    {
123        $this->rids[] = $rid;
124    }
125
126    /**
127     * @param int $rev
128     * @return void
129     */
130    public function addRev($rev)
131    {
132        $this->revs[] = $rev;
133    }
134
135    /**
136     * @param array $result
137     * @return void
138     */
139    public function addRow($row)
140    {
141        $this->rows[] = $row;
142    }
143
144    /**
145     * @return void
146     */
147    public function increaseCount()
148    {
149        $this->count++;
150    }
151/**
152     * @return void
153     */
154    public function decreaseCount()
155    {
156        $this->count--;
157    }
158
159    /**
160     * Check if the given row is empty or references our own row
161     *
162     * @param Value $value
163     * @return bool
164     */
165    protected function isEmptyValue(Value $value)
166    {
167        if ($value->isEmpty()) return true;
168        if ($value->getColumn()->getTid() == 0) return true;
169        return false;
170    }
171}
172