Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RowCommentFormatter
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rows
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatRows
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\CommentFormatter;
4
5use MediaWiki\CommentStore\CommentStore;
6use Traversable;
7use Wikimedia\Rdbms\IResultWrapper;
8
9/**
10 * This is basically a CommentFormatter with a CommentStore dependency, allowing
11 * it to retrieve comment texts directly from database result wrappers.
12 *
13 * @since 1.38
14 */
15class RowCommentFormatter extends CommentFormatter {
16    /** @var CommentStore */
17    private $commentStore;
18
19    /**
20     * @internal Use MediaWikiServices::getRowCommentFormatter()
21     *
22     * @param CommentParserFactory $commentParserFactory
23     * @param CommentStore $commentStore
24     */
25    public function __construct(
26        CommentParserFactory $commentParserFactory,
27        CommentStore $commentStore
28    ) {
29        parent::__construct( $commentParserFactory );
30        $this->commentStore = $commentStore;
31    }
32
33    /**
34     * Format DB rows using a fluent interface. Pass the return value of this
35     * function to CommentBatch::comments().
36     *
37     * Example:
38     *   $comments = $rowCommentFormatter->createBatch()
39     *       ->comments(
40     *           $rowCommentFormatter->rows( $rows )
41     *           ->commentField( 'img_comment' )
42     *       )
43     *       ->useBlock( true )
44     *       ->execute();
45     *
46     * @param Traversable|array $rows
47     * @return RowCommentIterator
48     */
49    public function rows( $rows ) {
50        return new RowCommentIterator( $this->commentStore, $rows );
51    }
52
53    /**
54     * Format DB rows using a parametric interface.
55     *
56     * @param iterable<\stdClass>|IResultWrapper $rows
57     * @param string $commentKey The comment key to pass through to CommentStore,
58     *   typically a legacy field name.
59     * @param string|null $namespaceField The namespace field for the self-link
60     *   target, or null to have no self-link target.
61     * @param string|null $titleField The title field for the self-link target,
62     *   or null to have no self-link target.
63     * @param string|null $indexField The field to use for array keys in the
64     *   result, or null to use the same keys as in the input $rows
65     * @param bool $useBlock Wrap the output in standard punctuation and
66     *   formatting if it's non-empty.
67     * @param bool $useParentheses Wrap the output with parentheses. Has no
68     *   effect if $useBlock is false.
69     * @return string[] The formatted comment. The key will be the value of the
70     *   index field if an index field was specified, or the key from the
71     *   corresponding element of $rows if no index field was specified.
72     */
73    public function formatRows( $rows, $commentKey, $namespaceField = null, $titleField = null,
74        $indexField = null, $useBlock = false, $useParentheses = true
75    ) {
76        return $this->createBatch()
77            ->comments(
78                $this->rows( $rows )
79                ->commentKey( $commentKey )
80                ->namespaceField( $namespaceField )
81                ->titleField( $titleField )
82                ->indexField( $indexField )
83            )
84            ->useBlock( $useBlock )
85            ->useParentheses( $useParentheses )
86            ->execute();
87    }
88}