Difference between revisions of "Categories.php"

From Organic Design wiki
(Add document.css to transform exceptions)
m
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<?
+
{{legacy}}
# XmlWiki Category Extension - 2005-08-25
+
<php><?
# This data-transform replaces the current MediaWiki category parsing
+
# XmlWiki Category Extension - 2005-08-25 - see discussion page
# - It allows categories to be included in either the article content or its XML properties
+
global $xwIsProperties,$xwSection,$xwSave,$xwUserName;
# - Maintains rules-based auto-categorisation
+
$update = ($xwSave && ($xwSection<1));
global $xwIsProperties;
 
$update = $GLOBALS['xwSave'];
 
 
$apply = true;
 
$apply = true;
  
Line 13: Line 11:
 
# If a category page, add class attribute to table since the MediaWiki fellas didn't for some reason
 
# If a category page, add class attribute to table since the MediaWiki fellas didn't for some reason
 
if ( ereg( '^Category:.+$', $title ) ) {
 
if ( ereg( '^Category:.+$', $title ) ) {
# Add this transform to the view stack to adjust page after it's built
+
# If event is view, adjust rendered output, else add this to view event
if ( $event == 'data' ) xwSetProperty( $properties, 'xpath:/properties:view', $tTitle );
+
if ( $event == 'view' ) {
else $article = preg_replace("/<h3>([A-Z]( cont\\.)?<\\/h3>)/i", '<h3 class="category-heading">$1', $article);
+
# Add CSS class to headings (see, not just me who hard-wires styles!)
 +
$article = preg_replace( '/<h3>(.( cont\\.)?<\\/h3>)/i', '<h3 class="category-heading">$1', $article );
 +
}
 +
else xwSetProperty( $properties, 'xpath:/properties:view', $tTitle );
 
$apply = false; # Just let wiki-parser handle category-links for category pages
 
$apply = false; # Just let wiki-parser handle category-links for category pages
 
}
 
}
  
if ( $apply ) {
+
if ($apply) {
  
 
# Begin extracting category-link info - first get stuff we'll be needing
 
# Begin extracting category-link info - first get stuff we'll be needing
$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');
 
 
$category_match = "\\[{2}\\s*category\\s*:\\s*(.+?)\\s*]]\\n?";
 
$category_match = "\\[{2}\\s*category\\s*:\\s*(.+?)\\s*]]\\n?";
 
$pt = $title;
 
$pt = $title;
Line 37: Line 37:
 
if ( !$language ) $language = xwArticleType( $title, $content );
 
if ( !$language ) $language = xwArticleType( $title, $content );
 
}
 
}
elseif ( eregi('^sys:(.+)$', $title, $match) ) {
+
elseif ( eregi( '^sys:(.+)$', $title, $match ) ) {
 
$title = $match[1];
 
$title = $match[1];
 
$ns = NS_USER;
 
$ns = NS_USER;
Line 43: Line 43:
 
preg_match_all( "/$category_match/i", $content, $match );
 
preg_match_all( "/$category_match/i", $content, $match );
 
$categories = $match[1];
 
$categories = $match[1];
foreach ( xwGetListByTagname($article, 'groups') as $group )
+
# Groups auto-categorisation
if ( ($group != $GLOBALS['xwUserName']) && ($group != 'anyone') )
+
foreach ( xwGetListByTagname( $article, 'groups' ) as $group )
$categories[] = "$group Group Members";
+
$categories[] = "Group:$group";
 
$language = '';
 
$language = '';
 
}
 
}
 
else {
 
else {
$categories = xwGetListByTagname( $properties, 'category' );
+
$categories = xwGetListByTagname($properties, 'category');
 
# Must remove category-links from article so wiki-parser doesn't process them
 
# Must remove category-links from article so wiki-parser doesn't process them
xwGetProperty( $properties, 'language', $language );
+
xwGetProperty($properties, 'language', $language);
if ( !$language ) $article = preg_replace( "/$category_match/ie", '($categories[]="$1")?"":""', $article );
+
if (!$language) {
 +
$article = preg_replace("/$category_match/ie", '($categories[]="$1")?"":""', $article);
 +
if (ereg('{'.'{stub}}',$article)) $categories[] = 'Stubs';
 +
}
 
}
 
}
  
 
# Only do the rest if this is the top level article
 
# Only do the rest if this is the top level article
 
# - the embeds still have to have their cat-links removed
 
