Index: trunk/extensions/PageInCat/PageInCat.i18n.php |
— | — | @@ -10,10 +10,15 @@ |
11 | 11 | |
12 | 12 | $messages['en'] = array( |
13 | 13 | 'pageincat-desc' => 'Adds a parser function <code><nowiki>{{#incat:...}}</nowiki></code> to determine if the current page is in a specified category', |
| 14 | + 'pageincat-wrong-warn' => '\'\'\'Warning:\'\'\' The {{PLURAL:$2|category $1 was|categories $1 were}} detected incorrectly by <code><nowiki>{{#incat:...}}</nowiki></code>, and as a result this preview may be incorrect. The saved version of this page should be displayed in the correct manner.', |
14 | 15 | ); |
15 | 16 | |
16 | 17 | $messages['qqq'] = array( |
17 | 18 | 'pagesincat-desc' => '{{desc}}', |
| 19 | + 'pageincat-wrong-warn' => 'Warning displayed during preview when editing a page if #incat parser function acted incorrectly (Acting incorrectly means acting as if page was not in category, but page actually is) . This can happen during preview, since the categories from the last saved revision are used instead of the categories specified in the page text. Once page is saved, the correct categories should be used. This error can also be caused by conditional category inclusion (<code><nowiki>{{#ifpageincat:Foo||[[category:Foo]]}}</nowiki></code> |
| 20 | + |
| 21 | +*$1 is the list of categories (in a localized comma seperated list with the last two items separated by {{msg-mw|and}}. The individual category names will be italicized). |
| 22 | +*$2 is how many categories', |
18 | 23 | ); |
19 | 24 | |
20 | 25 | /** German (Deutsch) |
Index: trunk/extensions/PageInCat/PageInCat.php |
— | — | @@ -31,7 +31,7 @@ |
32 | 32 | 'path' => __FILE__, |
33 | 33 | 'name' => 'PageInCat', |
34 | 34 | 'descriptionmsg' => 'pageincat-desc', |
35 | | - 'version' => 1, |
| 35 | + 'version' => 1.1, |
36 | 36 | 'url' => 'https://mediawiki.org/wiki/Extension:PageInCat', |
37 | 37 | 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]', |
38 | 38 | ); |
— | — | @@ -44,4 +44,5 @@ |
45 | 45 | $wgAutoloadClasses['PageInCat'] = $dir . 'PageInCat_body.php'; |
46 | 46 | |
47 | 47 | $wgHooks['ParserFirstCallInit'][] = 'PageInCat::register'; |
48 | | -$wgHooks['ParserClearState'][] = 'PageInCat::clearState'; |
| 48 | +$wgHooks['ParserClearState'][] = 'PageInCat::onClearState'; |
| 49 | +$wgHooks['ParserAfterTidy'][] = 'PageInCat::onParserAfterTidy'; |
Index: trunk/extensions/PageInCat/PageInCat_body.php |
— | — | @@ -44,7 +44,13 @@ |
45 | 45 | $catDBkey = $catTitle->getDBkey(); |
46 | 46 | |
47 | 47 | $pageId = $page->getArticleId(); |
48 | | - if ( !$pageId ) return false; // page hasn't yet been saved (preview) |
| 48 | + if ( !$pageId ) { |
| 49 | + // page hasn't yet been saved (preview) |
| 50 | + // add to the cache list so the other hook |
| 51 | + // will warn about incorrect value. |
| 52 | + $parser->pageInCat_cache[$catDBkey] = false; |
| 53 | + return false; |
| 54 | + } |
49 | 55 | |
50 | 56 | if ( !isset( $parser->pageInCat_cache ) ) { |
51 | 57 | $parser->pageInCat_cache = array(); |
— | — | @@ -104,8 +110,68 @@ |
105 | 111 | * that it could). |
106 | 112 | * @param $parser Parser |
107 | 113 | */ |
108 | | - public static function clearState( Parser $parser ) { |
| 114 | + public static function onClearState( Parser $parser ) { |
109 | 115 | $parser->pageInCat_cache = array(); |
110 | 116 | return true; |
111 | 117 | } |
| 118 | + |
| 119 | + /** |
| 120 | + * ParserAfterTidy hook to check for category/#incat mismatches. |
| 121 | + * |
| 122 | + * Used to check if the actual categories match the expected categories |
| 123 | + * and display a warning if they don't. Using ParserAfterTidy since it |
| 124 | + * runs so late in parse process. |
| 125 | + * |
| 126 | + * @param $parser Parser |
| 127 | + * @param $text String the resultant html (unused) |
| 128 | + * @return boolean true |
| 129 | + */ |
| 130 | + public static function onParserAfterTidy( Parser $parser, $text ) { |
| 131 | + global $wgLang; |
| 132 | + if ( !isset( $parser->pageInCat_cache ) |
| 133 | + || !$parser->getOptions()->getIsPreview() |
| 134 | + ) { |
| 135 | + # page in cat extension not even used |
| 136 | + # or this is not a preview. |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + $actualCategories = $parser->getOutput()->getCategories(); |
| 141 | + $wrongCategories = array(); |
| 142 | + |
| 143 | + foreach( $parser->pageInCat_cache as $catName => $catIncluded ) { |
| 144 | + # A little hacky, but I want the cat names italicized. |
| 145 | + $catNameDisplay = "''" . str_replace( '_', ' ', $catName ) . "''"; |
| 146 | + if ( $catIncluded ) { |
| 147 | + if ( isset( $actualCategories[$catName] ) ) { |
| 148 | + # Cat is included, and actually should be |
| 149 | + # So do nothing and continue |
| 150 | + } else { |
| 151 | + $wrongCategories[$catNameDisplay] = true; |
| 152 | + } |
| 153 | + } else { # Should not be included |
| 154 | + if ( isset( $actualCategories[$catName] ) ) { |
| 155 | + $wrongCategories[$catNameDisplay] = true; |
| 156 | + } else { |
| 157 | + # not included, like it should be. |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + # Since this is only on preview, user lang is ok. |
| 163 | + $catList = array_keys( $wrongCategories ); |
| 164 | + if ( count( $catList ) !== 0 ) { |
| 165 | + # We have at least 1 category that was treated |
| 166 | + # incorrectly by {{#incat: |
| 167 | + $msg = wfMessage( 'pageincat-wrong-warn' ) |
| 168 | + ->params( $wgLang->listToText( $catList ) ) |
| 169 | + ->numParams( count( $catList ) ) |
| 170 | + ->text(); |
| 171 | + |
| 172 | + $parser->getOutput()->addWarning( $msg ); |
| 173 | + } |
| 174 | + |
| 175 | + return true; |
| 176 | + } |
| 177 | + |
112 | 178 | } |