Florianschmidtwelzow has uploaded a new change for review.

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

Change subject: Make the main menu a bit more configurable
......................................................................

Make the main menu a bit more configurable

Added 4 new configuration variables to change/add/remove elements from
the main menu for extension developers or sysadmins.

- $wgMFMainMenuSections: Controls, which sections are visible in the
  main menu bar.
- $wgMFMainMenuSiteLinks Defines a list of links for the sitelinks
  section of the main menu bar (About, Disclaimer, e.g.)
- $wgMFMainMenuPersonalTools & $wgMFMainMenuDiscoveryTools: list of
  elements visible in the main menu for personal- and discovery
  tools.

Bug: T65459
Change-Id: Iaccaae2c8c07ee38f05d7b0414183865e7d0ef3d
---
M includes/config/Skin.php
M includes/skins/SkinMinerva.php
M includes/skins/SkinMinervaAlpha.php
M resources/mobile.mainMenu/menu.mustache
4 files changed, 289 insertions(+), 211 deletions(-)


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

diff --git a/includes/config/Skin.php b/includes/config/Skin.php
index 2147641..7a998d1 100644
--- a/includes/config/Skin.php
+++ b/includes/config/Skin.php
@@ -63,3 +63,63 @@
  * Set to false for "dictionary style", sections are not collapsed.
  */
 $wgMFCollapseSectionsByDefault = true;
