Extension:SpecialLastUserLoginEx

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
SpecialLastUserLoginEx

Release status: stable

Implementation Special page, User activity
Description Displays the last time a user logged in
Author(s) Justin G. Cramer, Danila Ulyanov, Thomas Klein',
Version 1.1.0 (15-10-2007)
MediaWiki 1.5.x and higher
Download see below
Added rights lastlogin

Last User Login displays a simple table of users and the last time they logged in. This can be useful, if your wiki is used as a knowledge base for outside personnel. If you have a requirement that they check the wiki prior to placing a call or email Last User Login can be used to verify that this policy is being enforced.

Contents

[edit] Security

While this extension only shows up in the restricted extension list for sysops, there is nothing in the code that authorizes the user to ensure they should be able to view this special page (i.e., 'if you type the path to this extension directly, it will show anyone your user list').

If you only want sysops to be able to see the user information you can add the following code at the top of the execte function (~line 71) JT 04:35, 19 December 2005 (UTC)

Add the following:

if ( ! $wgUser->isSysop() ) {
  $wgOut->sysopRequired();
  return;
}
  • Thank you for your reference. I built it into the new version. --Thomas Klein 11:42, 20 December 2005 (UTC)

[edit] Impact to other functions

Adding a log entry for every user login impacts the script /maintenance/removeUnusedAccounts.php as the script checks for the number of page-edits, image-uploads and log-entries. However you can patch removeUnusedAccounts.php to ignore the first login which is very common for spamming robots:

original

if( CountEdits( $user, false ) == 0 && CountImages( $user, false ) == 0 && CountLogs( $user, false ) ==0 ) {
                        # User has no edits or images, mark them for deletion
                        $del[] = $user;
                        $count++;
                }

change to that

if( CountEdits( $user, false ) == 0 && CountImages( $user, false ) == 0 && CountLogs( $user, false ) <= 1 ) {
                        # User has no edits or images, mark them for deletion
                        $del[] = $user;
                        $count++;
                }

[edit] Modified Version

This is yet another minor modified

  • version of the SpecialLastUserLogin extension by User:Yazheirx.

The following is new in the version:

  • Installation in the path extensions
  • Internationalization of the texts (s. MediaWiki:Lastuserlogin and next)
  • Internationalization of the format of date
  • Internationalization the output of database

Change to Version 1.0.1

  • Make a link to the user page

Change to Version 1.0.2

  • The code checked the user authorizes

Change to Version 1.0.3

  • Fixed problems with varibale $_COOKIE and $PHPSELF
  • Translation to german
  • Insert sytel cellpadding in table

Change to Version 1.0.4

  • Fixed problems with MediaWiki 1.6

Change to Version 1.0.5

  • Fixed some formals problems

Change to Version 1.0.6

  • Specialpage is only display, when user have right 'lastlogin'
  • Change name of COOKIE variable
  • Fixed problem with MediaWiki 1.5.x

Change to Version 1.0.7

  • Fixed update timestamp query to be database agnostic for MediaWiki Version >=1.6
    (Note: Tested with postgresql and mysql only on versions 1.10.0 and 1.11.0 only)

Change to Version 1.1.0

  • Separate file for i18n and a better i18n code
  • Added Dutch (nl) translation
  • Starting French (fr) and Latin (la) translations
  • Better English messages
  • New directory 'extensions/SpecialLastUserLogin/' for holding the new i18n file and the normal file together

[edit] Installation

[edit] Changing configuration

Add the following line to LocalSettings.php:

include('extensions/SpecialLastUserLogin/SpecialLastUserLogin.php');

and expand the rights in LocalSettings.php (sample):

$wgGroupPermissions['sysop']['lastlogin']       = true;

[edit] Source Code

[edit] SpecialLastUserLogin.php

Copy the following code in to extensions/SpecialLastUserLogin/SpecialLastUserLogin.php

<?php
#
# SpecialLastUserLogin Mediawiki extension
# 
# Original by Justin G. Cramer and Danila Ulyanov 22.11.2005
# 
# Extended by Thomas Klein
# 
# http://www.mediawiki.org/
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
 
