36 private $linkRenderer;
38 private $linkBatchFactory;
48 private $namespaceInfo;
65 private const MAX_ID_SIZE = 7;
67 private const MARKER_PREFIX =
"\x1B\"'";
91 $this->linkRenderer = $linkRenderer;
92 $this->linkBatchFactory = $linkBatchFactory;
93 $this->linkCache = $linkCache;
94 $this->repoGroup = $repoGroup;
95 $this->userLang = $userLang;
96 $this->contLang = $contLang;
97 $this->titleParser = $titleParser;
98 $this->namespaceInfo = $namespaceInfo;
99 $this->hookRunner =
new HookRunner( $hookContainer );
114 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
116 return $this->preprocessInternal( $comment,
false, $selfLinkTarget,
117 $samePage, $wikiId, $enableSectionLinks );
131 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
133 return $this->preprocessInternal( $comment,
true, $selfLinkTarget,
134 $samePage, $wikiId, $enableSectionLinks );
145 $this->flushLinkBatches();
146 return preg_replace_callback(
147 '/' . self::MARKER_PREFIX .
'([0-9]{' . self::MAX_ID_SIZE .
'})/',
149 $callback = $this->links[(int)$m[1]] ??
null;
153 return '<!-- MISSING -->';
169 private function preprocessInternal( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId,
174 $comment = strtr( $comment,
"\n\x1b",
" " );
177 $comment = \Sanitizer::escapeHtmlAllowEntities( $comment );
179 if ( $enableSectionLinks ) {
180 $comment = $this->doSectionLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
182 return $this->doWikiLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
201 private function doSectionLinks(
203 $selfLinkTarget =
null,
213 $comment = preg_replace_callback(
219 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
220 function ( $match ) use ( &$append, $selfLinkTarget, $samePage, $wikiId ) {
222 $match += [
'',
'',
'',
'' ];
224 $pre = $match[1] !==
'';
226 $post = $match[3] !==
'';
229 $this->hookRunner->onFormatAutocomments(
230 $comment, $pre, $auto, $post,
234 if ( $comment !==
null ) {
238 if ( $selfLinkTarget ) {
240 # Remove links that a user may have manually put in the autosummary
241 # This could be improved by copying as much of Parser::stripSectionName as desired.
242 $section = str_replace( [
250 $sectionText = str_replace(
'[[',
'[[', $auto );
253 if ( $section !==
'' ) {
257 $sectionTitle = $selfLinkTarget->createFragmentTarget( $section );
259 $auto = $this->makeSectionLink(
261 $this->userLang->getArrow() . $this->userLang->getDirMark() . $sectionText,
267 # written summary $presep autocomment (summary )
268 $pre =
wfMessage(
'autocomment-prefix' )->inContentLanguage()->escaped();
271 # autocomment $postsep written summary ( summary)
272 $auto .=
wfMessage(
'colon-separator' )->inContentLanguage()->escaped();
275 $auto =
'<span dir="auto"><span class="autocomment">' . $auto .
'</span>';
276 $append .=
'</span>';
278 $comment = $pre . $auto;
283 return $comment . $append;
297 private function makeSectionLink(
298 LinkTarget $target, $text, $wikiId
300 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
301 return Linker::makeExternalLink(
302 WikiMap::getForeignURL(
304 $target->getNamespace() === 0
305 ? $target->getDBkey()
306 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
307 ':' . $target->getDBkey(),
308 $target->getFragment()
314 return $this->linkRenderer->makePreloadedLink( $target,
new HtmlArmor( $text ),
'' );
335 private function doWikiLinks( $comment, $selfLinkTarget =
null, $samePage =
false, $wikiId =
false ) {
336 return preg_replace_callback(
339 \s*+ # ignore leading whitespace, the *+ quantifier disallows backtracking
340 :? # ignore optional leading colon
341 ([^[\]|]+) # 1. link target; page names cannot include [, ] or |
344 # Stop matching at ]] without relying on backtracking.
348 ([^[]*) # 3. link trail (the text up until the next link)
350 function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) {
352 $medians .= preg_quote(
353 $this->namespaceInfo->getCanonicalName(
NS_MEDIA ),
'/' );
355 $medians .= preg_quote(
356 $this->contLang->getNsText(
NS_MEDIA ),
360 $comment = $match[0];
363 if ( strpos( $match[1],
'%' ) !==
false ) {
365 rawurldecode( $match[1] ),
366 [
'<' =>
'<',
'>' =>
'>' ]
371 if ( $match[2] !=
"" ) {
378 if ( preg_match(
'/^' . $medians .
'(.*)$/i', $match[1], $submatch ) ) {
380 $linkRegexp =
'/\[\[(.*?)\]\]/';
381 $linkTarget = $this->titleParser->makeTitleValueSafe(
NS_FILE, $submatch[1] );
383 $linkMarker = $this->addFileLink( $linkTarget, $text );
388 if ( isset( $match[1][0] ) && $match[1][0] ==
':' ) {
389 $match[1] = substr( $match[1], 1 );
392 if ( $match[1] !==
false && $match[1] !==
null && $match[1] !==
'' ) {
394 $this->contLang->linkTrail(),
398 $trail = $submatch[1];
402 $linkRegexp =
'/\[\[(.*?)\]\]' . preg_quote( $trail,
'/' ) .
'/';
403 [ $inside, $trail ] = Linker::splitTrail( $trail );
406 $linkTarget = Linker::normalizeSubpageLink( $selfLinkTarget, $match[1], $linkText );
409 $target = $this->titleParser->parseTitle( $linkTarget );
411 if ( $target->getText() ==
'' && !$target->isExternal()
412 && !$samePage && $selfLinkTarget
414 $target = $selfLinkTarget->createFragmentTarget( $target->getFragment() );
417 $linkMarker = $this->addPageLink( $target, $linkText . $inside, $wikiId );
418 $linkMarker .= $trail;
426 $comment = preg_replace(
430 StringUtils::escapeRegexReplacement( $linkMarker ),
448 private function addLinkMarker( $callback ) {
449 $nextId = count( $this->links );
450 if ( strlen( (
string)$nextId ) > self::MAX_ID_SIZE ) {
451 throw new \RuntimeException(
'Too many links in comment batch' );
453 $this->links[] = $callback;
454 return sprintf( self::MARKER_PREFIX .
"%0" . self::MAX_ID_SIZE .
'd', $nextId );
466 private function addPageLink( LinkTarget $target, $text, $wikiId ) {
467 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
469 return Linker::makeExternalLink(
470 WikiMap::getForeignURL(
472 $target->getNamespace() === 0
473 ? $target->getDBkey()
474 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
475 ':' . $target->getDBkey(),
476 $target->getFragment()
481 } elseif ( $this->linkCache->getGoodLinkID( $target ) ||
485 return $this->linkRenderer->makeKnownLink( $target,
new HtmlArmor( $text ) );
486 } elseif ( $this->linkCache->isBadLink( $target ) ) {
488 return $this->linkRenderer->makeBrokenLink( $target,
new HtmlArmor( $text ) );
492 if ( !$this->linkBatch ) {
493 $this->linkBatch = $this->linkBatchFactory->newLinkBatch();
494 $this->linkBatch->setCaller( __METHOD__ );
496 $this->linkBatch->addObj( $target );
497 return $this->addLinkMarker(
function () use ( $target, $text ) {
498 return $this->linkRenderer->makeLink( $target,
new HtmlArmor( $text ) );
509 private function addFileLink( LinkTarget $target, $html ) {
510 $this->fileBatch[] = [
513 return $this->addLinkMarker(
function () use ( $target, $html ) {
514 return Linker::makeMediaLinkFile(
516 $this->files[$target->getDBkey()] ??
false,
525 private function flushLinkBatches() {
526 if ( $this->linkBatch ) {
527 $this->linkBatch->execute();
528 $this->linkBatch =
null;
530 if ( $this->fileBatch ) {
531 $this->files += $this->repoGroup->findFiles( $this->fileBatch );
532 $this->fileBatch = [];