r111563 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111562‎ | r111563 | r111564 >
Date:19:23, 15 February 2012
Author:maxsem
Status:ok
Tags:
Comment:
* Special Main Page parsing rules available via the API
* Fixed prepending and appending HTML
Modified paths:
  • /trunk/extensions/MobileFrontend/ApiParseExtender.php (modified) (history)
  • /trunk/extensions/MobileFrontend/MobileFormatter.php (modified) (history)
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/MobileFormatter.php
@@ -20,6 +20,7 @@
2121 protected $title;
2222
2323 protected $expandableSections = false;
 24+ protected $mainPage = false;
2425
2526 private $headings = 0;
2627
@@ -115,6 +116,10 @@
116117 $this->expandableSections = $flag;
117118 }
118119
 120+ public function setIsMainPage( $value = true ) {
 121+ $this->mainPage = $value;
 122+ }
 123+
119124 /**
120125 * Sets whether images should be removed from output
121126 * @param bool $flag
@@ -236,12 +241,16 @@
237242
238243 public function getText( $id = false, $prependHtml = '', $appendHtml = '' ) {
239244 wfProfileIn( __METHOD__ );
240 - $element = $id ? $this->doc->getElementById( $id ) : null;
241 - $html = $this->doc->saveXML( $element, LIBXML_NOEMPTYTAG );
 245+ if ( $this->mainPage ) {
 246+ $element = $this->parseMainPage( $this->doc );
 247+ } else {
 248+ $element = $id ? $this->doc->getElementById( $id ) : null;
 249+ }
 250+ $html = $prependHtml . $this->doc->saveXML( $element, LIBXML_NOEMPTYTAG ) . $appendHtml;
242251
243252 switch ( $this->format ) {
244253 case 'XHTML':
245 - if ( $this->expandableSections && strlen( $html ) > 4000 ) {
 254+ if ( $this->expandableSections && !$this->mainPage && strlen( $html ) > 4000 ) {
246255 $html = $this->headingTransform( $html );
247256 }
248257 break;
@@ -470,4 +479,58 @@
471480 wfProfileOut( __METHOD__ );
472481 return $removals;
473482 }
 483+
 484+ /**
 485+ * @param DOMDocument $mainPage
 486+ * @return DOMElement
 487+ */
 488+ protected function parseMainPage( DOMDocument $mainPage ) {
 489+ wfProfileIn( __METHOD__ );
 490+
 491+ $zeroLandingPage = $mainPage->getElementById( 'zero-landing-page' );
 492+ $featuredArticle = $mainPage->getElementById( 'mp-tfa' );
 493+ $newsItems = $mainPage->getElementById( 'mp-itn' );
 494+
 495+ $xpath = new DOMXpath( $mainPage );
 496+ $elements = $xpath->query( '//*[starts-with(@id, "mf-")]' );
 497+
 498+ $commonAttributes = array( 'mp-tfa', 'mp-itn' );
 499+
 500+ $content = $mainPage->createElement( 'div' );
 501+ $content->setAttribute( 'id', 'content' );
 502+
 503+ if ( $zeroLandingPage ) {
 504+ $content->appendChild( $zeroLandingPage );
 505+ }
 506+
 507+ if ( $featuredArticle ) {
 508+ $h2FeaturedArticle = $mainPage->createElement( 'h2', $this->msg( 'mobile-frontend-featured-article' ) );
 509+ $content->appendChild( $h2FeaturedArticle );
 510+ $content->appendChild( $featuredArticle );
 511+ }
 512+
 513+ if ( $newsItems ) {
 514+ $h2NewsItems = $mainPage->createElement( 'h2', $this->msg( 'mobile-frontend-news-items' ) );
 515+ $content->appendChild( $h2NewsItems );
 516+ $content->appendChild( $newsItems );
 517+ }
 518+
 519+ foreach ( $elements as $element ) {
 520+ if ( $element->hasAttribute( 'id' ) ) {
 521+ $id = $element->getAttribute( 'id' );
 522+ if ( !in_array( $id, $commonAttributes ) ) {
 523+ $elementTitle = $element->hasAttribute( 'title' ) ? $element->getAttribute( 'title' ) : '';
 524+ $h2UnknownMobileSection = $mainPage->createElement( 'h2', $elementTitle );
 525+ $br = $mainPage->createElement( 'br' );
 526+ $br->setAttribute( 'CLEAR', 'ALL' );
 527+ $content->appendChild( $h2UnknownMobileSection );
 528+ $content->appendChild( $element );
 529+ $content->appendChild( $br );
 530+ }
 531+ }
 532+ }
 533+
 534+ wfProfileOut( __METHOD__ );
 535+ return $content;
 536+ }
474537 }
Index: trunk/extensions/MobileFrontend/ApiParseExtender.php
@@ -17,6 +17,7 @@
1818 );
1919 $params['expandablesections'] = false;
2020 $params['noimages'] = false;
 21+ $params['mainpage'] = false;
