Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/401786 )

Change subject: Hygiene: LegacyMainPageTransform
......................................................................

Hygiene: LegacyMainPageTransform

The old way was a little hacky and returned a DOMNode.
Instead of doing that we replace the existing body with the
new body and modify the main page in place.

Change-Id: Icc3dd4ee8fe336102f514af0350c79e3d16cb0fa
---
M includes/MobileFormatter.php
A includes/transforms/LegacyMainPageTransform.php
2 files changed, 111 insertions(+), 85 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/86/401786/1

diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php
index 2ed464c..4db2f63 100644
--- a/includes/MobileFormatter.php
+++ b/includes/MobileFormatter.php
@@ -5,6 +5,7 @@
 use MobileFrontend\Transforms\MoveLeadParagraphTransform;
 use MobileFrontend\Transforms\AddMobileTocTransform;
 use MobileFrontend\Transforms\NoTransform;
+use MobileFrontend\Transforms\LegacyMainPageTransform;
 
 /**
  * Converts HTML into a mobile-friendly version
@@ -457,94 +458,12 @@
         */
        public function getText( $element = null ) {
                if ( $this->mainPage ) {
-                       $element = $this->parseMainPage( $this->getDoc() );
+                       $transform = new LegacyMainPageTransform();
+                       $doc = $this->getDoc();
+                       $transform->transform( $doc->getElementsByTagName( 
'body' )->item( 0 ) );
                }
 
                return parent::getText( $element );
-       }
-
-       /**
-        * Returns interface message text
-        * @param string $key Message key
-        * @return string Wiki text
-        */
-       protected function msg( $key ) {
-               return wfMessage( $key )->text();
-       }
-
-       /**
-        * Performs transformations specific to main page
-        * @param DOMDocument $mainPage Tree to process
-        * @return DOMElement|null
-        */
-       protected function parseMainPage( DOMDocument $mainPage ) {
-               $featuredArticle = $mainPage->getElementById( 'mp-tfa' );
-               $newsItems = $mainPage->getElementById( 'mp-itn' );
-               $centralAuthImages = $mainPage->getElementById( 
'central-auth-images' );
-
-               // Collect all the Main Page DOM elements that have an id 
starting with 'mf-'
-               $xpath = new DOMXpath( $mainPage );
-               $elements = $xpath->query( '//*[starts-with(@id, "mf-")]' );
-
-               // These elements will be handled specially
-               $commonAttributes = [ 'mp-tfa', 'mp-itn' ];
-
-               // Start building the new Main Page content in the $content var
-               $content = $mainPage->createElement( 'div' );
-               $content->setAttribute( 'id', 'mainpage' );
-
-               // If there is a featured article section, add it to the new 
Main Page content
-               if ( $featuredArticle ) {
-                       $h2FeaturedArticle = $mainPage->createElement(
-                               'h2',
-                               $this->msg( 'mobile-frontend-featured-article' )
-                       );
-                       $content->appendChild( $h2FeaturedArticle );
-                       $content->appendChild( $featuredArticle );
-               }
-
-               // If there is a news section, add it to the new Main Page 
content
-               if ( $newsItems ) {
-                       $h2NewsItems = $mainPage->createElement( 'h2', 
$this->msg( 'mobile-frontend-news-items' ) );
-                       $content->appendChild( $h2NewsItems );
-                       $content->appendChild( $newsItems );
-               }
-
-               // Go through all the collected Main Page DOM elements and 
format them for mobile
-               /** @var $element DOMElement */
-               foreach ( $elements as $element ) {
-                       if ( $element->hasAttribute( 'id' ) ) {
-                               $id = $element->getAttribute( 'id' );
-                               // Filter out elements processed specially
-                               if ( !in_array( $id, $commonAttributes ) ) {
-                                       // Convert title attributes into h2 
headers
-                                       $sectionTitle = $element->hasAttribute( 
'title' ) ? $element->getAttribute( 'title' ) : '';
-                                       if ( $sectionTitle !== '' ) {
-                                               $element->removeAttribute( 
'title' );
-                                               $h2UnknownMobileSection =
-                                                       
$mainPage->createElement( 'h2', htmlspecialchars( $sectionTitle ) );
-                                               $content->appendChild( 
$h2UnknownMobileSection );
-                                       }
-                                       $br = $mainPage->createElement( 'br' );
-                                       $br->setAttribute( 'clear', 'all' );
-                                       $content->appendChild( $element );
-                                       $content->appendChild( $br );
-                               }
-                       }
-               }
-
-               // If no mobile-appropriate content has been assembled at this 
point, return null.
-               // This will cause HtmlFormatter to fall back to using the 
entire page.
-               if ( $content->childNodes->length == 0 ) {
-                       return null;
-               }
-
-               // If there are CentralAuth 1x1 images, preserve them unmodified
-               if ( $centralAuthImages ) {
-                       $content->appendChild( $centralAuthImages );
-               }
-
-               return $content;
        }
 
        /**
diff --git a/includes/transforms/LegacyMainPageTransform.php 
b/includes/transforms/LegacyMainPageTransform.php
new file mode 100644
index 0000000..5b20f28
--- /dev/null
+++ b/includes/transforms/LegacyMainPageTransform.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace MobileFrontend\Transforms;
+
+use DOMElement;
+use DOMNode;
+use DOMXpath;
+use DOMDocument;
+
+class LegacyMainPageTransform implements IMobileTransform {
+       /**
+        * Returns interface message text
+        * @param string $key Message key
+        * @return string Wiki text
+        */
+       protected function msg( $key ) {
+               return wfMessage( $key )->text();
+       }
+
+       /**
+        * Takes a main page that has been written for desktop and attempts to
+        * make it mobile friendly.
+        * Will scan for #mp-tfa and #mp-itn elements and wrap them with a 
heading.
+        * Any elements with ids prefixed with `mf-` will be retained in the 
view any
+        * other elements will be discarded.
+        * @param DomElement $node to be transformed
+        */
+       public function transform( DOMElement $node ) {
+               $this->parseMainPage( $node->ownerDocument );
+       }
+
+       /**
+        * Performs transformations specific to main page
+        * @param DOMDocument $mainPage Tree to process
+        * @return DOMElement|null
+        */
+       protected function parseMainPage( DOMDocument $mainPage ) {
+               $featuredArticle = $mainPage->getElementById( 'mp-tfa' );
+               $newsItems = $mainPage->getElementById( 'mp-itn' );
+               $centralAuthImages = $mainPage->getElementById( 
'central-auth-images' );
+
+               // Collect all the Main Page DOM elements that have an id 
starting with 'mf-'
+               $xpath = new DOMXpath( $mainPage );
+               $elements = $xpath->query( '//*[starts-with(@id, "mf-")]' );
+
+               // These elements will be handled specially
+               $commonAttributes = [ 'mp-tfa', 'mp-itn' ];
+
+               // Start building the new Main Page content in the $content var
+               $content = $mainPage->createElement( 'div' );
+               $content->setAttribute( 'id', 'mainpage' );
+
+               // If there is a featured article section, add it to the new 
Main Page content
+               if ( $featuredArticle ) {
+                       $h2FeaturedArticle = $mainPage->createElement(
+                               'h2',
+                               $this->msg( 'mobile-frontend-featured-article' )
+                       );
+                       $content->appendChild( $h2FeaturedArticle );
+                       $content->appendChild( $featuredArticle );
+               }
+
+               // If there is a news section, add it to the new Main Page 
content
+               if ( $newsItems ) {
+                       $h2NewsItems = $mainPage->createElement( 'h2', 
$this->msg( 'mobile-frontend-news-items' ) );
+                       $content->appendChild( $h2NewsItems );
+                       $content->appendChild( $newsItems );
+               }
+
+               // Go through all the collected Main Page DOM elements and 
format them for mobile
+               /** @var $element DOMElement */
+               foreach ( $elements as $element ) {
+                       if ( $element->hasAttribute( 'id' ) ) {
+                               $id = $element->getAttribute( 'id' );
+                               // Filter out elements processed specially
+                               if ( !in_array( $id, $commonAttributes ) ) {
+                                       // Convert title attributes into h2 
headers
+                                       $sectionTitle = $element->hasAttribute( 
'title' ) ? $element->getAttribute( 'title' ) : '';
+                                       if ( $sectionTitle !== '' ) {
+                                               $element->removeAttribute( 
'title' );
+                                               $h2UnknownMobileSection =
+                                                       
$mainPage->createElement( 'h2', htmlspecialchars( $sectionTitle ) );
+                                               $content->appendChild( 
$h2UnknownMobileSection );
+                                       }
+                                       $br = $mainPage->createElement( 'br' );
+                                       $br->setAttribute( 'clear', 'all' );
+                                       $content->appendChild( $element );
+                                       $content->appendChild( $br );
+                               }
+                       }
+               }
+
+               // If there are CentralAuth 1x1 images, preserve them unmodified
+               if ( $centralAuthImages ) {
+                       $content->appendChild( $centralAuthImages );
+               }
+
+               // If mobile-appropriate content has been assembled at this 
point,
+               // we will need to replace the DOM
+               if ( $content->childNodes->length > 0 ) {
+                       $newBody = $mainPage->createElement( 'body' );
+                       $newBody->appendChild( $content );
+                       $oldBody = $mainPage->getElementsByTagName( 'body' 
)->item( 0 );
+                       $oldBody->parentNode->replaceChild( $newBody, $oldBody 
);
+               }
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/401786
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icc3dd4ee8fe336102f514af0350c79e3d16cb0fa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to