/**
* ChangeLog
*
* 15.10.2007 1.1.0
*  - separate file for i18n and a better i18n code
*  - added Dutch (nl) translation
*  - starting French (fr) and Latin (la) translations
*  - better English messages
*  - new directory 'extensions/SpecialLastUserLogin/' for holding the new i18n file and the normal file together
* 
* 29.09.2007 1.0.7
*  - Fixed update timestamp query to be database agnostic for MediaWiki Version >=1.6
*    (Note: Tested with postgresql and mysql only on versions 1.10.0 and 1.11.0 only)
*
* 19.07.2006 1.0.6
*  - Specialpage is only display, when user have right 'lastlogin'
*  - Change name of COOKIE variable 
*  - Fixed problem with MediaWiki 1.5.x 
*
* 19.06.2006 1.0.5
*  - Fixed some formals problems
*
* 04.04.2006 1.0.4
*  - Fixed problems with MediaWiki 1.6
*
* 29.03.2006 1.0.3
*  - Fixed problems with varibale $_COOKIE and $PHPSELF
*  - Translation to german
*  - Insert style cellpadding in table
*
* 20.12.2005 1.0.2
*  - The code checked the user authorizes
*
* 09.12.2005 1.0.1
*  - Make a link to the user page
*
* 30.11.2005 1.0.0
*  - Release of the first version
*/
 
if( !defined( 'MEDIAWIKI' ) ) {
  die();
}
 
require_once "$IP/includes/SpecialPage.php";
 
$wgExtensionFunctions[] = "wfLastUserLogin";
$wgExtensionFunctions[] = "wfUpdateUserTouched";
 
$wgAvailableRights[]    = 'lastlogin';
 
$wgExtensionCredits['specialpage'][] = array(
                                            'name' => 'LastUserLogin',
                                            'description' => 'Displays the last time a user logged in',
                                            'url' => 'http://www.mediawiki.org/wiki/Extension:SpecialLastUserLoginEx',
                                            'author' => 'Justin G. Cramer, Danila Ulyanov, Thomas Klein',
                                            'version'=>'1.1.0');
 
function wfUpdateUserTouched( ) {
  global $wgVersion;
 
  if ( version_compare( $wgVersion, '1.6', '<' ) ) {
    global $wgDBprefix, $wgOut, $wgDBname;
 
    if (isset($_COOKIE) && isset($_COOKIE["{$wgDBname}UserID"])) {
      $db = &wfGetDB(DB_SLAVE);
      $query = "UPDATE {$wgDBprefix}user SET user_touched = '".wfTimestamp(TS_MW)."' WHERE user_id = ".intval($_COOKIE["{$wgDBname}UserID"]);
      $db->doQuery($query);
    }
  }
  else {
    global $wgDBprefix, $wgOut, $wgDBname, $wgCookiePrefix;  
 
    if (isset($_COOKIE) && isset($_COOKIE["{$wgCookiePrefix}UserID"])) {
      $db = &wfGetDB(DB_SLAVE);
      $query = "UPDATE ".$db->tableName("user")." SET user_touched = '".$db->timestamp()."' WHERE user_id = ".intval($_COOKIE["{$wgCookiePrefix}UserID"]);
      $db->doQuery($query);
    }
  }
}
 
