wiki.techinc.nl/includes/CommentFormatter/RowCommentFormatter.php
Tim Starling f7f84dddb3 Introduce CommentFormatter
CommentParser:

* Move comment formatting backend from Linker to a CommentParser service.
  Allow link existence and file existence to be batched.
* Rename $local to $samePage since I think that is clearer.
* Rename $title to $selfLinkTarget since it was unclear what the title
  was used for.
* Rename the "autocomment" concept to "section link" in public
  interfaces, although the old term remains in CSS classes.
* Keep unsafe HTML pass-through in separate "unsafe" methods, for easier
  static analysis and code review.

CommentFormatter:

* Add CommentFormatter and RowCommentFormatter services as a usable
  frontend for comment batches, and to replace the Linker static methods.
* Provide fluent and parametric interfaces.

Linker:

* Remove Linker::makeCommentLink() without deprecation -- nothing calls
  it and it is obviously an internal helper.
* Soft-deprecate Linker methods formatComment(), formatLinksInComment(),
  commentBlock() and revComment().

Caller migration:

* CommentFormatter single: Linker, RollbackAction, ApiComparePages,
  ApiParse
* CommentFormatter parametric batch: ImageHistoryPseudoPager
* CommentFormatter fluent batch: ApiQueryFilearchive
* RowCommentFormatter sequential: History feed, BlocklistPager,
  ProtectedPagesPager, ApiQueryProtectedTitles
* RowCommentFormatter with index: ChangesFeed, ChangesList,
  ApiQueryDeletedrevs, ApiQueryLogEvents, ApiQueryRecentChanges
* RevisionCommentBatch: HistoryPager, ContribsPager

Bug: T285917
Change-Id: Ia3fd50a4a13138ba5003d884962da24746d562d0
2021-09-28 11:13:03 -07:00

88 lines
2.8 KiB
PHP

<?php
namespace MediaWiki\CommentFormatter;
use 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();
}
}