https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115719
Revision: 115719 Author: platonides Date: 2012-09-14 20:36:05 +0000 (Fri, 14 Sep 2012) Log Message: ----------- Add support for external links in wiki syntax to Intuition. Modified Paths: -------------- trunk/tools/ToolserverI18N/TsIntuition.php trunk/tools/ToolserverI18N/TsIntuitionUtil.php Modified: trunk/tools/ToolserverI18N/TsIntuition.php =================================================================== --- trunk/tools/ToolserverI18N/TsIntuition.php 2012-09-14 18:23:36 UTC (rev 115718) +++ trunk/tools/ToolserverI18N/TsIntuition.php 2012-09-14 20:36:05 UTC (rev 115719) @@ -385,6 +385,7 @@ 'raw-variables' => false, 'escape' => 'plain', 'parsemag' => true, + 'externallinks' => false, ); // If $options was a domain string, convert it now. @@ -451,6 +452,10 @@ $msg = TsIntuitionUtil::strEscape( $msg, $options['escape'] ); } + if ( $options['externallinks'] ) { + $msg = TsIntuitionUtil::parseExternalLinks( $msg ); + } + // Finally return $msg; Modified: trunk/tools/ToolserverI18N/TsIntuitionUtil.php =================================================================== --- trunk/tools/ToolserverI18N/TsIntuitionUtil.php 2012-09-14 18:23:36 UTC (rev 115718) +++ trunk/tools/ToolserverI18N/TsIntuitionUtil.php 2012-09-14 20:36:05 UTC (rev 115719) @@ -174,4 +174,44 @@ return $acceptableLanguages; } + const EXT_LINK_URL_CLASS = '[^][<>"\\x00-\\x20\\x7F\p{Zs}]'; // Copied from Parser class + + /** + * Given a text already html-escaped which contains urls in wiki format to html + */ + public static function parseExternalLinks($text) { + static $urlProtocols = false; + if ( !$urlProtocols ) { + if ( function_exists( 'wfUrlProtocols' ) ) { // Allow custom protocols + $urlProtocols = wfUrlProtocols(); + } else { + $urlProtocols = 'https?:\/\/|ftp:\/\/'; + } + } + + + $extLinkBracketedRegex = '/(?:(<[^>]*)|' . + '\[(((?i)' . $urlProtocols . ')' . self::EXT_LINK_URL_CLASS . '+)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F]*?)\]|' + . '(((?i)' . $urlProtocols . ')' . self::EXT_LINK_URL_CLASS . '+))/Su'; + + return preg_replace_callback( $extLinkBracketedRegex, 'self::parseExternalLinkArray', $text ); + } + + /** + * Changes the matches of parseExternalLinks into html + */ + public static function parseExternalLinkArray($bits) { + static $counter = 0; + + if ( $bits[1] != '' ) + return $bits[1]; + + if ( isset( $bits[4] ) && $bits[4] != '' ) { + return '<a href="' . $bits[2] . '">' . $bits[4] . '</a>'; + } elseif ( isset( $bits[5] ) ) { + return '<a href="' . $bits[5] . '">' . $bits[5] . '</a>'; + } else { + return '<a href="' . $bits[2] . '">[' . ++$counter . ']</a>'; + } + } } _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits