MediaWiki  master
MediaWiki\CommentFormatter\CommentParser Class Reference

The text processing backend for CommentFormatter. More...

Collaboration diagram for MediaWiki\CommentFormatter\CommentParser:

Public Member Functions

 __construct (LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, LinkCache $linkCache, RepoGroup $repoGroup, Language $userLang, Language $contLang, TitleParser $titleParser, NamespaceInfo $namespaceInfo, HookContainer $hookContainer)
 
 finalize ( $comments)
 Execute pending batch queries and replace markers in the specified string(s) with actual links. More...
 
 preprocess (string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $enableSectionLinks=true)
 Convert a comment to HTML, but replace links with markers which are resolved later. More...
 
 preprocessUnsafe ( $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $enableSectionLinks=true)
 Convert a comment in pseudo-HTML format to HTML, replacing links with markers. More...
 

Private Member Functions

 addFileLink (LinkTarget $target, $html)
 Link to a file, returning a marker. More...
 
 addLinkMarker ( $callback)
 Add a deferred link to the list and return its marker. More...
 
 addPageLink (LinkTarget $target, $text, $wikiId)
 Link to a LinkTarget. More...
 
 doSectionLinks ( $comment, $selfLinkTarget=null, $samePage=false, $wikiId=false)
 Converts C-style comments in edit summaries into section links. More...
 
 doWikiLinks ( $comment, $selfLinkTarget=null, $samePage=false, $wikiId=false)
 Formats wiki links and media links in text; all other wiki formatting is ignored. More...
 
 flushLinkBatches ()
 Execute any pending link batch or file batch. More...
 
 makeSectionLink (LinkTarget $target, $text, $wikiId)
 Make a section link. More...
 
 preprocessInternal ( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId, $enableSectionLinks)
 

Private Attributes

Language $contLang
 
array $fileBatch
 Input to RepoGroup::findFiles() More...
 
File[] $files = []
 Resolved File objects indexed by DB key. More...
 
HookRunner $hookRunner
 
LinkBatch null $linkBatch
 
LinkBatchFactory $linkBatchFactory
 
LinkCache $linkCache
 
LinkRenderer $linkRenderer
 
callable[] $links = []
 
NamespaceInfo $namespaceInfo
 
RepoGroup $repoGroup
 
TitleParser $titleParser
 
Language $userLang
 

Detailed Description

The text processing backend for CommentFormatter.

CommentParser objects should be discarded after the comment batch is complete, in order to reduce memory usage.

Access: internal

Definition at line 32 of file CommentParser.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\CommentFormatter\CommentParser::__construct ( LinkRenderer  $linkRenderer,
LinkBatchFactory  $linkBatchFactory,
LinkCache  $linkCache,
RepoGroup  $repoGroup,
Language  $userLang,
Language  $contLang,
TitleParser  $titleParser,
NamespaceInfo  $namespaceInfo,
HookContainer  $hookContainer 
)

Member Function Documentation

◆ addFileLink()

MediaWiki\CommentFormatter\CommentParser::addFileLink ( LinkTarget  $target,
  $html 
)
private

Link to a file, returning a marker.

Parameters
LinkTarget$targetThe name of the file.
string$htmlThe inner HTML of the link
Returns
string

Definition at line 502 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\addLinkMarker(), MediaWiki\Linker\LinkTarget\getDBkey(), and Linker\makeMediaLinkFile().

Referenced by MediaWiki\CommentFormatter\CommentParser\doWikiLinks().

◆ addLinkMarker()

MediaWiki\CommentFormatter\CommentParser::addLinkMarker (   $callback)
private

Add a deferred link to the list and return its marker.

Parameters
callable$callback
Returns
string

Definition at line 441 of file CommentParser.php.

Referenced by MediaWiki\CommentFormatter\CommentParser\addFileLink(), and MediaWiki\CommentFormatter\CommentParser\addPageLink().

◆ addPageLink()

MediaWiki\CommentFormatter\CommentParser::addPageLink ( LinkTarget  $target,
  $text,
  $wikiId 
)
private

Link to a LinkTarget.

