https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113028

Revision: 113028
Author:   jdlrobson
Date:     2012-03-05 13:14:37 +0000 (Mon, 05 Mar 2012)
Log Message:
-----------
provide toggle buttons in html for Wikipedia Mobile app

Older version of the Wikipedia Mobile app ( < 1.3 ) do not use
the api and instead request pages in mobile form and strip out the
html but do not run the javascript provided in MobileFrontend as a
result no toggle buttons are rendered and thus sections cannot be toggled
on or off

this fix injects these buttons into the html reversing the work in r111733

When usage of the versions of the Wikipedia Mobile app not using the api
is low we will want to completely revert this revision

Note it was not possible to serve the buttons based on the user agent as some
versions of the Wikipedia Mobile app do not serve a special user agent and it
is important we do not break toggling functionality for users of these apps

Modified Paths:
--------------
    trunk/extensions/MobileFrontend/MobileFormatter.php
    trunk/extensions/MobileFrontend/MobileFrontend.body.php
    trunk/extensions/MobileFrontend/javascripts/application.js
    trunk/extensions/MobileFrontend/javascripts/beta_application.js

Modified: trunk/extensions/MobileFrontend/MobileFormatter.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFormatter.php 2012-03-05 12:44:33 UTC 
(rev 113027)
+++ trunk/extensions/MobileFrontend/MobileFormatter.php 2012-03-05 13:14:37 UTC 
(rev 113028)
@@ -19,6 +19,7 @@
         */
        protected $title;
 
+       protected $expandableSections = false;
        protected $mainPage = false;
 
        private $headings = 0;
@@ -134,6 +135,14 @@
                return $this->format;
        }
 
+       /**
+        * @todo: kill with fire when there will be minimum of pre-1.1 app 
users remaining
+        * @param bool $flag 
+        */
+       public function enableExpandableSections( $flag = true ) {
+               $this->expandableSections = $flag;
+       }
+
        public function setIsMainPage( $value = true ) {
                $this->mainPage = $value;
        }
@@ -283,7 +292,7 @@
                
                switch ( $this->format ) {
                        case 'XHTML':
-                               if ( !$this->mainPage && strlen( $html ) > 4000 
) {
+                               if ( $this->expandableSections && 
!$this->mainPage && strlen( $html ) > 4000 ) {
                                        $html = $this->headingTransform( $html 
);
                                }
                                break;
@@ -348,13 +357,27 @@
                                                                '&#8593;' . 
$backToTop ) .
                                Html::closeElement( 'div' );
                // generate the HTML we are going to inject
-               $base .= Html::openElement( 'h2',
-                       array(
-                               'class' => 'section_heading',
-                               'id' => 'section_' . $this->headings
-                       ) 
-               );
-               $base .=
+               // TODO: remove legacy code for Wikipedia Mobile app < 1.3 
which is not using the api
+               // when usage of said apps is low
+               $buttons = Html::element( 'button',
+                                       array( 'class' => 'section_heading 
show',
+                                                       'section_id' => 
$this->headings ),
+                                                       $show ) .
+                       Html::element( 'button',
+                                       array( 'class' => 'section_heading 
hide',
+                                                       'section_id' => 
$this->headings ),
+                                                       $hide );
+               if ( $this->expandableSections ) {
+                       $h2OnClick = 'javascript:wm_toggle_section(' . 
$this->headings . ');';
+                       $base .= Html::openElement( 'h2',
+                                                       array( 'class' => 
'section_heading',
+                                                                       'id' => 
'section_' . $this->headings, 'onclick' => $h2OnClick ) );
+               } else {
+                       $base .= Html::openElement( 'h2',
+                                                       array( 'class' => 
'section_heading',
+                                                                       'id' => 
'section_' . $this->headings ) );
+               }
+               $base .= $buttons .
                                Html::rawElement( 'span',
                                                array( 'id' => $headlineId ),
                                                                $matches[2] ) .

Modified: trunk/extensions/MobileFrontend/MobileFrontend.body.php
===================================================================
--- trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-05 
12:44:33 UTC (rev 113027)
+++ trunk/extensions/MobileFrontend/MobileFrontend.body.php     2012-03-05 
13:14:37 UTC (rev 113028)
@@ -18,6 +18,7 @@
        public static $mainPageUrl;
        public static $randomPageUrl;
        public static $format;
+       public static $search;
        public static $callback;
        public static $useFormat;
        public static $disableImages;
@@ -417,6 +418,7 @@
                self::$format = $wgRequest->getText( 'format' );
                self::$callback = $wgRequest->getText( 'callback' );
                $this->wmlContext->setRequestedSegment( $wgRequest->getInt( 
'seg', 0 ) );
+               self::$search = $wgRequest->getText( 'search' );
                self::$searchField = $wgRequest->getText( 'search', '' );
 
                $device = new DeviceDetection();
@@ -1059,6 +1061,11 @@
                        $prepend = '<p><input emptyok="true" format="*M" 
type="text" name="search" value="" size="16" />' .
                                '<do type="accept" label="' . 
self::$messages['mobile-frontend-search-submit'] . '">' .
                                '<go href="' . $wgScript . 
'?title=Special%3ASearch&amp;search=$(search)"></go></do></p>';      
+               } elseif ( $this->contentFormat == 'XHTML'
+                       && self::$device['supports_javascript'] === true
+                       && empty( self::$search ) )
+               {
+                       $formatter->enableExpandableSections();
                }
                $contentHtml = $formatter->getText( 'content', $prepend );
 

