Merge "Implement static public Parser::getExternalLinkRel"
authorNikerabbit <niklas.laxstrom@gmail.com>
Fri, 30 Nov 2012 20:29:54 +0000 (20:29 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 30 Nov 2012 20:29:54 +0000 (20:29 +0000)
1  2 
includes/parser/Parser.php

@@@ -246,13 -246,6 +246,13 @@@ class Parser 
                }
        }
  
 +      /**
 +       * Allow extensions to clean up when the parser is cloned
 +       */
 +      function __clone() {
 +              wfRunHooks( 'ParserCloned', array( $this ) );
 +      }
 +
        /**
         * Do various kinds of initialisation on the first call of the parser
         */
         * Also removes comments.
         * @return mixed|string
         */
 -      function preprocess( $text, Title $title, ParserOptions $options, $revid = null ) {
 +      function preprocess( $text, Title $title = null, ParserOptions $options, $revid = null ) {
                wfProfileIn( __METHOD__ );
                $this->startParse( $title, $options, self::OT_PREPROCESS, true );
                if ( $revid !== null ) {
                if ( $text === false ) {
                        # Not an image, make a link
                        $text = Linker::makeExternalLink( $url,
 -                              $this->getConverterLanguage()->markNoConversion($url), true, 'free',
 +                              $this->getConverterLanguage()->markNoConversion( $url, true ),
 +                              true, 'free',
                                $this->getExternalLinkAttribs( $url ) );
                        # Register it in the output object...
                        # Replace unnecessary URL escape codes with their equivalent characters
                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
         */
        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();
                }
                        global $wgTitle;
                        $title = $wgTitle;
                }
 -              if ( !$title ) {
 -                      # It's not uncommon having a null $wgTitle in scripts. See r80898
 -                      # Create a ghost title in such case
 -                      $title = Title::newFromText( 'Dwimmerlaik' );
 -              }
 +
                $text = $this->preprocess( $text, $title, $options );
  
                $executing = false;