Installer is no longer hardcoded to xhtml doctype
authorNiklas Laxström <nikerabbit@users.mediawiki.org>
Sat, 8 May 2010 13:45:14 +0000 (13:45 +0000)
committerNiklas Laxström <nikerabbit@users.mediawiki.org>
Sat, 8 May 2010 13:45:14 +0000 (13:45 +0000)
Refactored the doctype and html tag building into Html::htmlHeader()
from OutputPage.

includes/Html.php
includes/OutputPage.php
includes/installer/WebInstallerOutput.php

index b56a6d0..f4cb9ce 100644 (file)
@@ -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 .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\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 .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
+                       } else {
+                               # Much saner.
+                               $ret .= "<!doctype html>\n";
+                       }
+                       if ( $wgHtml5Version ) {
+                               $attribs['version'] = $wgHtml5Version;
+                       }
+               } else {
+                       $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\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;
+               }
+       }
 }
index 8a18fa8..a135166 100644 (file)
@@ -2079,9 +2079,8 @@ class OutputPage {
         * @return String: The doctype, opening <html>, 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 .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\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 .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
-                       } else {
-                               # Much saner.
-                               $ret .= "<!doctype html>\n";
-                       }
-                       if ( $wgHtml5Version ) {
-                               $htmlAttribs['version'] = $wgHtml5Version;
-                       }
+                       # More succinct than <meta http-equiv=Content-Type>, has the
+                       # same effect
+                       $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n";
                } else {
-                       $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\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 .= "<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
+               $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
 
-               if ( $wgHtml5 ) {
-                       # More succinct than <meta http-equiv=Content-Type>, has the
-                       # same effect
-                       $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n";
-               }
 
                $ret .= implode( "\n", array(
                        $this->getHeadLinks(),
index 9d0c6b1..e8f746f 100644 (file)
@@ -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 {
                }
 
 ?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
+<?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
 <head>
        <meta name="robots" content="noindex, nofollow" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title><?php $this->outputTitle(); ?></title>
-       <link rel="stylesheet" type="text/css" href="../skins/common/shared.css"/>
-       <link rel="stylesheet" type="text/css" href="../skins/monobook/main.css"/>
-       <link rel="stylesheet" type="text/css" href="../skins/common/config.css"/>
-       <script type="text/javascript"><!--
-<?php echo "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) . ";\n"; ?>
-       // -->
-       </script>
-       <?php $this->outputJQuery(); ?>
-       <script type="text/javascript" src="../skins/common/config.js"></script>
+       <?php echo Html::linkedStyle( '../skins/common/shared.css' ) . "\n"; ?>
+       <?php echo Html::linkedStyle( '../skins/monobook/main.css' ) . "\n"; ?>
+       <?php echo Html::linkedStyle( '../skins/common/config.css' ) . "\n"; ?>
+       <?php echo Html::inlineScript(  "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) ) . "\n"; ?>
+       <?php $this->outputJQuery() . "\n"; ?>
+       <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
 </head>
 
-<body class="<?php print $this->getDir(); ?>">
+<?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
 <noscript>
 <style type="text/css">
 .config-help-message { display: block; }
@@ -168,18 +179,16 @@ class WebInstallerOutput {
        }
 
        function outputShortHeader() {
-               
 ?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
+<?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
 <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="robots" content="noindex, nofollow" />
        <title><?php $this->outputTitle(); ?></title>
-       <link rel="stylesheet" type="text/css" href="../skins/monobook/main.css"/>
-       <link rel="stylesheet" type="text/css" href="../skins/common/config.css"/>
+       <?php echo Html::linkedStyle( '../skins/monobook/main.css' ) . "\n"; ?>
+       <?php echo Html::linkedStyle( '../skins/common/config.css' ) . "\n"; ?>
        <?php $this->outputJQuery(); ?>
-       <script type="text/javascript" src="../skins/common/config.js"></script>
+       <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
 </head>
 
 <body style="background-image: none">
@@ -193,8 +202,7 @@ class WebInstallerOutput {
 
        function outputJQuery() {
                global $wgJQueryVersion;
-               echo '<script type="text/javascript" src="../skins/common/jquery-' .
-                       $wgJQueryVersion . '.min.js"></script>';
+               echo Html::linkedScript( "../skins/common/jquery-$wgJQueryVersion.min.js" );
        }
 
        function outputWarnings() {