Difference between revisions of "Categories.php"

From Organic Design wiki
 
Line 3: Line 3:
 
# This data-transform replaces the current MediaWiki category parsing
 
# This data-transform replaces the current MediaWiki category parsing
 
# - It allows categories to be included in either the article content or its XML properties
 
# - It allows categories to be included in either the article content or its XML properties
# - Maintains rules-based auto-categorisation (see list below)
+
# - Maintains rules-based auto-categorisation
  
 
# Only apply this transform for the root article, not embeds
 
# Only apply this transform for the root article, not embeds
if ($title == $GLOBALS['xwArticleTitle']) {
+
$update = $GLOBALS[xwSave];
 +
$apply = true;
 +
# Don't apply if page is a category,
 +
# or if action isn't view or submit
 +
if (($action != 'view') && ($action != '') && !$update) $apply = false;
 +
if ($event != 'data') $apply = false;
 +
 
 +
# If a category page, add class attribute to table since the MediaWiki fellas didn't for some reason
 +
if (ereg('^Category:.+$', $title)) {
 +
# Add this transform to the view stack to adjust page after it's built
 +
if ($event == 'data') xwSetProperty($properties, 'xpath:/properties:view', $tTitle);
 +
else {
 +
# If already in view event now, add a CSS class to letter-headings
 +
$article = preg_replace("/<h3>([A-Z]( cont\\.)?<\\/h3>)/i", '<h3 class="category-heading">$1', $article);
 +
}
 +
# Just let wiki-parser handle category-links for category pages
 +
$apply = false;
 +
}
  
# Get stuff we'll be needing
+
if ($apply) {
 +
# Begin extracting category-link info - first get stuff we'll be needing
 
$sk =& $GLOBALS[wgParser]->mOptions->getSkin();
 
$sk =& $GLOBALS[wgParser]->mOptions->getSkin();
 
$db =& wfGetDB(DB_SLAVE);
 
$db =& wfGetDB(DB_SLAVE);
 
$cur = $db->tableName('cur');
 
$cur = $db->tableName('cur');
 
$cl = $db->tableName('categorylinks');
 
$cl = $db->tableName('categorylinks');
$update = $GLOBALS[xwSave];
+
$category_match = "\\[{2}\\s*category\\s*:\\s*(.+?)\\s*]]\\n?";
$category_match = "\\[{2}\\s*category\\s*:\\s*([^|]+?)\\s*]]\\n?";
+
$nt = Title::newFromText($title);
 +
$ns = $nt->getNamespace();
  
 
# Extract category information from article's content and properties
 
# Extract category information from article's content and properties
Line 23: Line 42:
 
xwGetProperty($article, 'language', $language);
 
xwGetProperty($article, 'language', $language);
 
if (!$language) $language = xwArticleType($title, $content);
 
if (!$language) $language = xwArticleType($title, $content);
 +
}
 +
elseif (eregi('^sys:(.+)$', $title, $match)) {
 +
$title = $match[1];
 +
$ns = NS_USER;
 +
$content = xwArticleContent("User:$title", false);
 +
preg_match_all("/$category_match/i", $content, $match);
 +
$categories = $match[1];
 +
foreach (xwGetListByTagname($article, 'groups') as $group)
 +
if (($group != $GLOBALS[xwUserName]) && ($group != 'anyone'))
 +
$categories[] = "$group Group Members";
 +
$language = '';
 
}
 
}
 
else {
 
else {
Line 30: Line 60:
 
if (!$language) $article = preg_replace("/$category_match/ie", '($categories[]="$1")?"":""', $article);
 
if (!$language) $article = preg_replace("/$category_match/ie", '($categories[]="$1")?"":""', $article);
 
}
 
}
 +
 +
# Only do the rest if this is the top level article
 +
# - the embeds still had to have their cat-links removed
 +
