| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\CommentFormatter; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use IteratorIterator; |
| 7 |
use MediaWiki\CommentStore\CommentStore; |
| 8 |
use MediaWiki\Title\TitleValue; |
| 9 |
use Traversable; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class RowCommentIterator extends IteratorIterator { |
| 25 |
|
| 26 |
private $commentStore; |
| 27 |
|
| 28 |
private $commentKey; |
| 29 |
|
| 30 |
private $namespaceField; |
| 31 |
|
| 32 |
private $titleField; |
| 33 |
|
| 34 |
private $indexField; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function __construct( CommentStore $commentStore, $rows ) { |
| 42 |
if ( is_array( $rows ) ) { |
| 43 |
parent::__construct( new ArrayIterator( $rows ) ); |
| 44 |
} else { |
| 45 |
parent::__construct( $rows ); |
| 46 |
} |
| 47 |
|
| 48 |
$this->commentStore = $commentStore; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function commentKey( $key ) { |
| 60 |
$this->commentKey = $key; |
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function namespaceField( $field ) { |
| 72 |
$this->namespaceField = $field; |
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function titleField( $field ) { |
| 84 |
$this->titleField = $field; |
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function indexField( $field ) { |
| 97 |
$this->indexField = $field; |
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
public function key(): string { |
| 102 |
if ( $this->indexField ) { |
| 103 |
return parent::current()->{$this->indexField}; |
| 104 |
} else { |
| 105 |
return parent::key(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function current(): CommentItem { |
| 110 |
if ( $this->commentKey === null ) { |
| 111 |
throw new \RuntimeException( __METHOD__ . ': commentKey must be specified' ); |
| 112 |
} |
| 113 |
$row = parent::current(); |
| 114 |
$comment = $this->commentStore->getComment( $this->commentKey, $row ); |
| 115 |
$item = new CommentItem( (string)$comment->text ); |
| 116 |
if ( $this->namespaceField && $this->titleField ) { |
| 117 |
$item->selfLinkTarget( new TitleValue( |
| 118 |
(int)$row->{$this->namespaceField}, |
| 119 |
(string)$row->{$this->titleField} |
| 120 |
) ); |
| 121 |
} |
| 122 |
return $item; |
| 123 |
} |
| 124 |
} |