Introduce $wgEnableCanonicalServerLink
authorTim Starling <tstarling@wikimedia.org>
Mon, 31 Dec 2012 01:11:43 +0000 (12:11 +1100)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 7 Jan 2013 01:04:05 +0000 (01:04 +0000)
(bug 43466) If enabled, send a rel=canonical link on every page
indicating the correct server to use. I tested all three callers:
Article, ImagePage and the variant feature in getHeadLinksArray().

Change-Id: I59b7c1d5589825ea390967036190218d5ce2db88

RELEASE-NOTES-1.21
includes/Article.php
includes/DefaultSettings.php
includes/ImagePage.php
includes/OutputPage.php

index 27692e8..e5e5c68 100644 (file)
@@ -60,6 +60,9 @@ production.
   debug mode: wfMsg, wfMsgNoTrans, wfMsgForContent, wfMsgForContentNoTrans,
   wfMsgReal, wfMsgGetKey, wfMsgHtml, wfMsgWikiHtml, wfMsgExt, wfEmptyMsg. Use
   the Message class, or the global method wfMessage.
+* Added $wgEnableCanonicalServerLink, off by default. If enabled, a 
+  <link rel=canonical> tag is added to every page indicating the correct server
+  to use.
 * Debug message emitted by wfDebugLog() will now be prefixed with the group
   name when its logged to the default log file. That is the case whenever the
   group has no key in wgDebugLogGroups, that will help triage the default log.
index 5a887b6..7eb2ee8 100644 (file)
@@ -988,9 +988,7 @@ class Article extends Page {
                                }
 
                                // Add a <link rel="canonical"> tag
-                               $outputPage->addLink( array( 'rel' => 'canonical',
-                                       'href' => $this->getTitle()->getLocalURL() )
-                               );
+                               $outputPage->setCanonicalUrl( $this->getTitle()->getLocalURL() );
 
                                // Tell the output object that the user arrived at this article through a redirect
                                $outputPage->setRedirectedFrom( $this->mRedirectedFrom );
index 9cb1e00..3fe76fc 100644 (file)
@@ -2363,7 +2363,7 @@ $wgDisableLangConversion = false;
 /** Whether to enable language variant conversion for links. */
 $wgDisableTitleConversion = false;
 
-/** Whether to enable cononical language links in meta data. */
+/** Whether to enable canonical language links in meta data. */
 $wgCanonicalLanguageLinks = true;
 
 /** Default variant code, if false, the default will be the language code */
@@ -2832,6 +2832,14 @@ $wgSend404Code = true;
  */
 $wgShowRollbackEditCount = 10;
 
+/**
+ * Output a <link rel="canonical"> tag on every page indicating the canonical
+ * server which should be used, i.e. $wgServer or $wgCanonicalServer. Since 
+ * detection of the current server is unreliable, the link is sent
+ * unconditionally.
+ */
+$wgEnableCanonicalServerLink = false;
+
 /** @} */ # End of output format settings }
 
 /*************************************************************************//**
index c831f64..ae3a08e 100644 (file)
@@ -614,7 +614,7 @@ EOT
 
                /* Add canonical to head if there is no local page for this shared file */
                if( $descUrl && $this->mPage->getID() == 0 ) {
-                       $out->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
+                       $out->setCanonicalUrl( $descUrl );
                }
 
                $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
index c06b770..436e2f9 100644 (file)
@@ -43,6 +43,7 @@ class OutputPage extends ContextSource {
        var $mKeywords = array();
 
        var $mLinktags = array();
+       var $mCanonicalUrl = false;
 
        /// Additional stylesheets. Looks like this is for extensions. Might be replaced by resource loader.
        var $mExtStyles = array();
@@ -316,7 +317,9 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Add a new \<link\> tag to the page header
+        * Add a new \<link\> tag to the page header.
+        *
+        * Note: use setCanonicalUrl() for rel=canonical.
         *
         * @param $linkarr Array: associative array of attributes.
         */
@@ -336,6 +339,14 @@ class OutputPage extends ContextSource {
                $this->addLink( $linkarr );
        }
 
+       /**
+        * Set the URL to be used for the <link rel=canonical>. This should be used
+        * in preference to addLink(), to avoid duplicate link tags.
+        */
+       function setCanonicalUrl( $url ) {
+               $this->mCanonicalUrl = $url;
+       }
+
        /**
         * Get the value of the "rel" attribute for metadata links
         *
@@ -3040,6 +3051,8 @@ $templates
 
                $tags = array();
 
+               $canonicalUrl = $this->mCanonicalUrl;
+
                if ( $addContentType ) {
                        if ( $wgHtml5 ) {
                                # More succinct than <meta http-equiv=Content-Type>, has the
@@ -3184,10 +3197,7 @@ $templates
                                                );
                                        }
                                } else {
-                                       $tags['canonical'] = Html::element( 'link', array(
-                                               'rel' => 'canonical',
-                                               'href' => $this->getTitle()->getCanonicalUrl()
-                                       ) );
+                                       $canonicalUrl = $this->getTitle()->getLocalURL();
                                }
                        }
                }
@@ -3256,6 +3266,24 @@ $templates
                                }
                        }
                }
+
+               # Canonical URL
+               global $wgEnableCanonicalServerLink;
+               if ( $wgEnableCanonicalServerLink ) {
+                       if ( $canonicalUrl !== false ) {
+                               $canonicalUrl = wfExpandUrl( $canonicalUrl, PROTO_CANONICAL );
+                       } else {
+                               $reqUrl = $this->getRequest()->getRequestURL();
+                               $canonicalUrl = wfExpandUrl( $reqUrl, PROTO_CANONICAL );
+                       }
+               }
+               if ( $canonicalUrl !== false ) {
+                       $tags[] = Html::element( 'link', array(
+                               'rel' => 'canonical',
+                               'href' => $canonicalUrl
+                       ) );
+               }
+
                return $tags;
        }