Yurik has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/75289


Change subject: Handle MobileFrontendBeforeDOM hook instead of double parsing
......................................................................

Handle MobileFrontendBeforeDOM hook instead of double parsing

* Removed previous onMinervaPreRender DOM parsing and HTML replacing
* Added onMobileFrontendBeforeDOM handler that uses
formatter->getDoc() to modify DOM object
* For config:showImages==false, optimized execution speed and removed
URL's ?renderZeroRatedBanner=true rewriting
* Removed dead code in external link decoration
* Removed getVersion() call

Change-Id: I8f45e48ea6c3f6860641458c1da544b9067c2477
---
M ZeroRatedMobileAccess.php
M includes/PageRenderingHooks.php
2 files changed, 86 insertions(+), 107 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroRatedMobileAccess 
refs/changes/89/75289/1

diff --git a/ZeroRatedMobileAccess.php b/ZeroRatedMobileAccess.php
index 208877f..08de84f 100644
--- a/ZeroRatedMobileAccess.php
+++ b/ZeroRatedMobileAccess.php
@@ -87,6 +87,7 @@
 $wgHooks['BeforePageDisplayMobile'][] = $ns . 
'PageRenderingHooks::onBeforePageDisplay';
 $wgHooks['GetMobileUrl'][] = $ns . 'PageRenderingHooks::onGetMobileUrl';
 $wgHooks['MinervaPreRender'][] = $ns . 
'PageRenderingHooks::onMinervaPreRender';
+$wgHooks['MobileFrontendBeforeDOM'][] = $ns . 
'PageRenderingHooks::onMobileFrontendBeforeDOM';
 $wgHooks['GetMobileNotice'][] = $ns . 'PageRenderingHooks::onGetMobileNotice';
 
 // Registers hook and content handlers for JSON Zero config pages iff
diff --git a/includes/PageRenderingHooks.php b/includes/PageRenderingHooks.php
index 9ba87cb..3aa68a7 100644
--- a/includes/PageRenderingHooks.php
+++ b/includes/PageRenderingHooks.php
@@ -5,14 +5,16 @@
 use DOMDocument;
 use DOMElement;
 use DOMXPath;
-use Html;
 use FormatJson;
+use Html;
 use Language;
+use MinervaTemplate;
+use MobileContext;
 use OutputPage;
 use RawMessage;
 use ResourceLoader;
-use Title;
 use SkinTemplate;
+use Title;
 use WebRequest;
 
 /**
@@ -50,10 +52,86 @@
        }
 
        /**
-        * @param BaseTemplate $template
+        * @param MobileContext $context
+        * @param \MobileFormatter $formatter
         * @return bool
         */
