]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/TitleArrayFromResult.php
MediaWiki 1.30.2-scripts2
[autoinstallsdev/mediawiki.git] / includes / TitleArrayFromResult.php
1 <?php
2 /**
3  * Class to walk into a list of Title objects.
4  *
5  * Note: this entire file is a byte-for-byte copy of UserArrayFromResult.php
6  * with s/User/Title/.  If anyone can figure out how to do this nicely
7  * with inheritance or something, please do so.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 use Wikimedia\Rdbms\ResultWrapper;
28
29 class TitleArrayFromResult extends TitleArray implements Countable {
30         /** @var ResultWrapper */
31         public $res;
32
33         public $key;
34
35         public $current;
36
37         function __construct( $res ) {
38                 $this->res = $res;
39                 $this->key = 0;
40                 $this->setCurrent( $this->res->current() );
41         }
42
43         /**
44          * @param bool|ResultWrapper $row
45          * @return void
46          */
47         protected function setCurrent( $row ) {
48                 if ( $row === false ) {
49                         $this->current = false;
50                 } else {
51                         $this->current = Title::newFromRow( $row );
52                 }
53         }
54
55         /**
56          * @return int
57          */
58         public function count() {
59                 return $this->res->numRows();
60         }
61
62         function current() {
63                 return $this->current;
64         }
65
66         function key() {
67                 return $this->key;
68         }
69
70         function next() {
71                 $row = $this->res->next();
72                 $this->setCurrent( $row );
73                 $this->key++;
74         }
75
76         function rewind() {
77                 $this->res->rewind();
78                 $this->key = 0;
79                 $this->setCurrent( $this->res->current() );
80         }
81
82         /**
83          * @return bool
84          */
85         function valid() {
86                 return $this->current !== false;
87         }
88 }