Merge "prop=duplicatefiles does not show duplicates under same name"
[lhc/web/wiklou.git] / includes / TitleArray.php
1 <?php
2 /**
3 * Classes to walk into a list of Title objects.
4 *
5 * Note: this entire file is a byte-for-byte copy of UserArray.php with
6 * s/User/Title/. If anyone can figure out how to do this nicely with inheri-
7 * tance 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 /**
28 * The TitleArray class only exists to provide the newFromResult method at pre-
29 * sent.
30 */
31 abstract class TitleArray implements Iterator {
32 /**
33 * @param $res ResultWrapper A SQL result including at least page_namespace and
34 * page_title -- also can have page_id, page_len, page_is_redirect,
35 * page_latest (if those will be used). See Title::newFromRow.
36 * @return TitleArrayFromResult
37 */
38 static function newFromResult( $res ) {
39 $array = null;
40 if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
41 return null;
42 }
43 if ( $array === null ) {
44 $array = self::newFromResult_internal( $res );
45 }
46 return $array;
47 }
48
49 /**
50 * @param $res ResultWrapper
51 * @return TitleArrayFromResult
52 */
53 protected static function newFromResult_internal( $res ) {
54 $array = new TitleArrayFromResult( $res );
55 return $array;
56 }
57 }
58
59 class TitleArrayFromResult extends TitleArray {
60
61 /**
62 * @var ResultWrapper
63 */
64 var $res;
65 var $key, $current;
66
67 function __construct( $res ) {
68 $this->res = $res;
69 $this->key = 0;
70 $this->setCurrent( $this->res->current() );
71 }
72
73 /**
74 * @param $row ResultWrapper
75 * @return void
76 */
77 protected function setCurrent( $row ) {
78 if ( $row === false ) {
79 $this->current = false;
80 } else {
81 $this->current = Title::newFromRow( $row );
82 }
83 }
84
85 /**
86 * @return int
87 */
88 public function count() {
89 return $this->res->numRows();
90 }
91
92 function current() {
93 return $this->current;
94 }
95
96 function key() {
97 return $this->key;
98 }
99
100 function next() {
101 $row = $this->res->next();
102 $this->setCurrent( $row );
103 $this->key++;
104 }
105
106 function rewind() {
107 $this->res->rewind();
108 $this->key = 0;
109 $this->setCurrent( $this->res->current() );
110 }
111
112 /**
113 * @return bool
114 */
115 function valid() {
116 return $this->current !== false;
117 }
118 }