Jump to content

Extension talk:Uniwiki Authors/vpovilaitis

Add topic
From mediawiki.org
<?php
/* vim: noet ts=4 sw=4
 * http://www.mediawiki.org/wiki/Extension:Uniwiki_Authors
 * http://www.gnu.org/licenses/gpl-3.0.txt */

if ( !defined( "MEDIAWIKI" ) )
	die();

/* ---- CONFIGURABLE OPTIONS ---- */
$wgShowAuthorsNamespaces = array ( NS_MAIN );
$wgShowAuthors = true;
$wgShowAuthorsPercent = 10;
$wgShowAuthorsAddBytes = 200;

$wgExtensionCredits['other'][] = array(
	'path'           => __FILE__,
	'name'           => 'Authors',
	'author'         => array( 'Merrick Schaefer', 'Mark Johnston', 'Evan Wheeler', 'Adam Mckaig (at UNICEF)', 'Vitas Povilaitis' ),
	'url'            => 'https://www.mediawiki.org/wiki/Extension:Uniwiki_Authors',
	'descriptionmsg' => 'authors-desc',
);

$wgExtensionMessagesFiles['Authors'] = dirname( __FILE__ ) . '/Authors.i18n.php';

/* ---- HOOKS ---- */
$wgHooks['OutputPageBeforeHTML'][] = "UW_Authors_List";

function UW_Authors_List ( &$out, &$text ) {
	global $wgTitle, $wgRequest, $wgShowAuthorsNamespaces, $wgShowAuthors, $wgShowAuthorsPercent, $wgShowAuthorsAddBytes;

	/* do nothing if the option is disabled
	 * (but why would the extension be enabled?) */
	if ( !$wgShowAuthors )
		return true;

	// only build authors on namespaces in $wgShowAuthorsNamespaces
	if ( !in_array ( $wgTitle->getNamespace(), $wgShowAuthorsNamespaces ) )
		return true;

	/* get the contribs from the database (don't use the default
	 * MediaWiki one since it ignores the current user) */
	$article = new Article ( $wgTitle );
	$contribs = array();
	$usrs = array();
	$lens = 0;
	$db = wfGetDB ( DB_MASTER );
	$rev_table  = $db->tableName ( "revision" );
	$user_table = $db->tableName ( "user" );

	$sql = "SELECT rev_user, rev_user_text, user_real_name, rev_timestamp, rev_len
		FROM $rev_table LEFT JOIN $user_table ON rev_user = user_id
		WHERE rev_page = {$article->getID()}
		ORDER BY rev_timestamp";
		/*GROUP BY rev_user, rev_user_text, user_real_name*/

	$results = $db->query ( $sql, __METHOD__ );
	while ( $line = $db->fetchObject ( $results ) ) {
           $lenproc = 0;
	   if ( $lens <> 0 )
	      $lenproc = intval(intval($line->rev_len) / $lens);
	   if ( ($lens == 0) || (intval($line->rev_len) - $lens  > $wgShowAuthorsAddBytes) ||
	        ($lenproc > $wgShowAuthorsPercent) ) {
	      if ( !in_array( $line->rev_user, $usrs ) ) {
	        $usrs[] = $line->rev_user;
		$contribs[] = array(
			$line->rev_user,
			$line->rev_user_text,
			$line->user_real_name,
 			"{$line->rev_len}:{$lens}:{$lenproc}"
		);
              };
	   };
	   $lens = intval($line->rev_len);
	}

	$db->freeResult ( $results );


	// return if there are no authors
	if ( sizeof ( $results ) <= 0 )
		return true;

	// now build a sensible authors display in HTML

	

	$authors = "\n<div class='authors'>" .
		"<h4>" . wfMsg( 'authors_authors' ) . "</h4>" .
		"<ul>";
	$anons = 0;
	foreach ( $contribs as $author ) {
		$id       = $author[0];
		$username = $author[1];
		$realname = $author[2];
		$len = $author[3];
		$authorLinker = new Linker();
		if ( $id != "0" ) { // user with an id
			// FIME: broken. Incompatible with 1.14. Method creditLink() was renamed and changed.
			$author_link = $authorLinker->userLink( $id, $username );
			$authors .= "<li>$author_link – $realname</li>";
		} else { // anonymous
			$anons++;
		}
	}
	// add the anonymous entries
	if ( $anons > 0 )
		$authors .= "<li>" . wfMsg( 'authors_anonymous' ) . "</li>";
	$authors .= "</ul></div>";

	$text .= $authors;
	return true;
}