38 private $linkRenderer;
40 private $linkBatchFactory;
50 private $namespaceInfo;
67 private const MAX_ID_SIZE = 7;
69 private const MARKER_PREFIX =
"\x1B\"'";
93 $this->linkRenderer = $linkRenderer;
94 $this->linkBatchFactory = $linkBatchFactory;
95 $this->linkCache = $linkCache;
96 $this->repoGroup = $repoGroup;
97 $this->userLang = $userLang;
98 $this->contLang = $contLang;
99 $this->titleParser = $titleParser;
100 $this->namespaceInfo = $namespaceInfo;
101 $this->hookRunner =
new HookRunner( $hookContainer );
116 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
118 return $this->preprocessInternal( $comment,
false, $selfLinkTarget,
119 $samePage, $wikiId, $enableSectionLinks );
133 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
135 return $this->preprocessInternal( $comment,
true, $selfLinkTarget,
136 $samePage, $wikiId, $enableSectionLinks );
147 $this->flushLinkBatches();
148 return preg_replace_callback(
149 '/' . self::MARKER_PREFIX .
'([0-9]{' . self::MAX_ID_SIZE .
'})/',
151 $callback = $this->links[(int)$m[1]] ??
null;
155 return '<!-- MISSING -->';
171 private function preprocessInternal( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId,
176 $comment = strtr( $comment,
"\n\x1b",
" " );
179 $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
181 if ( $enableSectionLinks ) {
182 $comment = $this->doSectionLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
184 return $this->doWikiLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
203 private function doSectionLinks(
205 $selfLinkTarget =
null,
215 $comment = preg_replace_callback(
221 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
222 function ( $match ) use ( &$append, $selfLinkTarget, $samePage, $wikiId ) {
224 $match += [
'',
'',
'',
'' ];
226 $pre = $match[1] !==
'';
228 $post = $match[3] !==
'';
231 $this->hookRunner->onFormatAutocomments(
232 $comment, $pre, $auto, $post,
233 Title::castFromLinkTarget( $selfLinkTarget ),
236 if ( $comment !==
null ) {
240 if ( $selfLinkTarget ) {
242 # Remove links that a user may have manually put in the autosummary
243 # This could be improved by copying as much of Parser::stripSectionName as desired.
244 $section = str_replace( [
252 $sectionText = str_replace(
'[[',
'[[', $auto );
254 $section = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 );
255 if ( $section !==
'' ) {
257 $sectionTitle =
new TitleValue(
NS_MAIN,
'', $section );
259 $sectionTitle = $selfLinkTarget->createFragmentTarget( $section );
261 $auto = $this->makeSectionLink(
263 $this->userLang->getArrow() . $this->userLang->getDirMark() . $sectionText,
270 # written summary $presep autocomment (summary )
271 $pre =
wfMessage(
'autocomment-prefix' )->inContentLanguage()->escaped();
274 # autocomment $postsep written summary ( summary)
275 $auto .=
wfMessage(
'colon-separator' )->inContentLanguage()->escaped();
278 $auto =
'<span dir="auto"><span class="autocomment">' . $auto .
'</span>';
279 $append .=
'</span>';
281 $comment = $pre . $auto;
286 return $comment . $append;
301 private function makeSectionLink(
302 LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle
304 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
305 return $this->linkRenderer->makeExternalLink(
306 WikiMap::getForeignURL(
308 $target->getNamespace() === 0
309 ? $target->getDBkey()
310 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
311 ':' . $target->getDBkey(),
312 $target->getFragment()
318 return $this->linkRenderer->makePreloadedLink( $target,
new HtmlArmor( $text ),
'' );
339 private function doWikiLinks( $comment, $selfLinkTarget =
null, $samePage =
false, $wikiId =
false ) {
340 return preg_replace_callback(
343 \s*+ # ignore leading whitespace, the *+ quantifier disallows backtracking
344 :? # ignore optional leading colon
345 ([^[\]|]+) # 1. link target; page names cannot include [, ] or |
348 # Stop matching at ]] without relying on backtracking.
352 ([^[]*) # 3. link trail (the text up until the next link)
354 function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) {
356 $medians .= preg_quote(
357 $this->namespaceInfo->getCanonicalName(
NS_MEDIA ),
'/' );
359 $medians .= preg_quote(
360 $this->contLang->getNsText(
NS_MEDIA ),
364 $comment = $match[0];
367 if ( strpos( $match[1],
'%' ) !==
false ) {
369 rawurldecode( $match[1] ),
370 [
'<' =>
'<',
'>' =>
'>' ]
375 if ( $match[2] !=
"" ) {
382 if ( preg_match(
'/^' . $medians .
'(.*)$/i', $match[1], $submatch ) ) {
384 $linkRegexp =
'/\[\[(.*?)\]\]/';
385 $linkTarget = $this->titleParser->makeTitleValueSafe(
NS_FILE, $submatch[1] );
387 $linkMarker = $this->addFileLink( $linkTarget, $text );
392 if ( isset( $match[1][0] ) && $match[1][0] ==
':' ) {
393 $match[1] = substr( $match[1], 1 );
396 if ( $match[1] !==
false && $match[1] !==
null && $match[1] !==
'' ) {
398 $this->contLang->linkTrail(),
402 $trail = $submatch[1];
406 $linkRegexp =
'/\[\[(.*?)\]\]' . preg_quote( $trail,
'/' ) .
'/';
407 [ $inside, $trail ] = Linker::splitTrail( $trail );
410 $linkTarget = Linker::normalizeSubpageLink( $selfLinkTarget, $match[1], $linkText );
413 $target = $this->titleParser->parseTitle( $linkTarget );
415 if ( $target->getText() ==
'' && !$target->isExternal()
416 && !$samePage && $selfLinkTarget
418 $target = $selfLinkTarget->createFragmentTarget( $target->getFragment() );
425 $linkMarker = $this->addPageLink(
431 $linkMarker .= $trail;
432 }
catch ( MalformedTitleException $e ) {
439 $comment = preg_replace(
461 private function addLinkMarker( $callback ) {
462 $nextId = count( $this->links );
463 if ( strlen( (
string)$nextId ) > self::MAX_ID_SIZE ) {
464 throw new \RuntimeException(
'Too many links in comment batch' );
466 $this->links[] = $callback;
467 return sprintf( self::MARKER_PREFIX .
"%0" . self::MAX_ID_SIZE .
'd', $nextId );
480 private function addPageLink( LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle ) {
481 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
483 return $this->linkRenderer->makeExternalLink(
484 WikiMap::getForeignURL(
486 $target->getNamespace() === 0
487 ? $target->getDBkey()
488 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
489 ':' . $target->getDBkey(),
490 $target->getFragment()
495 } elseif ( $this->linkCache->getGoodLinkID( $target ) ||
496 Title::newFromLinkTarget( $target )->isAlwaysKnown()
499 return $this->linkRenderer->makeKnownLink( $target,
new HtmlArmor( $text ) );
500 } elseif ( $this->linkCache->isBadLink( $target ) ) {
502 return $this->linkRenderer->makeBrokenLink( $target,
new HtmlArmor( $text ) );
506 if ( !$this->linkBatch ) {
507 $this->linkBatch = $this->linkBatchFactory->newLinkBatch();
508 $this->linkBatch->setCaller( __METHOD__ );
510 $this->linkBatch->addObj( $target );
511 return $this->addLinkMarker(
function () use ( $target, $text ) {
512 return $this->linkRenderer->makeLink( $target,
new HtmlArmor( $text ) );
523 private function addFileLink( LinkTarget $target, $html ) {
524 $this->fileBatch[] = [
527 return $this->addLinkMarker(
function () use ( $target, $html ) {
528 return Linker::makeMediaLinkFile(
530 $this->files[$target->getDBkey()] ??
false,
539 private function flushLinkBatches() {
540 if ( $this->linkBatch ) {
541 $this->linkBatch->execute();
542 $this->linkBatch =
null;
544 if ( $this->fileBatch ) {
545 $this->files += $this->repoGroup->findFiles( $this->fileBatch );
546 $this->fileBatch = [];