Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
63.16% covered (warning)
63.16%
24 / 38
38.46% covered (danger)
38.46%
5 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevisionListBase
64.86% covered (warning)
64.86%
24 / 37
38.46% covered (danger)
38.46%
5 / 13
32.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
1.00
 getPage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 filterByIds
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initCurrent
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 reset
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
2.31
 rewind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 current
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 next
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 valid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 length
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 doQuery
n/a
0 / 0
n/a
0 / 0
0
 newItem
n/a
0 / 0
n/a
0 / 0
0
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
23namespace MediaWiki\RevisionList;
24
25use Iterator;
26use MediaWiki\Context\ContextSource;
27use MediaWiki\Context\IContextSource;
28use MediaWiki\Debug\DeprecationHelper;
29use MediaWiki\MediaWikiServices;
30use MediaWiki\Page\PageIdentity;
31use MediaWiki\Title\Title;
32use stdClass;
33use Wikimedia\Rdbms\IReadableDatabase;
34use Wikimedia\Rdbms\IResultWrapper;
35
36/**
37 * List for revision table items for a single page
38 */
39abstract class RevisionListBase extends ContextSource implements Iterator {
40    use DeprecationHelper;
41
42    /** @var PageIdentity */
43    protected $page;
44
45    /** @var int[]|null */
46    protected $ids;
47
48    /** @var IResultWrapper|false */
49    protected $res;
50
51    /** @var RevisionItemBase|false */
52    protected $current;
53
54    /**
55     * Construct a revision list for a given page identity
56     * @param IContextSource $context
57     * @param PageIdentity $page
58     */
59    public function __construct( IContextSource $context, PageIdentity $page ) {
60        $this->setContext( $context );
61        $this->page = $page;
62
63        $this->deprecatePublicPropertyFallback(
64            'title',
65            '1.37',
66            function (): Title {
67                return Title::newFromPageIdentity( $this->page );
68            },
69            function ( PageIdentity $page ) {
70                $this->page = $page;
71            }
72        );
73    }
74
75    public function getPage(): PageIdentity {
76        return $this->page;
77    }
78
79    /**
80     * @internal for use by RevDelItems
81     * @return string
82     */
83    public function getPageName(): string {
84        return Title::newFromPageIdentity( $this->page )->getPrefixedText();
85    }
86
87    /**
88     * Select items only where the ID is any of the specified values
89     * @param int[] $ids
90     */
91    public function filterByIds( array $ids ) {
92        $this->ids = $ids;
93    }
94
95    /**
96     * Get the internal type name of this list. Equal to the table name.
97     * Override this function.
98     * @return string|null
99     */
100    public function getType() {
101        return null;
102    }
103
104    /**
105     * Initialise the current iteration pointer
106     */
107    protected function initCurrent() {
108        $row = $this->res->current();
109        if ( $row ) {
110            $this->current = $this->newItem( $row );
111        } else {
112            $this->current = false;
113        }
114    }
115
116    /**
117     * Start iteration. This must be called before current() or next().
118     * @return RevisionItemBase First list item
119     */
120    public function reset() {
121        if ( !$this->res ) {
122            $this->res = $this->doQuery(
123                MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase()
124            );
125        } else {
126            $this->res->rewind();
127        }
128        $this->initCurrent();
129        return $this->current;
130    }
131
132    public function rewind(): void {
133        $this->reset();
134    }
135
136    /**
137     * Get the current list item, or false if we are at the end
138     * @return RevisionItemBase|false
139     */
140    #[\ReturnTypeWillChange]
141    public function current() {
142        return $this->current;
143    }
144
145    /**
146     * Move the iteration pointer to the next list item, and return it.
147     * @return RevisionItemBase
148     * @suppress PhanParamSignatureMismatchInternal
149     */
150    #[\ReturnTypeWillChange]
151    public function next() {
152        $this->res->next();
153        $this->initCurrent();
154        return $this->current;
155    }
156
157    public function key(): int {
158        return $this->res ? $this->res->key() : 0;
159    }
160
161    public function valid(): bool {
162        return $this->res && $this->res->valid();
163    }
164
165    /**
166     * Get the number of items in the list.
167     * @return int
168     */
169    public function length() {
170        if ( !$this->res ) {
171            return 0;
172        } else {
173            return $this->res->numRows();
174        }
175    }
176
177    /**
178     * Do the DB query to iterate through the objects.
179     * @param IReadableDatabase $db DB object to use for the query
180     * @return IResultWrapper
181     */
182    abstract public function doQuery( $db );
183
184    /**
185     * Create an item object from a DB result row
186     * @param stdClass $row
187     * @return RevisionItemBase
188     */
189    abstract public function newItem( $row );
190}
191/** @deprecated class alias since 1.43 */
192class_alias( RevisionListBase::class, 'RevisionListBase' );