Add function OutputPage::getHeadLinksArray()

...that returns head tags as associative array for the benefit of skin
makers who don't want to output every <meta> tag in the universe.

Patchset 2: make sure that array keys are unique

Change-Id: I6fa3c954d603e0e401cbdb68975d536cf07e11ab
This commit is contained in:
Max Semenik 2012-04-24 21:25:06 +04:00
parent 0477dc9473
commit 30d98d655a

View file

@ -2939,12 +2939,11 @@ $templates
} }
/** /**
* @param $unused * @param $addContentType bool: Whether <meta> specifying content type should be returned
* @param $addContentType bool
* *
* @return string HTML tag links to be put in the header. * @return array in format "link name or number => 'link html'".
*/ */
public function getHeadLinks( $unused = null, $addContentType = false ) { public function getHeadLinksArray( $addContentType = false ) {
global $wgUniversalEditButton, $wgFavicon, $wgAppleTouchIcon, $wgEnableAPI, global $wgUniversalEditButton, $wgFavicon, $wgAppleTouchIcon, $wgEnableAPI,
$wgSitename, $wgVersion, $wgHtml5, $wgMimeType, $wgSitename, $wgVersion, $wgHtml5, $wgMimeType,
$wgFeed, $wgOverrideSiteFeed, $wgAdvertisedFeedTypes, $wgFeed, $wgOverrideSiteFeed, $wgAdvertisedFeedTypes,
@ -2957,20 +2956,20 @@ $templates
if ( $wgHtml5 ) { if ( $wgHtml5 ) {
# More succinct than <meta http-equiv=Content-Type>, has the # More succinct than <meta http-equiv=Content-Type>, has the
# same effect # same effect
$tags[] = Html::element( 'meta', array( 'charset' => 'UTF-8' ) ); $tags['meta-charset'] = Html::element( 'meta', array( 'charset' => 'UTF-8' ) );
} else { } else {
$tags[] = Html::element( 'meta', array( $tags['meta-content-type'] = Html::element( 'meta', array(
'http-equiv' => 'Content-Type', 'http-equiv' => 'Content-Type',
'content' => "$wgMimeType; charset=UTF-8" 'content' => "$wgMimeType; charset=UTF-8"
) ); ) );
$tags[] = Html::element( 'meta', array( // bug 15835 $tags['meta-content-style-type'] = Html::element( 'meta', array( // bug 15835
'http-equiv' => 'Content-Style-Type', 'http-equiv' => 'Content-Style-Type',
'content' => 'text/css' 'content' => 'text/css'
) ); ) );
} }
} }
$tags[] = Html::element( 'meta', array( $tags['meta-generator'] = Html::element( 'meta', array(
'name' => 'generator', 'name' => 'generator',
'content' => "MediaWiki $wgVersion", 'content' => "MediaWiki $wgVersion",
) ); ) );
@ -2979,7 +2978,7 @@ $templates
if( $p !== 'index,follow' ) { if( $p !== 'index,follow' ) {
// http://www.robotstxt.org/wc/meta-user.html // http://www.robotstxt.org/wc/meta-user.html
// Only show if it's different from the default robots policy // Only show if it's different from the default robots policy
$tags[] = Html::element( 'meta', array( $tags['meta-robots'] = Html::element( 'meta', array(
'name' => 'robots', 'name' => 'robots',
'content' => $p, 'content' => $p,
) ); ) );
@ -2990,7 +2989,7 @@ $templates
"/<.*?" . ">/" => '', "/<.*?" . ">/" => '',
"/_/" => ' ' "/_/" => ' '
); );
$tags[] = Html::element( 'meta', array( $tags['meta-keywords'] = Html::element( 'meta', array(
'name' => 'keywords', 'name' => 'keywords',
'content' => preg_replace( 'content' => preg_replace(
array_keys( $strip ), array_keys( $strip ),
@ -3007,7 +3006,11 @@ $templates
} else { } else {
$a = 'name'; $a = 'name';
} }
$tags[] = Html::element( 'meta', $tagName = "meta-{$tag[0]}";
if ( isset( $tags[$tagName] ) ) {
$tagName .= $tag[1];
}
$tags[$tagName] = Html::element( 'meta',
array( array(
$a => $tag[0], $a => $tag[0],
'content' => $tag[1] 'content' => $tag[1]
@ -3026,14 +3029,14 @@ $templates
&& ( $this->getTitle()->exists() || $this->getTitle()->quickUserCan( 'create', $user ) ) ) { && ( $this->getTitle()->exists() || $this->getTitle()->quickUserCan( 'create', $user ) ) ) {
// Original UniversalEditButton // Original UniversalEditButton
$msg = $this->msg( 'edit' )->text(); $msg = $this->msg( 'edit' )->text();
$tags[] = Html::element( 'link', array( $tags['universal-edit-button'] = Html::element( 'link', array(
'rel' => 'alternate', 'rel' => 'alternate',
'type' => 'application/x-wiki', 'type' => 'application/x-wiki',
'title' => $msg, 'title' => $msg,
'href' => $this->getTitle()->getLocalURL( 'action=edit' ) 'href' => $this->getTitle()->getLocalURL( 'action=edit' )
) ); ) );
// Alternate edit link // Alternate edit link
$tags[] = Html::element( 'link', array( $tags['alternative-edit'] = Html::element( 'link', array(
'rel' => 'edit', 'rel' => 'edit',
'title' => $msg, 'title' => $msg,
'href' => $this->getTitle()->getLocalURL( 'action=edit' ) 'href' => $this->getTitle()->getLocalURL( 'action=edit' )
@ -3046,15 +3049,15 @@ $templates
# uses whichever one appears later in the HTML source. Make sure # uses whichever one appears later in the HTML source. Make sure
# apple-touch-icon is specified first to avoid this. # apple-touch-icon is specified first to avoid this.
if ( $wgAppleTouchIcon !== false ) { if ( $wgAppleTouchIcon !== false ) {
$tags[] = Html::element( 'link', array( 'rel' => 'apple-touch-icon', 'href' => $wgAppleTouchIcon ) ); $tags['apple-touch-icon'] = Html::element( 'link', array( 'rel' => 'apple-touch-icon', 'href' => $wgAppleTouchIcon ) );
} }
if ( $wgFavicon !== false ) { if ( $wgFavicon !== false ) {
$tags[] = Html::element( 'link', array( 'rel' => 'shortcut icon', 'href' => $wgFavicon ) ); $tags['favicon'] = Html::element( 'link', array( 'rel' => 'shortcut icon', 'href' => $wgFavicon ) );
} }
# OpenSearch description link # OpenSearch description link
$tags[] = Html::element( 'link', array( $tags['opensearch'] = Html::element( 'link', array(
'rel' => 'search', 'rel' => 'search',
'type' => 'application/opensearchdescription+xml', 'type' => 'application/opensearchdescription+xml',
'href' => wfScript( 'opensearch_desc' ), 'href' => wfScript( 'opensearch_desc' ),
@ -3066,7 +3069,7 @@ $templates
# for the MediaWiki API (and potentially additional custom API # for the MediaWiki API (and potentially additional custom API
# support such as WordPress or Twitter-compatible APIs for a # support such as WordPress or Twitter-compatible APIs for a
# blogging extension, etc) # blogging extension, etc)
$tags[] = Html::element( 'link', array( $tags['rsd'] = Html::element( 'link', array(
'rel' => 'EditURI', 'rel' => 'EditURI',
'type' => 'application/rsd+xml', 'type' => 'application/rsd+xml',
// Output a protocol-relative URL here if $wgServer is protocol-relative // Output a protocol-relative URL here if $wgServer is protocol-relative
@ -3086,14 +3089,14 @@ $templates
if ( !$urlvar ) { if ( !$urlvar ) {
$variants = $lang->getVariants(); $variants = $lang->getVariants();
foreach ( $variants as $_v ) { foreach ( $variants as $_v ) {
$tags[] = Html::element( 'link', array( $tags["variant-$_v"] = Html::element( 'link', array(
'rel' => 'alternate', 'rel' => 'alternate',
'hreflang' => $_v, 'hreflang' => $_v,
'href' => $this->getTitle()->getLocalURL( array( 'variant' => $_v ) ) ) 'href' => $this->getTitle()->getLocalURL( array( 'variant' => $_v ) ) )
); );
} }
} else { } else {
$tags[] = Html::element( 'link', array( $tags['canonical'] = Html::element( 'link', array(
'rel' => 'canonical', 'rel' => 'canonical',
'href' => $this->getTitle()->getCanonicalUrl() 'href' => $this->getTitle()->getCanonicalUrl()
) ); ) );
@ -3116,7 +3119,7 @@ $templates
} }
if ( $copyright ) { if ( $copyright ) {
$tags[] = Html::element( 'link', array( $tags['copyright'] = Html::element( 'link', array(
'rel' => 'copyright', 'rel' => 'copyright',
'href' => $copyright ) 'href' => $copyright )
); );
@ -3165,7 +3168,17 @@ $templates
} }
} }
} }
return implode( "\n", $tags ); return $tags;
}
/**
* @param $unused
* @param $addContentType bool: Whether <meta> specifying content type should be returned
*
* @return string HTML tag links to be put in the header.
*/
public function getHeadLinks( $unused = null, $addContentType = false ) {
return implode( "\n", $this->getHeadLinksArray( $addContentType ) );
} }
/** /**