-       public static function onMinervaPreRender( BaseTemplate $template ) {
+       public static function onMobileFrontendBeforeDOM( MobileContext 
$context, \MobileFormatter $formatter ) {
+
+               if ( !self::isZeroSite() ) {
+                       return true;
+               }
+
+               global $wgRequest;
+
+               wfProfileIn( __METHOD__ );
+               $isFilePage = $context->getTitle()->inNamespace( NS_FILE );
+
+               $doc = $formatter->getDoc();
+               $xpath = new DOMXpath( $doc );
+               $config = self::getConfig();
+
+               if ( !$isFilePage && self::disableImages() ) {
+                       $tagToReplaceNodes = $doc->getElementsByTagName( 'img' 
);
+                       $tagToReplaceNodesCollection = array();
+                       /* @var $tagToReplaceNode DOMElement */
+                       foreach ( $tagToReplaceNodes as $tagToReplaceNode ) {
+                               if ( $tagToReplaceNode ) {
+                                       $alt = $tagToReplaceNode->getAttribute( 
'alt' );
+                                       $spanNodeText = wfMessage(
+                                               
'zero-rated-mobile-access-click-to-view-image',
+                                               lcfirst( substr( $alt, 0, 40 ) )
+                                       )->text();
+                                       $spanNode = $doc->createElement( 
"span", str_replace( "&", "&", $spanNodeText ) );
+                                       if ( $alt ) {
+                                               $spanNode->setAttribute( 
'title', $alt );
+                                       }
+                                       $tagToReplaceNodesCollection[] = array( 
'tagToReplaceNode' => $tagToReplaceNode, 'spanNode' => $spanNode );
+                               }
+                       }
+
+                       foreach ( $tagToReplaceNodesCollection as $element ) {
+                               
$element['tagToReplaceNode']->parentNode->replaceChild( $element['spanNode'], 
$element['tagToReplaceNode'] );
+                       }
+               }
+
+               if ( !$config['showImages'] ) {
+                       $zeroRatedLinks = $xpath->query( "//a[@class='image']" 
);
+                       /* @var $zeroRatedLink DOMElement */
+                       foreach ( $zeroRatedLinks as $zeroRatedLink ) {
+                               $zeroRatedLinkHref = 
$zeroRatedLink->getAttribute( 'href' );
+                               if ( $zeroRatedLinkHref && substr( 
$zeroRatedLinkHref, 0, 1 ) !== '#' ) {
+                                       $zeroPartnerUrl = 
$wgRequest->appendQuery(
+                                               
'renderZeroRatedBanner=true&renderwarning=yes&returnto='
+                                               . urlencode( wfAppendQuery( 
$zeroRatedLinkHref, 'acceptbilling=yes' ) )
+                                       );
+                                       if ( $zeroPartnerUrl ) {
+                                               $zeroRatedLink->setAttribute( 
'href', $zeroPartnerUrl );
+                                       }
+                               }
+                       }
+               }
+
+               $zeroRatedExternalLinks = $xpath->query( 
"//a[contains(@class,'external')]" );
+               /* @var $zeroRatedExternalLink DOMElement */
+               foreach ( $zeroRatedExternalLinks as $zeroRatedExternalLink ) {
+                       $zeroRatedExternalLinkHref = 
$zeroRatedExternalLink->getAttribute( 'href' );
+                       if ( $zeroRatedExternalLinkHref && substr( 
$zeroRatedExternalLinkHref, 0, 1 ) !== '#' ) {
+                               $zeroRatedExternalLink->setAttribute(
+                                       'href',
+                                       
'?renderZeroRatedRedirect=true&returnto=' . urlencode( 
$zeroRatedExternalLinkHref ) );
+                       }
+               }
+
+               wfProfileOut( __METHOD__ );
+               return true;
+       }
+
+       /**
+        * @param MinervaTemplate $template
+        * @return bool
+        */
+       public static function onMinervaPreRender( MinervaTemplate $template ) {
                $req = $template->getSkin()->getRequest();
                $out = $template->getSkin()->getOutput();
 
@@ -207,19 +285,14 @@
                        $options['supported_languages'] = $config['showLangs'];
                }
 
-               if ( $showBanner && $showWarning && $acceptBilling !== 'yes' ) {
-                       $out->clearHTML();
-                       $out->setPageTitle( null );
-               } elseif ( $wgRequest->getFuzzyBool( 'renderZeroRatedRedirect' 
) ) {
+               if ( ( $showBanner && $showWarning && $acceptBilling !== 'yes' )
+                       || $wgRequest->getFuzzyBool( 'renderZeroRatedRedirect' )
+               ) {
                        $out->clearHTML();
                        $out->setPageTitle( null );
                } elseif ( $showBanner && $config !== null ) {
                        $imgHeader = self::disableImages() ? 'X-Images: no' : 
'X-Images: yes';
                        $wgRequest->response()->header( $imgHeader );
-                       $html = $out->getHTML();
-                       $parsedHtml = self::parseLinksForZeroQueryString( 
$config, $html, $isFilePage );
-                       $out->clearHTML();
-                       $out->addHTML( $parsedHtml );
                }
 
                wfProfileOut( __METHOD__ );
@@ -646,96 +719,6 @@
        }
 
        /**
-        * Returns the Html of a page with the various links appended with zero 
partner parameter
-        *
-        * @param array $config
-        * @param String $html: Html of current page
-        * @param bool $isFilePage
-        * @return String
-        */
-       private static function parseLinksForZeroQueryString( $config, $html, 
$isFilePage ) {
-               global $wgRequest;
-               wfProfileIn( __METHOD__ );
-               $html = mb_convert_encoding( $html, 'HTML-ENTITIES', "UTF-8" );
-               libxml_use_internal_errors( true );
-               $doc = new DOMDocument();
-               $doc->loadHTML( '<?xml encoding="UTF-8">' . $html );
-               libxml_use_internal_errors( false );
-               $doc->preserveWhiteSpace = false;
-               $doc->strictErrorChecking = false;
-               $doc->encoding = 'UTF-8';
-
-               $xpath = new DOMXpath( $doc );
-
-               if ( !$isFilePage && self::disableImages() ) {
-                       $tagToReplace = 'img';
-                       $tagToReplaceNodes = $doc->getElementsByTagName( 
$tagToReplace );
-                       $tagToReplaceNodesCollection = array();
-                       /* @var $tagToReplaceNode DOMElement */
-                       foreach ( $tagToReplaceNodes as $tagToReplaceNode ) {
-                               if ( $tagToReplaceNode ) {
-                                       $alt = $tagToReplaceNode->getAttribute( 
'alt' );
-                                       $spanNodeText = wfMessage(
-                                               
'zero-rated-mobile-access-click-to-view-image',
-                                               lcfirst( substr( $alt, 0, 40 ) )
-                                       )->text();
-                                       $spanNode = $doc->createElement( 
"span", str_replace( "&", "&amp;", $spanNodeText ) );
-                                       if ( $alt ) {
-                                               $spanNode->setAttribute( 
'title', $alt );
-                                       }
-                                       $tagToReplaceNodesCollection[] = array( 
'tagToReplaceNode' => $tagToReplaceNode, 'spanNode' => $spanNode );
-                               }
-                       }
-
-                       foreach ( $tagToReplaceNodesCollection as $element ) {
-                               
$element['tagToReplaceNode']->parentNode->replaceChild( $element['spanNode'], 
$element['tagToReplaceNode'] );
-                       }
-               }
-
-               $zeroRatedLinks = $xpath->query( 
"//a[not(contains(@class,'external'))]" );
-               /* @var $zeroRatedLink DOMElement */
-               foreach ( $zeroRatedLinks as $zeroRatedLink ) {
-                       $zeroRatedLinkHref = $zeroRatedLink->getAttribute( 
'href' );
-                       if ( $zeroRatedLinkHref && substr( $zeroRatedLinkHref, 
0, 1 ) !== '#' ) {
-                               if ( $zeroRatedLink->getAttribute( 'class' ) 
=== 'image' ) {
-                                       $zeroPartnerUrl = 
$wgRequest->appendQuery(
-                                               
'renderZeroRatedBanner=true&renderwarning=yes&returnto='
-                                               . urlencode( wfAppendQuery( 
$zeroRatedLinkHref, 'acceptbilling=yes' ) )
-                                       );
-                               } else {
-                                       $zeroPartnerUrl = wfAppendQuery( 
$zeroRatedLinkHref,
-                                               array( 'renderZeroRatedBanner' 
=> 'true' ) );
-                               }
-
-                               if ( $zeroPartnerUrl ) {
-                                       if ( !$config['showImages'] ) {
-                                               $zeroRatedLink->setAttribute( 
'href', $zeroPartnerUrl );
-                                       }
-                               }
-                       }
-               }
-
-               $zeroRatedExternalLinks = $xpath->query( 
"//a[contains(@class,'external')]" );
-               /* @var $zeroRatedExternalLink DOMElement */
-               $zeroRatedLinkHref = '';
-               foreach ( $zeroRatedExternalLinks as $zeroRatedExternalLink ) {
-                       $zeroRatedExternalLinkHref = 
$zeroRatedExternalLink->getAttribute( 'href' );
-                       if ( $zeroRatedExternalLinkHref && substr( 
$zeroRatedExternalLinkHref, 0, 1 ) !== '#' ) {
-                               $zeroPartnerUrl = wfAppendQuery( 
$zeroRatedLinkHref,
-                                       array( 'renderZeroRatedBanner' => 
'true' ) );
-                               if ( $zeroPartnerUrl ) {
-                                       $zeroRatedExternalLink->setAttribute( 
'href', '?renderZeroRatedRedirect=true&returnto=' . urlencode( 
$zeroRatedExternalLinkHref ) );
-                               }
-                       }
-               }
-
-               $output = $doc->saveXML( null, LIBXML_NOEMPTYTAG );
-               wfProfileOut( __METHOD__ );
-               return $output;
-       }
-
-
-       /**
         * Find a message in a dictionary for the given language,
         * or use language fallbacks if message is not defined.
         * @param array $map Dictionary of languageCode => string
@@ -802,10 +785,5 @@
                        $printedHeaders = true;
                }
                wfDebugLog( 'zero', $dbg );
-       }
-
-       public function getVersion() {
-               // @FIXME: Delete this?
-               return __CLASS__ . ': $Id$';
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8f45e48ea6c3f6860641458c1da544b9067c2477
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ZeroRatedMobileAccess
Gerrit-Branch: master
Gerrit-Owner: Yurik <yu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to