Debian Codegraph @

includes/CommentFormatter/RowCommentIterator.php

raw symbols SCIP
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 * An adaptor which converts a row iterator into a CommentItem iterator for
13 * batch formatting.
14 *
15 * Fluent-style mutators are provided to configure how comment text is extracted
16 * from rows.
17 *
18 * Using an iterator for this configuration, instead of putting the
19 * options in CommentBatch, allows CommentBatch to be a simple single
20 * class without a CommentStore dependency.
21 *
22 * @since 1.38
23 */
24 class RowCommentIterator extends IteratorIterator {
25 /** @var CommentStore */
26 private $commentStore;
27 /** @var string|null */
28 private $commentKey;
29 /** @var string|null */
30 private $namespaceField;
31 /** @var string|null */
32 private $titleField;
33 /** @var string|null */
34 private $indexField;
35
36 /**
37 * @internal Use RowCommentFormatter::rows()
38 * @param CommentStore $commentStore
39 * @param Traversable|array $rows
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 * Set what CommentStore calls the key -- typically a legacy field name
53 * which once held a comment. This must be called before attempting
54 * iteration.
55 *
56 * @param string $key
57 * @return $this
58 */
59 public function commentKey( $key ) {
60 $this->commentKey = $key;
61 return $this;
62 }
63
64 /**
65 * Set the namespace field. If this is not called, the item will not have
66 * a self-link target, although it may be provided by the batch.
67 *
68 * @param string $field
69 * @return $this
70 */
71 public function namespaceField( $field ) {
72 $this->namespaceField = $field;
73 return $this;
74 }
75
76 /**
77 * Set the title field. If this is not called, the item will not have
78 * a self-link target, although it may be provided by the batch.
79 *
80 * @param string $field
81 * @return $this
82 */
83 public function titleField( $field ) {
84 $this->titleField = $field;
85 return $this;
86 }
87
88 /**
89 * Set the index field. Values from this field will appear as array keys
90 * in the final formatted comment array. If unset, the array will be
91 * numerically indexed.
92 *
93 * @param string $field
94 * @return $this
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 }