

Tuesday, 06 April 2010 18:44
Extensions for mediawiki
Display a list of red links (links that are not related to an existing page). This is a beta version but seems to work on small wikis.
<redlinks v="my legend:" />
Copy the code below and place it in $IP/extensions/RedLinks/redlinks.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.
To install this extension, add the following to LocalSettings.php:
#add configuration parameters here
#setup user rights here
require_once("$IP/extensions/RedLinks/redlinks.php");
you must change these values according to your sql server configuration
$wgDBserver = "myserver";
$wgDBname = "my base";
$wgDBuser = "username";
$wgDBpassword = "password";
<?php
# Confirm MW environment
if (defined('MEDIAWIKI')) {
# Credits
$wgExtensionCredits['parserhook'][] = array(
'name'=>'RedLinks',
'author'=>'Luc Brunet(luc.brunet at cnri.fr)',
'url'=>'none',
'description'=>'list all the red links',
'version'=>'0.1'
);
# Register Extension initializer
$wgExtensionFunctions[] = "wfRedLinks";
# Extension initializer
function wfRedLinks() {
global $wgParser;
$wgParser->setHook( "redlinks", "renderRedLinks" );
}
/**
* Callback function for embedding video.
* @param String $input Text between open and close tags - should always be empty or null.
* @param Array $params Array of tag attributes.
* @param Parser $parser Instance of Parser performing the parse.
*/
function renderRedLinks( $input, $params, &$parser ) {
$v = htmlspecialchars($params['v']);
$wgDBserver = "myserver";
$wgDBname = "my base";
$wgDBuser = "username";
$wgDBpassword = "password";
$chandle = mysql_connect($wgDBserver, $wgDBuser, $wgDBpassword) ;
mysql_select_db($wgDBname, $chandle);
$query1="SELECT pl_title FROM risk_pagelinks WHERE pl_title NOT IN ( SELECT page_title FROM risk_page )";
$result = mysql_query($query1);
$rl="";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$itm=$row[0];
$rl.= "<a href=index.php?title=$itm target=_blank>$itm</a> ";
}
mysql_free_result($result);
$url = "$v$rl";
return $url;
}
} # Closing MW Environment wrapper
What can this extension do?
This extension is ported from WP-Cumulus[1] . Categories are recovered by a sub-version of Extension:NiceCategoryList2 extension
Usage
<cumulus style="12" color="0x242424" hicolor="0x966600">Category:Base_Category</cumulus>
Installation
To install this extension, add the following to LocalSettings.php:
#add configuration parameters here
#setup user rights here
require_once("$IP/extensions/cumulus/cumulus.php");
You must have in the cumulus directory
<?php
if (!defined('MEDIAWIKI')) die();
$wgExtensionFunctions[] = 'wfCumulusWiki';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'CumulusWiki',
'author' => 'Luc',
'url' => 'http://www.mediawiki.org/w/index.php?title=Extension:Cumulus',
'description' => 'WP-Cumulus Port to mediawiki',
);
/*
* Sets a parser hook for <cumulus></cumulus>.
*/
function wfCumulusWiki() {
new CumulusWiki();
}
/*
* Simple class to hold category's title, links list,
* and categories list., taken from NiceCategoryList extension
*/
class CumulusWiki_Links {
private $title;
private $articles = array();
private $categories = array();
private $subcats = array();
public function __construct($title) {
$this->title = $title;
}
public function addCategory($title, $links) {
$this->subcats[] = $title;
if ($links)
$this->categories[] = $links;
}
public function addArticle($title) {
$this->articles[] = $title;
}
/*
* Get the title of this category.
*/
public function getTitle() {
return $this->title;
}
/*
* Get the titles of the sub-categories of this category.
*/
public function getCatTitles() {
return $this->subcats;
}
/*
* Get the titles of the articles in this category.
*/
public function getArtTitles() {
return $this->articles;
}
/*
* Get the link records of the sub-categories of this category,
* if we have them.
* Returns an array of CumulusWiki_Links objects.
*/
public function getCategories() {
return $this->categories;
}
/*
* Return true iff we have link records for the sub-categories
* of this category.
*/
public function hasCatLinks() {
return count($this->categories) > 0;
}
/*
* Title comparison function
*/
private function titleCmp($a, $b) {
return $a->getText() > $b->getText();
}
/*
* CumulusWiki_Links comparison function
*/
private function categoryCmp($a, $b) {
return self::titleCmp($a->title, $b->title);
}
/*
* Sort links and categories alphabetically.
*/
public function sort() {
usort($this->articles, array(&$this, "titleCmp"));
usort($this->categories, array(&$this, "categoryCmp"));
}
}
class CumulusWiki {
////////////////////////////////////////////////////////////////////////
// Configuration
////////////////////////////////////////////////////////////////////////
/*
* Default settings for the category list.
*/
private $settings = array(
'maxdepth' => 32, // Sanity stop level.
'style' => 12, // No du style WP-Cumulus
'showcats' => 1, // Non-0 to display sub-cat links in a category.
'showarts' => 1, // Non-0 to display article links in a category.
'headings' => 'head', // Show category headings as headings.
'headstart' => 1, // Heading level to start at.
'sort' => 0, // Non-0 to sort the list alphabetically;
'color' =>'0x242424',
'hicolor' =>'0x996600'
);
////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////
/*
* Setup Nice Category List extension.
* Sets a parser hook for <ncl></ncl>.
*/
public function __construct() {
global $wgParser;
$wgParser->setHook('cumulus', array(&$this, 'hookCumulus'));
}
////////////////////////////////////////////////////////////////////////
// Hook
////////////////////////////////////////////////////////////////////////
/*
* The hook function. Handles <ncl></ncl>.
* $category The tag's text content
* this is the name of the category we want to index.
* $argv List of tag parameters; these can be any of the settings
* in $this->settings.
* $parser Parser handle.
*/
public function hookCumulus($category, $argv, &$parser) {
// Get any user-specified parameters, and save them in $this->settings.
$this->settings = array_merge($this->settings, $argv);
$localParser = new Parser();
$category = $localParser->preprocess($category, $parser->mTitle, $parser->mOptions, false);
// Make a Title objhect for the requested category.
$title = Title::newFromText($category);
if (!$title)
return '<p>Failed to create title!</p>';
// Get the database handle, and get all the category links for
// this category.
$dbr =& wfGetDB(DB_SLAVE);
$catData = $this->searchCategory($dbr, $title, 0);
// Generate the category listing.
$output = $this->outputCategory($catData);
$output="
<script type=\"text/javascript\" src=\"/extensions/cumulus/swfobject.js\"></script>
<div id=\"flashcontent\">This will be shown to users with no Flash or Javascript.</div>
<script type=\"text/javascript\">
var so = new SWFObject(\"/extensions/cumulus/tagcloud.swf\", \"tagcloud\", \"600\", \"400\", \"7\", \"#ffffff\");
// uncomment next line to enable transparency
so.addParam(\"wmode\", \"transparent\");
so.addVariable(\"tcolor\", \"0x333333\");
so.addVariable(\"mode\", \"tags\");
so.addVariable(\"distr\", \"true\");
so.addVariable(\"tspeed\", \"100\");
so.addVariable(\"tagcloud\", \"<tags>$output</tags>\");
so.write(\"flashcontent\");
</script>
";
$localParser = new Parser();
return $output;
}
////////////////////////////////////////////////////////////////////////
// Database Access
////////////////////////////////////////////////////////////////////////
/*
* Get all of the direct and indirect members of a given category: ie.
* all of the articles and categories which belong to that category
* and its children.
* $dbr The database handle
* $catTitle The Title object for the category to search
* $depth Our current recursion depth: starts at 0
* $processed List of categories that have been searched to date
* (to prevent looping)
*
* Returns null if this category has already been searched; otherwise,
* a CumulusWiki_Links object for the given category, containing all
* the sub-categories and member articles.
*/
private function searchCategory($dbr, $catTitle, $depth, $processed = array()) {
// Avoid endless recursion by making sure we haven't been here before.
if (in_array($catTitle->getText(), $processed))
return null;
$processed[] = $catTitle->getText();
// Get all of the category links for this category.
$links = $this->getCategoryLinks($dbr, $catTitle);
// Build a list of items which belong to this category.
$cl = new CumulusWiki_Links($catTitle);
foreach ($links as $l) {
// Make a Title for this item.
$title = Title::makeTitle($l->page_namespace, $l->page_title);
if ($title->getNamespace() == NS_CATEGORY) {
// This item is itself a category: recurse to find all its
// links, unless we've hit maxdepth.
$subLinks = null;
if ($depth + 1 < $this->settings['maxdepth'])
$subLinks = $this->searchCategory($dbr, $title,
$depth + 1, $processed);
// Record the subcategory name, and its links if we got any.
$cl->addCategory($title, $subLinks);
} else {
// This is a regular page; just add it to the list.
$cl->addArticle($title);
}
}
// Sort the item lists, if requested. (Thanks, Jej.)
//if ($this->settings['sort'])
// $cl->sort();
return $cl;
}
/*
* Get all of the direct members of a given category.
* $dbr The database handle
* $title The Title object for the category to search
*
* Returns an array of objects, each representing one member of the named
* caregory. Each object contains the following fields from the database:
* page_title
* page_namespace
* cl_sortkey
*/
private function getCategoryLinks($dbr, $title) {
// Query the database.
$res = $dbr->select(
array('page', 'categorylinks'),
array('page_title', 'page_namespace', 'cl_sortkey'),
array('cl_from = page_id', 'cl_to' => $title->getDBKey()),
'',
array('ORDER BY' => 'cl_sortkey')
);
if ($res === false)
return array();
// Convert the results list into an array.
$list = array();
while ($x = $dbr->fetchObject($res))
$list[] = $x;
// Free the results.
$dbr->freeResult($res);
return $list;
}
////////////////////////////////////////////////////////////////////////
// Output
////////////////////////////////////////////////////////////////////////
/*
* Generate output for the list.
*/
function outputCategory($category, $level = 0) {
global $wgContLang;
$output = '';
// The second level and onwards has a heading.
// The heading gets smaller as the level grows.
if ($level > 0) {
$title = $category->getTitle();
$ptitle = $title->getPrefixedText();
$title = $wgContLang->convert($title->getText());
$link = "[[:" . $ptitle . "|'''" . $title . "''']]";
$output .= "<a href='http://".$_SERVER['SERVER_NAME']."/index.php?title=Category:$title' style='".$this->settings['style']."' color='".$this->settings['color']."' hicolor='".$this->settings['hicolor']."'>$title</a>";
}
// Recurse into each subcategory.
$subCategories = $category->getCategories();
foreach ($subCategories as $cat)
{
$cttemp=$this->outputCategory($cat, $level + 1);
$output .= $this->outputCategory($cat, $level + 1);
}
return $output;
}
}