# - the embeds still have to have their cat-links removed
if ( $pt == $GLOBALS['xwArticleTitle'] ) {
+
if ($pt == $GLOBALS['xwArticleTitle']) {
  
$nt = Title::newFromText( $title );
+
$nt = Title::newFromText($title);
 
$ns = $nt->getNamespace();
 
$ns = $nt->getNamespace();
$t = addslashes( $nt->getDBkey() ); # Title used in db-tables
+
$t = addslashes($nt->getDBkey()); # Title used in db-tables
$ul = strtoupper( $language );
+
$ul = strtoupper($language);
 +
$content = xwArticleContent($title);
  
 
# Auto-Category: Language
 
# Auto-Category: Language
 
if ( $language ) $categories[] = $ul;
 
if ( $language ) $categories[] = $ul;
 +
 +
if (ereg('^(.+/)?[0-9]+ ([A-Z][a-z]+) [0-9]{4}$',$title,$m)) $categories[] = $m[1].'Date|'.$m[2];
  
 
# Auto-Category: Transforms
 
# Auto-Category: Transforms
Line 78: Line 84:
 
'Document.css'
 
'Document.css'
 
);
 
);
if ( $ul == 'CSS' or $ul == 'XSLT' ) $categories[] = 'Transforms';
+
if ($ul == 'CSS' or $ul == 'XSLT') $categories[] = 'Transforms';
elseif ( in_array( $title, $transformExceptions ) ) 0;
+
elseif (in_array($title, $transformExceptions)) 0;
elseif ( $ul == 'PHP' and count( preg_grep( '/^(admin)|(dev)$/i', xwGetListByTagname( xwArticleProperties( $title ), 'write' ) ) ) ) $categories[] = 'Transforms';
+
elseif ($ul == 'PHP' and count(preg_grep('/^(admin)|(dev)$/i',xwGetListByTagname(xwArticleProperties($title),'write'))))
elseif ( $ul == 'XML' and preg_match( '/<!DOCTYPE[^>]+?xmlwiki-properties.dtd"\\s*>/', xwArticleContent( $title ) ) )
 
 
$categories[] = 'Transforms';
 
$categories[] = 'Transforms';
 +
elseif ($ul == 'XML' and preg_match('/<!DOCTYPE[^>]+?xmlwiki-properties.dtd"\\s*>/',$content)) $categories[] = 'Transforms';
 +
if (preg_match('/<div\\s+class=["\']?tree-view["\']?.*?\\s*>/',$content)) $categories[] = 'Articles containing trees';
  
 
# 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' AND cur_namespace='$ns' LIMIT 1" );
+
$id = Title::makeTitle($ns,$t);
if ( $row = mysql_fetch_assoc($result) ) $id = $row['cur_id'];
+
$id = $update = $id->getArticleID();
else $id = $update = false;
 
 
}
 
}
  
 
# Delete all current category links from this title
 
# Delete all current category links from this title
if ( $update ) {
+
if ($update) {
 
$db->query( "DELETE FROM $cl WHERE cl_from='$id'" );
 
$db->query( "DELETE FROM $cl WHERE cl_from='$id'" );
 
# Stop parser from clearing them again after we repopulate it
 
# Stop parser from clearing them again after we repopulate it
Line 100: Line 106:
 
# Remove duplicates and sort categories
 
# Remove duplicates and sort categories
 
$tmp = array();
 
$tmp = array();
foreach ( $categories as $cat ) {
+
foreach ($categories as $cat) {
list( $cat, $sortkey ) = explode( '|', ucfirst($cat) );
+
list($cat,$sortkey) = explode('|',ucfirst($cat));
if ( !$tmp[$cat] = $sortkey ) $tmp[$cat] = $t;
+
if (!$tmp[$cat] = $sortkey) $tmp[$cat] = $t;
 
}
 
}
 +
unset($categories[$title]);
 
ksort($tmp);
 
ksort($tmp);
  
 
# Loop through categories and process each
 
# Loop through categories and process each
foreach ( $tmp as $cat => $sortkey ) {
+
if (is_object($GLOBALS['wgParser']->mOptions)) {
# Add to wiki-output if not properties
+
$sk =& $GLOBALS['wgParser']->mOptions->getSkin();
$nt = Title::makeTitle( NS_CATEGORY, $cat );
+
foreach ( $tmp as $cat => $sortkey ) {
$GLOBALS[wgParser]->mOutput->addCategoryLink( $sk->makeLinkObj($nt, $cat) );
+
 
# Update db if saving and db-key known
+
# Add to wiki-output
$cat = addslashes( str_replace(' ', '_', $cat) );
+
$nt = Title::makeTitle( NS_CATEGORY, $cat );
if ($update) $db->query( "INSERT INTO $cl (cl_from,cl_to,cl_sortkey) VALUES('$id','$cat','$sortkey')" );
+
$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')" );
 +
}
 
}
 
}
 
}
 
}
 
}
 
}
?>
+
 
 +