2122 }
2223 return true;
2324 }
@@ -33,6 +34,7 @@
3435 $params['expandablesections'] = 'Make sections in mobile output collapsed by default, expandable via JavaScript.'
3536 . " Ignored if `section' parameter is set.";
3637 $params['noimages'] = 'Disable images in mobile output';
 38+ $params['mainpage'] = 'Apply mobile main page transformations';
3739 }
3840 return true;
3941 }
@@ -80,12 +82,16 @@
8183 );
8284 if ( $params['expandablesections'] ) {
8385 if ( isset( $params['section'] ) ) {
84 - $module->setWarning( "`expandablesections' and `section' can't be used simultaneusly" );
 86+ $module->setWarning( "`expandablesections' and `section' can't be used simultaneously" );
8587 } elseif ( !$title->isMainPage() ) {
8688 $mf->enableExpandableSections();
8789 }
8890 }
8991 $mf->removeImages( $params['noimages'] );
 92+ $mf->setIsMainPage( $params['mainpage'] );
 93+ if ( $params['mainpage'] && $params['expandablesections'] ) {
 94+ $module->setWarning( "`mainpage' and `expandablesections' can't be used simultaneously" );
 95+ }
9096 $mf->filterContent();
9197 $data['parse']['text'] = $mf->getText( 'content' );
9298
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -921,58 +921,6 @@
922922 }
923923
924924 /**
925 - * @param DOMDocument $mainPage
926 - */
927 - public function DOMParseMainPage( DOMDocument $mainPage ) {
928 - wfProfileIn( __METHOD__ );
929 -
930 - $zeroLandingPage = $mainPage->getElementById( 'zero-landing-page' );
931 - $featuredArticle = $mainPage->getElementById( 'mp-tfa' );
932 - $newsItems = $mainPage->getElementById( 'mp-itn' );
933 -
934 - $xpath = new DOMXpath( $mainPage );
935 - $elements = $xpath->query( '//*[starts-with(@id, "mf-")]' );
936 -
937 - $commonAttributes = array( 'mp-tfa', 'mp-itn' );
938 -
939 - $content = $mainPage->createElement( 'div' );
940 - $content->setAttribute( 'id', 'content' );
941 -
942 - if ( $zeroLandingPage ) {
943 - $content->appendChild( $zeroLandingPage );
944 - }
945 -
946 - if ( $featuredArticle ) {
947 - $h2FeaturedArticle = $mainPage->createElement( 'h2', self::$messages['mobile-frontend-featured-article'] );
948 - $content->appendChild( $h2FeaturedArticle );
949 - $content->appendChild( $featuredArticle );
950 - }
951 -
952 - if ( $newsItems ) {
953 - $h2NewsItems = $mainPage->createElement( 'h2', self::$messages['mobile-frontend-news-items'] );
954 - $content->appendChild( $h2NewsItems );
955 - $content->appendChild( $newsItems );
956 - }
957 -
958 - foreach ( $elements as $element ) {
959 - if ( $element->hasAttribute( 'id' ) ) {
960 - $id = $element->getAttribute( 'id' );
961 - if ( !in_array( $id, $commonAttributes ) ) {
962 - $elementTitle = $element->hasAttribute( 'title' ) ? $element->getAttribute( 'title' ) : '';
963 - $h2UnknownMobileSection = $mainPage->createElement( 'h2', $elementTitle );
964 - $br = $mainPage->createElement( 'br' );
965 - $br->setAttribute( 'CLEAR', 'ALL' );
966 - $content->appendChild( $h2UnknownMobileSection );
967 - $content->appendChild( $element );
968 - $content->appendChild( $br );
969 - }
970 - }
971 - }
972 -
973 - wfProfileOut( __METHOD__ );
974 - }
975 -
976 - /**
977925 * @return DomElement
978926 */
979927 public function renderLogin() {
@@ -1134,9 +1082,7 @@
11351083 }
11361084 }
11371085
1138 - if ( self::$isMainPage ) {
1139 - $this->DOMParseMainPage( $doc );
1140 - }
 1086+ $formatter->setIsMainPage( self::$isMainPage );
11411087 $prepend = '';
11421088 if ( $this->contentFormat == 'WML' ) {
11431089 // Wml for searching
@@ -1145,7 +1091,7 @@
11461092 '<go href="' . $wgScript . '?title=Special%3ASearch&amp;search=$(search)"></go></do></p>';
11471093 } elseif ( $this->contentFormat == 'XHTML'
11481094 && self::$device['supports_javascript'] === true
1149 - && empty( self::$search ) && !self::$isMainPage )
 1095+ && empty( self::$search ) )
11501096 {
11511097 $formatter->enableExpandableSections();
11521098 }

Status & tagging log