From 0e3ed4741cad61b29dc86eee268e499f3512079d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Sat, 8 May 2010 13:45:14 +0000 Subject: [PATCH] Installer is no longer hardcoded to xhtml doctype Refactored the doctype and html tag building into Html::htmlHeader() from OutputPage. --- includes/Html.php | 62 +++++++++++++++++++++++ includes/OutputPage.php | 48 ++++-------------- includes/installer/WebInstallerOutput.php | 48 ++++++++++-------- 3 files changed, 99 insertions(+), 59 deletions(-) diff --git a/includes/Html.php b/includes/Html.php index b56a6d0c95..f4cb9cea44 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -561,4 +561,66 @@ class Html { } return self::element( 'textarea', $attribs, $value ); } + + /** + * Constructs the opening html-tag with necessary doctypes depending on + * global variables. + * + * @param $attribs array Associative array of miscellaneous extra + * attributes, passed to Html::element() of html tag. + * @return string Raw HTML + */ + public static function htmlHeader( $attribs = array() ) { + $ret = ''; + + global $wgMimeType, $wgOutputEncoding; + if ( self::isXmlMimeType( $wgMimeType ) ) { + $ret .= "\n"; + } + + global $wgHtml5, $wgHtml5Version, $wgWellFormedXml, $wgDocType, $wgDTD; + global $wgXhtmlNamespaces, $wgXhtmlDefaultNamespace; + if ( $wgHtml5 ) { + if ( $wgWellFormedXml ) { + # Unknown elements and attributes are okay in XML, but unknown + # named entities are well-formedness errors and will break XML + # parsers. Thus we need a doctype that gives us appropriate + # entity definitions. The HTML5 spec permits four legacy + # doctypes as obsolete but conforming, so let's pick one of + # those, although it makes our pages look like XHTML1 Strict. + # Isn't compatibility great? + $ret .= "\n"; + } else { + # Much saner. + $ret .= "\n"; + } + if ( $wgHtml5Version ) { + $attribs['version'] = $wgHtml5Version; + } + } else { + $ret .= "\n"; + $attribs['xmlns'] = $wgXhtmlDefaultNamespace; + foreach ( $wgXhtmlNamespaces as $tag => $ns ) { + $attribs["xmlns:$tag"] = $ns; + } + } + return $ret . Html::openElement( 'html', $attribs ) . "\n"; + } + + /** + * Determines if the given mime type is xml. + * + * @param $mimetype string MimeType + * @return Boolean + */ + public static function isXmlMimeType( $mimetype ) { + switch ( $mimetype ) { + case 'text/xml': + case 'application/xhtml+xml': + case 'application/xml': + return true; + default: + return false; + } + } } diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 8a18fa87f8..a135166e66 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2079,9 +2079,8 @@ class OutputPage { * @return String: The doctype, opening , and head element. */ public function headElement( Skin $sk, $includeStyle = true ) { - global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType; - global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces, $wgHtml5Version; - global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgHtml5, $wgWellFormedXml; + global $wgContLanguageCode, $wgOutputEncoding, $wgMimeType; + global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgHtml5; global $wgUser, $wgRequest, $wgLang; if ( $sk->commonPrintStylesheet() ) { @@ -2089,58 +2088,29 @@ class OutputPage { } $sk->setupUserCss( $this ); - $ret = ''; - - if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) { - $ret .= "\n"; - } + $dir = $wgContLang->getDir(); + $htmlAttribs = array( 'lang' => $wgContLanguageCode, 'dir' => $dir ); + $ret = Html::htmlHeader( $htmlAttribs ); if ( $this->getHTMLTitle() == '' ) { $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ) ); } - $dir = $wgContLang->getDir(); - - $htmlAttribs = array( 'lang' => $wgContLanguageCode, 'dir' => $dir ); if ( $wgHtml5 ) { - if ( $wgWellFormedXml ) { - # Unknown elements and attributes are okay in XML, but unknown - # named entities are well-formedness errors and will break XML - # parsers. Thus we need a doctype that gives us appropriate - # entity definitions. The HTML5 spec permits four legacy - # doctypes as obsolete but conforming, so let's pick one of - # those, although it makes our pages look like XHTML1 Strict. - # Isn't compatibility great? - $ret .= "\n"; - } else { - # Much saner. - $ret .= "\n"; - } - if ( $wgHtml5Version ) { - $htmlAttribs['version'] = $wgHtml5Version; - } + # More succinct than , has the + # same effect + $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n"; } else { - $ret .= "\n"; - $htmlAttribs['xmlns'] = $wgXhtmlDefaultNamespace; - foreach ( $wgXhtmlNamespaces as $tag => $ns ) { - $htmlAttribs["xmlns:$tag"] = $ns; - } $this->addMeta( 'http:Content-Type', "$wgMimeType; charset=$wgOutputEncoding" ); } - $ret .= Html::openElement( 'html', $htmlAttribs ) . "\n"; $openHead = Html::openElement( 'head' ); if ( $openHead ) { # Don't bother with the newline if $head == '' $ret .= "$openHead\n"; } - $ret .= "" . htmlspecialchars( $this->getHTMLTitle() ) . "\n"; + $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n"; - if ( $wgHtml5 ) { - # More succinct than , has the - # same effect - $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n"; - } $ret .= implode( "\n", array( $this->getHeadLinks(), diff --git a/includes/installer/WebInstallerOutput.php b/includes/installer/WebInstallerOutput.php index 9d0c6b1289..e8f746f9b1 100644 --- a/includes/installer/WebInstallerOutput.php +++ b/includes/installer/WebInstallerOutput.php @@ -79,6 +79,21 @@ class WebInstallerOutput { return 'rtl'; } + function getLanguageCode() { + global $wgLang; + if( !is_object( $wgLang ) ) + return 'en'; + else + return $wgLang->getCode(); + } + + function getHeadAttribs() { + return array( + 'dir' => $this->getDir(), + 'lang' => $this->getLanguageCode(), + ); + } + function headerDone() { return $this->headerDone; } @@ -99,24 +114,20 @@ class WebInstallerOutput { } ?> - - +getHeadAttribs() ); ?> <?php $this->outputTitle(); ?> - - - - - outputJQuery(); ?> - + + + + + outputJQuery() . "\n"; ?> + - + $this->getDir() ) ) . "\n"; ?>