r66068 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66067‎ | r66068 | r66069 >
Date:13:45, 8 May 2010
Author:nikerabbit
Status:resolved
Tags:
Comment:
Installer is no longer hardcoded to xhtml doctype

Refactored the doctype and html tag building into Html::htmlHeader()
from OutputPage.
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/installer/WebInstallerOutput.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2079,9 +2079,8 @@
20802080 * @return String: The doctype, opening <html>, and head element.
20812081 */
20822082 public function headElement( Skin $sk, $includeStyle = true ) {
2083 - global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
2084 - global $wgXhtmlDefaultNamespace, $wgXhtmlNamespaces, $wgHtml5Version;
2085 - global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgHtml5, $wgWellFormedXml;
 2083+ global $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
 2084+ global $wgContLang, $wgUseTrackbacks, $wgStyleVersion, $wgHtml5;
20862085 global $wgUser, $wgRequest, $wgLang;
20872086
20882087 if ( $sk->commonPrintStylesheet() ) {
@@ -2089,58 +2088,29 @@
20902089 }
20912090 $sk->setupUserCss( $this );
20922091
2093 - $ret = '';
 2092+ $dir = $wgContLang->getDir();
 2093+ $htmlAttribs = array( 'lang' => $wgContLanguageCode, 'dir' => $dir );
 2094+ $ret = Html::htmlHeader( $htmlAttribs );
20942095
2095 - if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
2096 - $ret .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
2097 - }
2098 -
20992096 if ( $this->getHTMLTitle() == '' ) {
21002097 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ) );
21012098 }
21022099
2103 - $dir = $wgContLang->getDir();
2104 -
2105 - $htmlAttribs = array( 'lang' => $wgContLanguageCode, 'dir' => $dir );
21062100 if ( $wgHtml5 ) {
2107 - if ( $wgWellFormedXml ) {
2108 - # Unknown elements and attributes are okay in XML, but unknown
2109 - # named entities are well-formedness errors and will break XML
2110 - # parsers. Thus we need a doctype that gives us appropriate
2111 - # entity definitions. The HTML5 spec permits four legacy
2112 - # doctypes as obsolete but conforming, so let's pick one of
2113 - # those, although it makes our pages look like XHTML1 Strict.
2114 - # Isn't compatibility great?
2115 - $ret .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
2116 - } else {
2117 - # Much saner.
2118 - $ret .= "<!doctype html>\n";
2119 - }
2120 - if ( $wgHtml5Version ) {
2121 - $htmlAttribs['version'] = $wgHtml5Version;
2122 - }
 2101+ # More succinct than <meta http-equiv=Content-Type>, has the
 2102+ # same effect
 2103+ $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n";
21232104 } else {
2124 - $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n";
2125 - $htmlAttribs['xmlns'] = $wgXhtmlDefaultNamespace;
2126 - foreach ( $wgXhtmlNamespaces as $tag => $ns ) {
2127 - $htmlAttribs["xmlns:$tag"] = $ns;
2128 - }
21292105 $this->addMeta( 'http:Content-Type', "$wgMimeType; charset=$wgOutputEncoding" );
21302106 }
2131 - $ret .= Html::openElement( 'html', $htmlAttribs ) . "\n";
21322107
21332108 $openHead = Html::openElement( 'head' );
21342109 if ( $openHead ) {
21352110 # Don't bother with the newline if $head == ''
21362111 $ret .= "$openHead\n";
21372112 }
2138 - $ret .= "<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
 2113+ $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n";
21392114
2140 - if ( $wgHtml5 ) {
2141 - # More succinct than <meta http-equiv=Content-Type>, has the
2142 - # same effect
2143 - $ret .= Html::element( 'meta', array( 'charset' => $wgOutputEncoding ) ) . "\n";
2144 - }
21452115
21462116 $ret .= implode( "\n", array(
21472117 $this->getHeadLinks(),
Index: trunk/phase3/includes/Html.php
@@ -561,4 +561,66 @@
562562 }
563563 return self::element( 'textarea', $attribs, $value );
564564 }
 565+
 566+ /**
 567+ * Constructs the opening html-tag with necessary doctypes depending on
 568+ * global variables.
 569+ *
 570+ * @param $attribs array Associative array of miscellaneous extra
 571+ * attributes, passed to Html::element() of html tag.
 572+ * @return string Raw HTML
 573+ */
 574+ public static function htmlHeader( $attribs = array() ) {
 575+ $ret = '';
 576+
 577+ global $wgMimeType, $wgOutputEncoding;
 578+ if ( self::isXmlMimeType( $wgMimeType ) ) {
 579+ $ret .= "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?" . ">\n";
 580+ }
 581+
 582+ global $wgHtml5, $wgHtml5Version, $wgWellFormedXml, $wgDocType, $wgDTD;
 583+ global $wgXhtmlNamespaces, $wgXhtmlDefaultNamespace;
 584+ if ( $wgHtml5 ) {
 585+ if ( $wgWellFormedXml ) {
 586+ # Unknown elements and attributes are okay in XML, but unknown
 587+ # named entities are well-formedness errors and will break XML
 588+ # parsers. Thus we need a doctype that gives us appropriate
 589+ # entity definitions. The HTML5 spec permits four legacy
 590+ # doctypes as obsolete but conforming, so let's pick one of
 591+ # those, although it makes our pages look like XHTML1 Strict.
 592+ # Isn't compatibility great?
 593+ $ret .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
 594+ } else {
 595+ # Much saner.
 596+ $ret .= "<!doctype html>\n";
 597+ }
 598+ if ( $wgHtml5Version ) {
 599+ $attribs['version'] = $wgHtml5Version;
 600+ }
 601+ } else {
 602+ $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\" \"$wgDTD\">\n";
 603+ $attribs['xmlns'] = $wgXhtmlDefaultNamespace;
 604+ foreach ( $wgXhtmlNamespaces as $tag => $ns ) {
 605+ $attribs["xmlns:$tag"] = $ns;
 606+ }
 607+ }
 608+ return $ret . Html::openElement( 'html', $attribs ) . "\n";
 609+ }
 610+
 611+ /**
 612+ * Determines if the given mime type is xml.
 613+ *
 614+ * @param $mimetype string MimeType
 615+ * @return Boolean
 616+ */
 617+ public static function isXmlMimeType( $mimetype ) {
 618+ switch ( $mimetype ) {
 619+ case 'text/xml':
 620+ case 'application/xhtml+xml':
 621+ case 'application/xml':
 622+ return true;
 623+ default:
 624+ return false;
 625+ }
 626+ }
