|
|
Line 9: |
Line 9: |
| # Don't apply if page is a category, or if action isn't view or submit | | # Don't apply if page is a category, or if action isn't view or submit |
| if ( ($action != 'view') && ($action != '') && !$update ) $apply = false; | | if ( ($action != 'view') && ($action != '') && !$update ) $apply = false; |
− |
| |
− | # Don't apply if this is an embed (data event has been called before)
| |
− | if ( $event == 'data' ) {
| |
− | if ( $GLOBALS[$tTitle] ) $apply = false; else $GLOBALS[$tTitle] = true;
| |
− | } else $apply = false;
| |
| | | |
| # 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 |
Line 87: |
Line 82: |
| | | |
| # 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 96: |
Line 91: |
| $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; |
| } | | } |
Line 104: |
Line 99: |
| foreach ( $tmp as $cat => $sortkey ) { | | foreach ( $tmp as $cat => $sortkey ) { |
| # 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 |
<?
- 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
$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 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;
}
xwMessage( "cat-apply=$apply, title=$title", 'red' );
if ( $apply ) {
# 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?";
$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];
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 );
}
# Only do the rest if this is the top level article
# - the embeds still had 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 );
# Auto-Category: Language
if ( $language ) $categories[] = $ul;
# Auto-Category: Transforms
if ( $ul == 'CSS' or $ul == 'XML' or $ul == 'XSLT' ) $categories[] = 'Transforms';
elseif ( $ul == 'PHP' and count( preg_grep( '/^(admin)|(dev)$/i', xwGetListByTagname( xwArticleProperties( $title ), 'write' ) ) ) )
$categories[] = 'Transforms';
# 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;
}
# 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;
}
ksort($tmp);
# 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')" );
}
}
}
?>