Extension:ConditionalMenus

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

ConditionalMenus Extension Leet Category 3

Link1 Link2 Link3

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 Characters Guide Joe Characters Guide 2 Joe Mac Peter 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.php" and put it under your "extensions" folder
 * 2) In LocalSettings.php, add: include("extensions/navigation.php");
 * 3) Done! :)

Code
setHook("submenu", "submenusExtension" ); }

/** * This extension parses: * *        *        *        *        *  */ function submenusExtension($input) {       global $wgTitle, $wgUser, $wgArticle, $wgContLang;

$namespaceNames = $wgContLang->getNamespaces; $categoryNamespaceName = $namespaceNames[NS_CATEGORY]; // initialize our array $matches = array;

// find out categories this page belongs to       $categories = $wgTitle->getParentCategories; $categories = array_keys($categories);

// strip namespace prefix foreach ($categories as &$category) { $category = preg_replace("@($categoryNamespaceName:\s?)?(.*)@is", "\\2", $category); }       // process allow list preg_match_all("@ (.*) @isU", $input, $matches); $allowtitle = array; foreach ($matches[1] as $match) {               $allowtitle[] = trim($match); }       preg_match_all("@ (.*) @isU", $input, $matches); $allowcategory = array; foreach ($matches[1] as $match) {               $match = preg_replace("@($categoryNamespaceName:\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("@ (.*) @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("@ (.*) @isU", $input, $matches); $level = intval(trim($matches[1])); if ($level) {               $returnval = preg_replace("@ (.*) @isU", "\\1", $returnval); $returnval = preg_replace("@^.*$@m", "\\0", $returnval); $returnval = preg_replace("@\s*@", "", $returnval); for ($i = 0; $i < $level; $i++) {                       $returnval = "$returnval"; }       }        // return final value return "\r\n".$returnval; }

?>