+
+/**
+ * Associative array to configure different sections in MobileFrontend's main 
menu. Structure:
+ * Defines different sections with a callback, that will be called when the 
main menu elements are
+ * built. Possible values for a section:
+ * - (required)callback: The function to call when the main menu is built
+ *   (must be callable by SkinMinerva{Beta|Alpha}
+ * - (optional)cssclass: Can be used to add a css class to the ul-element of 
this section
+ *
+ * Note: Sections will be added in the order they are defined in this array. 
The predefined
+ * sitelinks section should be the last entry.
+ *
+ * Example: Add a new section:
+ * 'test' => array( 'callback' => function ( Config $config, Skin $sk ) {
+ *   return array(
+ *     array(
+ *       'links' => array(
+ *         array(
+ *           'text' => 'someitem-msg',
+ *            'href' => 'someitem-destination',
+ *            'class' => MobileUI::iconClass( 'cool-class', 'before' ),
+ *            'data-event-name' => 'cool-data',
+ *         ),
+ *       ),
+ *     ),
+ *   )
+ * }
+ */
+$wgMFMainMenuSections = array(
+       'discovery' => array( 'callback' => 'self::getDiscoveryTools' ),
+       'personal' => array( 'callback' => 'self::getPersonalTools' ),
+       'sitelinks' => array( 'callback' => 'self::getSiteLinks', 'cssclass' => 
'hlist' ),
+);
+
+/**
+ * Defines links and elements that are added to the predefined section 
sitelinks.
+ * @see $wgMFMainMenuSections
+ */
+$wgMFMainMenuSiteLinks = array(
+       array(
+               'title' => 'aboutpage',
+               'msg' => 'aboutsite',
+       ),
+       array(
+               'title' => 'disclaimerpage',
+               'msg' => 'disclaimers',
+       ),
+);
+
+/**
+ * Defines elements of predefined main menu section personal, that are visible.
+ * @see $wgMFMainMenuSections
+ */
+$wgMFMainMenuPersonalTools = array( 'watchlist', 'uploads', 'settings', 
'preferences', 'auth' );
+
+/**
+ * Defines elements of predefined main menu section discovery, that are 
visible.
+ * @see $wgMFMainMenuSections
+ */
+$wgMFMainMenuDiscoveryTools = array( 'home', 'random', 'nearby1' );
diff --git a/includes/skins/SkinMinerva.php b/includes/skins/SkinMinerva.php
index d9ea162..8aed102 100644
--- a/includes/skins/SkinMinerva.php
+++ b/includes/skins/SkinMinerva.php
@@ -49,7 +49,8 @@
         * @return QuickTemplate
         */
        protected function prepareQuickTemplate() {
-               $appleTouchIcon = $this->getConfig()->get( 'AppleTouchIcon' );
+               $config = $this->getMFConfig();
+               $appleTouchIcon = $config->get( 'AppleTouchIcon' );
 
                $out = $this->getOutput();
                // add head items
@@ -88,11 +89,11 @@
                $tpl->set( 'unstyledContent', $out->getProperty( 
'unstyledContent' ) );
 
                // Deprecate use of this in favour of menu_data.
-               $tpl->set( 'site_urls', $this->getSiteLinks() );
+               $tpl->set( 'site_urls', self::getSiteLinks( $config, $this ) );
                // Deprecate use of this in favour of menu_data.
-               $tpl->set( 'personal_urls', $this->getPersonalTools() );
+               $tpl->set( 'personal_urls', $this->getPersonalTools( $config, 
$this ) );
                // Deprecate use of this in favour of menu_data.
-               $tpl->set( 'discovery_urls', $this->getDiscoveryTools() );
+               $tpl->set( 'discovery_urls', $this->getDiscoveryTools( $config, 
$this ) );
 
                $tpl->set( 'menu_data', $this->getMenuData() );
                $tpl->set( 'secondary_actions', $this->getSecondaryActions( 
$tpl ) );
@@ -345,97 +346,6 @@
        }
 
        /**
-        * Prepares and returns urls and links personal to the given user
-        * @return array
-        */
-       protected function getPersonalTools() {
-               $returnToTitle = $this->getTitle()->getPrefixedText();
-               $donateTitle = SpecialPage::getTitleFor( 'Uploads' );
-               $watchTitle = SpecialPage::getTitleFor( 'Watchlist' );
-
-               // watchlist link
-               $watchlistQuery = array();
-               $user = $this->getUser();
-               if ( $user ) {
-                       $view = $user->getOption( 
SpecialMobileWatchlist::VIEW_OPTION_NAME, false );
-                       $filter = $user->getOption( 
SpecialMobileWatchlist::FILTER_OPTION_NAME, false );
-                       if ( $view ) {
-                               $watchlistQuery['watchlistview'] = $view;
-                       }
-                       if ( $filter && $view === 'feed' ) {
-                               $watchlistQuery['filter'] = $filter;
-                       }
-               }
-
-               $items = array(
-                       'watchlist' => array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'mobile-frontend-main-menu-watchlist' )->escaped(),
-                                               'href' => $this->getPersonalUrl(
-                                                       $watchTitle,
-                                                       
'mobile-frontend-watchlist-purpose',
-                                                       $watchlistQuery
-                                               ),
-                                               'class' => MobileUI::iconClass( 
'watchlist', 'before' ),
-                                               'data-event-name' => 
'watchlist',
-                                       ),
-                               ),
-                               'class' => 'jsonly'
-                       )
-               );
-               if ( $this->isMobileMode ) {
-                       if ( $this->mobileContext->userCanUpload() ) {
-                               $items['uploads'] = array(
-                                       'links' => array(
-                                               array(
-                                                       'text' => wfMessage( 
'mobile-frontend-main-menu-upload' )->escaped(),
-                                                       'href' => 
$this->getPersonalUrl(
-                                                               $donateTitle,
-                                                               
'mobile-frontend-donate-image-anon'
-                                                       ),
-                                                       'class' => 
MobileUI::iconClass( 'uploads', 'before', 'menu-item-upload' ),
-                                                       'data-event-name' => 
'uploads',
-                                               ),
-                                       ),
-                                       'class' => 'jsonly',
-                               );
-                       }
-                       $items['settings'] = array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'mobile-frontend-main-menu-settings' )->escaped(),
-                                               'href' => 
SpecialPage::getTitleFor( 'MobileOptions' )->
-                                                       getLocalUrl( array( 
'returnto' => $returnToTitle ) ),
-                                               'class' => MobileUI::iconClass( 
'mobileoptions', 'before' ),
-                                               'data-event-name' => 'settings',
-                                       ),
-                               ),
-                       );
-               } else {
-                       $items['preferences'] = array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'preferences' )->escaped(),
-                                               'href' => $this->getPersonalUrl(
-                                                       
SpecialPage::getTitleFor( 'Preferences' ),
-                                                       'prefsnologintext2'
-                                               ),
-                                               'class' => MobileUI::iconClass( 
'settings', 'before' ),
-                                               'data-event-name' => 
'preferences',
-                                       ),
-                               ),
-                       );
-               }
-               $items['auth'] = $this->getLogInOutLink();
-
-               // Allow other extensions to add or override tools
-               Hooks::run( 'MobilePersonalTools', array( &$items ) );
-
-               return $items;
-       }
-
-       /**
         * Rewrites the language list so that it cannot be contaminated by 
other extensions with things
         * other than languages
         * See bug 57094.
@@ -453,62 +363,6 @@
                } else {
                        $tpl->set( 'language_urls', false );
                }
-       }
-
-       /**
-        * Prepares a list of links that have the purpose of discovery in the 
main navigation menu
-        * @return array
-        */
-       protected function getDiscoveryTools() {
-               $config = $this->getMFConfig();
-
-               $items = array(
-                       'home' => array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'mobile-frontend-home-button' )->escaped(),
-                                               'href' => 
Title::newMainPage()->getLocalUrl(),
-                                               'class' => MobileUI::iconClass( 
'home', 'before' ),
-                                               'data-event-name' => 'home',
-                                       ),
-                               ),
-                       ),
-                       'random' => array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'mobile-frontend-random-button' )->escaped(),
-                                               'href' => 
SpecialPage::getTitleFor( 'Randompage',
-                                                       
MWNamespace::getCanonicalName( $config->get( 'MFContentNamespace' ) ) 
)->getLocalUrl() .
-                                                               '#/random',
-                                               'class' => MobileUI::iconClass( 
'random', 'before' ),
-                                               'id' => 'randomButton',
-                                               'data-event-name' => 'random',
-                                       ),
-                               ),
-                       ),
-                       'nearby' => array(
-                               'links' => array(
-                                       array(
-                                               'text' => wfMessage( 
'mobile-frontend-main-menu-nearby' )->escaped(),
-                                               'href' => 
SpecialPage::getTitleFor( 'Nearby' )->getLocalURL(),
-                                               'class' => MobileUI::iconClass( 
'nearby', 'before', 'nearby' ),
-                                               'data-event-name' => 'nearby',
-                                       ),
-                               ),
-                               'class' => 'jsonly',
-                       ),
-               );
-               if (
-                       !$config->get( 'MFNearby' ) ||
-                       ( !$config->get( 'MFNearbyEndpoint' ) && !class_exists( 
'GeoData' ) )
-               ) {
-                       unset( $items['nearby'] );
-               }
-
-               // Allow other extensions to add or override discovery tools
-               Hooks::run( 'MinervaDiscoveryTools', array( &$items ) );
-
-               return $items;
        }
 
        /**
@@ -748,38 +602,6 @@
        }
 
        /**
-        * Returns an array of sitelinks to add into the main menu footer.
-        * @return Array array of site links
-        */
-       protected function getSiteLinks() {
-               $siteLinks = array(
-                       array(
-                               'title' => 'aboutpage',
-                               'msg' => 'aboutsite',
-                       ),
-                       array(
-                               'title' => 'disclaimerpage',
-                               'msg' => 'disclaimers',
-                       ),
-               );
-               $urls = array();
-               foreach ( $siteLinks as $param ) {
-                       $title = Title::newFromText(
-                               $this->msg( $param['title'] 
)->inContentLanguage()->text()
-                       );
-                       $msg = $this->msg( $param['msg'] );
-                       if ( $title && !$msg->isDisabled() ) {
-                               $urls[] = array(
-                                       'href' => $title->getLocalUrl(),
-                                       'text'=> $msg->text(),
-                               );
-                       }
-               }
-
-               return $urls;
-       }
-
-       /**
         * Prepare warnings for mobile output
         * @param BaseTemplate $tpl
         */
