Followup to r86053 - fix special page cases
[mediawiki.git] / includes / TitleArray.php
blob969600897c5b911be38692d527aca00836cf8ff0
1 <?php
2 /**
3 * Note: this entire file is a byte-for-byte copy of UserArray.php with
4 * s/User/Title/. If anyone can figure out how to do this nicely with inheri-
5 * tance or something, please do so.
6 */
8 /**
9 * The TitleArray class only exists to provide the newFromResult method at pre-
10 * sent.
12 abstract class TitleArray implements Iterator {
13 /**
14 * @param $res ResultWrapper A SQL result including at least page_namespace and
15 * page_title -- also can have page_id, page_len, page_is_redirect,
16 * page_latest (if those will be used). See Title::newFromRow.
17 * @return TitleArrayFromResult
19 static function newFromResult( $res ) {
20 $array = null;
21 if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
22 return null;
24 if ( $array === null ) {
25 $array = self::newFromResult_internal( $res );
27 return $array;
30 /**
31 * @param $res ResultWrapper
32 * @return TitleArrayFromResult
34 protected static function newFromResult_internal( $res ) {
35 $array = new TitleArrayFromResult( $res );
36 return $array;
40 class TitleArrayFromResult extends TitleArray {
42 /**
43 * @var ResultWrapper
45 var $res;
46 var $key, $current;
48 function __construct( $res ) {
49 $this->res = $res;
50 $this->key = 0;
51 $this->setCurrent( $this->res->current() );
54 /**
55 * @param $row ResultWrapper
56 * @return void
58 protected function setCurrent( $row ) {
59 if ( $row === false ) {
60 $this->current = false;
61 } else {
62 $this->current = Title::newFromRow( $row );
66 /**
67 * @return int
69 public function count() {
70 return $this->res->numRows();
73 function current() {
74 return $this->current;
77 function key() {
78 return $this->key;
81 function next() {
82 $row = $this->res->next();
83 $this->setCurrent( $row );
84 $this->key++;
87 function rewind() {
88 $this->res->rewind();
89 $this->key = 0;
90 $this->setCurrent( $this->res->current() );
93 /**
94 * @return bool
96 function valid() {
97 return $this->current !== false;