From: Tim Starling Date: Mon, 31 Dec 2012 01:11:43 +0000 (+1100) Subject: Introduce $wgEnableCanonicalServerLink X-Git-Tag: 1.31.0-rc.0~21127 X-Git-Url: http://git.cyclocoop.org/%7B%7B%20url_for%28%27admin_vote_add%27%29%20%7D%7D?a=commitdiff_plain;h=e0a78d0a33ea19c71c163e57ed0be7dc15a4a871;p=lhc%2Fweb%2Fwiklou.git Introduce $wgEnableCanonicalServerLink (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 --- diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21 index 27692e873d..e5e5c6880e 100644 --- a/RELEASE-NOTES-1.21 +++ b/RELEASE-NOTES-1.21 @@ -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 + 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. diff --git a/includes/Article.php b/includes/Article.php index 5a887b6e8f..7eb2ee8c0c 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -988,9 +988,7 @@ class Article extends Page { } // Add a 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 ); diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 9cb1e008ed..3fe76fc264 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 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 } /*************************************************************************//** diff --git a/includes/ImagePage.php b/includes/ImagePage.php index c831f64370..ae3a08e214 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -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 = "
\n$1\n
\n"; diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c06b7704bf..436e2f9e42 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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 \ tag to the page header + * Add a new \ 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 . 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 , 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; }