Topic on Project:Support desk

Jump to navigation Jump to search

[RESOLVED] How to hide "View History" button for unlogged users?

37
Summary last edited by Moscowamerican 10:29, 26 June 2016 3 years ago

See the solution for the vector skin in the last post These edits to localsettings.php works for MW 1.22+ (and probably earlier).

79.164.209.97 (talkcontribs)

Hello all! I used search and I could not find any information how to hide "View History" button for unlogged users! Do you know how o do that?

Krinkle (talkcontribs)
88.130.126.140 (talkcontribs)

@ Krinkle: I understood the question that he wants to remove that tab, which you have in the upper right-hand corner of each page.

This is possible with two lines of CSS. E.g. something like this:

div.vectorTabs ul li#ca-history {
   visibility: hidden;
}
Fokebox (talkcontribs)

You have understood it correctly, but I wanted to hide this option only for unregistered users. CSS hides this link but the space for this button is there and it looks not so good.

Ciencia Al Poder (talkcontribs)

Replace visibility: hidden; with display: none;

Fokebox (talkcontribs)

No no ... both ways work perfect! But I want to hide history only for unregistered users!

Malyacko (talkcontribs)
Malyacko (talkcontribs)
Fokebox (talkcontribs)

Well "You may want to have pages editable only by their creator, or ban viewing of history, or any of a number of other things. None of these features are available " ... so I guess it is impossible to that now (

88.130.69.96 (talkcontribs)

It is possible, but not out of the box. ;-)

You can do that in your skin files. If you use the Vector skin, then you can do that in skins/Vector.php. Somewhere in the head of your page, add some lines of PHP code, which check, if a user is logged in currently and if not, then include a CSS file with these few lines we gave you (which hide the button).


<head>
  <!-- ... much stuff ... -->
  <?php if(!$wgUser->isLoggedIn()) { ?>
    <link href="/skins/vector/hide-tab.css" media="all" rel="stylesheet" type="text/css" />
  <?php } ?>
</head>

(I have not tested the above; but it should give you an idea, of what you should be able to do. :-) )

Fokebox (talkcontribs)

I got your idea, but I need to remove bar completely, if I use css it hides only a word history, but ths tab is there and it looks not so good!

Allen4names (talkcontribs)
Ciencia Al Poder (talkcontribs)

Groups "*" and "user" do not get those MediaWiki pages loaded. So it should be hidden by default to anyone, and probably use JavaScript to check wgUserName to see if the user is logged in and re-display it.

Allen4names (talkcontribs)

You can be logged in without JavaScript enabled so just add the code to MediaWiki:Common.js to hide the link.

59.90.74.221 (talkcontribs)

In vector.php add these code:(put after code)

<?php if(!$wgUser->isLoggedIn()) 
{
 #ca-history {display:none;} // similar way you can hide many things here like view, talk etc....
} 
?>

Try this. I hope it works. it is tested at my application also.

Ciencia Al Poder (talkcontribs)

This code is missing something. It's not inside a <style> tag.

Fokebox (talkcontribs)

Yes something is missing! It gives me also an php error

Fokebox (talkcontribs)

I have tried this, but it gives me a error Fatal error: Call to a member function isLoggedIn() on a non-object in /usr/home/philcrosoft/wikijournal.ru/www/skins/Vector.php on line 3

Ciencia Al Poder (talkcontribs)

Try using the BeforePageDisplay hook instead of editing the skin file.