if ($title == $GLOBALS[xwArticleTitle]) {
  
 
# Title used in db-tables
 
# Title used in db-tables
$t = str_replace(' ', '_', $title);
+
$t = addslashes($nt->getDBkey());
  
 
# Auto-Category: Language
 
# Auto-Category: Language
if ($language) $categories[] = strtoupper($language).' Scripts';
+
if ($language) $categories[] = strtoupper($language);
  
 
# Get the db-key of this article
 
# Get the db-key of this article
 
if ($update) {
 
if ($update) {
$result = $db->query("SELECT cur_id FROM $cur WHERE cur_title='$t' LIMIT 1");
+
$result = $db->query("SELECT cur_id FROM $cur WHERE cur_title='$t' AND cur_namespace='$ns' LIMIT 1");
 
if ($row = mysql_fetch_assoc($result)) $id = $row['cur_id'];
 
if ($row = mysql_fetch_assoc($result)) $id = $row['cur_id'];
 
else $id = $update = false;
 
else $id = $update = false;
Line 50: Line 84:
 
$GLOBALS[wgUseCategoryMagic] = false;
 
$GLOBALS[wgUseCategoryMagic] = false;
 
}
 
}
 +
 +
# Remove duplicates and sort categories
 +
$tmp = array();
 +
foreach ($categories as $cat) {
 +
list($cat, $sortkey) = explode('|', ucfirst($cat));
 +
if (!$tmp[$cat] = $sortkey) $tmp[$cat] = $t;
 +
}
 +
ksort($tmp);
  
 
# Loop through categories and process each
 
# Loop through categories and process each
foreach ($categories as $cat) {
+
foreach ($tmp as $cat => $sortkey) {
$cat = ucfirst($cat);
 
 
# Add to wiki-output if not properties
 
# Add to wiki-output if not properties
 
$nt = Title::makeTitle(NS_CATEGORY, $cat);
 
$nt = Title::makeTitle(NS_CATEGORY, $cat);
 
$GLOBALS[wgParser]->mOutput->addCategoryLink($sk->makeLinkObj($nt, $cat));
 
$GLOBALS[wgParser]->mOutput->addCategoryLink($sk->makeLinkObj($nt, $cat));
 
# Update db if saving and db-key known
 
# Update db if saving and db-key known
$cat = str_replace(' ', '_', $cat);
+
$cat = addslashes(str_replace(' ', '_', $cat));
if ($update) $db->query("INSERT INTO $cl (cl_from,cl_to,cl_sortkey) VALUES('$id','$cat','$t')");
+
if ($update) $db->query("INSERT INTO $cl (cl_from,cl_to,cl_sortkey) VALUES('$id','$cat','$sortkey')");
 
}
 
}
 +
}
 
}
 
}
 
?>
 
?>

Revision as of 20:46, 9 September 2005

<?

  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
  1. Only apply this transform for the root article, not embeds

$update = $GLOBALS[xwSave]; $apply = true;

  1. Don't apply if page is a category,
  2. or if action isn't view or submit

if (($action != 'view') && ($action != ) && !$update) $apply = false; if ($event != 'data') $apply = false;

  1. If a category page, add class attribute to table since the MediaWiki fellas didn't for some reason

if (ereg('^Category:.+$', $title)) { # Add this transform to the view stack to adjust page after it's built if ($event == 'data') xwSetProperty($properties, 'xpath:/properties:view', $tTitle); else { # If already in view event now, add a CSS class to letter-headings

$article = preg_replace("/

([A-Z]( cont\\.)?<\\/h3>)/i", '

$1', $article); } # Just let wiki-parser handle category-links for category pages $apply = false; } if ($apply) {
  1. Begin extracting category-link info - first get stuff we'll be needing
$sk =& $GLOBALS[wgParser]->mOptions->getSkin(); $db =& wfGetDB(DB_SLAVE); $cur = $db->tableName('cur'); $cl = $db->tableName('categorylinks'); $category_match = "\\[{2}\\s*category\\s*:\\s*(.+?)\\s*]]\\n?"; $nt = Title::newFromText($title); $ns = $nt->getNamespace();
  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); } elseif (eregi('^sys:(.+)$', $title, $match)) { $title = $match[1]; $ns = NS_USER; $content = xwArticleContent("User:$title", false); preg_match_all("/$category_match/i", $content, $match); $categories = $match[1]; foreach (xwGetListByTagname($article, 'groups') as $group) if (($group != $GLOBALS[xwUserName]) && ($group != 'anyone')) $categories[] = "$group Group Members"; $language = ; } 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. Only do the rest if this is the top level article
  2. - the embeds still had to have their cat-links removed
if ($title == $GLOBALS[xwArticleTitle]) {
  1. Title used in db-tables
$t = addslashes($nt->getDBkey());
  1. Auto-Category: Language
if ($language) $categories[] = strtoupper($language);
  1. Get the db-key of this article
if ($update) { $result = $db->query("SELECT cur_id FROM $cur WHERE cur_title='$t' AND cur_namespace='$ns' 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. Remove duplicates and sort categories
$tmp = array(); foreach ($categories as $cat) { list($cat, $sortkey) = explode('|', ucfirst($cat)); if (!$tmp[$cat] = $sortkey) $tmp[$cat] = $t; } ksort($tmp);
  1. Loop through categories and process each
foreach ($tmp as $cat => $sortkey) { # 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 = addslashes(str_replace(' ', '_', $cat)); if ($update) $db->query("INSERT INTO $cl (cl_from,cl_to,cl_sortkey) VALUES('$id','$cat','$sortkey')"); } } } ?>