Categories.php
<?
- XmlWiki Category Extension - 2005-08-25
- This data-transform replaces the current MediaWiki category parsing
- - It allows categories to be included in either the article content or its XML properties
- - Maintains rules-based auto-categorisation (see list below)
- Only apply this transform for the root article, not embeds
if ($title == $GLOBALS['xwArticleTitle']) {
- Get stuff we'll be needing
$sk =& $GLOBALS[wgParser]->mOptions->getSkin(); $db =& wfGetDB(DB_SLAVE); $cur = $db->tableName('cur'); $cl = $db->tableName('categorylinks'); $update = $GLOBALS[xwSave]; $category_match = "\\[{2}\\s*category\\s*:\\s*([^|]+?)\\s*]]\\n?";
- Extract category information from article's content and properties
if (eregi('^xml:(.+)$', $title, $match)) { $content = xwArticleContent($title = $match[1], false); preg_match_all("/$category_match/i", $content, $match); $categories = array_merge(xwGetListByTagname($article, 'category'), $match[1]); xwGetProperty($article, 'language', $language); if (!$language) $language = xwArticleType($title, $content); } else { $categories = xwGetListByTagname($properties, 'category'); # Must remove category-links from article so wiki-parser doesn't process them xwGetProperty($properties, 'language', $language); if (!$language) $article = preg_replace("/$category_match/ie", '($categories[]="$1")?"":""', $article); }
- Title used in db-tables
$t = str_replace(' ', '_', $title);
- Auto-Category: Language
if ($language) $categories[] = strtoupper($language).' Scripts';
- Get the db-key of this article
if ($update) { $result = $db->query("SELECT cur_id FROM $cur WHERE cur_title='$t' LIMIT 1"); if ($row = mysql_fetch_assoc($result)) $id = $row['cur_id']; else $id = $update = false; }
- Delete all current category links from this title
if ($update) { $db->query("DELETE FROM $cl WHERE cl_from='$id'"); # Stop parser from clearing them again after we repopulate it $GLOBALS[wgUseCategoryMagic] = false; }
- Loop through categories and process each
foreach ($categories as $cat) { $cat = ucfirst($cat); # Add to wiki-output if not properties $nt = Title::makeTitle(NS_CATEGORY, $cat); $GLOBALS[wgParser]->mOutput->addCategoryLink($sk->makeLinkObj($nt, $cat)); # Update db if saving and db-key known $cat = str_replace(' ', '_', $cat); if ($update) $db->query("INSERT INTO $cl (cl_from,cl_to,cl_sortkey) VALUES('$id','$cat','$t')"); } } ?>