Type hinting
[lhc/web/wiklou.git] / includes / TitleArray.php
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 */
7
8 /**
9 * The TitleArray class only exists to provide the newFromResult method at pre-
10 * sent.
11 */
12 abstract class TitleArray implements Iterator {
13 /**
14 * @param $res result A MySQL 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
18 */
19 static function newFromResult( $res ) {
20 $array = null;
21 if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
22 return null;
23 }
24 if ( $array === null ) {
25 $array = self::newFromResult_internal( $res );
26 }
27 return $array;
28 }
29
30 /**
31 * @param $res
32 * @return TitleArrayFromResult
33 */
34 protected static function newFromResult_internal( $res ) {
35 $array = new TitleArrayFromResult( $res );
36 return $array;
37 }
38 }
39
40 class TitleArrayFromResult extends TitleArray {
41 var $res;
42 var $key, $current;
43
44 function __construct( $res ) {
45 $this->res = $res;
46 $this->key = 0;
47 $this->setCurrent( $this->res->current() );
48 }
49
50 protected function setCurrent( $row ) {
51 if ( $row === false ) {
52 $this->current = false;
53 } else {
54 $this->current = Title::newFromRow( $row );
55 }
56 }
57
58 public function count() {
59 return $this->res->numRows();
60 }
61
62 function current() {
63 return $this->current;
64 }
65
66 function key() {
67 return $this->key;
68 }
69
70 function next() {
71 $row = $this->res->next();
72 $this->setCurrent( $row );
73 $this->key++;
74 }
75
76 function rewind() {
77 $this->res->rewind();
78 $this->key = 0;
79 $this->setCurrent( $this->res->current() );
80 }
81
82 function valid() {
83 return $this->current !== false;
84 }
85 }