/** * Adds class="external" & rel="nofollow" to external links. * * @param \DOMNode $node Link * @param \Title $title */ public function apply(\DOMNode $node, \Title $title) { if (!$node instanceof \DOMElement) { return; } $node->setAttribute('class', 'external'); global $wgNoFollowLinks, $wgNoFollowDomainExceptions; if ($wgNoFollowLinks && !wfMatchesDomainList($node->getAttribute('href'), $wgNoFollowDomainExceptions)) { $node->setAttribute('rel', 'nofollow'); } }
/** * Get the rel attribute for a particular external link. * * @since 1.21 * @param string|bool $url optional URL, to extract the domain from for rel => * nofollow if appropriate * @param $title Title optional Title, for wgNoFollowNsExceptions lookups * @return string|null rel attribute for $url */ public static function getExternalLinkRel($url = false, $title = null) { global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions; $ns = $title ? $title->getNamespace() : false; if ($wgNoFollowLinks && !in_array($ns, $wgNoFollowNsExceptions) && !wfMatchesDomainList($url, $wgNoFollowDomainExceptions)) { return 'nofollow'; } return null; }
/** * Get an associative array of additional HTML attributes appropriate for a * particular external link. This currently may include rel => nofollow * (depending on configuration, namespace, and the URL's domain) and/or a * target attribute (depending on configuration). * * @param $url String|bool optional URL, to extract the domain from for rel => * nofollow if appropriate * @return Array associative array of HTML attributes */ function getExternalLinkAttribs($url = false) { $attribs = array(); global $wgNoFollowLinks, $wgNoFollowNsExceptions, $wgNoFollowDomainExceptions; $ns = $this->mTitle->getNamespace(); if ($wgNoFollowLinks && !in_array($ns, $wgNoFollowNsExceptions) && !wfMatchesDomainList($url, $wgNoFollowDomainExceptions)) { $attribs['rel'] = 'nofollow'; } if ($this->mOptions->getExternalLinkTarget()) { $attribs['target'] = $this->mOptions->getExternalLinkTarget(); } return $attribs; }
/** * @dataProvider provideWfMatchesDomainList * @covers ::wfMatchesDomainList */ public function testWfMatchesDomainList($url, $domains, $expected, $description) { $actual = wfMatchesDomainList($url, $domains); $this->assertEquals($expected, $actual, $description); }
/** * Add content from plain text * @since 1.17 * @param $bar array * @param $text string * @return Array */ function addToSidebarPlain(&$bar, $text) { $lines = explode("\n", $text); $heading = ''; foreach ($lines as $line) { if (strpos($line, '*') !== 0) { continue; } $line = rtrim($line, "\r"); // for Windows compat if (strpos($line, '**') !== 0) { $heading = trim($line, '* '); if (!array_key_exists($heading, $bar)) { $bar[$heading] = array(); } } else { $line = trim($line, '* '); if (strpos($line, '|') !== false) { // sanity check $line = MessageCache::singleton()->transform($line, false, null, $this->getTitle()); $line = array_map('trim', explode('|', $line, 2)); if (count($line) !== 2) { // Second sanity check, could be hit by people doing // funky stuff with parserfuncs... (bug 33321) continue; } $extraAttribs = array(); $msgLink = $this->msg($line[0])->inContentLanguage(); if ($msgLink->exists()) { $link = $msgLink->text(); if ($link == '-') { continue; } } else { $link = $line[0]; } $msgText = $this->msg($line[1]); if ($msgText->exists()) { $text = $msgText->text(); } else { $text = $line[1]; } if (preg_match('/^(?i:' . wfUrlProtocols() . ')/', $link)) { $href = $link; // Parser::getExternalLinkAttribs won't work here because of the Namespace things global $wgNoFollowLinks, $wgNoFollowDomainExceptions; if ($wgNoFollowLinks && !wfMatchesDomainList($href, $wgNoFollowDomainExceptions)) { $extraAttribs['rel'] = 'nofollow'; } global $wgExternalLinkTarget; if ($wgExternalLinkTarget) { $extraAttribs['target'] = $wgExternalLinkTarget; } } else { $title = Title::newFromText($link); if ($title) { $title = $title->fixSpecialName(); $href = $title->getLinkURL(); } else { $href = 'INVALID-TITLE'; } } $bar[$heading][] = array_merge(array('text' => $text, 'href' => $href, 'id' => 'n-' . Sanitizer::escapeId(strtr($line[1], ' ', '-'), 'noninitial'), 'active' => false), $extraAttribs); } else { continue; } } } return $bar; }
/** * Parse specific link * * @param $line string * @param $parser Parser current parser * @param $frame Frame current frame * @return array */ static function parseButtonLink($line, $parser, $frame) { $extraAttribs = array(); $href_implicit = false; $active = false; // semantic queries if (strpos($line, '{{#ask:') === 0) { if ($parser->getTitle() instanceof Title) { $semanticQuery = substr($line, 7, -2); $semanticHitNumber = $parser->recursiveTagParse('{{#ask:' . $semanticQuery . '|format=count}}', false); if (!is_numeric($semanticHitNumber) || $semanticHitNumber < 1) { return array(array('text' => $semanticQuery, 'href' => 'INVALID QUERY')); } $semanticHits = $parser->recursiveTagParse('{{#ask:' . $semanticQuery . '|format=list|link=none}}', false); $semanticHits = explode(',', $semanticHits); $semanticLinks = array(); foreach ($semanticHits as $semanticHit) { $semanticLink = TweekiHooks::parseButtonLink($semanticHit, $parser, $frame); $semanticLinks[] = $semanticLink[0]; } return $semanticLinks; } else { $text = 'broken'; } } $line = explode('|', $line); foreach ($line as &$single_line) { $single_line = trim($single_line); } // is the text explicitly set? $href = $line[0]; if (isset($line[1]) && $line[1] != "") { $text = $line[1]; } else { $href_implicit = true; $text = $line[0]; } // parse text $msgText = wfMessage($text)->inContentLanguage(); if ($msgText->exists()) { $text = $msgText->parse(); } else { if ($parser->getTitle() instanceof Title) { $text = $parser->recursiveTagParse($text, $frame); } else { $text = 'INVALID-TITLE/PARSER-BROKEN'; } } // parse href $msgLink = wfMessage($href)->inContentLanguage(); if ($msgLink->exists()) { $href = $msgLink->parse(); } else { if ($parser->getTitle() instanceof Title) { $href = $parser->replaceVariables($href, $frame); } else { $href = 'INVALID-HREF/PARSER-BROKEN'; } } if (preg_match('/^(?i:' . wfUrlProtocols() . ')/', $href)) { // Parser::getExternalLinkAttribs won't work here because of the Namespace things global $wgNoFollowLinks, $wgNoFollowDomainExceptions; if ($wgNoFollowLinks && !wfMatchesDomainList($href, $wgNoFollowDomainExceptions)) { $extraAttribs['rel'] = 'nofollow'; } global $wgExternalLinkTarget; if ($wgExternalLinkTarget) { $extraAttribs['target'] = $wgExternalLinkTarget; } } else { $title = Title::newFromText($href); if ($title) { if ($title->equals($parser->getTitle())) { $active = true; } $title = $title->fixSpecialName(); $href = $title->getLinkURL(); } else { $href = 'INVALID-TITLE:' . $href; } } if (isset($line[2]) && $line[2] != "") { $extraAttribs['class'] = $line[2]; } $link = array_merge(array('html' => $text, 'href' => $href, 'href_implicit' => $href_implicit, 'id' => 'n-' . Sanitizer::escapeId(strtr($line[0], ' ', '-'), 'noninitial'), 'active' => $active), $extraAttribs); return array($link); }