JavaScript:References [[JS:Array]] ! //Note this extension only works as written for PHP version 4.0.5 and up //This is due to our use of PREG_SPLIT_DELIM_CAPTURE in preg_split $wgExtensionFunctions[] = "mzBreadcrumbs"; function mzBreadCrumbs() { global $wgParser; $wgParser->setHook("breadcrumbs", "renderBreadcrumbs"); } $verboseDebug = false; /** * @param Title $title identifies the input page. * @return an array of parent pages, including the page itself if it exists. * Each item of the returned array is itself an array, with first item being a * prefixedDbKey and second item being the title-override value for that page (optional). */ function findParents($title) { global $verboseDebug; $pagekey = $title->getPrefixedDbKey(); $namespace = $title->getNamespace(); // Build a list of possible pages in our parent chain $parts = preg_split("/([.:])/", $pagekey, -1, PREG_SPLIT_DELIM_CAPTURE); if ($namespace != 0) //two array shifts here will grab the ":" this will also fix up $parts //because PREG_SPLIT_DELIM_CAPTURE stores the delim between every split portion. $namespace_prefix = array_shift($parts) . array_shift($parts); else $namespace_prefix = ''; if (empty($parts)) return $parts; $possiblePages = array(); for ($i = 0; $i < count($parts ); $i+=2) { if ($i > 1) { $possiblePage = $parts[0]; $j = 1; while ($j < $i) { $possiblePage .= $parts[$j] . $parts[$j+1]; $j += 2; } array_push($possiblePages, $possiblePage); } else { array_push($possiblePages, $parts[0]); } } global $wgVersion; $dbr =& wfGetDB(DB_SLAVE); $title_override = $dbr->tableName('title_override'); $cur = $dbr->tableName('cur'); if (substr($wgVersion, 0, 3) == "1.5") { $query = "SELECT page_id as cur_id, page_title as cur_title, LENGTH(page_title) as cur_len from " . $dbr->tableName('page') . " WHERE (page_namespace=$namespace AND page_title IN("; } else { $query = "SELECT cur_id, cur_title, LENGTH(cur_title) as cur_len from $cur WHERE " . "(cur_namespace=$namespace AND cur_title IN("; } $query .= join(", ", array_map(array(&$dbr, "addQuotes"), $possiblePages)); $query .= ")) ORDER BY cur_len ASC"; wfDebug("Query: $query\n"); $res = $dbr->query($query, "Breadcrumbs::findKnownPages"); $knownPages = array(); while ($s = $dbr->fetchObject($res)) { $title_query = "SELECT to_title from $title_override where to_id = $s->cur_id"; wfDebug("Query: $title_query\n"); $title_res = $dbr->query($title_query, "Breadcrumbs::findKnownPages"); $title = ''; while ($title_s = $dbr->fetchObject($title_res)) $title = $title_s->to_title; array_push($knownPages, array($namespace_prefix . $s->cur_title, $title)); } if ($verboseDebug) { ob_start(); var_dump($knownPages); $debug_content = ob_get_contents(); ob_end_clean(); wfDebug("knownPages: " . $debug_content); } return $knownPages; } /** * Generates breadcrumbs for the specified page. * @param Title $title The Title object for which the breadcrumbs need to be generated. * @param array $crumbs An array, each item of which corresponds to a chunk of breadcrumbs. * This function appends the generated breadcrumbs to this array. * @return The modified $crumbs array. Note, this function always appends at * least one element to the $crumbs array. */ function generateBreadcrumbs($title, $crumbs) { global $verboseDebug; $parents = findParents($title); $parentsCount = count($parents); if ($parentsCount == 0 or $parents[$parentsCount-1][0] != $title->getPrefixedDBkey()) { // This happens when the page we're generating breadcrumbs for does not exist yet array_push($parents, array($title->getPrefixedDBkey(), "")); $parentsCount++; } for ($i = 0; $i < $parentsCount; $i++) { if ($i == 0) { $parent_portion = $parents[0][0]; } else { $grandparent_link = $parents[$i - 1][0]; $parent_portion = substr($parents[$i][0], strlen($grandparent_link) + 1); } $parent_name = $parents[$i][0]; if (empty($parents[$i][1])) { $parent_pretty = str_replace("_", " ", $parents[$i][0]); if ($i > 0) $parent_pretty = substr($parent_pretty, strlen($parents[$i - 1][0]) + 1); } else $parent_pretty = $parents[$i][1]; $parent_title = Title::newFromText($parent_name); $parent_link = $parent_title->getLocalURL(); wfDebug(" $parent_name / $parent_pretty -> $parent_link \n"); array_push($crumbs, array($parent_link, $parent_pretty)); } if ($verboseDebug) { ob_start(); var_dump($crumbs); $debug_content = ob_get_contents(); ob_end_clean(); wfDebug("generateBreadcrumbs: $debug_content"); } return $crumbs; } /** * Returns HTML markup for the breadcrumbs on this page. * @param string $input The contents of the tag on a wiki page. */ function renderBreadcrumbs($input) { global $wgTitle, $verboseDebug; // generate breadcrumbs for the page specified in $input $crumbs = array(); $input = trim($input); if ($input != "") { $prefixTitle = Title::newFromText($input); generateBreadcrumbs($prefixTitle, &$crumbs); } // append breadcrumbs for the current page generateBreadcrumbs($wgTitle, &$crumbs); if ($verboseDebug) { ob_start(); var_dump($crumbs); $debug_content = ob_get_contents(); ob_end_clean(); wfDebug("crumbs: $debug_content"); } // generate link to Main Page $mainTitle = Title::newFromText(wfMsgForContent('mainpage')); $mainHref = $mainTitle->getLocalURL(); global $dmgBreadcrumbsHomeName; if (isset($dmgBreadcrumbsHomeName)) $homeName = $dmgBreadcrumbsHomeName; else $homeName = wfMsg('mainpage'); $crumbsText = "$homeName "; // render $crumbs array as HTML $local_title = array_pop($crumbs); for ($i = 0; $i < count($crumbs); $i++) { $crumbsText .= "> {$crumbs[$i][1]} "; } $crumbsText .= "> $local_title[1]"; $output = ""; return $output; } ?>