Return either HTML or a marker depending on whether existence checks are deferred.

Parameters
LinkTarget$target
string$text
string | false | null$wikiId
Returns
string

Definition at line 459 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\addLinkMarker(), MediaWiki\Linker\LinkTarget\getDBkey(), WikiMap\getForeignURL(), MediaWiki\Linker\LinkTarget\getFragment(), MediaWiki\Linker\LinkTarget\getNamespace(), MediaWiki\Linker\LinkTarget\isExternal(), and Linker\makeExternalLink().

Referenced by MediaWiki\CommentFormatter\CommentParser\doWikiLinks().

◆ doSectionLinks()

MediaWiki\CommentFormatter\CommentParser::doSectionLinks (   $comment,
  $selfLinkTarget = null,
  $samePage = false,
  $wikiId = false 
)
private

Converts C-style comments in edit summaries into section links.

Too many things are called "comments", so these are mostly now called section links rather than autocomments.

We look for all comments, match any text before and after the comment, add a separator where needed and format the comment itself with CSS.

Parameters
string$commentComment text
LinkTarget | null$selfLinkTargetAn optional LinkTarget object used to links to sections
bool$samePageWhether section links should refer to local page
string | false | null$wikiIdId of the wiki to link to (if not the local wiki), as used by WikiMap.
Returns
string Preprocessed comment

Definition at line 197 of file CommentParser.php.

References Title\castFromLinkTarget(), Parser\guessSectionNameFromStrippedText(), MediaWiki\CommentFormatter\CommentParser\makeSectionLink(), NS_MAIN, and wfMessage().

Referenced by MediaWiki\CommentFormatter\CommentParser\preprocessInternal().

◆ doWikiLinks()

MediaWiki\CommentFormatter\CommentParser::doWikiLinks (   $comment,
  $selfLinkTarget = null,
  $samePage = false,
  $wikiId = false 
)
private

Formats wiki links and media links in text; all other wiki formatting is ignored.

Todo:
FIXME: Doesn't handle sub-links as in image thumb texts like the main parser
Parameters
string$commentText to format links in. WARNING! Since the output of this function is html, $comment must be sanitized for use as html. You probably want to pass $comment through Sanitizer::escapeHtmlAllowEntities() before calling this function. as used by WikiMap.
LinkTarget | null$selfLinkTargetAn optional LinkTarget object used to links to sections
bool$samePageWhether section links should refer to local page
string | false | null$wikiIdId of the wiki to link to (if not the local wiki), as used by WikiMap.
Returns
string HTML

Definition at line 331 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\addFileLink(), MediaWiki\CommentFormatter\CommentParser\addPageLink(), Linker\normalizeSubpageLink(), NS_FILE, NS_MEDIA, and Linker\splitTrail().

Referenced by MediaWiki\CommentFormatter\CommentParser\preprocessInternal().

◆ finalize()

MediaWiki\CommentFormatter\CommentParser::finalize (   $comments)

Execute pending batch queries and replace markers in the specified string(s) with actual links.

Parameters
string | string[]$comments
Returns
string|string[]

Definition at line 140 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\flushLinkBatches().

◆ flushLinkBatches()

MediaWiki\CommentFormatter\CommentParser::flushLinkBatches ( )
private

Execute any pending link batch or file batch.

Definition at line 518 of file CommentParser.php.

Referenced by MediaWiki\CommentFormatter\CommentParser\finalize().

◆ makeSectionLink()

MediaWiki\CommentFormatter\CommentParser::makeSectionLink ( LinkTarget  $target,
  $text,
  $wikiId 
)
private

Make a section link.

These don't need to go into the LinkBatch, since the link class does not depend on whether the link is known.

Parameters
LinkTarget$target
string$text
string | false | null$wikiIdId of the wiki to link to (if not the local wiki), as used by WikiMap.
Returns
string HTML link

Definition at line 293 of file CommentParser.php.

References MediaWiki\Linker\LinkTarget\getDBkey(), WikiMap\getForeignURL(), MediaWiki\Linker\LinkTarget\getFragment(), MediaWiki\Linker\LinkTarget\getNamespace(), MediaWiki\Linker\LinkTarget\isExternal(), and Linker\makeExternalLink().

