b2f3d2f379e46a312716f6da1e120b06bfa7bb88
[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 TitleArray
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 protected static function newFromResult_internal( $res ) {
31 $array = new TitleArrayFromResult( $res );
32 return $array;
33 }
34 }
35
36 class TitleArrayFromResult extends TitleArray {
37 var $res;
38 var $key, $current;
39
40 function __construct( $res ) {
41 $this->res = $res;
42 $this->key = 0;
43 $this->setCurrent( $this->res->current() );
44 }
45
46 protected function setCurrent( $row ) {
47 if ( $row === false ) {
48 $this->current = false;
49 } else {
50 $this->current = Title::newFromRow( $row );
51 }
52 }
53
54 function current() {
55 return $this->current;
56 }
57
58 function key() {
59 return $this->key;
60 }
61
62 function next() {
63 $row = $this->res->next();
64 $this->setCurrent( $row );
65 $this->key++;
66 }
67
68 function rewind() {
69 $this->res->rewind();
70 $this->key = 0;
71 $this->setCurrent( $this->res->current() );
72 }
73
74 function valid() {
75 return $this->current !== false;
76 }
77 }