@@ -871,15 +693,221 @@
        }
 
        /**
+        * Prepares a list of links that have the purpose of discovery in the 
main navigation menu
+        * @param Config $config MobileFrontend's config object
+        * @param Skin $sk Skin object this function is called from
+        * @return array
+        */
+       public static function getDiscoveryTools( Config $config, Skin $sk ) {
+               $visibleLinks = $config->get( 'MFMainMenuDiscoveryTools' );
+               $items = array();
+
+               if ( in_array( 'home', $visibleLinks ) ) {
+                       $items['home'] = array(
+                               'links' => array(
+                                       array(
+                                               'text' => $sk->msg( 
'mobile-frontend-home-button' )->escaped(),
+                                               'href' => 
Title::newMainPage()->getLocalUrl(),
+                                               'class' => MobileUI::iconClass( 
'home', 'before' ),
+                                               'data-event-name' => 'home',
+                                       ),
+                               ),
+                       );
+               }
+               if ( in_array( 'home', $visibleLinks ) ) {
+                       $items['random'] = array(
+                               'links' => array(
+                                       array(
+                                               'text' => $sk->msg( 
'mobile-frontend-random-button' )->escaped(),
+                                               'href' => 
SpecialPage::getTitleFor( 'Randompage',
+                                                       
MWNamespace::getCanonicalName( $config->get( 'MFContentNamespace' ) ) 
)->getLocalUrl() .
+                                                               '#/random',
+                                               'class' => MobileUI::iconClass( 
'random', 'before' ),
+                                               'id' => 'randomButton',
+                                               'data-event-name' => 'random',
+                                       ),
+                               ),
+                       );
+               }
+               if (
+                       $config->get( 'MFNearby' ) &&
+                       in_array( 'nearby', $visibleLinks ) &&
+                       ( $config->get( 'MFNearbyEndpoint' ) || class_exists( 
'GeoData' ) )
+               ) {
+                       $items['nearby'] = array(
+                               'links' => array(
+                                       array(
+                                               'text' => $sk->msg( 
'mobile-frontend-main-menu-nearby' )->escaped(),
+                                               'href' => 
SpecialPage::getTitleFor( 'Nearby' )->getLocalURL(),
+                                               'class' => MobileUI::iconClass( 
'nearby', 'before', 'nearby' ),
+                                               'data-event-name' => 'nearby',
+                                       ),
+                               ),
+                               'class' => 'jsonly',
+                       );
+               }
+
+               // Allow other extensions to add or override discovery tools
+               Hooks::run( 'MinervaDiscoveryTools', array( &$items ) );
+
+               return $items;
+       }
+
+       /**
+        * Prepares and returns urls and links personal to the given user
+        * @param Config $config MobileFrontend's config object
+        * @param Skin $sk Skin object this function is called from
+        * @return array
+        */
+       public static function getPersonalTools( Config $config, Skin $sk ) {
+               $returnToTitle = $sk->getTitle()->getPrefixedText();
+               $donateTitle = SpecialPage::getTitleFor( 'Uploads' );
+               $watchTitle = SpecialPage::getTitleFor( 'Watchlist' );
+               $visibleLinks = $config->get( 'MFMainMenuPersonalTools' );
+
+               // watchlist link
+               $watchlistQuery = array();
+               $user = $sk->getUser();
+               if ( $user ) {
+                       $view = $user->getOption( 
SpecialMobileWatchlist::VIEW_OPTION_NAME, false );
+                       $filter = $user->getOption( 
SpecialMobileWatchlist::FILTER_OPTION_NAME, false );
+                       if ( $view ) {
+                               $watchlistQuery['watchlistview'] = $view;
+                       }
+                       if ( $filter && $view === 'feed' ) {
+                               $watchlistQuery['filter'] = $filter;
+                       }
+               }
+
+               $items = array();
+               if ( in_array( 'watchlist', $visibleLinks ) ) {
+                       $items = array(
+                               'watchlist' => array(
+                                       'links' => array(
+                                               array(
+                                                       'text' => $sk->msg( 
'mobile-frontend-main-menu-watchlist' )->escaped(),
+                                                       'href' => 
$sk->getPersonalUrl(
+                                                               $watchTitle,
+                                                               
'mobile-frontend-watchlist-purpose',
+                                                               $watchlistQuery
+                                                       ),
+                                                       'class' => 
MobileUI::iconClass( 'watchlist', 'before' ),
+                                                       'data-event-name' => 
'watchlist',
+                                               ),
+                                       ),
+                                       'class' => 'jsonly'
+                               )
+                       );
+               }
+               if ( $sk->isMobileMode ) {
+                       if ( $sk->mobileContext->userCanUpload() && in_array( 
'uploads', $visibleLinks ) ) {
+                               $items['uploads'] = array(
+                                       'links' => array(
+                                               array(
+                                                       'text' => $sk->msg( 
'mobile-frontend-main-menu-upload' )->escaped(),
+                                                       'href' => 
$sk->getPersonalUrl(
+                                                               $donateTitle,
+                                                               
'mobile-frontend-donate-image-anon'
+                                                       ),
+                                                       'class' => 
MobileUI::iconClass( 'uploads', 'before', 'menu-item-upload' ),
+                                                       'data-event-name' => 
'uploads',
+                                               ),
+                                       ),
+                                       'class' => 'jsonly',
+                               );
+                       }
+                       if ( in_array( 'settings', $visibleLinks ) ) {
+                               $items['settings'] = array(
+                                       'links' => array(
+                                               array(
+                                                       'text' => $sk->msg( 
'mobile-frontend-main-menu-settings' )->escaped(),
+                                                       'href' => 
SpecialPage::getTitleFor( 'MobileOptions' )->
+                                                               getLocalUrl( 
array( 'returnto' => $returnToTitle ) ),
+                                                       'class' => 
MobileUI::iconClass( 'mobileoptions', 'before' ),
+                                                       'data-event-name' => 
'settings',
+                                               ),
+                                       ),
+                               );
+                       }
+               } elseif ( in_array( 'preferences', $visibleLinks ) ) {
+                       $items['preferences'] = array(
+                               'links' => array(
+                                       array(
+                                               'text' => $sk->msg( 
'preferences' )->escaped(),
+                                               'href' => $sk->getPersonalUrl(
+                                                       
SpecialPage::getTitleFor( 'Preferences' ),
+                                                       'prefsnologintext2'
+                                               ),
+                                               'class' => MobileUI::iconClass( 
'settings', 'before' ),
+                                               'data-event-name' => 
'preferences',
+                                       ),
+                               ),
+                       );
+               }
+               if ( in_array( 'auth', $visibleLinks ) ) {
+                       $items['auth'] = $sk->getLogInOutLink();
+               }
+
+               // Allow other extensions to add or override tools
+               Hooks::run( 'MobilePersonalTools', array( &$items ) );
+
+               return $items;
+       }
+
+       /**
+        * Returns an array of sitelinks to add into the main menu footer.
+        * @param Config $config MobileFrontend's config object
+        * @param Skin $sk Skin object this function is called from
+        * @return Array array of site links
+        */
+       public static function getSiteLinks( Config $config, Skin $sk ) {
+               $siteLinks = $config->get( 'MFMainMenuSiteLinks' );
+               $urls = array();
+               foreach ( $siteLinks as $param ) {
+                       $title = Title::newFromText(
+                               $sk->msg( $param['title'] 
)->inContentLanguage()->text()
+                       );
+                       $msg = $sk->msg( $param['msg'] );
+                       if ( $title && !$msg->isDisabled() ) {
+                               $urls[] = array(
+                                       'href' => $title->getLocalUrl(),
+                                       'text'=> $msg->text(),
+                               );
+                       }
+               }
+
+               return $urls;
+       }
+
+       /**
         * Returns a data representation of the main menus
         * @return array
         */
        protected function getMenuData() {
-               return array(
-                       'discovery' => $this->flattenLinkArray( 
$this->getDiscoveryTools() ),
-                       'personal' => 
$this->flattenLinkArray($this->getPersonalTools() ),
-                       'sitelinks' => $this->flattenLinkArray( 
$this->getSiteLinks() ),
-               );
+               $config = $this->getMFConfig();
+               // define an empty menu at start
+               $menu = array( 'menu' => array() );
+
+               // wgMFMainMenuSections holds an array with all sections to 
show and information how to built it
+               foreach ( $config->get( 'MFMainMenuSections' ) as $key => 
$value ) {
+                       // if there is no callback (or it isn't callable), 
throw an exception,
+                       // the callback has to return the items to show in this 
section
+                       if ( !isset( $value['callback'] ) || !is_callable( 
$value['callback'] ) ) {
+                               throw new InvalidArgumentException( 'Invalid 
callback provided for main menu section.' );
+                       }
+                       // create a new array with all elements in main menu
+                       $menu['menu'][] = array(
+                               'cssclass' => ( isset( $value['cssclass'] ) ? 
$value['cssclass'] : null ),
+                               'values' => $this->flattenLinkArray(
+                                       call_user_func_array(
+                                               $value['callback'],
+                                               array( $config, $this )
+                                       )
+                               ),
+                       );
+               }
+
+               return $menu;
        }
        /**
         * Returns array of config variables that should be added only to this 
skin
diff --git a/includes/skins/SkinMinervaAlpha.php 
b/includes/skins/SkinMinervaAlpha.php
index 54b56f0..b7496f8 100644
--- a/includes/skins/SkinMinervaAlpha.php
+++ b/includes/skins/SkinMinervaAlpha.php
@@ -28,11 +28,13 @@
 
        /**
         * Returns an array of sitelinks to add into the main menu footer.
+        * @param Config $config MobileFrontend's config object
+        * @param Skin $sk Skin object this function is called from
         * @return Array array of site links
         */
-       protected function getSiteLinks() {
+       public static function getSiteLinks( Config $config, Skin $sk ) {
                $urls = parent::getSiteLinks();
-               $msg = $this->msg( 'mobile-frontend-fontchanger-link' );
+               $msg = $sk->msg( 'mobile-frontend-fontchanger-link' );
                // Don't add elements, where the message does not exist
                if ( !$msg->isDisabled() ) {
                        $urls[] = array(
diff --git a/resources/mobile.mainMenu/menu.mustache 
b/resources/mobile.mainMenu/menu.mustache
index e4bae19..7e47c19 100644
--- a/resources/mobile.mainMenu/menu.mustache
+++ b/resources/mobile.mainMenu/menu.mustache
@@ -1,23 +1,11 @@
 <div class="menu">
-       <ul>
-               {{#discovery}}
-                       <li>
-                               <a href="{{href}}" class="{{class}}" 
data-event-name="{{data-event-name}}">{{text}}</a>
-                       </li>
-               {{/discovery}}
-       </ul>
-       <ul>
-               {{#personal}}
-                       <li>
-                               <a href="{{href}}" class="{{class}}" 
data-event-name="{{data-event-name}}">{{text}}</a>
-                       </li>
-               {{/personal}}
-       </ul>
-       <ul class="hlist">
-               {{#sitelinks}}
-                       <li>
-                               <a href="{{href}}" class="{{class}}" 
data-event-name="{{data-event-name}}">{{text}}</a>
-                       </li>
-               {{/sitelinks}}
-       </ul>
+       {{#menu}}
+               <ul class="{{cssclass}}">
+                       {{#values}}
+                               <li>
+                                       <a href="{{href}}" class="{{class}}" 
data-event-name="{{data-event-name}}">{{text}}</a>
+                               </li>
+                       {{/values}}
+               </ul>
+       {{/menu}}
 </div>

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaccaae2c8c07ee38f05d7b0414183865e7d0ef3d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.wel...@t-online.de>

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

Reply via email to