How can I hide tabs for users not logged in?

Fragment of a discussion from Project:Support desk
Jump to: navigation, search

Here it is for Vector skin. And don't remove my name, it's a real extension. Just create HideVariousTabsFromUnauthorizedUsers.php, put this code into it, include it in LocalSettings.php and if anonymous users DON'T have edit permission, it'll work. Actions at the arrow shouldn't need to be removed because I can't see an action shown under the arrow that can be executed by anonymous users with default or restricted permissions.

<?php
/**
* HideVariousTabsFromUnauthorizedUsers
*
* @package MediaWiki
* @subpackage Extensions
*
* @author: Tim 'SVG' Weyer <t.weyer@ymail.com>
*
* @copyright Copyright (C) 2012 Tim Weyer
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
*/
 
$wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'HideVariousTabsFromUnauthorizedUsers',
        'author'         => array( 'Tim Weyer' ),
        'url'            => 'https://www.mediawiki.org/wiki/User:SVG',
        'description'    => 'Disables various view and namespace tabs from users without <tt>edit</tt> permission for Vector skin',
        'version'        => '04-12-2012',
);
 
// Hooks
$wgHooks['SkinTemplateNavigation'][] = 'fnHVTFUUremoveTabsFromVector';
 
// Tabs of view to remove
$wgHVTFUUviewsToRemove = array( 'view' /* read */, 'edit', 'addsection' /* on talkpages */, 'history' );
 
/**
 * @param $sktemplate Title
 * @param $links
 * @return bool
 */
function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
        global $wgUser, $wgHVTFUUviewsToRemove;
 
        // Only remove tabs if user isn't allowed to edit pages
        if ( $wgUser->isAllowed( 'edit' ) ) {
                return false;
        }
 
        // Generate XML IDs from namespace names
        $subjectId = $sktemplate->mTitle->getNamespaceKey( '' );
 
        // Determine if this is a talk page
        $isTalk = $sktemplate->mTitle->isTalkPage();
 
        // Remove talkpage tab
        if ( $subjectId == 'main' ) {
                        $talkId = 'talk';
        } else {
                        $talkId = "{$subjectId}_talk";
        }
        if ( !$isTalk && $links['namespaces'][$talkId] )
                unset( $links['namespaces'][$talkId] );
 
        // Remove actions tabs
        foreach ( $wgHVTFUUviewsToRemove as $view ) {
                if ( $links['views'][$view] )
                        unset( $links['views'][$view] );
        }
 
        return true;
}
Tim (SVG)12:51, 12 April 2012

Works perfectly. Thanks. However the View Source tab still shows. Although you don't need permission to view that tab I would like to remove it as it's wasteful to Search Engine Optimization.

Holygamer (talk)13:10, 12 April 2012

Sorry, forgot this tab because I didn't remove the edit permission when I was coding it. Just add 'viewsource' to $wgHVTFUUviewsToRemove array and it should work.

Tim (SVG)13:33, 12 April 2012

A long extension name at that :)

Jasper Deng (talk)16:46, 12 April 2012

Yes, thought the same when I gave it its name. I just wanted to use a very meaningful name ^^

Tim (SVG)17:14, 12 April 2012
 
 
 
Edited by another user.
Last edit: 14:17, 27 April 2012

Which version of mediawiki is this for?

I think I've followed the instructions above, but get an internal server error on

        // Generate XML IDs from namespace names
        $subjectId = $sktemplate->mTitle->getNamespaceKey( '' );
        // Determine if this is a talk page
        $isTalk = $sktemplate->mTitle->isTalkPage();
Bjoern (talk)14:12, 27 April 2012

I've coded it in MediaWiki 1.17.0. Due to Holygamer's testimony, it does also work with MediaWiki 1.16.5 and I've checked both Vector.php. It might not work with MediaWiki 1.18, the code of Vector.php is quite different.

Tim (SVG)14:27, 27 April 2012
 
Edited by author.
Last edit: 01:22, 26 May 2012

For MediaWiki 1.19, try this:

function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
        global $wgUser, $wgHVTFUUviewsToRemove;
 
        // Only remove tabs if user isn't allowed to edit pages
        if ( $wgUser->isAllowed( 'edit' ) ) {
                return false;
        }
 
        // Remove talkpage tab
        if ( isset( $links['namespaces']['talk'] ))
                unset( $links['namespaces']['talk'] );
 
        // Remove actions tabs
        foreach ( $wgHVTFUUviewsToRemove as $view ) {
                if ( isset( $links['views'][$view] ))
                        unset( $links['views'][$view] );
        }
 
        return true;
}
Cheeyang (talk)06:30, 23 May 2012

HI I am using Midiawiki-1.18.3 version. i have followed the following suggestion to hide "view source" tab for anonymous user.

URL - http://www.mediawiki.org/wiki/Extension:ProtectSource#Configuration

But still i could see option. so please help me solve this.

Operation system: Windows-xp

Rgds, Mohan

125.21.230.13210:50, 25 May 2012

You just need to add 'viewsource' to $wgHVTFUUviewsToRemove array and it'll work. I don't get how is this related to ProtectSource extension. HideVariousTabsFromUnauthorizedUsers is NOT ProtectSource.