function wfLastUserLogin() {
 
  class SpecialLastUserLogin extends SpecialPage {
  function SpecialLastUserLogin() {
    SpecialPage::SpecialPage('LastUserLogin', 'lastlogin');
  }
 
  function execute() {
    global $wgDBprefix, $wgOut, $wgLang, $PHPSELF;
    global $wgUser;
 
    if ( ! $wgUser->isAllowed('lastlogin') ) {
      $wgOut->permissionRequired('lastlogin');
      return;
    }
 
                $this->setHeaders();
    $skin = $wgUser->getSkin( );
 
    $wgOut->setPagetitle( wfMsg( 'lastuserlogin' ) );
 
    $db = &wfGetDB(DB_SLAVE);
    $style = 'style="border:1px solid #000;text-align:left;"';
    $fields = array('user_name'=>'lastuserlogin_userid',
                    'user_real_name'=>'lastuserlogin_username',
                    'user_email'=>'lastuserlogin_useremail',
                    'user_touched'=>'lastuserlogin_lastlogin'
                    );
    //get order by and check it
    if(isset($_REQUEST['order_by'])){
        if(isset($fields[$_REQUEST['order_by']])){
          $orderby = $_REQUEST['order_by'];
        }else{
          $orderby = 'user_name';
        }               
    }else{
       $orderby = 'user_name';
    }       
 
    //get order type and check it
    if(isset($_REQUEST['order_type'])){
      if($_REQUEST['order_type']=='DESC'){
        $ordertype = $_REQUEST['order_type'];
      }else{
        $ordertype = 'ASC';
      }               
    }else{
      $ordertype = 'ASC';
    }       
 
    $query = "SELECT user_name, user_real_name, user_email, user_touched FROM ".$db->tableName("user")." ORDER BY ".$orderby." ".$ordertype;
    $ordertype = $ordertype=='ASC'?'DESC':'ASC';
 
    if ($result = $db->doQuery($query)) {
      $out = '<table width="100%" cellpadding="3" '.$style.'><tr>';
 
      foreach($fields as $key=>$value){
        $out .= '<th '.$style.'><a href="?order_by='.$key.'&order_type='.$ordertype.'">'.wfMsg( $value ).'</a></th>';
      }
 
      $out .= "<th $style>".wfMsg( 'lastuserlogin_daysago' )."</th>";
      $out .= '</tr>';
 
      while ($row = $db->fetchRow($result)) {
        $out .= '<tr>';
        foreach($fields as $key=>$value){
 
        if ($key == "user_touched") {
          $style = 'style="border:1px solid #000"';
          $out .= "<td $style>".$wgLang->timeanddate( wfTimestamp(TS_MW, $row[$key]), true).
                  '</td><td style="border: 1px solid #000; text-align:right;">'.
                  $wgLang->formatNum(round((mktime() - wfTimestamp(TS_UNIX, $row[$key]))/3600/24, 2), 2)."</td>";
        } else {
          if ($key == "user_name") {
            $userPage = Title::makeTitle( NS_USER, htmlspecialchars($row[$key]));
            $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
 
            $out .= '<td '.$style.'>'.$name.'</a></td>';          
          } else { 
            $out .= '<td '.$style.'>'.htmlspecialchars($row[$key]).'&nbsp;</td>';
          }
        }
      }
 
      $out .= '</tr>';
    }
        }
    $out .= '</table>';
    $wgOut->addHTML( $out );
 
    }
  }
 
  SpecialPage::addPage( new SpecialLastUserLogin );
 
  global $wgMessageCache, $wgSpecialLastUserLoginMessages;
  require_once( 'SpecialLastUserLogin.i18n.php');
  foreach( $wgSpecialLastUserLoginMessages as $key => $value ) {
  $wgMessageCache->addMessages( $wgSpecialLastUserLoginMessages[$key], $key );}
}

[edit] SpecialLastUserLogin.i18n.php

<?php
 
$wgSpecialLastUserLoginMessages = array();
 
$wgSpecialLastUserLoginMessages['en'] = array(
        'lastuserlogin'           => 'Last user login',
        'lastuserlogin_userid'    => 'Username',
        'lastuserlogin_username'  => 'Real name',
        'lastuserlogin_useremail' => 'User email',
        'lastuserlogin_lastlogin' => 'Last login',
        'lastuserlogin_daysago'   => 'Days ago',
);
 
$wgSpecialLastUserLoginMessages['de'] = array(
        'lastuserlogin'           => 'Letzte Anmeldungen',
        'lastuserlogin_userid'    => 'Benutzername',
        'lastuserlogin_username'  => 'Echter Name',
        'lastuserlogin_useremail' => 'E-Mail-Adresse',
        'lastuserlogin_lastlogin' => 'Letzte Anmeldung',
        'lastuserlogin_daysago'   => 'Tage',
);
 
 
$wgSpecialLastUserLoginMessages['fr'] = array(
        'lastuserlogin_userid'    => 'Nom d’utilisateur',
        'lastuserlogin_username'  => 'Nom réel',
);
 
$wgSpecialLastUserLoginMessages['la'] = array(
        'lastuserlogin_userid'    => 'Nomen usoris',
        'lastuserlogin_username'  => 'Nomen tuum verum',
);
 
$wgSpecialLastUserLoginMessages['nl'] = array(
        'lastuserlogin'           => 'Laatste aanmeldingen van gebruikers',
        'lastuserlogin_userid'    => 'Gebruikersnaam',
        'lastuserlogin_username'  => 'Echte naam',
        'lastuserlogin_useremail' => 'E-mail',
        'lastuserlogin_lastlogin' => 'Laatste aanmelding',
        'lastuserlogin_daysago'   => 'Aantal dagen geleden',
);
 
$wgSpecialLastUserLoginMessages['lt'] = array(
        'lastuserlogin'           => 'Paskutinis naudotojo prisijungimas',
        'lastuserlogin_userid'    => 'Naudotojo vardas',
        'lastuserlogin_username'  => 'Tikras vardas',
        'lastuserlogin_useremail' => 'Naudotojo el. paštas',
        'lastuserlogin_lastlogin' => 'Paskutinis prisijungimas',
        'lastuserlogin_daysago'   => 'Dienų prieš',
);
Personal tools