Difference between revisions of "Extension:ListView.php"

From Organic Design wiki
(DBKey not title)
(wf -> ef)
 
(4 intermediate revisions by one other user not shown)
Line 9: Line 9:
 
define('LISTVIEW_VERSION','0.0.0, 2008-04-16');
 
define('LISTVIEW_VERSION','0.0.0, 2008-04-16');
  
$wgExtensionFunctions[]        = 'wfSetupListView';
+
$egListViewAction    = 'list';
 +
$egRecordTemplatesCat = 'Record_templates'; # DBkey of category that valid record templates are members of
 +
 
 +
$wgExtensionFunctions[]        = 'efSetupListView';
 
$wgExtensionCredits['other'][] = array(
 
$wgExtensionCredits['other'][] = array(
 
'name'        => 'ListView',
 
'name'        => 'ListView',
Line 18: Line 21:
 
);
 
);
  
function wfSetupListView() {
+
function efSetupListView() {
 
global $wgHooks,$wgOut,$wgUser;
 
global $wgHooks,$wgOut,$wgUser;
$wgHooks['SkinTemplateTabs'][] = 'wfListViewUpdateActions';
+
$wgHooks['SkinTemplateTabs'][] = 'efListViewUpdateActions';
$wgHooks['UnknownAction'][]    = 'wfListViewUnknownAction';
+
$wgHooks['UnknownAction'][]    = 'efListViewUnknownAction';
 
}
 
}
  
