r110889 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110888‎ | r110889 | r110890 >
Date:22:34, 7 February 2012
Author:maxsem
Status:ok
Tags:mobile 
Comment:
Avoid extra DOM parse when we already have a document. Includes r110876
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -6,7 +6,7 @@
77 /**
88 * @var DOMDocument
99 */
10 - private $doc, $mainPage;
 10+ private $doc;
1111 public $contentFormat = '';
1212 public $WMLSectionSeperator = '***************************************************************************';
1313
@@ -711,7 +711,7 @@
712712 $domainParts = array_reverse( $domainParts );
713713 // Although some browsers will accept cookies without the initial ., » RFC 2109 requires it to be included.
714714 wfProfileOut( __METHOD__ );
715 - return '.' . $domainParts[1] . '.' . $domainParts[0];
 715+ return count( $domainParts ) >= 2 ? '.' . $domainParts[1] . '.' . $domainParts[0] : $_SERVER['HTTP_HOST'];
716716 }
717717 wfProfileOut( __METHOD__ );
718718 return $_SERVER['HTTP_HOST'];
@@ -1125,32 +1125,22 @@
11261126 }
11271127
11281128 /**
1129 - * @param $html string
 1129+ * @param DOMNode $mainPage
11301130 * @return string
11311131 */
1132 - public function DOMParseMainPage( $html ) {
 1132+ public function DOMParseMainPage( DOMDocument $mainPage ) {
11331133 wfProfileIn( __METHOD__ );
1134 - $html = mb_convert_encoding( $html, 'HTML-ENTITIES', "UTF-8" );
1135 - libxml_use_internal_errors( true );
1136 - $this->mainPage = new DOMDocument();
1137 - // It seems that loadhtml() does not "attach" the html dtd that defines id as an id-attribute to the DOM.
1138 - $this->mainPage->loadHTML( '<?xml encoding="UTF-8"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
1139 - <html><head><title></title></head><body>' . $html . '</body></html>' );
1140 - libxml_use_internal_errors( false );
1141 - $this->mainPage->preserveWhiteSpace = false;
1142 - $this->mainPage->strictErrorChecking = false;
1143 - $this->mainPage->encoding = 'UTF-8';
11441134
1145 - $zeroLandingPage = $this->mainPage->getElementById( 'zero-landing-page' );
1146 - $featuredArticle = $this->mainPage->getElementById( 'mp-tfa' );
1147 - $newsItems = $this->mainPage->getElementById( 'mp-itn' );
 1135+ $zeroLandingPage = $mainPage->getElementById( 'zero-landing-page' );
 1136+ $featuredArticle = $mainPage->getElementById( 'mp-tfa' );
 1137+ $newsItems = $mainPage->getElementById( 'mp-itn' );
11481138
1149 - $xpath = new DOMXpath( $this->mainPage );
 1139+ $xpath = new DOMXpath( $mainPage );
11501140 $elements = $xpath->query( '//*[starts-with(@id, "mf-")]' );
11511141
11521142 $commonAttributes = array( 'mp-tfa', 'mp-itn' );
11531143
1154 - $content = $this->mainPage->createElement( 'div' );
 1144+ $content = $mainPage->createElement( 'div' );
11551145 $content->setAttribute( 'id', 'content' );
11561146
11571147 if ( $zeroLandingPage ) {
@@ -1158,13 +1148,13 @@
11591149 }
11601150
11611151 if ( $featuredArticle ) {
1162 - $h2FeaturedArticle = $this->mainPage->createElement( 'h2', self::$messages['mobile-frontend-featured-article'] );
 1152+ $h2FeaturedArticle = $mainPage->createElement( 'h2', self::$messages['mobile-frontend-featured-article'] );
11631153 $content->appendChild( $h2FeaturedArticle );
11641154 $content->appendChild( $featuredArticle );
11651155 }
11661156
11671157 if ( $newsItems ) {
1168 - $h2NewsItems = $this->mainPage->createElement( 'h2', self::$messages['mobile-frontend-news-items'] );
 1158+ $h2NewsItems = $mainPage->createElement( 'h2', self::$messages['mobile-frontend-news-items'] );
11691159 $content->appendChild( $h2NewsItems );
11701160 $content->appendChild( $newsItems );
11711161 }
@@ -1174,8 +1164,8 @@
11751165 $id = $element->getAttribute( 'id' );
11761166 if ( !in_array( $id, $commonAttributes ) ) {
11771167 $elementTitle = $element->hasAttribute( 'title' ) ? $element->getAttribute( 'title' ) : '';
1178 - $h2UnknownMobileSection = $this->mainPage->createElement( 'h2', $elementTitle );
1179 - $br = $this->mainPage->createElement( 'br' );
 1168+ $h2UnknownMobileSection = $mainPage->createElement( 'h2', $elementTitle );
 1169+ $br = $mainPage->createElement( 'br' );
11801170 $br->setAttribute( 'CLEAR', 'ALL' );
11811171 $content->appendChild( $h2UnknownMobileSection );
11821172 $content->appendChild( $element );
@@ -1184,7 +1174,7 @@
11851175 }
11861176 }
11871177
1188 - $contentHtml = $this->mainPage->saveXML( $content, LIBXML_NOEMPTYTAG );
 1178+ $contentHtml = $mainPage->saveXML( $content, LIBXML_NOEMPTYTAG );
11891179 wfProfileOut( __METHOD__ );
11901180 return $contentHtml;
11911181 }
@@ -1443,12 +1433,11 @@
14441434 }
14451435 }
14461436
1447 - $content = $this->doc->getElementById( 'content' );
1448 -
1449 - $contentHtml = $this->doc->saveXML( $content, LIBXML_NOEMPTYTAG );
1450 -
14511437 if ( self::$isMainPage ) {
1452 - $contentHtml = $this->DOMParseMainPage( $contentHtml );
 1438+ $contentHtml = $this->DOMParseMainPage( $this->doc );
 1439+ } else {
 1440+ $content = $this->doc->getElementById( 'content' );
 1441+ $contentHtml = $this->doc->saveXML( $content, LIBXML_NOEMPTYTAG );
14531442 }
14541443
14551444 $title = htmlspecialchars( self::$title->getText() );

Follow-up revisions

RevisionCommit summaryAuthorDate
r110890Follow-up r110889: fix commentmaxsem22:42, 7 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r110876Fix for localhostmaxsem21:51, 7 February 2012

Status & tagging log