* @param string $comment
* @param Title|null $title Title object (to generate link to the section in autocomment) or null
* @param bool $local Whether section links should refer to local page
+ * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to. For use with external changes.
+ *
* @return mixed|string
*/
- public static function formatComment( $comment, $title = null, $local = false ) {
+ public static function formatComment( $comment, $title = null, $local = false, $wikiId = null ) {
# Sanitize text a bit:
$comment = str_replace( "\n", " ", $comment );
$comment = Sanitizer::escapeHtmlAllowEntities( $comment );
# Render autocomments and make links:
- $comment = self::formatAutocomments( $comment, $title, $local );
- $comment = self::formatLinksInComment( $comment, $title, $local );
+ $comment = self::formatAutocomments( $comment, $title, $local, $wikiId );
+ $comment = self::formatLinksInComment( $comment, $title, $local, $wikiId );
return $comment;
}
* @param string $comment Comment text
* @param Title|null $title An optional title object used to links to sections
* @param bool $local Whether section links should refer to local page
- * @return string Formatted comment
+ * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), as used by WikiMap.
+ *
+ * @return string Formatted comment (wikitext)
*/
- private static function formatAutocomments( $comment, $title = null, $local = false ) {
+ private static function formatAutocomments( $comment, $title = null, $local = false, $wikiId = null ) {
// @todo $append here is something of a hack to preserve the status
// quo. Someone who knows more about bidi and such should decide
// (1) what sane rendering even *is* for an LTR edit summary on an RTL
// zero-width assertions optional, so wrap them in a non-capturing
// group.
'!(?:(?<=(.)))?/\*\s*(.*?)\s*\*/(?:(?=(.)))?!',
- function ( $match ) use ( $title, $local, &$append ) {
+ function ( $match ) use ( $title, $local, $wikiId, &$append ) {
global $wgLang;
// Ensure all match positions are defined
$auto = $match[2];
$post = $match[3] !== '';
$comment = null;
- Hooks::run( 'FormatAutocomments', array( &$comment, $pre, $auto, $post, $title, $local ) );
+ Hooks::run( 'FormatAutocomments', array( &$comment, $pre, $auto, $post, $title, $local, $wikiId ) );
if ( $comment === null ) {
$link = '';
if ( $title ) {
$title->getDBkey(), $section );
}
if ( $sectionTitle ) {
- $link = Linker::link( $sectionTitle,
- $wgLang->getArrow(), array(), array(),
- 'noclasses' );
+ $link = Linker::makeCommentLink( $sectionTitle, $wgLang->getArrow(), $wikiId, 'noclasses' );
} else {
$link = '';
}
* @param string $comment Text to format links in
* @param Title|null $title An optional title object used to links to sections
* @param bool $local Whether section links should refer to local page
- * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), as used by WikiMap
+ * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), as used by WikiMap.
*
* @return string
*/
$newTarget = clone ( $title );
$newTarget->setFragment( '#' . $target->getFragment() );
$target = $newTarget;
-
- }
-
- if ( $wikiId !== null ) {
- $thelink = Linker::makeExternalLink(
- WikiMap::getForeignURL( $wikiId, $target->getPrefixedText(), $target->getFragment() ),
- $linkText . $inside,
- /* escape = */ false // Already escaped
- ) . $trail;
- } else {
- $thelink = Linker::link(
- $target,
- $linkText . $inside
- ) . $trail;
}
+ $thelink = Linker::makeCommentLink( $target, $linkText . $inside, $wikiId ) . $trail;
}
}
if ( $thelink ) {
);
}
+ /**
+ * Generates a link to the given Title
+ *
+ * @note This is only public for technical reasons. It's not intended for use outside Linker.
+ *
+ * @param Title $title
+ * @param string $text
+ * @param string|null $wikiId Id of the wiki to link to (if not the local wiki), as used by WikiMap.
+ * @param string|string[] $options See the $options parameter in Linker::link.
+ *
+ * @return string HTML link
+ */
+ public static function makeCommentLink( Title $title, $text, $wikiId = null, $options = array() ) {
+ if ( $wikiId !== null && !$title->isExternal() ) {
+ $link = Linker::makeExternalLink(
+ WikiMap::getForeignURL( $wikiId, $title->getPrefixedText(), $title->getFragment() ),
+ $text,
+ /* escape = */ false // Already escaped
+ );
+ } else {
+ $link = Linker::link( $title, $text, array(), array(), $options );
+ }
+
+ return $link;
+ }
+
/**
* @param Title $contextTitle
* @param string $target
* @param string $comment
* @param Title|null $title Title object (to generate link to section in autocomment) or null
* @param bool $local Whether section links should refer to local page
+ * @param string|null $wikiId Id (as used by WikiMap) of the wiki to generate links to. For use with external changes.
*
* @return string
*/
- public static function commentBlock( $comment, $title = null, $local = false ) {
+ public static function commentBlock( $comment, $title = null, $local = false, $wikiId = null ) {
// '*' used to be the comment inserted by the software way back
// in antiquity in case none was provided, here for backwards
// compatibility, acc. to brion -ævar
if ( $comment == '' || $comment == '*' ) {
return '';
} else {
- $formatted = self::formatComment( $comment, $title, $local );
+ $formatted = self::formatComment( $comment, $title, $local, $wikiId );
$formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
return " <span class=\"comment\">$formatted</span>";
}
'title' => $tooltip
) );
}
+
}
/**
* @covers Linker::formatAutocomments
* @covers Linker::formatLinksInComment
*/
- public function testFormatComment( $expected, $comment, $title = false, $local = false ) {
+ public function testFormatComment( $expected, $comment, $title = false, $local = false, $wikiId = null ) {
+ $conf = new SiteConfiguration();
+ $conf->settings = array(
+ 'wgServer' => array(
+ 'enwiki' => '//en.example.org',
+ 'dewiki' => '//de.example.org',
+ ),
+ 'wgArticlePath' => array(
+ 'enwiki' => '/w/$1',
+ 'dewiki' => '/w/$1',
+ ),
+ );
+ $conf->suffixes = array( 'wiki' );
+
$this->setMwGlobals( array(
'wgScript' => '/wiki/index.php',
'wgArticlePath' => '/wiki/$1',
'wgWellFormedXml' => true,
'wgCapitalLinks' => true,
+ 'wgConf' => $conf,
) );
if ( $title === false ) {
$this->assertEquals(
$expected,
- Linker::formatComment( $comment, $title, $local )
+ Linker::formatComment( $comment, $title, $local, $wikiId )
);
}
- public static function provideCasesForFormatComment() {
+ public function provideCasesForFormatComment() {
+ $wikiId = 'enwiki'; // $wgConf has a fake entry for this
+
return array(
// Linker::formatComment
array(
"'''not bolded'''",
"'''not bolded'''",
),
+ array(
+ "try <script>evil</scipt> things",
+ "try <script>evil</scipt> things",
+ ),
// Linker::formatAutocomments
array(
'<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
'<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
"/* autocomment containing /* */ T70361"
),
+ array(
+ '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing "quotes"</span></span>',
+ "/* autocomment containing \"quotes\" */"
+ ),
+ array(
+ '<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment containing <script>tags</script></span></span>',
+ "/* autocomment containing <script>tags</script> */"
+ ),
array(
'<a href="#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
"/* autocomment */",
"/* autocomment */",
null
),
+ array(
+ '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
+ "/* autocomment */",
+ false, false
+ ),
+ array(
+ '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a><span dir="auto"><span class="autocomment">autocomment</span></span>',
+ "/* autocomment */",
+ false, false, $wikiId
+ ),
// Linker::formatLinksInComment
array(
'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
'abc <a href="/wiki/index.php?title=/subpage&action=edit&redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
"abc [[/subpage]] def",
),
+ array(
+ 'abc <a href="/wiki/index.php?title=%22evil!%22&action=edit&redlink=1" class="new" title=""evil!" (page does not exist)">"evil!"</a> def',
+ "abc [[\"evil!\"]] def",
+ ),
+ array(
+ 'abc [[<script>very evil</script>]] def',
+ "abc [[<script>very evil</script>]] def",
+ ),
+ array(
+ 'abc [[|]] def',
+ "abc [[|]] def",
+ ),
+ array(
+ 'abc <a href="/wiki/index.php?title=Link&action=edit&redlink=1" class="new" title="Link (page does not exist)">link</a> def',
+ "abc [[link]] def",
+ false, false
+ ),
+ array(
+ 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
+ "abc [[link]] def",
+ false, false, $wikiId
+ )
);
}