?></php>

Latest revision as of 09:37, 27 April 2007

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.

<php><?

  1. XmlWiki Category Extension - 2005-08-25 - see discussion page

global $xwIsProperties,$xwSection,$xwSave,$xwUserName; $update = ($xwSave && ($xwSection<1)); $apply = true;

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

if ( ($action != 'view') && ($action != ) && !$update ) $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 ) ) { # If event is view, adjust rendered output, else add this to view event if ( $event == 'view' ) { # Add CSS class to headings (see, not just me who hard-wires styles!)

$article = preg_replace( '/

(.( cont\\.)?<\\/h3>)/i', '

$1', $article ); } else xwSetProperty( $properties, 'xpath:/properties:view', $tTitle ); $apply = false; # Just let wiki-parser handle category-links for category pages } if ($apply) { # Begin extracting category-link info - first get stuff we'll be needing $db =& wfGetDB( DB_SLAVE ); $cur = $db->tableName( 'cur' ); $cl = $db->tableName( 'categorylinks' ); $category_match = "\\[{2}\\s*category\\s*:\\s*(.+?)\\s*]]\\n?"; $pt = $title; # 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]; # Groups auto-categorisation foreach ( xwGetListByTagname( $article, 'groups' ) as $group ) $categories[] = "Group:$group"; $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); if (ereg('{'.'{stub}}',$article)) $categories[] = 'Stubs'; } } # Only do the rest if this is the top level article # - the embeds still have to have their cat-links removed if ($pt == $GLOBALS['xwArticleTitle']) { $nt = Title::newFromText($title); $ns = $nt->getNamespace(); $t = addslashes($nt->getDBkey()); # Title used in db-tables $ul = strtoupper($language); $content = xwArticleContent($title); # Auto-Category: Language if ( $language ) $categories[] = $ul; if (ereg('^(.+/)?[0-9]+ ([A-Z][a-z]+) [0-9]{4}$',$title,$m)) $categories[] = $m[1].'Date|'.$m[2]; # Auto-Category: Transforms # - CSS and XSLT are included unconditionally # - PHP included if writable only by dev or admin # - XML included if DocType DTD is xmlwiki-properties (but not Xml:name) $transformExceptions = array( 'Xmlwiki.php', 'Default-properties.xml', 'Document.php', 'Geshi.php', 'Default-skin.php', 'LocalSettings.php', 'Transform-changes.php', 'Transform-all-pages-list.php', 'Categories.php', 'Properties.php', 'Calendar.php', 'Document.css' ); if ($ul == 'CSS' or $ul == 'XSLT') $categories[] = 'Transforms'; elseif (in_array($title, $transformExceptions)) 0; elseif ($ul == 'PHP' and count(preg_grep('/^(admin)|(dev)$/i',xwGetListByTagname(xwArticleProperties($title),'write')))) $categories[] = 'Transforms'; elseif ($ul == 'XML' and preg_match('/<!DOCTYPE[^>]+?xmlwiki-properties.dtd"\\s*>/',$content)) $categories[] = 'Transforms'; if (preg_match('/<div\\s+class=["\']?tree-view["\']?.*?\\s*>/',$content)) $categories[] = 'Articles containing trees'; # Get the db-key of this article if ($update) { $id = Title::makeTitle($ns,$t); $id = $update = $id->getArticleID(); } # 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; } # Remove duplicates and sort categories $tmp = array(); foreach ($categories as $cat) { list($cat,$sortkey) = explode('|',ucfirst($cat)); if (!$tmp[$cat] = $sortkey) $tmp[$cat] = $t; } unset($categories[$title]); ksort($tmp); # Loop through categories and process each if (is_object($GLOBALS['wgParser']->mOptions)) { $sk =& $GLOBALS['wgParser']->mOptions->getSkin(); foreach ( $tmp as $cat => $sortkey ) { # Add to wiki-output $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')" ); } } } } ?></php>