Modified: trunk/extensions/MobileFrontend/javascripts/application.js
===================================================================
--- trunk/extensions/MobileFrontend/javascripts/application.js  2012-03-05 
12:44:33 UTC (rev 113027)
+++ trunk/extensions/MobileFrontend/javascripts/application.js  2012-03-05 
13:14:37 UTC (rev 113028)
@@ -7,6 +7,11 @@
                var i, results, languageSelection, a, heading,
                        sectionHeadings = utilities( '.section_heading' );
                utilities( document.body ).addClass( 'jsEnabled' );
+
+               // TODO: remove in future - currently enables toggling in 
Wikipedia Mobile App v < 1.3
+               window.wm_toggle_section = wm_toggle_section;
+               utilities( '.section_heading button' ).remove();
+
                function openSectionHandler() {
                        var sectionNumber = this.id ? this.id.split( '_' )[1] : 
-1;
                        if( sectionNumber > -1 ) {
@@ -29,6 +34,7 @@
                }
                for( i = 0; i < sectionHeadings.length; i++ ) {
                        heading = sectionHeadings[i];
+                       heading.removeAttribute( 'onclick' ); // TODO: remove 
any legacy onclick handlers
                        heading.insertBefore( createButton( true ), 
heading.firstChild );
                        heading.insertBefore( createButton( false ), 
heading.firstChild );
                        utilities( heading ).bind( 'click', openSectionHandler 
);
@@ -127,9 +133,20 @@
                function bind( type, handler ) {
                        el.addEventListener( type, handler, false );
                }
+
+               // TODO: support single elements
+               function remove(els) {
+                       var i, el;
+                       for( i = 0; i < els.length; i++ ) {
+                               el = els[i];
+                               el.parentNode.removeChild(el);
+                       }
+               }
+
                return {
                        addClass: addClass,
                        bind: bind,
+                       remove: remove,
                        removeClass: removeClass
                };
        }

Modified: trunk/extensions/MobileFrontend/javascripts/beta_application.js
===================================================================
--- trunk/extensions/MobileFrontend/javascripts/beta_application.js     
2012-03-05 12:44:33 UTC (rev 113027)
+++ trunk/extensions/MobileFrontend/javascripts/beta_application.js     
2012-03-05 13:14:37 UTC (rev 113028)
@@ -11,6 +11,11 @@
                var i, search, clearSearch, results, languageSelection, a, 
heading,
                        sectionHeadings = utilities( '.section_heading' );
                utilities( document.body ).addClass( 'jsEnabled' );
+
+               // TODO: remove in future - currently enables toggling in 
Wikipedia Mobile App v < 1.3
+               window.wm_toggle_section = wm_toggle_section;
+               utilities( '.section_heading button' ).remove();
+
                function openSectionHandler() {
                        var sectionNumber = this.id ? this.id.split( '_' )[1] : 
-1;
                        if( sectionNumber > -1 ) {
@@ -33,6 +38,7 @@
                }
                for( i = 0; i < sectionHeadings.length; i++ ) {
                        heading = sectionHeadings[i];
+                       heading.removeAttribute( 'onclick' ); // TODO: remove 
any legacy onclick handlers
                        heading.insertBefore( createButton( true ), 
heading.firstChild );
                        heading.insertBefore( createButton( false ), 
heading.firstChild );
                        utilities( heading ).bind( 'click', openSectionHandler 
);
@@ -132,9 +138,20 @@
                function bind( type, handler ) {
                        el.addEventListener( type, handler, false );
                }
+
+               // TODO: support single elements
+               function remove(els) {
+                       var i, el;
+                       for( i = 0; i < els.length; i++ ) {
+                               el = els[i];
+                               el.parentNode.removeChild(el);
+                       }
+               }
+
                return {
                        addClass: addClass,
                        bind: bind,
+                       remove: remove,
                        removeClass: removeClass
                };
        }


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

Reply via email to