Try this (haven't tested) in LocalSettings.php:

public static function efAddSkinStylesAnon( OutputPage &$out, Skin &$skin ) {
	if( !$wgUser->isLoggedIn() ) {
		// Hide history tab
		if ( $skin->getSkinName() == 'vector' ) {
			$out->addInlineStyle( '#ca-history { display:none; }' );
		}
	}
	return true;
}

$wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon';
Fokebox (talkcontribs)

It Doesn't work! Parse error: syntax error, unexpected T_PUBLIC I like an idea adding a script to vector.php. Is it possible to make it correct?

<?php if(!$wgUser->isLoggedIn()) 
{
 #ca-history {display:none;} // similar way you can hide many things here like view, talk etc....
} 
?>
Ciencia Al Poder (talkcontribs)

Remove the "public" word and try again. Sorry, I don't have PHP nor a MediaWiki installation at hand to test my code.

And the most correct way to do it is what I said. You shouldn't alter core MediaWiki files for this since on next upgrade you'll lost all modifications.

Fokebox (talkcontribs)

I have such result: Parse error: syntax error, unexpected T_FUNCTION, expecting T_VARIABLE in /usr/home/philcrosoft/www/LocalSettings.php on line 146

88.130.123.155 (talkcontribs)

Make sure that you put the function definition at a place where PHP allows you to define a function.

Fokebox (talkcontribs)

So where should I insert the code at LocalSettings.php?

MarkAHershberger (talkcontribs)

you need to remove the "public" and "static" words. This will give you the following snippet to put at the end of your LocalSettings.php:

function efAddSkinStylesAnon( OutputPage &$out, Skin &$skin ) {
       if( !$wgUser->isLoggedIn() ) {
               // Hide history tab
               if ( $skin->getSkinName() == 'vector' ) {
                       $out->addInlineStyle( '#ca-history { display:none; }' );
               }
       }
       return true;
}

$wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon';
Fokebox (talkcontribs)

I've put the code at the end of my LocalSettings.php result is: Fatal error: Call to a member function isLoggedIn() on a non-object in /usr/home/philcrosoft/wikijournal/www/LocalSettings.php on line 148

Here is my full Localsettings.php with this code which does not work!

<?php
# This file was automatically generated by the MediaWiki 1.19.3
# installer. If you make manual changes, please keep track in case you
# need to recreate them later.
#
# See includes/DefaultSettings.php for all configurable settings
# and their default values, but don't forget to make changes in _this_
# file, not there.
#
# Further documentation for configuration settings may be found at:
# http://www.mediawiki.org/wiki/Manual:Configuration_settings

# Protect against web entry
if ( !defined( 'MEDIAWIKI' ) ) {
	exit;
}

## Uncomment this to disable output compression
# $wgDisableOutputCompression = true;

$wgSitename      = "Wiki Journal";
$wgMetaNamespace = "Wiki_Journal";

## The URL base path to the directory containing the wiki;
## defaults for all runtime URL paths are based off of this.
## For more information on customizing the URLs please see:
## http://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath       = "";
$wgScriptExtension  = ".php";

## The protocol and server name to use in fully-qualified URLs
$wgServer           = "http://www.wikijournal.ru";

## The relative URL path to the skins directory
$wgStylePath        = "$wgScriptPath/skins";

## The relative URL path to the logo.  Make sure you change this from the default,
## or else you'll overwrite your logo when you upgrade!
$wgLogo             = "$wgStylePath/common/images/wiki.png";

## UPO means: this is also a user preference option

$wgEnableEmail      = false;
$wgEnableUserEmail  = false; # UPO

$wgEmergencyContact = "philcrosoft@mail.ru";
$wgPasswordSender   = "philcrosoft@mail.ru";

$wgEnotifUserTalk      = false; # UPO
$wgEnotifWatchlist     = false; # UPO
$wgEmailAuthentication = true;

## Database settings
$wgDBtype           = "mysql";
$wgDBserver         = "localhost";
$wgDBname           = "philcrosoft_wiki";
$wgDBuser           = "philcrosoft_wiki";
$wgDBpassword       = "mkx0B4pv";

# MySQL specific settings
$wgDBprefix         = "";

# MySQL table options to use during installation or update
$wgDBTableOptions   = "ENGINE=InnoDB, DEFAULT CHARSET=binary";

# Experimental charset support for MySQL 5.0.
$wgDBmysql5 = false;

## Shared memory settings
$wgMainCacheType    = CACHE_NONE;
$wgMemCachedServers = array();

## To enable image uploads, make sure the 'images' directory
## is writable, then set this to true:
$wgEnableUploads  = true;
#$wgUseImageMagick = true;
#$wgImageMagickConvertCommand = "/usr/bin/convert";

# InstantCommons allows wiki to use images from http://commons.wikimedia.org
$wgUseInstantCommons  = false;

## If you use ImageMagick (or any other shell command) on a
## Linux server, this will need to be set to the name of an
## available UTF-8 locale
$wgShellLocale = "en_US.utf8";

## If you want to use image uploads under safe mode,
## create the directories images/archive, images/thumb and
## images/temp, and make them all writable. Then uncomment
## this, if it's not already uncommented:
#$wgHashedUploadDirectory = false;

## Set $wgCacheDirectory to a writable directory on the web server
## to make your wiki go slightly faster. The directory should not
## be publically accessible from the web.
#$wgCacheDirectory = "$IP/cache";

# Site language code, should be one of the list in ./languages/Names.php
$wgLanguageCode = "ru";

$wgSecretKey = "e1dc56b6b7e1c24712fc1c4688931414882b0dcdebebd9678a75887ebb933b94";

# Site upgrade key. Must be set to a string (default provided) to turn on the
# web installer while LocalSettings.php is in place
$wgUpgradeKey = "d741e62558422194";

## Default skin: you can change the default skin. Use the internal symbolic
## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook', 'vector':
$wgDefaultSkin = "vector";

## For attaching licensing metadata to pages, and displaying an
## appropriate copyright notice / icon. GNU Free Documentation
## License and Creative Commons licenses are supported so far.
$wgRightsPage = ""; # Set to the title of a wiki page that describes your license/copyright
$wgRightsUrl  = "";
$wgRightsText = "";
$wgRightsIcon = "";

# Path to the GNU diff3 utility. Used for conflict resolution.
$wgDiff3 = "";

# Query string length limit for ResourceLoader. You should only set this if
# your web server has a query string length limit (then set it to that limit),
# or if you have suhosin.get.max_value_length set in php.ini (then set it to
# that value)
$wgResourceLoaderMaxQueryLength = -1;

# The following permissions were set based on your choice in the installer
$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['*']['edit'] = false;

# Enabled Extensions. Most extensions are enabled by including the base extension file here
# but check specific extension documentation for more details
# The following extensions were automatically enabled:
require_once( "$IP/extensions/ConfirmEdit/ConfirmEdit.php" );
require_once( "$IP/extensions/Gadgets/Gadgets.php" );
require_once( "$IP/extensions/Nuke/Nuke.php" );
require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );
require_once( "$IP/extensions/Renameuser/Renameuser.php" );
require_once( "$IP/extensions/Vector/Vector.php" );
require_once( "$IP/extensions/WikiEditor/WikiEditor.php" );


