r107317 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107316‎ | r107317 | r107318 >
Date:09:28, 26 December 2011
Author:bawolff
Status:deferred
Tags:
Comment:
Make this extension issue a warning if the preview is likely to be incorrect
Modified paths:
  • /trunk/extensions/PageInCat/PageInCat.i18n.php (modified) (history)
  • /trunk/extensions/PageInCat/PageInCat.php (modified) (history)
  • /trunk/extensions/PageInCat/PageInCat_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PageInCat/PageInCat.i18n.php
@@ -10,10 +10,15 @@
1111
1212 $messages['en'] = array(
1313 '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.',
1415 );
1516
1617 $messages['qqq'] = array(
1718 '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',
1823 );
1924
2025 /** German (Deutsch)
Index: trunk/extensions/PageInCat/PageInCat.php
@@ -31,7 +31,7 @@
3232 'path' => __FILE__,
3333 'name' => 'PageInCat',
3434 'descriptionmsg' => 'pageincat-desc',
35 - 'version' => 1,
 35+ 'version' => 1.1,
3636 'url' => 'https://mediawiki.org/wiki/Extension:PageInCat',
3737 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]',
3838 );
@@ -44,4 +44,5 @@
4545 $wgAutoloadClasses['PageInCat'] = $dir . 'PageInCat_body.php';
4646
4747 $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 @@
4545 $catDBkey = $catTitle->getDBkey();
4646
4747 $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+ }
4955
5056 if ( !isset( $parser->pageInCat_cache ) ) {
5157 $parser->pageInCat_cache = array();
@@ -104,8 +110,68 @@
105111 * that it could).
106112 * @param $parser Parser
107113 */
108 - public static function clearState( Parser $parser ) {
 114+ public static function onClearState( Parser $parser ) {
109115 $parser->pageInCat_cache = array();
110116 return true;
111117 }
 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+
112178 }

Status & tagging log