Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Iterator for skipping search recordset documents that are in the future.
  19   *
  20   * @package core_search
  21   * @copyright 2017 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Iterator for skipping search recordset documents that are in the future.
  31   *
  32   * This iterator stops iterating if it receives a document that was modified later than the
  33   * specified cut-off time (usually current time).
  34   *
  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
  38   *
  39   * @copyright 2017 The Open University
  40   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class skip_future_documents_iterator implements \Iterator {
  43      /** @var \Iterator Parent iterator */
  44      protected $parent;
  45  
  46      /** @var int Cutoff time; anything later than this will cause the iterator to stop */
  47      protected $cutoff;
  48  
  49      /** @var mixed Current value of iterator */
  50      protected $currentdoc;
  51  
  52      /** @var bool True if current value is available */
  53      protected $gotcurrent;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param \Iterator $parent Parent iterator, must return search documents in modified order
  59       * @param int $cutoff Cut-off time, default is current time
  60       */
  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();
  67          }
  68      }
  69  
  70      public function current() {
  71          if (!$this->gotcurrent) {
  72              $this->currentdoc = $this->parent->current();
  73              $this->gotcurrent = true;
  74          }
  75          return $this->currentdoc;
  76      }
  77  
  78      public function next() {
  79          $this->parent->next();
  80          $this->gotcurrent = false;
  81      }
  82  
  83      public function key() {
  84          return $this->parent->key();
  85      }
  86  
  87      public function valid() {
  88          // Check that the parent is valid.
  89          if (!$this->parent->valid()) {
  90              return false;
  91          }
  92  
  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;
  99          }
 100      }
 101  
 102      public function rewind() {
 103          $this->parent->rewind();
 104          $this->gotcurrent = false;
 105      }
 106  }