Last modified: 2009-03-13 20:01:38 UTC
If Categories are listed in an @optional section (typically the last section), they won't be assigned to the article. This patch strips the categories and applies to the new page, before applying layout directives. @@ -252,11 +252,15 @@ // fetch the layout object $layout_title = Title::newFromURL ( "Layout:" . $layout_slug ); $layout_article = new Article ( $layout_title ); - + $layout_cats = ''; + /* if the layout article exists, pre-fill the textarea with its * wiki text. if it doesn't exist, do nothing (no error) */ if ( ( $layout_text = $layout_article->fetchContent() ) !== false ) { + $layout_text = UW_Layouts_stripCats($layout_text, $layout_cats); + $text .= $layout_cats; + /* break the layout text into sections by splitting * at header level =one= or ==two==, and iterate */ $nodes = preg_split ( "/^(==?[^=].*)$/mi", $layout_text, - 1, PREG_SPLIT_DELIM_CAPTURE ); @@ -282,3 +286,39 @@ return true; } + + +function UW_Layouts_stripCats($texttostrip,&$catsintext){ + global $wgContLang, $wgOut; + + # Get localised namespace string: + $m_catString = strtolower( $wgContLang->getNsText( NS_CATEGORY ) ); + # The regular expression to find the category links: + $m_pattern = "\[\[({$m_catString}|category):(.*?)\]\]"; + + $m_replace = "$2"; + # The container to store the processed text: + $m_cleanText = ''; + + # Check linewise for category links: + foreach( explode( "\n", $texttostrip ) as $m_textLine ) { + # Filter line through pattern and store the result: + $m_cleanText .= trim( preg_replace( "/{$m_pattern}/i", "", $m_textLine ) ) . "\n"; + + # Check if we have found a category, else proceed with next line: + if( preg_match_all( "/{$m_pattern}/i", $m_textLine,$catsintext2,PREG_SET_ORDER) ){ + foreach( $catsintext2 as $local_cat => $m_prefix ) { + //Set first letter to upper case to match MediaWiki standard + $strFirstLetter = substr($m_prefix[2], 0,1); + strtoupper($strFirstLetter); + $newString = strtoupper($strFirstLetter) . substr($m_prefix[2], 1); + $catsintext .= "[[" . $m_catString . ":" . $newString . "]]\n"; + } + # Get the category link from the original text and store it in our list: + preg_replace( "/.*{$m_pattern}/i", $m_replace, $m_textLine,-1,$intNumber ); + } + + } + + return $m_cleanText; +}
Thank you for the patch, but is there a reason why categories in optional sections should be applied if that section is not included - this behaviour could even be seen as a feature (optional categories tied to sections), and any categories that you want to be applied no-matter-what sections are included can simply be put at the top of the page. I would lean towards closing this as INVALID/WONTFIX, but I'm sure you can provide a good reason for doing it the way you have proposed!
In combination with the SelectCategoryTagCloud extension, this is a problem. SelectCategoryTagCloud always places the Category tags in the last section, so it's not possible to make the last section optional. And actually, I think categories in @optional sections will _never_ be applied .. later adding that section in GenericEditPage will not add the section-text, only the section-title. Still I could see how this might be confusing. Perhaps make this an optional behavior, which is turned off by default?
That is true, if there aren't any further comments by tomorrow I will apply the patch at the weekend.
Patch applied in r48378, in future please can you supply patches as an attachment including the full header - so that they can be automatically applied. Thank you.