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