From: Philip Tzou Date: Thu, 28 Oct 2010 16:58:39 +0000 (+0000) Subject: 1. Revert the complicated redirection I made in r59754; X-Git-Tag: 1.31.0-rc.0~34237 X-Git-Url: http://git.cyclocoop.org/data/%7B%24admin_url%7Dconfig?a=commitdiff_plain;h=e6c51a414386182a726bcecd76f4d91807efa304;p=lhc%2Fweb%2Fwiklou.git 1. Revert the complicated redirection I made in r59754; 2. Add more Accept-Language XVO cache name for IE; 3. Use hreflang to specify canonical and alternate links, it's search engine friendly when a wiki has multiple variant languages. --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 8489eb9b5f..eaf0f7c804 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -186,6 +186,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN is usually to set it to -1 to disable the limit) * (bug 25397) Allow uploading (not displaying) of WebP images, disabled by default * (bug 23194) Special:ListFiles now has thumbnails +* Use hreflang to specify canonical and alternate links, search engine friendly + when a wiki has multiple variant languages. === Bug fixes in 1.17 === * (bug 17560) Half-broken deletion moved image files to deletion archive diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index fd6bcbb0f7..55774cd8f8 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1971,6 +1971,9 @@ $wgDisableLangConversion = false; /** Whether to enable language variant conversion for links. */ $wgDisableTitleConversion = false; +/** Whether to enable language cononical in meta data. */ +$wgDisableLangCanonical = false; + /** Default variant code, if false, the default will be the language code */ $wgDefaultLanguageVariant = false; diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 1baf8e248e..81918c8d63 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -1430,7 +1430,17 @@ class OutputPage { if( $variant === $wgContLang->getCode() ) { continue; } else { - $aloption[] = "string-contains=$variant"; + $aloption[] = 'string-contains=' . $variant; + + // IE and some other browsers use another form of language code + // in their Accept-Language header, like "zh-CN" or "zh-TW". + // We should handle these too. + $ievariant = explode( '-', $variant ); + if ( count( $ievariant ) == 2 ) { + $ievariant[1] = strtoupper( $ievariant[1] ); + $ievariant = implode( '-', $ievariant ); + $aloption[] = 'string-contains=' . $ievariant; + } } } $this->addVaryHeader( 'Accept-Language', $aloption ); diff --git a/includes/Skin.php b/includes/Skin.php index eb8ebf97a5..dc53517644 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -249,12 +249,23 @@ class Skin extends Linker { } /** - * Adds metadata links (Creative Commons/Dublin Core/copyright) to the HTML - * output. + * Adds metadata links below to the HTML output. + *
    + *
  1. Creative Commons + *
    See http://wiki.creativecommons.org/Extend_Metadata. + *
  2. + *
  3. Dublin Core
  4. + *
  5. Use hreflang to specify canonical and alternate links + *
    See http://www.google.com/support/webmasters/bin/answer.py?answer=189077 + *
  6. + *
  7. Copyright
  8. + *
      + * * @param $out Object: instance of OutputPage */ function addMetadataLinks( OutputPage $out ) { global $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf; + global $wgDisableLangConversion, $wgDisableLangCanonical, $wgContLang; global $wgRightsPage, $wgRightsUrl; if ( $out->isArticleRelated() ) { @@ -275,6 +286,29 @@ class Skin extends Linker { ); } } + + if ( !$wgDisableLangConversion && !$wgDisableLangCanonical + && $wgContLang->hasVariants() ) { + + $urlvar = $wgContLang->getURLVariant(); + + if ( !$urlvar ) { + $variants = $wgContLang->getVariants(); + foreach ( $variants as $_v ) { + $out->addLink( array( + 'rel' => 'alternate', + 'hreflang' => $_v, + 'href' => $this->mTitle->getLocalURL( '', $_v ) ) + ); + } + } else { + $out->addLink( array( + 'rel' => 'canonical', + 'href' => $this->mTitle->getFullURL() ) + ); + } + } + $copyright = ''; if ( $wgRightsPage ) { $copy = Title::newFromText( $wgRightsPage ); diff --git a/includes/Title.php b/includes/Title.php index f3a79edede..517fdea659 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -869,14 +869,6 @@ class Title { $query = wfArrayToCGI( $query ); } - // internal links should point to same variant as current page (only anonymous users) - if ( !$variant && $wgContLang->hasVariants() && !$wgUser->isLoggedIn() ) { - $pref = $wgContLang->getPreferredVariant( false ); - if ( $pref != $wgContLang->getCode() ) { - $variant = $pref; - } - } - if ( $this->isExternal() ) { $url = $this->getFullURL(); if ( $query ) { diff --git a/includes/User.php b/includes/User.php index 9eec233e2c..22ea6dcc5c 100644 --- a/includes/User.php +++ b/includes/User.php @@ -1024,7 +1024,7 @@ class User { /** * default language setting */ - $variant = $wgContLang->getPreferredVariant( false ); + $variant = $wgContLang->getDefaultVariant(); $defOpt['variant'] = $variant; $defOpt['language'] = $variant; foreach( SearchEngine::searchableNamespaces() as $nsnum => $nsname ) { diff --git a/includes/Wiki.php b/includes/Wiki.php index 0c4943d6c4..5bbbe9f466 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -208,19 +208,11 @@ class MediaWiki { throw new ErrorPageError( 'badtitle', 'badtitletext' ); } // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant - } else if( $action == 'view' && !$request->wasPosted() && - ( ( !$request->getVal( 'title' ) || $title->getPrefixedDBKey() != $request->getText( 'title' ) ) || - // No valid variant in URL (if the main-language has multi-variants), to ensure - // anonymous access would always be redirect to a URL with 'variant' parameter - ( !$request->getVal( 'variant' ) && $wgContLang->hasVariants() && !$wgUser->isLoggedIn() ) ) && - !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) ) + } else if( $action == 'view' && !$request->wasPosted() + && ( ( !$request->getVal( 'title' ) || $title->getPrefixedDBKey() != $request->getText( 'title' ) ) ) + && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) ) { - if( !$wgUser->isLoggedIn() ) { - $pref = $wgContLang->getPreferredVariant( false, $fromHeader = true ); - $targetUrl = $title->getFullURL( '', $variant = $pref ); - } - else - $targetUrl = $title->getFullURL(); + $targetUrl = $title->getFullURL(); // Redirect to canonical url, make it a 301 to allow caching if( $targetUrl == $request->getFullRequestURL() ) { $message = "Redirect loop detected!\n\n" . diff --git a/languages/Language.php b/languages/Language.php index 965be76639..0a8bb1e2f1 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -44,6 +44,8 @@ class FakeConverter { function convertTitle( $t ) { return $t->getPrefixedText(); } function getVariants() { return array( $this->mLang->getCode() ); } function getPreferredVariant() { return $this->mLang->getCode(); } + function getDefaultVariant() { return $this->mLang->getCode(); } + function getURLVariant() { return ''; } function getConvRuleTitle() { return false; } function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { } function getExtraHashOptions() { return ''; } @@ -2673,8 +2675,16 @@ class Language { return $this->mConverter->getVariants(); } - function getPreferredVariant( $fromUser = true, $fromHeader = false ) { - return $this->mConverter->getPreferredVariant( $fromUser, $fromHeader ); + function getPreferredVariant() { + return $this->mConverter->getPreferredVariant(); + } + + function getDefaultVariant() { + return $this->mConverter->getDefaultVariant(); + } + + function getURLVariant() { + return $this->mConverter->getURLVariant(); } /** diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 26352436f7..083874cac4 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -134,20 +134,18 @@ class LanguageConverter { /** * Get preferred language variant. - * @param $fromUser Boolean: get it from $wgUser's preferences - * @param $fromHeader Boolean: get it from Accept-Language * @return String: the preferred language code */ - public function getPreferredVariant( $fromUser = true, $fromHeader = false ) { - global $wgDefaultLanguageVariant; + public function getPreferredVariant() { + global $wgDefaultLanguageVariant, $wgUser; $req = $this->getURLVariant(); - if ( $fromUser && !$req ) { + if ( $wgUser->isLoggedIn() && !$req ) { $req = $this->getUserVariant(); } - if ( $fromHeader && !$req ) { + elseif ( !$req ) { $req = $this->getHeaderVariant(); } @@ -165,6 +163,26 @@ class LanguageConverter { return $this->mMainLanguageCode; } + /** + * Get default variant. + * This function would not be affected by user's settings or headers + * @return String: the default variant code + */ + public function getDefaultVariant() { + global $wgDefaultLanguageVariant; + + $req = $this->getURLVariant(); + + if ( $wgDefaultLanguageVariant && !$req ) { + $req = $this->validateVariant( $wgDefaultLanguageVariant ); + } + + if ( $req ) { + return $req; + } + return $this->mMainLanguageCode; + } + /** * Validate the variant * @param $variant String: the variant to validate @@ -183,7 +201,7 @@ class LanguageConverter { * * @return Mixed: variant if one found, false otherwise. */ - protected function getURLVariant() { + public function getURLVariant() { global $wgRequest; if ( $this->mURLVariant ) {