Difference between revisions of "Extension:RecentActivity"

From Organic Design wiki
(free results)
(fix bug breaking if invalid titles produced)
Line 12: Line 12:
 
  */
 
  */
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
 
define('RECENTACTIVITY_VERSION', '1.0.2, 2009-03-16');
+
define('RECENTACTIVITY_VERSION', '1.0.3, 2009-03-17');
+
 
 
$egRecentActivityMagic        = "RecentActivity";
 
$egRecentActivityMagic        = "RecentActivity";
 
$wgExtensionFunctions[]        = 'efSetupRecentActivity';
 
$wgExtensionFunctions[]        = 'efSetupRecentActivity';
Line 26: Line 26:
 
'version'    => RECENTACTIVITY_VERSION
 
'version'    => RECENTACTIVITY_VERSION
 
);
 
);
+
 
 
class RecentActivity {
 
class RecentActivity {
  
Line 62: Line 62:
 
);
 
);
 
while ( $row = $dbr->fetchRow( $res ) ) {
 
while ( $row = $dbr->fetchRow( $res ) ) {
$page = Title::newFromId($row['rev_page'])->getPrefixedText();
+
$title = Title::newFromId($row['rev_page']);
$items[] = $format."[[:$page|$page]]";
+
if (is_object($title)) {
 +
$page = $title->getPrefixedText();
 +
$items[] = $format."[[:$page|$page]]";
 +
}
 
}
 
}
 
$dbr->freeResult( $res );
 
$dbr->freeResult( $res );
Line 80: Line 83:
 
);
 
);
 
while ( $row = $dbr->fetchRow( $res ) ) {
 
while ( $row = $dbr->fetchRow( $res ) ) {
$page = Title::newFromId($row['rev_page'])->getPrefixedText();
+
$title = Title::newFromId($row['rev_page']);
$items[] = $format."[[:$page|$page]]";
+
if (is_object($title)) {
 +
$page = $title->getPrefixedText();
 +
$items[] = $format."[[:$page|$page]]";
 +
}
 
}
 
}
 
$dbr->freeResult( $res );
 
$dbr->freeResult( $res );

Revision as of 23:36, 16 March 2009

<?php /**

* RecentActivity extension - Adds parser functions for listing recently created and edited articles
*
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:Extension

* See http://www.mediawiki.org/wiki/Extension:RecentActivity for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Nad
* @copyright © 2007 User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('RECENTACTIVITY_VERSION', '1.0.3, 2009-03-17');

$egRecentActivityMagic = "RecentActivity"; $wgExtensionFunctions[] = 'efSetupRecentActivity'; $wgHooks['LanguageGetMagic'][] = 'efRecentActivityLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'RecentActivity', 'author' => 'User:Nad', 'description' => 'Adds parser functions for listing recently created and edited articles', 'url' => 'http://www.mediawiki.org/wiki/Extension:RecentActivity', 'version' => RECENTACTIVITY_VERSION );

class RecentActivity {

function __construct() { global $wgParser, $egRecentActivityMagic;

		$wgParser->setFunctionHook($egRecentActivityMagic, array($this, 'expandMagic'), SFH_NO_HASH);

}

function expandMagic(&$parser) {

# Populate $argv with both named and numeric parameters $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; } $type = isset($argv['type'])  ? strtolower($argv['type']) : ; $user = isset($argv['user'])  ? $argv['user']  : false; $count = isset($argv['count'])  ? $argv['count']  : 5; $format = isset($argv['format']) ? $argv['format'] : '*';

# Build the list $items = array(); switch ($type) {

case 'edits': $dbr = wfGetDB( DB_SLAVE ); $tbl = $dbr->tableName( 'revision' ); $user = $user ? 'rev_user_text = '.$dbr->addQuotes( $user ) : ; $res = $dbr->select( $tbl, 'distinct rev_page', $user, __METHOD__, array( 'ORDER BY' => 'rev_timestamp DESC', 'LIMIT' => $count ) ); while ( $row = $dbr->fetchRow( $res ) ) { $title = Title::newFromId($row['rev_page']); if (is_object($title)) { $page = $title->getPrefixedText(); $items[] = $format."$page"; } } $dbr->freeResult( $res ); break;

case 'new': $dbr = wfGetDB( DB_SLAVE ); $tbl = $dbr->tableName( 'revision' ); $user = $user ? 'rev_user_text = '.$dbr->addQuotes( $user ) : ; $res = $dbr->select( $tbl, 'rev_page, MIN(rev_id) as minid', $user, __METHOD__, array( 'GROUP BY' => 'rev_page', 'ORDER BY' => 'minid DESC', 'LIMIT' => $count ) ); while ( $row = $dbr->fetchRow( $res ) ) { $title = Title::newFromId($row['rev_page']); if (is_object($title)) { $page = $title->getPrefixedText(); $items[] = $format."$page"; } } $dbr->freeResult( $res ); break;

default: $items[] = 'Bad activity type specified!'; }

return join("\n", $items); } }

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

function efSetupRecentActivity() { global $egRecentActivity; $egRecentActivity = new RecentActivity(); }

/**

* Register magic words
*/

function efRecentActivityLanguageGetMagic(&$magicWords, $langCode = 0) { global $egRecentActivityMagic; $magicWords[$egRecentActivityMagic] = array($langCode, $egRecentActivityMagic); return true; }