TitleArray.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * The TitleArray class only exists to provide the newFromResult method at pre-
  9. * sent.
  10. */
  11. abstract class TitleArray implements Iterator {
  12. /**
  13. * @param $res result A MySQL result including at least page_namespace and
  14. * page_title -- also can have page_id, page_len, page_is_redirect,
  15. * page_latest (if those will be used). See Title::newFromRow.
  16. * @return TitleArray
  17. */
  18. static function newFromResult( $res ) {
  19. $array = null;
  20. if ( !wfRunHooks( 'TitleArrayFromResult', array( &$array, $res ) ) ) {
  21. return null;
  22. }
  23. if ( $array === null ) {
  24. $array = self::newFromResult_internal( $res );
  25. }
  26. return $array;
  27. }
  28. protected static function newFromResult_internal( $res ) {
  29. $array = new TitleArrayFromResult( $res );
  30. return $array;
  31. }
  32. }
  33. class TitleArrayFromResult extends TitleArray {
  34. var $res;
  35. var $key, $current;
  36. function __construct( $res ) {
  37. $this->res = $res;
  38. $this->key = 0;
  39. $this->setCurrent( $this->res->current() );
  40. }
  41. protected function setCurrent( $row ) {
  42. if ( $row === false ) {
  43. $this->current = false;
  44. } else {
  45. $this->current = Title::newFromRow( $row );
  46. }
  47. }
  48. public function count() {
  49. return $this->res->numRows();
  50. }
  51. function current() {
  52. return $this->current;
  53. }
  54. function key() {
  55. return $this->key;
  56. }
  57. function next() {
  58. $row = $this->res->next();
  59. $this->setCurrent( $row );
  60. $this->key++;
  61. }
  62. function rewind() {
  63. $this->res->rewind();
  64. $this->key = 0;
  65. $this->setCurrent( $this->res->current() );
  66. }
  67. function valid() {
  68. return $this->current !== false;
  69. }
  70. }