File: RowCommentFormatter.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (88 lines) | stat: -rw-r--r-- 2,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace MediaWiki\CommentFormatter;

use MediaWiki\CommentStore\CommentStore;
use Traversable;
use Wikimedia\Rdbms\IResultWrapper;

/**
 * This is basically a CommentFormatter with a CommentStore dependency, allowing
 * it to retrieve comment texts directly from database result wrappers.
 *
 * @since 1.38
 */
class RowCommentFormatter extends CommentFormatter {
	/** @var CommentStore */
	private $commentStore;

	/**
	 * @internal Use MediaWikiServices::getRowCommentFormatter()
	 *
	 * @param CommentParserFactory $commentParserFactory
	 * @param CommentStore $commentStore
	 */
	public function __construct(
		CommentParserFactory $commentParserFactory,
		CommentStore $commentStore
	) {
		parent::__construct( $commentParserFactory );
		$this->commentStore = $commentStore;
	}

	/**
	 * Format DB rows using a fluent interface. Pass the return value of this
	 * function to CommentBatch::comments().
	 *
	 * Example:
	 *   $comments = $rowCommentFormatter->createBatch()
	 *       ->comments(
	 *           $rowCommentFormatter->rows( $rows )
	 *           ->commentField( 'img_comment' )
	 *       )
	 *       ->useBlock( true )
	 *       ->execute();
	 *
	 * @param Traversable|array $rows
	 * @return RowCommentIterator
	 */
	public function rows( $rows ) {
		return new RowCommentIterator( $this->commentStore, $rows );
	}

	/**
	 * Format DB rows using a parametric interface.
	 *
	 * @param iterable<\stdClass>|IResultWrapper $rows
	 * @param string $commentKey The comment key to pass through to CommentStore,
	 *   typically a legacy field name.
	 * @param string|null $namespaceField The namespace field for the self-link
	 *   target, or null to have no self-link target.
	 * @param string|null $titleField The title field for the self-link target,
	 *   or null to have no self-link target.
	 * @param string|null $indexField The field to use for array keys in the
	 *   result, or null to use the same keys as in the input $rows
	 * @param bool $useBlock Wrap the output in standard punctuation and
	 *   formatting if it's non-empty.
	 * @param bool $useParentheses Wrap the output with parentheses. Has no
	 *   effect if $useBlock is false.
	 * @return string[] The formatted comment. The key will be the value of the
	 *   index field if an index field was specified, or the key from the
	 *   corresponding element of $rows if no index field was specified.
	 */
	public function formatRows( $rows, $commentKey, $namespaceField = null, $titleField = null,
		$indexField = null, $useBlock = false, $useParentheses = true
	) {
		return $this->createBatch()
			->comments(
				$this->rows( $rows )
				->commentKey( $commentKey )
				->namespaceField( $namespaceField )
				->titleField( $titleField )
				->indexField( $indexField )
			)
			->useBlock( $useBlock )
			->useParentheses( $useParentheses )
			->execute();
	}
}