39 private $linkRenderer;
41 private $linkBatchFactory;
51 private $namespaceInfo;
68 private const MAX_ID_SIZE = 7;
70 private const MARKER_PREFIX =
"\x1B\"'";
94 $this->linkRenderer = $linkRenderer;
95 $this->linkBatchFactory = $linkBatchFactory;
96 $this->linkCache = $linkCache;
97 $this->repoGroup = $repoGroup;
98 $this->userLang = $userLang;
99 $this->contLang = $contLang;
100 $this->titleParser = $titleParser;
101 $this->namespaceInfo = $namespaceInfo;
102 $this->hookRunner =
new HookRunner( $hookContainer );
117 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
119 return $this->preprocessInternal( $comment,
false, $selfLinkTarget,
120 $samePage, $wikiId, $enableSectionLinks );
134 $samePage =
false, $wikiId =
false, $enableSectionLinks =
true
136 return $this->preprocessInternal( $comment,
true, $selfLinkTarget,
137 $samePage, $wikiId, $enableSectionLinks );
148 $this->flushLinkBatches();
149 return preg_replace_callback(
150 '/' . self::MARKER_PREFIX .
'([0-9]{' . self::MAX_ID_SIZE .
'})/',
152 $callback = $this->links[(int)$m[1]] ??
null;
156 return '<!-- MISSING -->';
172 private function preprocessInternal( $comment, $unsafe, $selfLinkTarget, $samePage, $wikiId,
177 $comment = strtr( $comment,
"\n\x1b",
" " );
180 $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
182 if ( $enableSectionLinks ) {
183 $comment = $this->doSectionLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
185 return $this->doWikiLinks( $comment, $selfLinkTarget, $samePage, $wikiId );
204 private function doSectionLinks(
206 $selfLinkTarget =
null,
210 $comment = preg_replace_callback(
216 '!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
217 function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) {
219 $match += [
'',
'',
'',
'' ];
221 $pre = $match[1] !==
'';
223 $post = $match[3] !==
'';
226 $this->hookRunner->onFormatAutocomments(
227 $comment, $pre, $auto, $post,
228 Title::castFromLinkTarget( $selfLinkTarget ),
231 if ( $comment !==
null ) {
235 if ( $selfLinkTarget ) {
237 # Remove links that a user may have manually put in the autosummary
238 # This could be improved by copying as much of Parser::stripSectionName as desired.
239 $section = str_replace( [
247 $sectionText = str_replace(
'[[',
'[[', $auto );
249 $section = substr( Parser::guessSectionNameFromStrippedText( $section ), 1 );
250 if ( $section !==
'' ) {
252 $sectionTitle =
new TitleValue(
NS_MAIN,
'', $section );
254 $sectionTitle = $selfLinkTarget->createFragmentTarget( $section );
256 $auto = $this->makeSectionLink(
258 $this->userLang->getArrow() .
259 Html::rawElement(
'bdi', [
'dir' => $this->userLang->getDir() ], $sectionText ),
266 # written summary $presep autocomment (summary )
267 $pre =
wfMessage(
'autocomment-prefix' )->inContentLanguage()->escaped();
270 # autocomment $postsep written summary ( summary)
271 $auto .=
wfMessage(
'colon-separator' )->inContentLanguage()->escaped();
274 $auto = Html::rawElement(
'span', [
'class' =>
'autocomment' ], $auto );
295 private function makeSectionLink(
296 LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle
298 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
299 return $this->linkRenderer->makeExternalLink(
300 WikiMap::getForeignURL(
302 $target->getNamespace() === 0
303 ? $target->getDBkey()
304 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
305 ':' . $target->getDBkey(),
306 $target->getFragment()
312 return $this->linkRenderer->makePreloadedLink( $target,
new HtmlArmor( $text ),
'' );
333 private function doWikiLinks( $comment, $selfLinkTarget =
null, $samePage =
false, $wikiId =
false ) {
334 return preg_replace_callback(
337 \s*+ # ignore leading whitespace, the *+ quantifier disallows backtracking
338 :? # ignore optional leading colon
339 ([^[\]|]+) # 1. link target; page names cannot include [, ] or |
342 # Stop matching at ]] without relying on backtracking.
346 ([^[]*) # 3. link trail (the text up until the next link)
348 function ( $match ) use ( $selfLinkTarget, $samePage, $wikiId ) {
350 $medians .= preg_quote(
351 $this->namespaceInfo->getCanonicalName(
NS_MEDIA ),
'/' );
353 $medians .= preg_quote(
354 $this->contLang->getNsText(
NS_MEDIA ),
358 $comment = $match[0];
361 if ( strpos( $match[1],
'%' ) !==
false ) {
363 rawurldecode( $match[1] ),
364 [
'<' =>
'<',
'>' =>
'>' ]
369 if ( $match[2] !=
"" ) {
376 if ( preg_match(
'/^' . $medians .
'(.*)$/i', $match[1], $submatch ) ) {
378 $linkRegexp =
'/\[\[(.*?)\]\]/';
379 $linkTarget = $this->titleParser->makeTitleValueSafe(
NS_FILE, $submatch[1] );
381 $linkMarker = $this->addFileLink( $linkTarget, $text );
386 if ( isset( $match[1][0] ) && $match[1][0] ==
':' ) {
387 $match[1] = substr( $match[1], 1 );
390 if ( $match[1] !==
false && $match[1] !==
null && $match[1] !==
'' ) {
392 $this->contLang->linkTrail(),
396 $trail = $submatch[1];
400 $linkRegexp =
'/\[\[(.*?)\]\]' . preg_quote( $trail,
'/' ) .
'/';
401 [ $inside, $trail ] = Linker::splitTrail( $trail );
404 $linkTarget = Linker::normalizeSubpageLink( $selfLinkTarget, $match[1], $linkText );
407 $target = $this->titleParser->parseTitle( $linkTarget );
409 if ( $target->getText() ==
'' && !$target->isExternal()
410 && !$samePage && $selfLinkTarget
412 $target = $selfLinkTarget->createFragmentTarget( $target->getFragment() );
420 $linkMarker = $this->addPageLink(
426 $linkMarker .= $trail;
427 }
catch ( MalformedTitleException $e ) {
434 $comment = preg_replace(
456 private function addLinkMarker( $callback ) {
457 $nextId = count( $this->links );
458 if ( strlen( (
string)$nextId ) > self::MAX_ID_SIZE ) {
459 throw new \RuntimeException(
'Too many links in comment batch' );
461 $this->links[] = $callback;
462 return sprintf( self::MARKER_PREFIX .
"%0" . self::MAX_ID_SIZE .
'd', $nextId );
475 private function addPageLink( LinkTarget $target, $text, $wikiId, LinkTarget $contextTitle ) {
476 if ( $wikiId !==
null && $wikiId !==
false && !$target->isExternal() ) {
478 return $this->linkRenderer->makeExternalLink(
479 WikiMap::getForeignURL(
481 $target->getNamespace() === 0
482 ? $target->getDBkey()
483 : $this->namespaceInfo->getCanonicalName( $target->getNamespace() ) .
484 ':' . $target->getDBkey(),
485 $target->getFragment()
490 } elseif ( $this->linkCache->getGoodLinkID( $target ) ||
491 Title::newFromLinkTarget( $target )->isAlwaysKnown()
494 return $this->linkRenderer->makeKnownLink( $target,
new HtmlArmor( $text ) );
495 } elseif ( $this->linkCache->isBadLink( $target ) ) {
497 return $this->linkRenderer->makeBrokenLink( $target,
new HtmlArmor( $text ) );
501 if ( !$this->linkBatch ) {
502 $this->linkBatch = $this->linkBatchFactory->newLinkBatch();
503 $this->linkBatch->setCaller( __METHOD__ );
505 $this->linkBatch->addObj( $target );
506 return $this->addLinkMarker(
function () use ( $target, $text ) {
507 return $this->linkRenderer->makeLink( $target,
new HtmlArmor( $text ) );
518 private function addFileLink( LinkTarget $target, $html ) {
519 $this->fileBatch[] = [
522 return $this->addLinkMarker(
function () use ( $target, $html ) {
523 return Linker::makeMediaLinkFile(
525 $this->files[$target->getDBkey()] ??
false,
534 private function flushLinkBatches() {
535 if ( $this->linkBatch ) {
536 $this->linkBatch->execute();
537 $this->linkBatch =
null;
539 if ( $this->fileBatch ) {
540 $this->files += $this->repoGroup->findFiles( $this->fileBatch );
541 $this->fileBatch = [];