Extension:LastModifiedLink

From MediaWiki.org
Jump to navigation Jump to search

Last Modified Link provides the option to append the recency of the last modified date / time to an internal link.

MediaWiki extensions manual
OOjs UI icon advanced.svg
Last Modified Link
Release status: beta
Implementation Parser extension, Link markup
Description Provides the option to append the recency of the last modified date / time to an internal link.
Author(s) Jordan Hoff (jhofftalk)
License GNU General Public License 2.0 or later
Download copy & paste
Hooks used
ParserBeforeStrip
Translate the LastModifiedLink extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

The Last Modified Link extension extends link markup to append the last modified date of an internal page to it's referring link.

Usage[edit]

The link markup is extended by simply appending an "(lm)" to the end of any internal link.

Example[edit]

  1. [[MyInternalPage]](lm)
    displays as
    MyInternalPage (23 hours)

Limitations[edit]

I can't guarantee that this is the best practice, or takes every precaution against cross site scripting attacks ( note the page url is queried against the database. ) Perhaps there should be some sanity checks, but I wrote this for an internal wiki, so I'm not concerned about that.

Download and Installation[edit]

Please copy and paste the code found below and place it in

$IP/extensions/LastModifiedLink/LastModifiedLink.php

Then add the following to LocalSettings.php:

require_once("$IP/extensions/LastModifiedLink/LastModifiedLink.php");

Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Code[edit]

<?php
/*
  MediaWiki extension, provides shortcut markup in Wikitext links.
  Copyright (c) 2011 Jordan Hoff
 
  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  USA.
*/
 
//  Thanks to author of Extension:TernaryPipedLinks for the skeleton code.
 
if( !defined( 'MEDIAWIKI' ) ) {
    echo <<<EOT
This is an extension to the MediaWiki software and cannot be used alone.
To install it, please put the following line in LocalSettings.php:
require_once( "\$IP/extensions/LastModified.php" );
EOT;
    exit(1);
}
 
$wgExtensionCredits['parser'][] = array(
    'name' => 'Last Modified Link',
    'author' =>'Jordan Hoff',
    'url' => 'http://www.mediawiki.org/wiki/Extension:LastModifiedLink',
    'description' => 'Appends the last modified date to the specified link'
                                                     );
 
//  Parser hook looking for links with the Last Modified option
$wgHooks['ParserBeforeStrip'][] = 'efParseLastModifiedLinks';
 
//  If a link with an appended "(lm)" is found ( ex. [[Home Page]](lm) ), lookup the last modified date for the internal page
//		and then append it to the end of the link ( with a space )
function efParseLastModifiedLinks(&$parser, &$text, &$strip_state) {
	$matches = array();
	preg_match_all('/\[\[(.*)\]\]\(lm\)/i',$text,$matches);
	foreach($matches[0] as $key => $a) {
		$out = $matches[0][$key];
		$in = $matches[1][$key];
		//breakdown $in and lookup page for mod time
		$link = str_replace(' ','_',trim(ucfirst(current(explode('|',$in)))));
		if(!empty($link)) {
			$query = "SELECT * FROM {$GLOBALS['wgDBprefix']}page WHERE page_title = '" . mysql_escape_string($link) . "'";
			$result = mysql_query($query);
			if($row = mysql_fetch_assoc($result)) { //page_title is unique
				$mod_time = time_ago(wfTimestamp( TS_UNIX, $row['page_touched']));
				$text = str_replace($out,"[[$in]] <span class='modified'>($mod_time)</span>",$text);
			} else {
				$text = str_replace($out,"[[$in]]",$text); // stripping the (lm) from the end on invalid links
			}
		}
	}
    return true;
}

//http://css-tricks.com/snippets/php/time-ago-function/
function time_ago($tm,$rcs = 0) {
   $cur_tm = time(); $dif = $cur_tm-$tm;
   $pds = array('second','minute','hour','day','week','month','year','decade');
   $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
   for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);

   $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
   if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
   return trim($x);
}

Username and Date[edit]

I've customised the function "efParseLastModifiedLinks" to display the username and the date of the last modification.

If you keep this patch you can also delete the "time_ago" function that won't be used anymore.

function efParseLastModifiedLinks(&$parser, &$text, &$strip_state) {
        $matches = array();
        preg_match_all('/\[\[(.*)\]\]\(lm\)/i',$text,$matches);
        foreach($matches[0] as $key => $a) {
                $out = $matches[0][$key];
                $in = $matches[1][$key];
                //breakdown $in and lookup page for mod time
                $link = str_replace(' ','_',ucfirst(current(explode('|',$in))));
                if(!empty($link)) {
                        $query = "SELECT * FROM {$GLOBALS['wgDBprefix']}page JOIN {$GLOBALS['wgDBprefix']}revision ON {$GLOBALS['wgDBprefix']}page.page_id={$GLOBALS['wgDBprefix']}revision.rev_page WHERE page_title = '$link' ORDER by rev_id DESC" ;
                        $result = mysql_query($query);
                        if($row = mysql_fetch_assoc($result)) { //page_title is unique
                                $timeval = wfTimestamp( TS_UNIX, $row['page_touched']);
                                $username = strtoupper($row['rev_user_text']);
                                $mod_time = strftime("%d/%m/%Y (%H:%M)",$timeval);
                                $text = str_replace($out,"<span class='modified'><b>[[$in|$username]]</b> ".$mod_time."</span>",$text);
                        } else {
                                $text = str_replace($out,"",$text); // stripping the (lm) from the end on invalid links
                        }
                }
        }
    return true;
}

See also[edit]