565627 }
Index: trunk/phase3/includes/installer/WebInstallerOutput.php
@@ -79,6 +79,21 @@
8080 return 'rtl';
8181 }
8282
 83+ function getLanguageCode() {
 84+ global $wgLang;
 85+ if( !is_object( $wgLang ) )
 86+ return 'en';
 87+ else
 88+ return $wgLang->getCode();
 89+ }
 90+
 91+ function getHeadAttribs() {
 92+ return array(
 93+ 'dir' => $this->getDir(),
 94+ 'lang' => $this->getLanguageCode(),
 95+ );
 96+ }
 97+
8398 function headerDone() {
8499 return $this->headerDone;
85100 }
@@ -99,24 +114,20 @@
100115 }
101116
102117 ?>
103 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
104 -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
 118+<?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
105119 <head>
106120 <meta name="robots" content="noindex, nofollow" />
107121 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
108122 <title><?php $this->outputTitle(); ?></title>
109 - <link rel="stylesheet" type="text/css" href="../skins/common/shared.css"/>
110 - <link rel="stylesheet" type="text/css" href="../skins/monobook/main.css"/>
111 - <link rel="stylesheet" type="text/css" href="../skins/common/config.css"/>
112 - <script type="text/javascript"><!--
113 -<?php echo "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) . ";\n"; ?>
114 - // -->
115 - </script>
116 - <?php $this->outputJQuery(); ?>
117 - <script type="text/javascript" src="../skins/common/config.js"></script>
 123+ <?php echo Html::linkedStyle( '../skins/common/shared.css' ) . "\n"; ?>
 124+ <?php echo Html::linkedStyle( '../skins/monobook/main.css' ) . "\n"; ?>
 125+ <?php echo Html::linkedStyle( '../skins/common/config.css' ) . "\n"; ?>
 126+ <?php echo Html::inlineScript( "var dbTypes = " . Xml::encodeJsVar( $dbTypes ) ) . "\n"; ?>
 127+ <?php $this->outputJQuery() . "\n"; ?>
 128+ <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
118129 </head>
119130
120 -<body class="<?php print $this->getDir(); ?>">
 131+<?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
121132 <noscript>
122133 <style type="text/css">
123134 .config-help-message { display: block; }
@@ -168,18 +179,16 @@
169180 }
170181
171182 function outputShortHeader() {
172 -
173183 ?>
174 -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
175 -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
 184+<?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
176185 <head>
177186 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
178187 <meta name="robots" content="noindex, nofollow" />
179188 <title><?php $this->outputTitle(); ?></title>
180 - <link rel="stylesheet" type="text/css" href="../skins/monobook/main.css"/>
181 - <link rel="stylesheet" type="text/css" href="../skins/common/config.css"/>
 189+ <?php echo Html::linkedStyle( '../skins/monobook/main.css' ) . "\n"; ?>
 190+ <?php echo Html::linkedStyle( '../skins/common/config.css' ) . "\n"; ?>
182191 <?php $this->outputJQuery(); ?>
183 - <script type="text/javascript" src="../skins/common/config.js"></script>
 192+ <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
184193 </head>
185194
186195 <body style="background-image: none">
@@ -193,8 +202,7 @@
194203
195204 function outputJQuery() {
196205 global $wgJQueryVersion;
197 - echo '<script type="text/javascript" src="../skins/common/jquery-' .
198 - $wgJQueryVersion . '.min.js"></script>';
 206+ echo Html::linkedScript( "../skins/common/jquery-$wgJQueryVersion.min.js" );
199207 }
200208
201209 function outputWarnings() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r66279Fix validity error caused by r66068...simetrical18:25, 12 May 2010

Status & tagging log