Introduce $wgEnableCanonicalServerLink
[lhc/web/wiklou.git] / includes / OutputPage.php
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;
        }