mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CategoryTree
synced 2024-11-27 09:43:06 +00:00
Adding Ajax based CategoryTree extension.
Pending: internationalization (weeee... first commit to the MW repository... thanks Tim!)
This commit is contained in:
commit
5a5f823367
42
CategoryTree.css
Normal file
42
CategoryTree.css
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Stylesheet for the CategoryTree extension, an AJAX based gadget
|
||||
* to display the category structure of a wiki
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
.CategoryTreeChildren {
|
||||
margin-left: 1.5ex;
|
||||
}
|
||||
|
||||
.CategoryTreeBullet a:link {
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.CategoryTreeLabelPage {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.CategoryTreeParents {
|
||||
margin-top: 1.5ex;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.CategoryTreeResult {
|
||||
margin-top: 1.5ex;
|
||||
}
|
||||
|
||||
.CategoryTreeTag {
|
||||
margin-top: 0.5ex;
|
||||
margin-bottom: 0.5ex;
|
||||
}
|
||||
|
||||
.CategoryTreeNotice {
|
||||
font-style: italic;
|
||||
}
|
36
CategoryTree.i18n.php
Normal file
36
CategoryTree.i18n.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Internationalisation file for the CategoryTree extension
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
$messages['categorytree']= 'CategoryTree';
|
||||
$messages['categorytree-tab']= 'Tree';
|
||||
$messages['categorytree-header']=
|
||||
'Enter a category name to see its contents as a tree structure.
|
||||
Note that this requires advanced [[:en:JavaScript|JavaScript]] functionality known as [[:en:AJAX|AJAX]].
|
||||
If you have a very old browser, or have JavaScript disabled, it will not work.
|
||||
';
|
||||
|
||||
$messages['categorytree-category']= 'Category';
|
||||
$messages['categorytree-go']= 'Show Tree';
|
||||
$messages['categorytree-parents']= 'Parents';
|
||||
|
||||
$messages['categorytree-mode-categories']= 'categories only';
|
||||
$messages['categorytree-mode-pages']= 'pages except images';
|
||||
$messages['categorytree-mode-all']= 'all pages';
|
||||
|
||||
$messages['categorytree-collapse']= 'collapse';
|
||||
$messages['categorytree-expand']= 'expand';
|
||||
$messages['categorytree-load']= 'load';
|
||||
$messages['categorytree-loading']= 'loading';
|
||||
$messages['categorytree-nothing-found']= 'nothing found';
|
||||
$messages['categorytree-not-found']= "Category ''\$1'' not found";
|
||||
|
||||
?>
|
54
CategoryTree.js
Normal file
54
CategoryTree.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* JavaSCript functions for the CategoryTree extension, an AJAX based gadget
|
||||
* to display the category structure of a wiki
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
function categoryTreeExpandNode(cat, mode, lnk) {
|
||||
var div= lnk.parentNode.parentNode.nextSibling;
|
||||
|
||||
div.style.display= 'block';
|
||||
lnk.innerHTML= '–';
|
||||
lnk.title= categoryTreeCollapseMsg;
|
||||
lnk.onclick= function() { categoryTreeCollapseNode(cat, mode, lnk) }
|
||||
|
||||
if (lnk.className != "CategoryTreeLoaded") {
|
||||
categoryTreeLoadNode(cat, mode, lnk, div);
|
||||
}
|
||||
}
|
||||
|
||||
function categoryTreeCollapseNode(cat, mode, lnk) {
|
||||
var div= lnk.parentNode.parentNode.nextSibling;
|
||||
|
||||
div.style.display= 'none';
|
||||
lnk.innerHTML= '+';
|
||||
lnk.title= categoryTreeExpandMsg;
|
||||
lnk.onclick= function() { categoryTreeExpandNode(cat, mode, lnk) }
|
||||
}
|
||||
|
||||
function categoryTreeLoadNode(cat, mode, lnk, div) {
|
||||
var page_request = false;
|
||||
|
||||
div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>';
|
||||
div.style.display= 'block';
|
||||
lnk.className= 'CategoryTreeLoaded';
|
||||
lnk.innerHTML= '–';
|
||||
lnk.title= categoryTreeCollapseMsg;
|
||||
lnk.onclick= function() { categoryTreeCollapseNode(cat, mode, lnk) }
|
||||
|
||||
function f( result ) {
|
||||
if ( result == '' ) result= '<i class="CategorTreeNotice">' + categoryTreeNothingFoundMsg + '</i>';
|
||||
div.innerHTML= result;
|
||||
}
|
||||
|
||||
categoryTreeDoCall( cat, mode, f );
|
||||
}
|
||||
|
||||
function categoryTreeDoCall() {
|
||||
sajax_do_call( "efCategoryTreeAjaxWrapper", categoryTreeDoCall.arguments );
|
||||
}
|
182
CategoryTree.php
Normal file
182
CategoryTree.php
Normal file
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Setup and Hooks for the CategoryTree extension, an AJAX based gadget
|
||||
* to display the category structure of a wiki
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
if( !defined( 'MEDIAWIKI' ) ) {
|
||||
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
|
||||
die( 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constants for use with efCategoryTreeRenderChildren,
|
||||
* defining what should be shown in the tree
|
||||
*/
|
||||
define('CT_MODE_CATEGORIES', 0);
|
||||
define('CT_MODE_PAGES', 10);
|
||||
define('CT_MODE_ALL', 20);
|
||||
|
||||
/**
|
||||
* Abort if AJAX is not enabled
|
||||
**/
|
||||
if ( !$wgUseAjax ) {
|
||||
wgDebug( 'CategoryTree: Ajax is not enabled, aborting extension setup.' );
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options:
|
||||
*
|
||||
* $wgCategoryTreeMaxChildren - maximum number of children shown in a tree node. Default is 200
|
||||
* $wgCategoryTreeAllowTag - enable <categorytree> tag. Default is true.
|
||||
* $wgCategoryTreeDisableCache - disabled the parser cache for pages with a <categorytree> tag. Default is true.
|
||||
*/
|
||||
if ( !isset( $wgCategoryTreeMaxChildren ) ) $wgCategoryTreeMaxChildren = 200;
|
||||
if ( !isset( $wgCategoryTreeAllowTag ) ) $wgCategoryTreeAllowTag = true;
|
||||
if ( !isset( $wgCategoryTreeDisableCache ) ) $wgCategoryTreeDisableCache = true;
|
||||
|
||||
/**
|
||||
* Register extension setup hook and credits
|
||||
*/
|
||||
$wgExtensionFunctions[] = 'efCategoryTree';
|
||||
$wgExtensionCredits['specialpage'][] = array( 'name' => 'CategoryTree', 'author' => 'Daniel Kinzler', 'url' => 'http://meta.wikimedia.org/wiki/CategoryTree extension' );
|
||||
$wgExtensionCredits['parserhook'][] = array( 'name' => 'CategoryTree', 'author' => 'Daniel Kinzler', 'url' => 'http://meta.wikimedia.org/wiki/CategoryTree extension' );
|
||||
|
||||
/**
|
||||
* Register the special page
|
||||
*/
|
||||
$wgAutoloadClasses['CategoryTree'] = dirname( __FILE__ ) . '/CategoryTreePage.php';
|
||||
$wgSpecialPages['CategoryTree'] = 'CategoryTree';
|
||||
$wgHooks['SkinTemplateTabs'][] = 'efCategoryTreeInstallTabs';
|
||||
|
||||
/**
|
||||
* register Ajax function
|
||||
*/
|
||||
$wgAjaxExportList[] = 'efCategoryTreeAjaxWrapper';
|
||||
|
||||
/**
|
||||
* Internal state
|
||||
*/
|
||||
$wgCategoryTreeHeaderDone = false; #set to true by efCategoryTreeHeader after registering the code
|
||||
$wgCategoryTreeMessagesDone = false; #set to true by efInjectCategoryTreeMessages after registering the messages
|
||||
|
||||
/**
|
||||
* Hook it up
|
||||
*/
|
||||
function efCategoryTree() {
|
||||
global $wgParser, $wgCategoryTreeAllowTag;
|
||||
|
||||
if ( $wgCategoryTreeAllowTag ) $wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Entry point for Ajax, registered in $wgAjaxExportList.
|
||||
* This loads CategoryTreeFunctions.php and calls efCategoryTreeAjax()
|
||||
*/
|
||||
function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) {
|
||||
require_once( 'CategoryTreeFunctions.php' );
|
||||
|
||||
return efCategoryTreeAjax( $category, $mode );
|
||||
}
|
||||
|
||||
/**
|
||||
* Entry point for the <categorytree> tag parser hook.
|
||||
* This loads CategoryTreeFunctions.php and calls efCategoryTreeTag()
|
||||
*/
|
||||
function efCategoryTreeParserHook( $cat, $argv ) {
|
||||
$style= @$argv[ 'style' ];
|
||||
|
||||
$mode= @$argv[ 'mode' ];
|
||||
if ( $mode !== NULL ) {
|
||||
$mode= trim( strtolower( $mode ) );
|
||||
|
||||
if ( $mode == 'all' ) $mode = CT_MODE_ALL;
|
||||
else if ( $mode == 'pages' ) $mode = CT_MODE_PAGES;
|
||||
else if ( $mode == 'categories' ) $mode = CT_MODE_CATEGORIES;
|
||||
}
|
||||
else {
|
||||
$mode = CT_MODE_CATEGORIES;
|
||||
}
|
||||
|
||||
$hideroot = @$argv[ 'hideroot' ];
|
||||
if ( $hideroot !== NULL ) {
|
||||
$hideroot = trim( strtolower( $hideroot ) );
|
||||
|
||||
if ( $hideroot === '1' || $hideroot === 'yes' || $hideroot === 'on' || $hideroot === 'true' ) $hideroot = true;
|
||||
else if ( $hideroot === '0' || $hideroot === 'no' || $hideroot === 'off' || $hideroot === 'false' ) $hideroot = false;
|
||||
}
|
||||
|
||||
return efCategoryTreeTag( $cat, $mode, $hideroot, $style );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook callback that installs a tab for CategoryTree on Category pages
|
||||
*/
|
||||
function efCategoryTreeInstallTabs( &$skin, &$content_actions ) {
|
||||
global $wgTitle;
|
||||
|
||||
if ( $wgTitle->getNamespace() != NS_CATEGORY ) return true;
|
||||
|
||||
$special = Title::makeTitle( NS_SPECIAL, 'CategoryTree' );
|
||||
|
||||
efInjectCategoryTreeMessages();
|
||||
|
||||
$content_actions['categorytree'] = array(
|
||||
'class' => false,
|
||||
'text' => wfMsgHTML( 'categorytree-tab' ),
|
||||
'href' => $special->getLocalUrl() . '/' . $wgTitle->getPartialURL() );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* inject messages used by CategoryTree into the message cache
|
||||
*/
|
||||
function efInjectCategoryTreeMessages() {
|
||||
global $wgMessageCache, $wgCategoryTreeMessagesDone;
|
||||
|
||||
if ( $wgCategoryTreeMessagesDone ) return;
|
||||
else $wgCategoryTreeMessagesDone = true;
|
||||
|
||||
$msg = efLoadCategoryTreeMessages();
|
||||
$wgMessageCache->addMessages( $msg );
|
||||
}
|
||||
|
||||
/**
|
||||
* load the CategoryTree internationalization file
|
||||
*/
|
||||
function efLoadCategoryTreeMessages() {
|
||||
global $wgLanguageCode, $wgContLanguageCode;
|
||||
|
||||
$messages= array();
|
||||
|
||||
$f= dirname( __FILE__ ) . '/CategoryTree.i18n.php';
|
||||
include( $f );
|
||||
|
||||
$f= dirname( __FILE__ ) . '/CategoryTree-' . $wgLanguageCode . '.i18n.php';
|
||||
if ( file_exists( $f ) ) include( $f );
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Title object from a user provided (and thus unsafe) string
|
||||
*/
|
||||
function & efCategoryTreeMakeTitle( $title ) {
|
||||
global $wgContLang, $wgCanonicalNamespaceNames;
|
||||
|
||||
#HACK to strip redundant namespace name
|
||||
$title = preg_replace( '~^\s*(' . $wgCanonicalNamespaceNames[ NS_CATEGORY ] . '|' . $wgContLang->getNsText( NS_CATEGORY ) . ')\s*:\s*~i', '', $title );
|
||||
|
||||
$t = Title::makeTitleSafe( NS_CATEGORY, $title );
|
||||
return $t;
|
||||
}
|
||||
|
||||
?>
|
256
CategoryTreeFunctions.php
Normal file
256
CategoryTreeFunctions.php
Normal file
|
@ -0,0 +1,256 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Core functions for the CategoryTree extension, an AJAX based gadget
|
||||
* to display the category structure of a wiki
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
if( !defined( 'MEDIAWIKI' ) ) {
|
||||
echo( "This file is part of an extension to the MediaWiki software and cannot be used standalone.\n" );
|
||||
die( 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts code into the HTML head. This mainly links CategoryTree.js and CategoryTree.css
|
||||
*/
|
||||
function efCategoryTreeHeader() {
|
||||
global $wgOut, $wgCategoryTreeHeaderDone;
|
||||
global $wgJsMimeType, $wgScriptPath;
|
||||
|
||||
if ( $wgCategoryTreeHeaderDone ) return;
|
||||
else $wgCategoryTreeHeaderDone = true;
|
||||
|
||||
#register css file for CategoryTree
|
||||
$wgOut->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css' ) );
|
||||
|
||||
#register main js file for CategoryTree
|
||||
$wgOut->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/CategoryTree/CategoryTree.js\"></script>\n" );
|
||||
|
||||
#make some localized messages available in JS
|
||||
$wgOut->addScript( "<script type=\"{$wgJsMimeType}\">
|
||||
categoryTreeCollapseMsg = \"".Xml::escapeJsString(wfMsg('categorytree-collapse'))."\";
|
||||
categoryTreeExpandMsg = \"".Xml::escapeJsString(wfMsg('categorytree-expand'))."\";
|
||||
categoryTreeLoadingMsg = \"".Xml::escapeJsString(wfMsg('categorytree-loading'))."\";
|
||||
categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(wfMsg('categorytree-nothing-found'))."\";
|
||||
</script>\n" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to
|
||||
* load CategoryTreeFunctions.php on demand.
|
||||
*/
|
||||
function efCategoryTreeAjax( $category, $mode ) {
|
||||
efInjectCategoryTreeMessages();
|
||||
|
||||
$title = efCategoryTreeMakeTitle( $category );
|
||||
|
||||
return efCategoryTreeRenderChildren( $title, $mode );
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to
|
||||
* load CategoryTreeFunctions.php on demand.
|
||||
*/
|
||||
function efCategoryTreeTag( $category, $mode, $hideroot = false, $style = '' ) {
|
||||
global $wgOut, $wgParser, $wgCategoryTreeDisableCache;
|
||||
|
||||
efInjectCategoryTreeMessages();
|
||||
efCategoryTreeHeader();
|
||||
|
||||
if ( $wgCategoryTreeDisableCache ) $wgParser->disableCache();
|
||||
|
||||
$title = efCategoryTreeMakeTitle( $category );
|
||||
|
||||
$html = '';
|
||||
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeTag', 'style' => $style ) );
|
||||
|
||||
if ( !$title->getArticleID() ) {
|
||||
$html .= wfOpenElement( 'span', array( 'class' => 'CategoryTreeNotice' ) );
|
||||
$html .= $wgOut->parse( wfMsg( 'categorytree-not-found' , $category ) );
|
||||
$html .= wfCloseElement( 'span' );
|
||||
}
|
||||
else {
|
||||
if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true );
|
||||
else $html .= efCategoryTreeRenderChildren( $title, $mode );
|
||||
}
|
||||
|
||||
$html .= wfCloseElement( 'div' );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with an HTML representation of the children of the given category.
|
||||
* $title must be a Title object
|
||||
*/
|
||||
function efCategoryTreeRenderChildren( &$title, $mode = CT_MODE_CATEGORIES ) {
|
||||
global $wgCategoryTreeMaxChildren;
|
||||
|
||||
$dbr =& wfGetDB( DB_SLAVE );
|
||||
|
||||
#additional stuff to be used if "transaltion" by interwiki-links is desired
|
||||
$transFields = '';
|
||||
$transJoin = '';
|
||||
$transWhere = '';
|
||||
|
||||
#namespace filter. Should be configurable
|
||||
if ( $mode == CT_MODE_ALL ) $nsmatch = '';
|
||||
else if ( $mode == CT_MODE_PAGES ) $nsmatch = ' AND cat.page_namespace != ' . NS_IMAGE;
|
||||
else $nsmatch = ' AND cat.page_namespace = ' . NS_CATEGORY;
|
||||
|
||||
$sql = "SELECT cat.page_namespace, cat.page_title,
|
||||
if( cat.page_namespace = " . NS_CATEGORY . ", 0, 1) as presort
|
||||
$transFields
|
||||
FROM page as cat
|
||||
JOIN categorylinks ON cl_from = cat.page_id
|
||||
$transJoin
|
||||
WHERE cl_to = " . $dbr->addQuotes( $title->getDBKey() ) . "
|
||||
$nsmatch
|
||||
AND cat.page_is_redirect = 0
|
||||
$transWhere
|
||||
ORDER BY presort, cat.page_namespace DESC, cat.page_title
|
||||
LIMIT " . (int)$wgCategoryTreeMaxChildren;
|
||||
|
||||
$res = $dbr->query( $sql, 'efCategoryTreeRenderChildren' );
|
||||
|
||||
$s= '';
|
||||
|
||||
while ( $row = $dbr->fetchRow( $res ) ) {
|
||||
#TODO: translation support; ideally added to Title object
|
||||
$t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
|
||||
$s .= efCategoryTreeRenderNode( $t, $mode, false );
|
||||
$s .= "\n\t\t";
|
||||
}
|
||||
|
||||
$dbr->freeResult( $res );
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with an HTML representation of the parents of the given category.
|
||||
* $title must be a Title object
|
||||
*/
|
||||
function efCategoryTreeRenderParents( &$title, $mode ) {
|
||||
global $wgCategoryTreeMaxChildren;
|
||||
|
||||
$dbr =& wfGetDB( DB_SLAVE );
|
||||
|
||||
#additional stuff to be used if "transaltion" by interwiki-links is desired
|
||||
$transFields = '';
|
||||
$transJoin = '';
|
||||
$transWhere = '';
|
||||
|
||||
$sql = "SELECT " . NS_CATEGORY . " as page_namespace, cl_to as page_title $transFields
|
||||
FROM categorylinks
|
||||
$transJoin
|
||||
WHERE cl_from = " . $title->getArticleID() . "
|
||||
$transWhere
|
||||
ORDER BY cl_to
|
||||
LIMIT " . (int)$wgCategoryTreeMaxChildren;
|
||||
|
||||
$res = $dbr->query( $sql, 'efCategoryTreeRenderParents' );
|
||||
|
||||
$special = Title::makeTitle( NS_SPECIAL, 'CategoryTree' );
|
||||
|
||||
$s= '';
|
||||
|
||||
while ( $row = $dbr->fetchRow( $res ) ) {
|
||||
#TODO: translation support; ideally added to Title object
|
||||
$t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
|
||||
|
||||
#$trans = $title->getLocalizedText();
|
||||
$trans = ''; #place holder for when translated titles are available
|
||||
|
||||
$label = htmlspecialchars( $t->getText() );
|
||||
if ( $trans && $trans!=$label ) $label.= ' ' . wfElement( 'i', array( 'class' => 'translation'), $trans );
|
||||
|
||||
$wikiLink = $special->getLocalURL( 'target=' . $t->getPartialURL() . '&mode=' . $mode );
|
||||
|
||||
if ( $s !== '' ) $s .= ' | ';
|
||||
|
||||
$s .= wfOpenElement( 'span', array( 'class' => 'CategoryTreeItem' ) );
|
||||
$s .= wfOpenElement( 'a', array( 'class' => 'CategoryTreeLabel', 'href' => $wikiLink ) ) . $label . wfCloseElement( 'a' );
|
||||
$s .= wfCloseElement( 'span' );
|
||||
|
||||
$s .= "\n\t\t";
|
||||
}
|
||||
|
||||
$dbr->freeResult( $res );
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with a HTML represenation of the given page.
|
||||
* $title must be a Title object
|
||||
*/
|
||||
function efCategoryTreeRenderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false ) {
|
||||
|
||||
$ns = $title->getNamespace();
|
||||
$key = $title->getDBKey();
|
||||
|
||||
#$trans = $title->getLocalizedText();
|
||||
$trans = ''; #place holder for when translated titles are available
|
||||
|
||||
#when showing only categories, omit namespace in label
|
||||
if ( $mode == CT_MODE_CATEGORIES ) $label = htmlspecialchars( $title->getText() );
|
||||
else $label = htmlspecialchars( $title->getPrefixedText() );
|
||||
|
||||
if ( $trans && $trans!=$label ) $label.= ' ' . wfElement( 'i', array( 'class' => 'translation'), $trans );
|
||||
|
||||
$wikiLink = $title->getLocalURL();
|
||||
|
||||
$labelClass = 'CategoryTreeLabel ' . ' CategoryTreeLabelNs' . $ns;
|
||||
|
||||
if ( $ns == NS_CATEGORY ) $labelClass .= ' CategoryTreeLabelCategory';
|
||||
else $labelClass .= ' CategoryTreeLabelPage';
|
||||
|
||||
if ( ( $ns % 2 ) > 0 ) $labelClass .= ' CategoryTreeLabelTalk';
|
||||
|
||||
if ( !$children ) {
|
||||
$js = "categoryTreeExpandNode('".addslashes($key)."','".$mode."',this);";
|
||||
$txt = '+';
|
||||
$ttl = wfMsg('categorytree-load');
|
||||
$cls = '';
|
||||
}
|
||||
else {
|
||||
$js = "categoryTreeCollapseNode('".addslashes($key)."','".$mode."',this);";
|
||||
$txt = '–'; #NOTE: that'S not a minus but a unicode ndash!
|
||||
$ttl = wfMsg('categorytree-collapse');
|
||||
$cls = 'CategoryTreeLoaded';
|
||||
}
|
||||
|
||||
$s = '';
|
||||
|
||||
#NOTE: things in CategoryTree.js rely on the exact order of tags and absense of text nodes!
|
||||
# Specifically, to get from the a tag in the bullet to the CategoryTreeChildren div,
|
||||
# the following is used: div= lnk.parentNode.parentNode.nextSibling
|
||||
|
||||
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeSection' ) );
|
||||
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeItem' ) );
|
||||
|
||||
$s .= wfOpenElement( 'span', array( 'class' => 'CategoryTreeBullet' ) );
|
||||
if ( $ns == NS_CATEGORY ) $s .= '[' . wfElement( 'a', array( 'href' => 'javascript:void(0)', 'onclick' => $js, 'title' => $ttl, 'class' => $cls ), $txt ) . '] ';
|
||||
else $s .= ' ';
|
||||
$s .= wfCloseElement( 'span' );
|
||||
|
||||
$s .= wfOpenElement( 'a', array( 'class' => $labelClass, 'href' => $wikiLink ) ) . $label . wfCloseElement( 'a' );
|
||||
$s .= wfCloseElement( 'div' );
|
||||
|
||||
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) );
|
||||
if ( $children ) $s .= efCategoryTreeRenderChildren( $title, $mode );
|
||||
$s .= wfCloseElement( 'div' );
|
||||
$s .= wfCloseElement( 'div' );
|
||||
$s .= "\n\t\t";
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
?>
|
114
CategoryTreePage.php
Normal file
114
CategoryTreePage.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
/**
|
||||
* Special page for the CategoryTree extension, an AJAX based gadget
|
||||
* to display the category structure of a wiki
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author Daniel Kinzler <duesentrieb@brightbyte.de>
|
||||
* @copyright © 2006 Daniel Kinzler
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
if( !defined( 'MEDIAWIKI' ) ) {
|
||||
echo( "This file is part of an extension to the MediaWiki software and cannot be used standalone.\n" );
|
||||
die( 1 );
|
||||
}
|
||||
|
||||
require_once( 'SpecialPage.php' );
|
||||
require_once( 'CategoryTreeFunctions.php' );
|
||||
|
||||
class CategoryTree extends SpecialPage {
|
||||
|
||||
var $target = '';
|
||||
var $mode = CT_MODE_CATEGORIES;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
function CategoryTree() {
|
||||
SpecialPage::SpecialPage( 'CategoryTree', 'categorytree' );
|
||||
|
||||
#inject messages
|
||||
efInjectCategoryTreeMessages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Main execution function
|
||||
* @param $par Parameters passed to the page
|
||||
*/
|
||||
function execute( $par ) {
|
||||
global $wgRequest, $wgOut, $wgMakeBotPrivileged, $wgUser;
|
||||
|
||||
$this->setHeaders();
|
||||
|
||||
if ( $par ) $this->target = $par;
|
||||
else $this->target = $wgRequest->getVal( 'target', wfMsg( 'rootcategory') );
|
||||
|
||||
#HACK for undefined root category
|
||||
if ( $this->target == '<rootcategory>' || $this->target == '<rootcategory>' ) $this->target = NULL;
|
||||
|
||||
$this->mode = $wgRequest->getVal( 'mode', CT_MODE_CATEGORIES );
|
||||
|
||||
if ( $this->mode == 'all' ) $this->mode = CT_MODE_ALL;
|
||||
else if ( $this->mode == 'pages' ) $this->mode = CT_MODE_PAGES;
|
||||
else if ( $this->mode == 'categories' ) $this->mode = CT_MODE_CATEGORIES;
|
||||
|
||||
$this->mode = (int)$this->mode;
|
||||
|
||||
$wgOut->addWikiText( wfMsg( 'categorytree-header' ) );
|
||||
|
||||
$wgOut->addHtml( $this->makeInputForm() );
|
||||
|
||||
if( $this->target !== '' && $this->target !== NULL ) {
|
||||
efCategoryTreeHeader();
|
||||
|
||||
$title = efCategoryTreeMakeTitle( $this->target );
|
||||
|
||||
if ( $title->getArticleID() ) {
|
||||
$html = '';
|
||||
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeParents' ) );
|
||||
$html .= wfElement( 'span', array( 'class' => 'CategoryTreeParents' ), wfMsg( 'categorytree-parents' ) ) . ': ';
|
||||
|
||||
$parents = efCategoryTreeRenderParents( $title, $this->mode );
|
||||
|
||||
if ( $parents == '' ) $html .= wfMsg( 'categorytree-nothing-found' );
|
||||
else $html .= $parents;
|
||||
|
||||
$html .= wfCloseElement( 'div' );
|
||||
|
||||
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeResult' ) );
|
||||
$html .= efCategoryTreeRenderNode( $title, $this->mode, true );
|
||||
$html .= wfCloseElement( 'div' );
|
||||
$wgOut->addHtml( $html );
|
||||
}
|
||||
else {
|
||||
$wgOut->addHtml( wfOpenElement( 'div', array( 'class' => 'CategoryTreeNotice' ) ) );
|
||||
$wgOut->addWikiText( wfMsg( 'categorytree-not-found' , $this->target ) );
|
||||
$wgOut->addHtml( wfCloseElement( 'div' ) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Input form for entering a category
|
||||
*/
|
||||
function makeInputForm() {
|
||||
$thisTitle = Title::makeTitle( NS_SPECIAL, $this->getName() );
|
||||
$form = '';
|
||||
$form .= wfOpenElement( 'form', array( 'name' => 'categorytree', 'method' => 'get', 'action' => $thisTitle->getLocalUrl() ) );
|
||||
$form .= wfElement( 'label', array( 'for' => 'target' ), wfMsg( 'categorytree-category' ) ) . ' ';
|
||||
$form .= wfElement( 'input', array( 'type' => 'text', 'name' => 'target', 'id' => 'target', 'value' => $this->target ) ) . ' ';
|
||||
$form .= wfOpenElement( 'select', array( 'name' => 'mode' ) );
|
||||
$form .= wfElement( 'option', array( 'value' => 'categories' ) + ( $this->mode == CT_MODE_CATEGORIES ? array ( 'selected' => 'selected' ) : array() ), wfMsg( 'categorytree-mode-categories' ) );
|
||||
$form .= wfElement( 'option', array( 'value' => 'pages' ) + ( $this->mode == CT_MODE_PAGES ? array ( 'selected' => 'selected' ) : array() ), wfMsg( 'categorytree-mode-pages' ) );
|
||||
$form .= wfElement( 'option', array( 'value' => 'all' ) + ( $this->mode == CT_MODE_ALL ? array ( 'selected' => 'selected' ) : array() ), wfMsg( 'categorytree-mode-all' ) );
|
||||
$form .= wfCloseElement( 'select' );
|
||||
$form .= wfElement( 'input', array( 'type' => 'submit', 'name' => 'dotree', 'value' => wfMsg( 'categorytree-go' ) ) );
|
||||
$form .= wfCloseElement( 'form' );
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue