* Incorporated tree display into the default category page view

* Implemented memcached caching for ajax requests with invalidation via page_touched
* General refactoring, moved some global functions into a class
* Lazy initialisation of messages, initialise the <head> section in the output only when a categorytree tag is present.
* Eliminated all $wgUser and $wgLang accesses from ajax requests, using message placeholders.
* Added speak:none style in .CategoryTreeBullet in case voice browsers try to read out the punctuation
This commit is contained in:
Tim Starling 2006-08-24 17:12:13 +00:00
parent d2b21e46fa
commit 026cc40790
6 changed files with 464 additions and 376 deletions

View file

@ -17,6 +17,7 @@
text-decoration: none;
color: inherit;
font-weight: bold;
speak: none;
}
.CategoryTreeLabelPage {

View file

@ -31,6 +31,10 @@ $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";
$messages['categorytree-not-found']= "Category <i>$1</i> not found";
?>
$messages['categorytree-show-list'] = "Show as list";
$messages['categorytree-show-tree'] = "Show as tree";
$messages['categorytree-too-many-subcats'] = "Can't show subcategories as a tree, there's too many of them.";
?>

View file

@ -1,5 +1,5 @@
/*
* JavaSCript functions for the CategoryTree extension, an AJAX based gadget
* JavaScript functions for the CategoryTree extension, an AJAX based gadget
* to display the category structure of a wiki
*
* @package MediaWiki
@ -55,7 +55,8 @@
div.innerHTML= '<i class="CategoryTreeNotice">' + categoryTreeLoadingMsg + '</i>';
function f( result ) {
if ( result == '' ) result= '<i class="CategorTreeNotice">' + categoryTreeNothingFoundMsg + '</i>';
if ( result == '' ) result= '<i class="CategoryTreeNotice">' + categoryTreeNothingFoundMsg + '</i>';
result = result.replace(/##LOAD##/g, categoryTreeLoadMsg);
div.innerHTML= result;
}

View file

@ -29,7 +29,7 @@ define('CT_MODE_ALL', 20);
**/
if ( !$wgUseAjax ) {
#NOTE: GlobalFunctions is not yet loaded, so use standard API only.
trigger_error( 'CategoryTree: Ajax is not enabled, aborting extension setup.', E_USER_WARNING );
trigger_error( 'CategoryTree: $wgUseAjax is not enabled, aborting extension setup.', E_USER_WARNING );
return;
}
@ -53,16 +53,28 @@ if ( !isset( $wgCategoryTreeHTTPCache ) ) $wgCategoryTreeHTTPCache = false;
* 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' );
$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';
#$wgHooks['OutputPageBeforeHTML'][] = 'efCategoryTreeHeadHook';
$wgAutoloadClasses['CategoryTreePage'] = dirname( __FILE__ ) . '/CategoryTreePage.php';
$wgAutoloadClasses['CategoryTree'] = dirname( __FILE__ ) . '/CategoryTreeFunctions.php';
$wgAutoloadClasses['CategoryTreeCategoryPage'] = dirname( __FILE__ ) . '/CategoryPageSubclass.php';
$wgSpecialPages['CategoryTree'] = 'CategoryTreePage';
#$wgHooks['SkinTemplateTabs'][] = 'efCategoryTreeInstallTabs';
$wgHooks['OutputPageParserOutput'][] = 'efCategoryTreeParserOutput';
$wgHooks['LoadAllMessages'][] = 'efInjectCategoryTreeMessages';
$wgHooks['ArticleFromTitle'][] = 'efCategoryTreeArticleFromTitle';
/**
* register Ajax function
@ -73,23 +85,13 @@ $wgAjaxExportList[] = 'efCategoryTreeAjaxWrapper';
* Hook it up
*/
function efCategoryTree() {
global $wgParser, $wgOut, $wgCategoryTreeAllowTag;
global $wgJsMimeType, $wgScriptPath;
global $wgParser, $wgCategoryTreeAllowTag;
if ( $wgCategoryTreeAllowTag ) $wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
#TODO: injecting scripts should be done on demand, by "somehow" using the ParserOutput
#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" );
}
/**
* Entry point for Ajax, registered in $wgAjaxExportList.
* This loads CategoryTreeFunctions.php and calls efCategoryTreeAjax()
* This loads CategoryTreeFunctions.php and calls CategoryTree::ajax()
*/
function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) {
global $wgAjaxCachePolicy, $wgCategoryTreeHTTPCache, $wgSquidMaxAge, $wgUseSquid;
@ -99,20 +101,17 @@ function efCategoryTreeAjaxWrapper( $category, $mode = CT_MODE_CATEGORIES ) {
$wgAjaxCachePolicy->setVary( 'Accept-Encoding, Cookie' ); #cache for anons only
#TODO: purge the squid cache when a category page is invalidated
}
require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
efInjectCategoryTreeMessages();
return efCategoryTreeAjax( $category, $mode );
$ct = new CategoryTree;
return $ct->ajax( $category, $mode );
}
/**
* Entry point for the <categorytree> tag parser hook.
* This loads CategoryTreeFunctions.php and calls efCategoryTreeTag()
* This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
*/
function efCategoryTreeParserHook( $cat, $argv, &$parser ) {
#$parser->mOutput->mCategoryTreeTag = true; #HACK: flag for use by efCategoryTreeHeadHook
$parser->mOutput->mCategoryTreeTag = true; # flag for use by efCategoryTreeParserOutput
static $initialized = false;
@ -134,31 +133,22 @@ function efCategoryTreeParserHook( $cat, $argv, &$parser ) {
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;
if ( $hideroot === '1' || $hideroot === 'yes' || $hideroot === 'on' || $hideroot === 'true' ) {
$hideroot = true;
}
else if ( $hideroot === '0' || $hideroot === 'no' || $hideroot === 'off' || $hideroot === 'false' ) {
$hideroot = false;
}
}
if ( !$initialized ) {
require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
efInjectCategoryTreeMessages();
#HACK for inlining JS messages "on demand". Putting them into the head would be nicer,
# but that would require some changes to ParserOutput to deal with the parser cache
$m = efCategoryTreeGetJsMessages();
}
else {
$m = '';
}
$initialized = true;
return $m . efCategoryTreeTag( $parser, $cat, $mode, $hideroot, $style );
$ct = new CategoryTree;
return $ct->getTag( $parser, $cat, $mode, $hideroot, $style );
}
/**
* Hook callback that installs a tab for CategoryTree on Category pages
*/
*/
/*
function efCategoryTreeInstallTabs( &$skin, &$content_actions ) {
global $wgTitle;
@ -166,77 +156,39 @@ function efCategoryTreeInstallTabs( &$skin, &$content_actions ) {
$special = Title::makeTitle( NS_SPECIAL, 'CategoryTree' );
efInjectCategoryTreeMessages();
$content_actions['categorytree'] = array(
'class' => false,
'text' => wfMsgHTML( 'categorytree-tab' ),
'text' => htmlspecialchars( CategoryTree::msg( 'tab' ) ),
'href' => $special->getLocalUrl() . '/' . $wgTitle->getPartialURL() );
return true;
}
}*/
/**
* Hook callback that injects messages and things into the <head> tag
* Does nothing if $parserOutput->mCategoryTreeTag is not set
*/
/* function efCategoryTreeHeadHook( &$parserOutput, &$text ) {
if ( ! @$parserOutput->mCategoryTreeTag ) return;
require_once( dirname( __FILE__ ) . '/CategoryTreeFunctions.php' );
efInjectCategoryTreeMessages();
efCategoryTreeHeader();
} */
function efCategoryTreeParserOutput( &$outputPage, &$parserOutput ) {
if ( !empty( $parserOutput->mCategoryTreeTag ) ) {
CategoryTree::setHeaders( $outputPage );
}
}
/**
* inject messages used by CategoryTree into the message cache
*/
function efInjectCategoryTreeMessages() {
global $wgMessageCache;
static $done = false;
if ( $done ) return;
else $done = true;
$msg = efLoadCategoryTreeMessages();
$wgMessageCache->addMessages( $msg );
CategoryTree::msg(false);
return true;
}
/**
* load the CategoryTree internationalization file
*/
function efLoadCategoryTreeMessages() {
global $wgLang;
$messages= array();
$f= dirname( __FILE__ ) . '/CategoryTree.i18n.php';
include( $f );
$f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgLang->getCode() . '.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;
$title = trim($title);
if ( $title === NULL || $title === '' || $title === false ) {
$dummy = NULL; #php sucks
return $dummy;
* ArticleFromTitle hook, override category page handling
*/
function efCategoryTreeArticleFromTitle( &$title, &$article ) {
if ( $title->getNamespace() == NS_CATEGORY ) {
$article = new CategoryTreeCategoryPage( $title );
}
#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;
return true;
}
?>
?>

View file

@ -16,270 +16,399 @@ if( !defined( 'MEDIAWIKI' ) ) {
die( 1 );
}
/*
* Returns a JS script that sets up messages as global JS variables.
*/
function efCategoryTreeGetJsMessages() {
global $wgJsMimeType;
class CategoryTree {
var $mIsAjaxRequest = false;
/**
* Set the script tags in an OutputPage object
* @param OutputPage $outputPage
*/
static function setHeaders( &$outputPage ) {
global $wgJsMimeType, $wgScriptPath;
efInjectCategoryTreeMessages();
# Register css file for CategoryTree
$outputPage->addLink(
array(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => $wgScriptPath . '/extensions/CategoryTree/CategoryTree.css'
)
);
# Register main js file for CategoryTree
$outputPage->addScript(
"<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/CategoryTree/CategoryTree.js\">" .
"</script>\n"
);
# Add messages
$outputPage->addScript(
" <script type=\"{$wgJsMimeType}\">
categoryTreeCollapseMsg = \"".Xml::escapeJsString(self::msg('collapse'))."\";
categoryTreeExpandMsg = \"".Xml::escapeJsString(self::msg('expand'))."\";
categoryTreeLoadMsg = \"".Xml::escapeJsString(self::msg('load'))."\";
categoryTreeLoadingMsg = \"".Xml::escapeJsString(self::msg('loading'))."\";
categoryTreeNothingFoundMsg = \"".Xml::escapeJsString(self::msg('nothing-found'))."\";
</script>\n"
);
}
return
" <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 ajax( $category, $mode ) {
global $wgMemc, $wgDBname;
$title = self::makeTitle( $category );
if ( ! $title ) return false; #TODO: error message?
$this->mIsAjaxRequest = true;
/**
* Ajax call. This is called by efCategoryTreeAjaxWrapper, which is used to
* load CategoryTreeFunctions.php on demand.
*/
function efCategoryTreeAjax( $category, $mode ) {
$title = efCategoryTreeMakeTitle( $category );
if ( ! $title ) return false; #TODO: error message?
return efCategoryTreeRenderChildren( $title, $mode );
}
# Retrieve page_touched for the category
$dbkey = $title->getDBkey();
$dbr =& wfGetDB( DB_SLAVE );
$touched = $dbr->selectField( 'page', 'page_touched',
array(
'page_namespace' => NS_CATEGORY,
'page_title' => $dbkey,
), __METHOD__ );
/**
* Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to
* load CategoryTreeFunctions.php on demand.
*/
function efCategoryTreeTag( &$parser, $category, $mode, $hideroot = false, $style = '' ) {
global $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag;
static $uniq = 0;
$category = trim( $category );
if ( $category === '' ) return false;
if ( $wgCategoryTreeDisableCache && !$wgCategoryTreeDynamicTag ) $parser->disableCache();
$title = efCategoryTreeMakeTitle( $category );
if ( $title === false || $title === NULL ) return false;
$html = '';
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeTag', 'style' => $style ) );
if ( !$title->getArticleID() ) {
$html .= wfOpenElement( 'span', array( 'class' => 'CategoryTreeNotice' ) );
$html .= $parser->recursiveTagParse( wfMsg( 'categorytree-not-found' , $category ) );
$html .= wfCloseElement( 'span' );
}
else {
if ( !$hideroot ) $html .= efCategoryTreeRenderNode( $title, $mode, true, $wgCategoryTreeDynamicTag );
else if ( !$wgCategoryTreeDynamicTag ) $html .= efCategoryTreeRenderChildren( $title, $mode );
# Try to retrieve it from memcached
if ( $touched ) {
$mckey = "$wgDBname:categorytree:$dbkey";
$mcvalue = $wgMemc->get( $mckey );
if ( $mcvalue ) {
# Check to see if the value has been invalidated
if ( $touched <= $mcvalue['timestamp'] ) {
wfDebug( "Got $mckey from cache\n" );
return $mcvalue['value'];
} else {
wfDebug( "$mckey has expired\n" );
}
}
}
$value = $this->renderChildren( $title, $mode );
# Save it to memcached
if ( $touched ) {
$wgMemc->set( $mckey,
array(
'timestamp' => wfTimestampNow(),
'value' => $value
), 86400 /* expiry 1 day */
);
}
return $value;
}
/**
* Custom tag implementation. This is called by efCategoryTreeParserHook, which is used to
* load CategoryTreeFunctions.php on demand.
*/
function getTag( &$parser, $category, $mode, $hideroot = false, $style = '' ) {
global $wgCategoryTreeDisableCache, $wgCategoryTreeDynamicTag;
static $uniq = 0;
$this->mIsAjaxRequest = false;
$category = trim( $category );
if ( $category === '' ) {
return false;
}
if ( $wgCategoryTreeDisableCache && !$wgCategoryTreeDynamicTag ) {
$parser->disableCache();
}
$title = self::makeTitle( $category );
if ( $title === false || $title === NULL ) return false;
$html = '';
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeTag', 'style' => $style ) );
if ( !$title->getArticleID() ) {
$html .= wfOpenElement( 'span', array( 'class' => 'CategoryTreeNotice' ) );
$html .= self::msg( 'not-found' , htmlspecialchars( $category ) );
$html .= wfCloseElement( 'span' );
}
else {
$uniq += 1;
$load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
if ( !$hideroot ) $html .= CategoryTree::renderNode( $title, $mode, true, $wgCategoryTreeDynamicTag );
else if ( !$wgCategoryTreeDynamicTag ) $html .= $this->renderChildren( $title, $mode );
else {
$uniq += 1;
$load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
$html .= wfOpenElement( 'script', array( 'type' => 'text/javascript', 'id' => $load ) );
$html .= 'categoryTreeLoadChildren("' . Xml::escapeJsString( $title->getDBKey() ) . '", "' . $mode . '", document.getElementById("' . $load . '").parentNode );';
$html .= wfCloseElement( 'script' );
}
}
$html .= wfCloseElement( 'div' );
$html .= "\n\t\t";
return $html;
}
/**
* Returns a string with an HTML representation of the children of the given category.
* $title must be a Title object
*/
function renderChildren( &$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;
$page = $dbr->tableName( 'page' );
$categorylinks = $dbr->tableName( 'categorylinks' );
$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, __METHOD__ );
$s= '';
while ( $row = $dbr->fetchRow( $res ) ) {
#TODO: translation support; ideally added to Title object
$t = Title::makeTitle( $row['page_namespace'], $row['page_title'] );
$s .= $this->renderNode( $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
*/
static function renderParents( &$title, $mode ) {
global $wgCategoryTreeMaxChildren;
$dbr =& wfGetDB( DB_SLAVE );
#additional stuff to be used if "transaltion" by interwiki-links is desired
$transFields = '';
$transJoin = '';
$transWhere = '';
$categorylinks = $dbr->tableName( 'categorylinks' );
$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, __METHOD__ );
$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'] );
$html .= wfOpenElement( 'script', array( 'type' => 'text/javascript', 'id' => $load ) );
$html .= 'categoryTreeLoadChildren("' . Xml::escapeJsString( $title->getDBKey() ) . '", "' . $mode . '", document.getElementById("' . $load . '").parentNode );';
$html .= wfCloseElement( 'script' );
#$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 renderNode( &$title, $mode = CT_MODE_CATEGORIES, $children = false, $loadchildren = false ) {
static $uniq = 0;
$load = false;
if ( $loadchildren ) {
$uniq += 1;
$load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
$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';
$linkattr= array( 'href' => 'javascript:void(0)' );
if ( $load ) $linkattr[ 'id' ] = $load;
if ( !$children ) {
$txt = '+';
$linkattr[ 'onclick' ] = "categoryTreeExpandNode('".Xml::escapeJsString($key)."','".$mode."',this);";
# Don't load this message for ajax requests, so that we don't have to initialise $wgLang
$linkattr[ 'title' ] = $this->mIsAjaxRequest ? '##LOAD##' : self::msg('load');
}
else {
$txt = ''; #NOTE: that's not a minus but a unicode ndash!
$linkattr[ 'onclick' ] = "categoryTreeCollapseNode('".Xml::escapeJsString($key)."','".$mode."',this);";
$linkattr[ 'title' ] = self::msg('collapse');
$linkattr[ 'class' ] = 'CategoryTreeLoaded';
}
$s = '';
#NOTE: things in CategoryTree.js rely on the exact order of tags!
# Specifically, the CategoryTreeChildren div must be the first
# sibling with nodeName = DIV of the grandparent of the expland link.
$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', $linkattr, $txt ) . '] ';
} else {
$s .= ' ';
}
$s .= wfCloseElement( 'span' );
$s .= wfOpenElement( 'a', array( 'class' => $labelClass, 'href' => $wikiLink ) ) . $label . wfCloseElement( 'a' );
$s .= wfCloseElement( 'div' );
$s .= "\n\t\t";
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) );
if ( $children ) $s .= $this->renderChildren( $title, $mode );
$s .= wfCloseElement( 'div' );
$s .= wfCloseElement( 'div' );
if ( $load ) {
$s .= "\n\t\t";
$s .= wfOpenElement( 'script', array( 'type' => 'text/javascript' ) );
$s .= 'categoryTreeExpandNode("'.Xml::escapeJsString($key).'", "'.$mode.'", document.getElementById("'.$load.'") );';
$s .= wfCloseElement( 'script' );
}
$s .= "\n\t\t";
return $s;
}
/**
* Creates a Title object from a user provided (and thus unsafe) string
*/
static function makeTitle( $title ) {
global $wgContLang, $wgCanonicalNamespaceNames;
$title = trim($title);
if ( $title === NULL || $title === '' || $title === false ) {
return NULL;
}
# The title must be in the category namespace
# Ignore a leading Category: if there is one
$t = Title::newFromText( $title, NS_CATEGORY );
if ( $t->getNamespace() != NS_CATEGORY ) {
$title = "Category:$title";
$t = Title::newFromText( $title );
}
return $t;
}
/**
* load the CategoryTree internationalization file
*/
static function loadMessages() {
global $wgLang;
$messages= array();
$f= dirname( __FILE__ ) . '/CategoryTree.i18n.php';
include( $f );
$f= dirname( __FILE__ ) . '/CategoryTree.i18n.' . $wgLang->getCode() . '.php';
if ( file_exists( $f ) ) include( $f );
return $messages;
}
/**
* Get a CategoryTree message, "categorytree-" prefix added automatically
*/
static function msg( $msg /*, ...*/ ) {
static $initialized = false;
global $wgMessageCache;
if ( !$initialized ) {
$wgMessageCache->addMessages( self::loadMessages() );
}
if ( $msg === false ) {
return null;
}
$args = func_get_args();
$msg = array_shift( $args );
if ( $msg == '' ) {
return wfMsgReal( $msg, $args );
} else {
return wfMsgReal( "categorytree-$msg", $args );
}
}
$html .= wfCloseElement( 'div' );
$html .= "\n\t\t";
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;
$page = $dbr->tableName( 'page' );
$categorylinks = $dbr->tableName( 'categorylinks' );
$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 = '';
$categorylinks = $dbr->tableName( 'categorylinks' );
$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, $loadchildren = false ) {
static $uniq = 0;
$load = false;
if ( $loadchildren ) {
$uniq += 1;
$load = 'ct-' . $uniq . '-' . mt_rand( 1, 100000 );
$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';
$linkattr= array( 'href' => 'javascript:void(0)' );
if ( $load ) $linkattr[ 'id' ] = $load;
if ( !$children ) {
$txt = '+';
$linkattr[ 'onclick' ] = "categoryTreeExpandNode('".Xml::escapeJsString($key)."','".$mode."',this);";
$linkattr[ 'title' ] = wfMsg('categorytree-load');
}
else {
$txt = ''; #NOTE: that's not a minus but a unicode ndash!
$linkattr[ 'onclick' ] = "categoryTreeCollapseNode('".Xml::escapeJsString($key)."','".$mode."',this);";
$linkattr[ 'title' ] = wfMsg('categorytree-collapse');
$linkattr[ 'class' ] = 'CategoryTreeLoaded';
}
$s = '';
#NOTE: things in CategoryTree.js rely on the exact order of tags!
# Specifically, the CategoryTreeChildren div must be the first
# sibling with nodeName = DIV of the grandparent of the expland link.
$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', $linkattr, $txt ) . '] ';
else $s .= ' ';
$s .= wfCloseElement( 'span' );
$s .= wfOpenElement( 'a', array( 'class' => $labelClass, 'href' => $wikiLink ) ) . $label . wfCloseElement( 'a' );
$s .= wfCloseElement( 'div' );
$s .= "\n\t\t";
$s .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeChildren', 'style' => $children ? "display:block" : "display:none" ) );
if ( $children ) $s .= efCategoryTreeRenderChildren( $title, $mode );
$s .= wfCloseElement( 'div' );
$s .= wfCloseElement( 'div' );
if ( $load ) {
$s .= "\n\t\t";
$s .= wfOpenElement( 'script', array( 'type' => 'text/javascript' ) );
$s .= 'categoryTreeExpandNode("'.Xml::escapeJsString($key).'", "'.$mode.'", document.getElementById("'.$load.'") );';
$s .= wfCloseElement( 'script' );
}
$s .= "\n\t\t";
return $s;
}
?>
?>

View file

@ -15,7 +15,7 @@ if( !defined( 'MEDIAWIKI' ) ) {
die( 1 );
}
class CategoryTree extends SpecialPage {
class CategoryTreePage extends SpecialPage {
var $target = '';
var $mode = CT_MODE_CATEGORIES;
@ -23,10 +23,10 @@ class CategoryTree extends SpecialPage {
/**
* Constructor
*/
function CategoryTree() {
function __construct() {
global $wgOut;
SpecialPage::SpecialPage( 'CategoryTree', '', true );
#inject messages
efInjectCategoryTreeMessages();
}
@ -40,8 +40,6 @@ class CategoryTree extends SpecialPage {
$this->setHeaders();
require_once( dirname(__FILE__) . '/CategoryTreeFunctions.php' );
if ( $par ) $this->target = $par;
else $this->target = $wgRequest->getVal( 'target', wfMsg( 'rootcategory') );
@ -63,16 +61,19 @@ class CategoryTree extends SpecialPage {
$wgOut->addHtml( $this->makeInputForm() );
if( $this->target !== '' && $this->target !== NULL ) {
$wgOut->addScript( efCategoryTreeGetJsMessages() ); #TODO: move it...
CategoryTree::setHeaders( $wgOut );
$title = efCategoryTreeMakeTitle( $this->target );
$title = CategoryTree::makeTitle( $this->target );
if ( $title && $title->getArticleID() ) {
$html = '';
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeParents' ) );
$html .= wfElement( 'span', array( 'class' => 'CategoryTreeParents' ), wfMsg( 'categorytree-parents' ) ) . ': ';
$parents = efCategoryTreeRenderParents( $title, $this->mode );
$html .= wfElement( 'span',
array( 'class' => 'CategoryTreeParents' ),
wfMsg( 'categorytree-parents' ) ) . ': ';
$ct = new CategoryTree;
$parents = $ct->renderParents( $title, $this->mode );
if ( $parents == '' ) $html .= wfMsg( 'categorytree-nothing-found' );
else $html .= $parents;
@ -80,7 +81,7 @@ class CategoryTree extends SpecialPage {
$html .= wfCloseElement( 'div' );
$html .= wfOpenElement( 'div', array( 'class' => 'CategoryTreeResult' ) );
$html .= efCategoryTreeRenderNode( $title, $this->mode, true, false );
$html .= $ct->renderNode( $title, $this->mode, true, false );
$html .= wfCloseElement( 'div' );
$wgOut->addHtml( $html );
}
@ -113,4 +114,4 @@ class CategoryTree extends SpecialPage {
}
}
?>
?>