MDL-72203 curl: Improve redirect unit testing and update upgrade.txt
[moodle.git] / search / classes / skip_future_documents_iterator.php
blob6297fd80503ce985ecba217bf3ffc524254e1416
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Iterator for skipping search recordset documents that are in the future.
20 * @package core_search
21 * @copyright 2017 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_search;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Iterator for skipping search recordset documents that are in the future.
32 * This iterator stops iterating if it receives a document that was modified later than the
33 * specified cut-off time (usually current time).
35 * This iterator assumes that its parent iterator returns documents in modified order (which is
36 * required to be the case for search indexing). This means we will stop retrieving data from the
37 * recordset
39 * @copyright 2017 The Open University
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class skip_future_documents_iterator implements \Iterator {
43 /** @var \Iterator Parent iterator */
44 protected $parent;
46 /** @var int Cutoff time; anything later than this will cause the iterator to stop */
47 protected $cutoff;
49 /** @var mixed Current value of iterator */
50 protected $currentdoc;
52 /** @var bool True if current value is available */
53 protected $gotcurrent;
55 /**
56 * Constructor.
58 * @param \Iterator $parent Parent iterator, must return search documents in modified order
59 * @param int $cutoff Cut-off time, default is current time
61 public function __construct(\Iterator $parent, $cutoff = 0) {
62 $this->parent = $parent;
63 if ($cutoff) {
64 $this->cutoff = $cutoff;
65 } else {
66 $this->cutoff = time();
70 public function current() {
71 if (!$this->gotcurrent) {
72 $this->currentdoc = $this->parent->current();
73 $this->gotcurrent = true;
75 return $this->currentdoc;
78 public function next() {
79 $this->parent->next();
80 $this->gotcurrent = false;
83 public function key() {
84 return $this->parent->key();
87 public function valid() {
88 // Check that the parent is valid.
89 if (!$this->parent->valid()) {
90 return false;
93 if ($doc = $this->current()) {
94 // This document is valid if the modification date is before the cutoff.
95 return $doc->get('modified') <= $this->cutoff;
96 } else {
97 // If the document is false/null, allow iterator to continue.
98 return true;
102 public function rewind() {
103 $this->parent->rewind();
104 $this->gotcurrent = false;