function wfListViewUpdateActions(&$skin,&$actions) {
+
function efListViewUpdateActions(&$skin,&$actions) {
global $wgTitle,$wgRequest;
+
global $wgTitle,$wgRequest,$egListViewAction;
$selected = $wgRequest->getText('action') == 'listview' ? 'selected' : false;
+
$selected = $wgRequest->getText('action') == $egListViewAction ? 'selected' : false;
$url      = $wgTitle->getLocalURL("action=listview");
+
$url      = $wgTitle->getLocalURL("action=$egListViewAction");
 
if (is_object($wgTitle)) {
 
if (is_object($wgTitle)) {
$actions['listview'] = array(
+
$actions[$egListViewAction] = array(
'text'  => 'view as list',
+
'text'  => 'list', # todo: should use wfMsg($egListViewAction)
 
'class' => $selected,
 
'class' => $selected,
 
'href'  => $url
 
'href'  => $url
Line 38: Line 41:
 
}
 
}
  
function wfListViewUnknownAction($action,&$article) {
+
function efListViewUnknownAction($action,&$article) {
global $wgOut,$wgUser,$wgTitle,$wgParser;
+
global $wgOut,$wgUser,$wgTitle,$wgParser,$wgRequest,$egRecordTemplatesCat,$egListViewAction;
if ($action != 'listview') return true;
+
if ($action != $egListViewAction) return true;
+
 
$title = $article->getTitle();
+
# Get the list of titles
if ($title->getNamespace() == NS_CATEGORY) {
+
if ($wgRequest->getText('template')) {
$links = array();
+
$dbr  = &wfGetDB(DB_SLAVE);
+
# Template was specified, list comes from template and query if specified
$cl    = $dbr->tableName('categorylinks');
+
# - also other parameters incl. query=DPL|SMW
$to    = $title->getDbKey();
+
 
$res  = $dbr->select($cl,'cl_from',"cl_to = '$to'",__METHOD__,array('ORDER BY' => 'cl_sortkey'));
 
while ($row = $dbr->fetchRow($res)) {
 
$t = Title::newFromID($row[0]);
 
$u = $t->getLocalURL();
 
$t = $t->getPrefixedText();
 
$links[] = array(1 => "<a title=\"$t\" href=\"$u\">$t</a>");
 
}
 
 
}
 
}
 
else {
 
else {
if (is_object($wgParser)) { $psr = $wgParser; $opt = $wgParser->mOptions; }
+
 
else { $psr = new Parser; $opt = NULL; }
+
# Read the valid links into $links array
if (!is_object($opt)) $opt = ParserOptions::newFromUser($wgUser);
+
$title = $article->getTitle();
$html = $psr->parse($article->fetchContent(),$title,$opt,true,true)->getText();
+
if ($title->getNamespace() == NS_CATEGORY) {
preg_match_all("|<li>\\s*(<a[^>]+title=[\"'](.*?)[\"'][^>]*>(.+?)</a>)|s",$html,$links,PREG_SET_ORDER);
+
$links = array();
 +
$dbr  = &wfGetDB(DB_SLAVE);
 +
$cl    = $dbr->tableName('categorylinks');
 +
$to    = $title->getDbKey();
 +
$res  = $dbr->select($cl,'cl_from',"cl_to = '$to'",__METHOD__,array('ORDER BY' => 'cl_sortkey'));
 +
while ($row = $dbr->fetchRow($res)) {
 +
$t = Title::newFromID($row[0]);
 +
$u = $t->getLocalURL();
 +
$t = $t->getPrefixedText();
 +
$links[] = array(1 => "<a title=\"$t\" href=\"$u\">$t</a>");
 +
}
 +
}
 +
else {
 +
if (is_object($wgParser)) { $psr = $wgParser; $opt = $wgParser->mOptions; }
 +
else { $psr = new Parser; $opt = NULL; }
 +
if (!is_object($opt)) $opt = ParserOptions::newFromUser($wgUser);
 +
$html = $psr->parse($article->fetchContent(),$title,$opt,true,true)->getText();
 +
preg_match_all("|<li>\\s*(<a[^>]+title=[\"'](.*?)[\"'][^>]*>(.+?)</a>)|s",$html,$links,PREG_SET_ORDER);
 +
}
 
}
 
}
  
 +
# Render a generic selection form (a dropdown of all the record types found in Category:Record templates)
 +
# - later add generic query form
 +
$select = '<option selected>Select record type</option>';
 +
$dbr    = &wfGetDB(DB_SLAVE);
 +
$cl    = $dbr->tableName('categorylinks');
 +
$res    = $dbr->select($cl,'cl_from',"cl_to = '$egRecordTemplatesCat'",__METHOD__,array('ORDER BY' => 'cl_sortkey'));
 +
while ($row = $dbr->fetchRow($res)) $select .= '<option>'.(Title::newFromID($row[0])->getText()).'</option>';
 +
$select = "<select name=\"template\">\n$select\n</select>";
 +
$wgOut->addHtml($select);
 +
 +
# Pager
 +
# - todo: add pager links accounting for limit and offset
 +
$pager = '....';
 +
 +
# PreProcess the list
 +
# - get common templates
 +
# - calculate totals
 +
 +
# Get meta information (if any) from templates
 +
# - could be useful to allow record templates to contain parameters for list-view
 +
 +
# Render the list
 +
# - todo: add limit and offset
 
$wgOut->setPageTitle($title);
 
$wgOut->setPageTitle($title);
 +
$wgOut->addHtml($pager);
 
$wgOut->addHtml("<table>\n");
 
$wgOut->addHtml("<table>\n");
 
 
$class = 'even';
 
$class = 'even';
 
foreach ($links as $link) {
 
foreach ($links as $link) {
Line 74: Line 111:
 
$wgOut->addHtml("<td>$link[1]</td>");
 
$wgOut->addHtml("<td>$link[1]</td>");
 
$wgOut->addHtml("</tr>\n");
 
$wgOut->addHtml("</tr>\n");
}
+
}
+
$wgOut->addHtml($totals);
 
$wgOut->addHtml("</table>\n");
 
$wgOut->addHtml("</table>\n");
 +
$wgOut->addHtml($pager);
 
return false;
 
return false;
 
}
 
}

Latest revision as of 04:17, 13 May 2008

<?php

  1. Extension:ListView
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:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:Extension
  3. - Started: 2008-04-16

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

define('LISTVIEW_VERSION','0.0.0, 2008-04-16');

$egListViewAction = 'list'; $egRecordTemplatesCat = 'Record_templates'; # DBkey of category that valid record templates are members of

$wgExtensionFunctions[] = 'efSetupListView'; $wgExtensionCredits['other'][] = array( 'name' => 'ListView', 'author' => 'User:Nad', 'description' => 'Allows categories or articles containing lists of article links to be rendered as a sortable list', 'url' => 'http://www.organicdesign.co.nz/Extension:ListView.php', 'version' => LISTVIEW_VERSION );

function efSetupListView() { global $wgHooks,$wgOut,$wgUser; $wgHooks['SkinTemplateTabs'][] = 'efListViewUpdateActions'; $wgHooks['UnknownAction'][] = 'efListViewUnknownAction'; }

function efListViewUpdateActions(&$skin,&$actions) { global $wgTitle,$wgRequest,$egListViewAction; $selected = $wgRequest->getText('action') == $egListViewAction ? 'selected' : false; $url = $wgTitle->getLocalURL("action=$egListViewAction"); if (is_object($wgTitle)) { $actions[$egListViewAction] = array( 'text' => 'list', # todo: should use wfMsg($egListViewAction) 'class' => $selected, 'href' => $url ); } return true; }

function efListViewUnknownAction($action,&$article) { global $wgOut,$wgUser,$wgTitle,$wgParser,$wgRequest,$egRecordTemplatesCat,$egListViewAction; if ($action != $egListViewAction) return true;

# Get the list of titles if ($wgRequest->getText('template')) {

# Template was specified, list comes from template and query if specified # - also other parameters incl. query=DPL|SMW

} else {

# Read the valid links into $links array $title = $article->getTitle(); if ($title->getNamespace() == NS_CATEGORY) { $links = array(); $dbr = &wfGetDB(DB_SLAVE); $cl = $dbr->tableName('categorylinks'); $to = $title->getDbKey(); $res = $dbr->select($cl,'cl_from',"cl_to = '$to'",__METHOD__,array('ORDER BY' => 'cl_sortkey')); while ($row = $dbr->fetchRow($res)) { $t = Title::newFromID($row[0]); $u = $t->getLocalURL(); $t = $t->getPrefixedText(); $links[] = array(1 => "<a title=\"$t\" href=\"$u\">$t</a>"); } } else { if (is_object($wgParser)) { $psr = $wgParser; $opt = $wgParser->mOptions; } else { $psr = new Parser; $opt = NULL; } if (!is_object($opt)) $opt = ParserOptions::newFromUser($wgUser); $html = $psr->parse($article->fetchContent(),$title,$opt,true,true)->getText();

preg_match_all("|

  • \\s*(<a[^>]+title=[\"'](.*?)[\"'][^>]*>(.+?)</a>)|s",$html,$links,PREG_SET_ORDER); } } # Render a generic selection form (a dropdown of all the record types found in Category:Record templates) # - later add generic query form $select = '<option selected>Select record type</option>'; $dbr = &wfGetDB(DB_SLAVE); $cl = $dbr->tableName('categorylinks'); $res = $dbr->select($cl,'cl_from',"cl_to = '$egRecordTemplatesCat'",__METHOD__,array('ORDER BY' => 'cl_sortkey')); while ($row = $dbr->fetchRow($res)) $select .= '<option>'.(Title::newFromID($row[0])->getText()).'</option>'; $select = "<select name=\"template\">\n$select\n</select>"; $wgOut->addHtml($select); # Pager # - todo: add pager links accounting for limit and offset $pager = '....'; # PreProcess the list # - get common templates # - calculate totals # Get meta information (if any) from templates # - could be useful to allow record templates to contain parameters for list-view # Render the list # - todo: add limit and offset $wgOut->setPageTitle($title); $wgOut->addHtml($pager); $wgOut->addHtml("\n"); $class = 'even'; foreach ($links as $link) { $class = $class == 'even' ? 'odd' : 'even'; $wgOut->addHtml(""); $wgOut->addHtml(""); $wgOut->addHtml(""); $wgOut->addHtml("\n"); } $wgOut->addHtml($totals); $wgOut->addHtml("
    <input type=\"checkbox\" />$link[1]
    \n");

    $wgOut->addHtml($pager); return false; }