$wgTmpDirectory = "$IP/images/temp";


function efAddSkinStylesAnon( OutputPage &$out, Skin &$skin ) {
        if( !$wgUser->isLoggedIn() ) {
                // Hide history tab
                if ( $skin->getSkinName() == 'vector' ) {
                        $out->addInlineStyle( '#ca-history { display:none; }' );
                }
        }
        return true;
 }
 
 $wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon';

MarkAHershberger (talkcontribs)

The error you're getting points to the following line:

       if( !$wgUser->isLoggedIn() ) {

The error was "Call to a member function isLoggedIn() on a non-object".

Since $wgUser is a global object, telling PHP to get $wgUser from the global scope just before that line should solve your problem:

       global $wgUser;
       if( !$wgUser->isLoggedIn() ) {

Let me know if that works.

Fokebox (talkcontribs)

Hello Mark! I have one more question according to this thread: So I use this code in Localsettings.php

function efAddSkinStylesAnon( OutputPage &$out, Skin &$skin ) {
        if( !$wgUser->isLoggedIn() ) {
                // Hide history tab
                if ( $skin->getSkinName() == 'vector' ) {
                        $out->addInlineStyle( '#ca-history { display:none; }' );
                }
        }
        return true;
 }
 
 $wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon';

What should I add to hide some buttons for those who logged in?

88.130.91.250 (talkcontribs)

Between the closing bracket and directly in top of the line "return true;" add

       if( $wgUser->isLoggedIn() ) {
               // Hide some buttons for those logged in
               if ( $skin->getSkinName() == 'vector' ) {
                       # Modify the following CSS code so that the definition matches the one you need.
                       $out->addInlineStyle( '#my-example { display:none; }' );
               }
       }

This code will then hide the CSS element with the ID "my-example". Replace my-example with the ID of the elemnt, which you want to hide.

Fokebox (talkcontribs)

No no I tried to do this, but it hides some elements for those who is not logged in! And I need to hide some elements for those who is logged in

88.130.91.250 (talkcontribs)

> it hides some elements for those who is not logged in

No, it does not. ;-) Have another look at the condition, which I posted: It does not say if( !$wgUser, but if( $wgUser. This condition will only be met, if a user is logged in.

MarkAHershberger (talkcontribs)

Try the following snippet which should hide the history tab for anon users or the "Read" tab for logged in users. Note this only works if they're using the vector skin.

function efAddSkinStyles( OutputPage &$out, Skin &$skin ) {
        if( !$wgUser->isLoggedIn() ) {
                // Hide history tab for anons
                if ( $skin->getSkinName() == 'vector' ) {
                        $out->addInlineStyle( '#ca-history { display:none; }' );
                }
        } else {
                // Hide "Read" tab for those who are logged in
                if ( $skin->getSkinName() == 'vector' ) {
                        $out->addInlineStyle( '#ca-view { display:none; }' );
                }       
        }

        return true;
}
 
$wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon';
Fokebox (talkcontribs)

Thx it works!!! Yes I do use only vector skin =)

Wess (talkcontribs)

Does this still work for you? I was trying to implement this script in different ways but I keep getting this exception:

MediaWiki internal error.

Original exception: [5b40cf63] Exception from line 181 of ...includes/Hooks.php: Invalid callback in hooks for BeforePageDisplay

Backtrace:
#0 ...includes/GlobalFunctions.php(3877): Hooks::run(string, array)
#1 ...includes/OutputPage.php(2075): wfRunHooks(string, array)
#2 ...includes/Wiki.php(609): OutputPage->output()
#3 ...includes/Wiki.php(467): MediaWiki->main()
#4 ...index.php(49): MediaWiki->run()
#5 {main}

Exception caught inside exception handler: [5f04ab04] Exception from line 181 of includes/Hooks.php: Invalid callback in hooks for BeforePageDisplay

Backtrace:
#0 ...includes/GlobalFunctions.php(3877): Hooks::run(string, array)
#1 ...includes/OutputPage.php(2075): wfRunHooks(string, array)
#2 ...includes/Exception.php(220): OutputPage->output()...

Any Idea?

Yakiniku~mediawikiwiki (talkcontribs)

Made the following changes and it worked on Mediawiki 1.22.5

$wgUser->isLoggedIn() >>>> $skin->getUser()->isLoggedIn()

$wgHooks['BeforePageDisplay'][] = 'efAddSkinStylesAnon'; >>>> 'efAddSkinStyles'

function efAddSkinStyles(OutputPage &$out, Skin &$skin) {
    if(!$skin->getUser()->isLoggedIn()) {
        if ($skin->getSkinName() == 'vector') {
            $out->addInlineStyle('#ca-history { display:none; }');
        }
    } else {
        if ($skin->getSkinName() == 'vector') {
            $out->addInlineStyle('#ca-view { display:none; }');
        }
    }

    return true;
}
$wgHooks['BeforePageDisplay'][] = 'efAddSkinStyles';

This post was posted by Yakiniku~mediawikiwiki, but signed as Yakiniku.

Kghbln (talkcontribs)

This is a great one. Also works on MW 1.25.x

Reply to "[RESOLVED] How to hide "View History" button for unlogged users?"