Referenced by MediaWiki\CommentFormatter\CommentParser\doSectionLinks().

◆ preprocess()

MediaWiki\CommentFormatter\CommentParser::preprocess ( string  $comment,
LinkTarget  $selfLinkTarget = null,
  $samePage = false,
  $wikiId = false,
  $enableSectionLinks = true 
)

Convert a comment to HTML, but replace links with markers which are resolved later.

Parameters
string$comment
LinkTarget | null$selfLinkTarget
bool$samePage
string | false | null$wikiId
bool$enableSectionLinks
Returns
string

Definition at line 109 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\preprocessInternal().

Referenced by MediaWiki\CommentFormatter\CommentFormatter\preprocessRevComment().

◆ preprocessInternal()

MediaWiki\CommentFormatter\CommentParser::preprocessInternal (   $comment,
  $unsafe,
  $selfLinkTarget,
  $samePage,
  $wikiId,
  $enableSectionLinks 
)
private
Parameters
string$comment
bool$unsafe
LinkTarget | null$selfLinkTarget
bool$samePage
string | false | null$wikiId
bool$enableSectionLinks
Returns
string

Definition at line 165 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\doSectionLinks(), MediaWiki\CommentFormatter\CommentParser\doWikiLinks(), and Sanitizer\escapeHtmlAllowEntities().

Referenced by MediaWiki\CommentFormatter\CommentParser\preprocess(), and MediaWiki\CommentFormatter\CommentParser\preprocessUnsafe().

◆ preprocessUnsafe()

MediaWiki\CommentFormatter\CommentParser::preprocessUnsafe (   $comment,
LinkTarget  $selfLinkTarget = null,
  $samePage = false,
  $wikiId = false,
  $enableSectionLinks = true 
)

Convert a comment in pseudo-HTML format to HTML, replacing links with markers.

Parameters
string$comment
LinkTarget | null$selfLinkTarget
bool$samePage
string | false | null$wikiId
bool$enableSectionLinks
Returns
string

Definition at line 126 of file CommentParser.php.

References MediaWiki\CommentFormatter\CommentParser\preprocessInternal().

Member Data Documentation

◆ $contLang

Language MediaWiki\CommentFormatter\CommentParser::$contLang
private

◆ $fileBatch

array MediaWiki\CommentFormatter\CommentParser::$fileBatch
private

Input to RepoGroup::findFiles()

Definition at line 58 of file CommentParser.php.

◆ $files

File [] MediaWiki\CommentFormatter\CommentParser::$files = []
private

Resolved File objects indexed by DB key.

Definition at line 60 of file CommentParser.php.

◆ $hookRunner

HookRunner MediaWiki\CommentFormatter\CommentParser::$hookRunner
private

Definition at line 48 of file CommentParser.php.

◆ $linkBatch

LinkBatch null MediaWiki\CommentFormatter\CommentParser::$linkBatch
private

Definition at line 55 of file CommentParser.php.

◆ $linkBatchFactory

LinkBatchFactory MediaWiki\CommentFormatter\CommentParser::$linkBatchFactory
private

◆ $linkCache

LinkCache MediaWiki\CommentFormatter\CommentParser::$linkCache
private

◆ $linkRenderer

LinkRenderer MediaWiki\CommentFormatter\CommentParser::$linkRenderer
private

◆ $links

callable [] MediaWiki\CommentFormatter\CommentParser::$links = []
private

Definition at line 53 of file CommentParser.php.

◆ $namespaceInfo

NamespaceInfo MediaWiki\CommentFormatter\CommentParser::$namespaceInfo
private

◆ $repoGroup

RepoGroup MediaWiki\CommentFormatter\CommentParser::$repoGroup
private

◆ $titleParser

TitleParser MediaWiki\CommentFormatter\CommentParser::$titleParser
private

◆ $userLang

Language MediaWiki\CommentFormatter\CommentParser::$userLang
private

The documentation for this class was generated from the following file: