allwiki首页  
天下维客 你可以修改的网络知识库
首页最近更改优秀条目专题展示电脑科技词典软件学习网络知识电脑安全明星时尚天下百科
 

MediaWiki扩展:条件菜单

天下维客,你可以修改的网络知识库

Jump to: navigation, search

本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文wiki发展^_^

This extension, allows for "conditional menus". It parses the following (example)

<submenu>
<iftitle>ConditionalMenus Extension</iftitle>
<ifcategory>Leet Category</ifcategory>
<level>3</level>

<content>
[[Link1]]
[[Link2]]
[[Link3]]
</content>

</submenu>

目录

What does it do?

In our example:
If the page's name is "ConditionalMenus Extension" then anything inside the content tag is shown.
If the page is of category "Leet Category" then anything inside the content tag is shown
In any other scenario nothing is parsed

What is it used for?

For example, you have navigation like:

[[Introduction]]
[[Series information]]
[[:Category:Characters Guide]]
[[:Category:Episode List]]
[[Goofs]]

And you want to display the list of characters but ONLY in the character pages (and maybe in an intro page), then you'd use:


[[Introduction]]
[[Series information]]
[[:Category:Characters Guide]]
<submenu>
<iftitle>Characters Guide</iftitle>
<iftitle>Joe</iftitle>
<ifcategory>Characters Guide</ifcategory>
<level>2</level>
<content>
[[Joe]]
[[Mac]]
[[Peter]]
</content>
</submenu>
[[:Category:Episode List]]
[[Goofs]]

this would show the "Joe, Mac and Peter" links to the user ONLY if he is inside the "Characters Guide" page, or if he is inside the Joe page, or if he is inside any page that belongs to the "characters guide" category

This way if the user is browsing the "Introduction" pages he sees the small menu, and as soon as he enters to the "Characters Guide" category page he also sees the list of characters

How to use it

  1. Copy everything inside "Code" below into a file called "navigation_en.php" and put it under your "extensions" folder
  2. In LocalSettings.php, add: include("extensions/navigation_en.php");
  3. Done! :)

Code


<?php

$wgExtensionFunctions[] = "wfNavigationExtension";

function wfNavigationExtension()
{
	global $wgParser;
	$wgParser->setHook("submenu", "submenusExtension" );
}

/**
 * This extension parses:
 * <submenu>
 *	<iftitle></iftitle>
 *	<ifcategory></ifcategory>
 *	<content></content>
 *	<level></level>
 * </submenu>
 */
function submenusExtension($input)
{
	global $wgTitle, $wgUser, $wgArticle;
        
        // initialize our array
	$matches = array();

        // we need title and categories
        preg_match_all("@\[Category:\s?(.*)\]@isU", @utf8_decode($wgArticle->mContent), $matches);                        
        $categories = array();
        foreach ($matches[1] as $match)
        {
        	$categories[] = trim($match);
        }
        
	// process allow list
        preg_match_all("@<iftitle>(.*)</iftitle>@isU", $input, $matches);
	$allowtitle = array();
        foreach ($matches[1] as $match)
        {
        	$allowtitle[] = trim($match);
        }
        
        preg_match_all("@<ifcategory>(.*)</ifcategory>@isU", @utf8_decode($input), $matches);
	$allowcategory = array();
        foreach ($matches[1] as $match)
        {
        	$match = preg_replace("@(Category:\s?)?(.*)@is", "\\2", $match);
        	$allowcategory[] = trim($match);
        }        

        // check if we are to parse content
        $allowed = false;
        if (in_array($wgTitle->mTextform, $allowtitle) || in_array($wgTitle->mDbkeyform, $allowtitle))
        {
        	$allowed = true;
        }
        
        foreach ($categories as $category)
        {
	        if (in_array(trim($category), $allowcategory))
	        {
	                $allowed = true;
                        break;
	        }        	
        }

	// if allowed print content, otherwise return blank
        if (!$allowed)
        {
        	return "";
        }
        
        preg_match("@<content>(.*)</content>@isU", $input, $matches);
        $content = trim($matches[1]);
        if (!$content)
        {
        	return "";
        }
        
        // parse the content
	$parser = new Parser();
        $parserOptions = ParserOptions::newFromUser($wgUser);
	$output = $parser->parse($content, $wgTitle, $parserOptions);
        
        // rather ugly hack to delete empty dots
        $returnval = trim($output->getText());
                
        preg_match("@<level>(.*)</level>@isU", $input, $matches);
        $level = intval(trim($matches[1]));
        if ($level)
        {
        	$returnval = preg_replace("@<p>(.*)</p>@isU", "\\1", $returnval);
        	$returnval = preg_replace("@^.*$@m", "<li>\\0</li>", $returnval);
        	$returnval = preg_replace("@<li>\s*</li>@", "", $returnval);                
                for ($i = 0; $i < $level; $i++)
                {
                	$returnval = "<ul>$returnval</ul>";
                }
        }
        
        // return final value
	return "\r\n".$returnval;
}


?>


原文参见: ConditionalMenus Extension

Personal tools
工具
金银币拍卖 金币拍卖预展  金银币网店 熊猫金银币 生肖金银币