Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / revisionlist / RevisionListBase.php
1 <?php
2 /**
3 * Holders of revision list for a single page
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use Wikimedia\Rdbms\ResultWrapper;
24 use Wikimedia\Rdbms\IDatabase;
25
26 /**
27 * List for revision table items for a single page
28 */
29 abstract class RevisionListBase extends ContextSource implements Iterator {
30 /** @var Title */
31 public $title;
32
33 /** @var array */
34 protected $ids;
35
36 /** @var ResultWrapper|bool */
37 protected $res;
38
39 /** @var bool|Revision */
40 protected $current;
41
42 /**
43 * Construct a revision list for a given title
44 * @param IContextSource $context
45 * @param Title $title
46 */
47 function __construct( IContextSource $context, Title $title ) {
48 $this->setContext( $context );
49 $this->title = $title;
50 }
51
52 /**
53 * Select items only where the ID is any of the specified values
54 * @param array $ids
55 */
56 function filterByIds( array $ids ) {
57 $this->ids = $ids;
58 }
59
60 /**
61 * Get the internal type name of this list. Equal to the table name.
62 * Override this function.
63 * @return null
64 */
65 public function getType() {
66 return null;
67 }
68
69 /**
70 * Initialise the current iteration pointer
71 */
72 protected function initCurrent() {
73 $row = $this->res->current();
74 if ( $row ) {
75 $this->current = $this->newItem( $row );
76 } else {
77 $this->current = false;
78 }
79 }
80
81 /**
82 * Start iteration. This must be called before current() or next().
83 * @return Revision First list item
84 */
85 public function reset() {
86 if ( !$this->res ) {
87 $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
88 } else {
89 $this->res->rewind();
90 }
91 $this->initCurrent();
92 return $this->current;
93 }
94
95 public function rewind() {
96 $this->reset();
97 }
98
99 /**
100 * Get the current list item, or false if we are at the end
101 * @return Revision
102 */
103 public function current() {
104 return $this->current;
105 }
106
107 /**
108 * Move the iteration pointer to the next list item, and return it.
109 * @return Revision
110 * @suppress PhanParamSignatureMismatchInternal
111 */
112 public function next() {
113 $this->res->next();
114 $this->initCurrent();
115 return $this->current;
116 }
117
118 public function key() {
119 return $this->res ? $this->res->key() : 0;
120 }
121
122 public function valid() {
123 return $this->res ? $this->res->valid() : false;
124 }
125
126 /**
127 * Get the number of items in the list.
128 * @return int
129 */
130 public function length() {
131 if ( !$this->res ) {
132 return 0;
133 } else {
134 return $this->res->numRows();
135 }
136 }
137
138 /**
139 * Do the DB query to iterate through the objects.
140 * @param IDatabase $db DB object to use for the query
141 */
142 abstract public function doQuery( $db );
143
144 /**
145 * Create an item object from a DB result row
146 * @param object $row
147 */
148 abstract public function newItem( $row );
149 }