Categories.php

From Organic Design wiki
Revision as of 12:24, 26 August 2005 by Nad (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<?

  1. XmlWiki Category Extension - 2005-08-25
  2. This data-transform replaces the current MediaWiki category parsing
  3. - It allows categories to be included in either the article content or its XML properties
  4. - Maintains rules-based auto-categorisation (see list below)
  1. Only apply this transform for the root article, not embeds

if ($title == $GLOBALS['xwArticleTitle']) {

  1. 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?";

  1. 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); }

  1. Title used in db-tables

$t = str_replace(' ', '_', $title);

  1. Auto-Category: Language

if ($language) $categories[] = strtoupper($language).' Scripts';

  1. 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; }

  1. 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; }

  1. 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')"); } } ?>