Tim (SVG)10:03, 26 May 2012
Edited by 2 users.
Last edit: 05:57, 28 May 2012

HI Tim,

Could you please tell me in detail. Like do i need to update "LocalSettings.php" with below changes? $wgHVTFUUviewsToRemove['*']['viewsource'] = false; (or) do i need add like $wgHVTFUUviewsToRemove = array('viewsource');

I tried above given two option still i am able to see "View Source".

Thanks, Mohan

125.21.230.6804:35, 28 May 2012

Hide viewsource tab for users without edit permission (usually unregistered users if you've set it so): $wgHVTFUUviewsToRemove = array( 'viewsource' );

Tim (SVG)06:56, 28 May 2012

Hi Tim,

i want to hide "View Source" Tab for Anonymous users. In the LocalSettings.php If i set $wgGroupPermissions['*']['edit'] = false; then its hides the Edit Tab, Where as if i set $wgHVTFUUviewsToRemove = array( 'viewsource' ); its still shows the "View soruce" tab. So could you please help me. Thanks in advance.

Thanks, Mohan

125.21.230.6808:31, 28 May 2012

If you are not loggedin, you shouldn't be able to see viewsource tab (and no edit tab because edit tab gets into viewsource tab if you have no permission to edit pages). Do you use my code from above? You can try to purge the cache with ?action=purge

Tim (SVG)08:42, 28 May 2012

Hi Tim,

i am not logged in but still it shows the "View source" tab. Can you tell me which code & in which file i have to update your code? bcoz my mediawiki version is -1.18.3. The tell me how to try Purge option (under in which file). Rgds, Mohan

125.21.230.6808:56, 28 May 2012

Create folder HideVariousTabsFromUnauthorizedUsers in extensions directory, create HideVariousTabsFromUnauthorizedUsers.php file in HideVariousTabsFromUnauthorizedUsers folder and add the code below to your this file. Then add require_once ( "$IP/extensions/HideVariousTabsFromUnauthorizedUsers/HideVariousTabsFromUnauthorizedUsers.php" ); to your LocalSettings.php. Remove $wgGroupPermissions['*']['edit'] = false; and any other earlier setting of HideVariousTabsFromUnauthorizedUsers extension from LocalSettings.php.

<?php
/**
* HideVariousTabsFromUnauthorizedUsers
*
* @package MediaWiki
* @subpackage Extensions
*
* @author: Tim 'SVG' Weyer <t.weyer@ymail.com>
*
* @copyright Copyright (C) 2012 Tim Weyer
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
*/
 
$wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'HideVariousTabsFromUnauthorizedUsers',
        'author'         => array( 'Tim Weyer' ),
        'url'            => 'https://www.mediawiki.org/wiki/User:SVG',
        'description'    => 'Disables various view tabs (and originally also namespace tabs) from users without <tt>edit</tt> permission for Vector skin',
        'version'        => '05-28-2012',
);
 
// Hooks
$wgHooks['SkinTemplateNavigation'][] = 'fnHVTFUUremoveTabsFromVector';
 
// Tabs of view to remove
$wgHVTFUUviewsToRemove = array( 'viewsource' );
 
// Remove 'edit' permission from anonymous users
$wgGroupPermissions['*']['edit'] = false;
 
/**
 * @param $sktemplate Title
 * @param $links
 * @return bool
 */
function fnHVTFUUremoveTabsFromVector( SkinTemplate &$sktemplate, array &$links ) {
        global $wgUser, $wgHVTFUUviewsToRemove;
 
        // Only remove tabs if user isn't allowed to edit pages
        if ( $wgUser->isAllowed( 'edit' ) ) {
                return false;
        }
 
        // Remove actions tabs
        foreach ( $wgHVTFUUviewsToRemove as $view ) {
                if ( $links['views'][$view] )
                        unset( $links['views'][$view] );
        }
 
        return true;
}
Tim (SVG)09:02, 28 May 2012

Hi Tim

It seems to work so far for me, THANKS for this extension. I altered this

// Tabs of view to remove

$wgHVTFUUviewsToRemove = array( 'edit', 'history', 'read', 'talk', 'viewsource' );

But it looks like it only works in the main namespace. Tabs of Pages in the MediaWiki or User Namespace still appear. Any ideas ? If possible I would like to hide all Tabs for unlogged users

Brocchinia (talk)08:49, 31 August 2012

What version of MediaWiki do you use and how are the settings of your wiki's user rights? Sorry for my late reply!

Tim (SVG)11:11, 19 September 2012

Hi Tim,

is your extension still working with version 1.19.2. Maybe because of some different code in the newest vector skin? Thanks a lot!

Tickihgk (talk)22:43, 2 November 2012
 
 
 
 
 
 
Edited by 2 users.
Last edit: 08:45, 28 May 2012

Hi Tim,

i want to hide "View Source" Tab for Anonymous users. In the LocalSettings.php If i set $wgGroupPermissions['*']['edit'] = false; then its hides the Edit Tab, Where as if i set $wgHVTFUUviewsToRemove = array( 'viewsource' ); its still shows the "View soruce" tab. So could you please help me. Thanks in advance.

Here my requirement is that Anonymous users should not see "Edit & View Source" tabs.

Thanks, Mohan

Duplicate striked by SVG.

125.21.230.13208:32, 28 May 2012