Example #1
0
File: Linker.php Project: paladox/2
    /**
     * Formats wiki links and media links in text; all other wiki formatting
     * is ignored
     *
     * @todo FIXME: Doesn't handle sub-links as in image thumb texts like the main parser
     * @param string $comment Text to format links in. WARNING! Since the output of this
     *	function is html, $comment must be sanitized for use as html. You probably want
     *	to pass $comment through Sanitizer::escapeHtmlAllowEntities() before calling
     *	this function.
     * @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.
     *
     * @return string
     */
    public static function formatLinksInComment($comment, $title = null, $local = false, $wikiId = null)
    {
        return preg_replace_callback('/
				\\[\\[
				:? # ignore optional leading colon
				([^\\]|]+) # 1. link target; page names cannot include ] or |
				(?:\\|
					# 2. a pipe-separated substring; only the last is captured
					# Stop matching at | and ]] without relying on backtracking.
					((?:]?[^\\]|])*+)
				)*
				\\]\\]
				([^[]*) # 3. link trail (the text up until the next link)
			/x', function ($match) use($title, $local, $wikiId) {
            global $wgContLang;
            $medians = '(?:' . preg_quote(MWNamespace::getCanonicalName(NS_MEDIA), '/') . '|';
            $medians .= preg_quote($wgContLang->getNsText(NS_MEDIA), '/') . '):';
            $comment = $match[0];
            # fix up urlencoded title texts (copied from Parser::replaceInternalLinks)
            if (strpos($match[1], '%') !== false) {
                $match[1] = strtr(rawurldecode($match[1]), array('<' => '&lt;', '>' => '&gt;'));
            }
            # Handle link renaming [[foo|text]] will show link as "text"
            if ($match[2] != "") {
                $text = $match[2];
            } else {
                $text = $match[1];
            }
            $submatch = array();
            $thelink = null;
            if (preg_match('/^' . $medians . '(.*)$/i', $match[1], $submatch)) {
                # Media link; trail not supported.
                $linkRegexp = '/\\[\\[(.*?)\\]\\]/';
                $title = Title::makeTitleSafe(NS_FILE, $submatch[1]);
                if ($title) {
                    $thelink = Linker::makeMediaLinkObj($title, $text);
                }
            } else {
                # Other kind of link
                if (preg_match($wgContLang->linkTrail(), $match[3], $submatch)) {
                    $trail = $submatch[1];
                } else {
                    $trail = "";
                }
                $linkRegexp = '/\\[\\[(.*?)\\]\\]' . preg_quote($trail, '/') . '/';
                if (isset($match[1][0]) && $match[1][0] == ':') {
                    $match[1] = substr($match[1], 1);
                }
                list($inside, $trail) = Linker::splitTrail($trail);
                $linkText = $text;
                $linkTarget = Linker::normalizeSubpageLink($title, $match[1], $linkText);
                $target = Title::newFromText($linkTarget);
                if ($target) {
                    if ($target->getText() == '' && !$target->isExternal() && !$local && $title) {
                        $newTarget = clone $title;
                        $newTarget->setFragment('#' . $target->getFragment());
                        $target = $newTarget;
                    }
                    $thelink = Linker::makeCommentLink($target, $linkText . $inside, $wikiId) . $trail;
                }
            }
            if ($thelink) {
                // If the link is still valid, go ahead and replace it in!
                $comment = preg_replace($linkRegexp, StringUtils::escapeRegexReplacement($thelink), $comment, 1);
            }
            return $comment;
        }, $comment);
    }
Example #2
0
 public function makeCommentLink(Title $title, $text, $wikiId = null, $options = [])
 {
     return Linker::makeCommentLink($title, $text, $wikiId, $options);
 }