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
/** 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;
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 );
}
/**
- * Adds metadata links (Creative Commons/Dublin Core/copyright) to the HTML
- * output.
+ * Adds metadata links below to the HTML output.
+ * <ol>
+ * <li>Creative Commons
+ * <br />See http://wiki.creativecommons.org/Extend_Metadata.
+ * </li>
+ * <li>Dublin Core</li>
+ * <li>Use hreflang to specify canonical and alternate links
+ * <br />See http://www.google.com/support/webmasters/bin/answer.py?answer=189077
+ * </li>
+ * <li>Copyright</li>
+ * <ol>
+ *
* @param $out Object: instance of OutputPage
*/
function addMetadataLinks( OutputPage $out ) {
global $wgEnableDublinCoreRdf, $wgEnableCreativeCommonsRdf;
+ global $wgDisableLangConversion, $wgDisableLangCanonical, $wgContLang;
global $wgRightsPage, $wgRightsUrl;
if ( $out->isArticleRelated() ) {
);
}
}
+
+ 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 );
$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 ) {
/**
* default language setting
*/
- $variant = $wgContLang->getPreferredVariant( false );
+ $variant = $wgContLang->getDefaultVariant();
$defOpt['variant'] = $variant;
$defOpt['language'] = $variant;
foreach( SearchEngine::searchableNamespaces() as $nsnum => $nsname ) {
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" .
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 ''; }
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();
}
/**
/**
* 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();
}
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
*
* @return Mixed: variant if one found, false otherwise.
*/
- protected function getURLVariant() {
+ public function getURLVariant() {
global $wgRequest;
if ( $this->mURLVariant ) {