Implement static public Parser::getExternalLinkRel
authorMarius Hoch <hoo@online.de>
Mon, 12 Nov 2012 22:52:43 +0000 (23:52 +0100)
committerMarius Hoch <hoo@online.de>
Fri, 30 Nov 2012 20:20:38 +0000 (21:20 +0100)
I've implemented the function Parser::getExternalLinkRel which
gives the 'rel' attribute for a given link in a given NS. Per Tim's
suggestion, as it's currently impossible to invoke the logic in
Parser::getExternalLinkAttribs externally.

Change-Id: Id0bfed81e2afd6730d820b6c9a4a09155a557f37

includes/parser/Parser.php

index 9dad1e5..d9fcdf9 100644 (file)
@@ -1606,7 +1606,25 @@ class Parser {
                wfProfileOut( __METHOD__ );
                return $s;
        }
-
+       /**
+        * Get the rel attribute for a particular external link.
+        *
+        * @since 1.21
+        * @param $url String|bool 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
@@ -1619,13 +1637,8 @@ class Parser {
         */
        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';
-               }
+               $attribs['rel'] = self::getExternalLinkRel( $url, $this->mTitle );
+
                if ( $this->mOptions->getExternalLinkTarget() ) {
                        $attribs['target'] = $this->mOptions->getExternalLinkTarget();
                }