r96863 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96862‎ | r96863 | r96864 >
Date:16:45, 12 September 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
RL2: Move category title message logic to GadgetRepo::getCategoryTitle(). This BREAKS using wikitext in category titles (->plain() is used throughout), which was already broken in some places, wasn't documented that I know of. Besides, enwiki didn't even use it :)
Modified paths:
  • /branches/RL2/extensions/Gadgets/GadgetHooks.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/SpecialGadgetManager.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/api/ApiQueryGadgetCategories.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/GadgetRepo.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/GadgetHooks.php
@@ -219,7 +219,7 @@
220220 $options = array(); // array( desc1 => gadget1, category1 => array( desc2 => gadget2 ) )
221221 foreach ( $categories as $category => $gadgets ) {
222222 if ( $category !== '' ) {
223 - $categoryMsg = wfMsgExt( "gadgetcategory-$category", 'parseinline' );
 223+ $categoryMsg = htmlspecialchars( GadgetRepo::getCategoryTitle( $category ) );
224224 $options[$categoryMsg] = $gadgets;
225225 } else {
226226 $options += $gadgets;
Index: branches/RL2/extensions/Gadgets/backend/GadgetRepo.php
@@ -12,6 +12,8 @@
1313 // TODO: Common config stuff for all repos?
1414 }
1515
 16+ /**** Abstract functions ****/
 17+
1618 /**
1719 * Get the name of the ResourceLoader source of the modules
1820 * returned by this repository.
@@ -74,5 +76,29 @@
7577 */
7678 abstract public function deleteGadget( $id );
7779
78 - // TODO: cache purging
 80+ /**** Public functions ****/
 81+
 82+ /**
 83+ * Get the localized title for a given category in a given language.
 84+ *
 85+ * The "gadgetcategory-$category" message is used, if it exists.
 86+ * If it doesn't exist, ucfirst( $category ) is returned.
 87+ *
 88+ * @param $category string Category ID
 89+ * @param $lang string Language code. If null, $wgLang is used
 90+ * @return string Localized category title
 91+ */
 92+ public static function getCategoryTitle( $category, $lang = null ) {
 93+ $msg = wfMessage( "gadgetcategory-$category" );
 94+ if ( $lang !== null ) {
 95+ $msg = $msg->inLanguage( $lang );
 96+ }
 97+ if ( !$msg->exists() ) {
 98+ global $wgLang;
 99+ $langObj = $lang === null ? $wgLang : Language::factory( $lang );
 100+ return $lang->ucfirst( $category );
 101+ }
 102+ return $msg->plain();
 103+ }
 104+
79105 }
Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgetCategories.php
@@ -60,18 +60,10 @@
6161 $row['name'] = $category;
6262 }
6363 if ( isset( $this->props['title'] ) ) {
64 - // TODO: Put all this logic in the repo class
65 - $message = $category === '' ? 'gadgetmanager-uncategorized' : "gadgetcategory-$category";
66 - $message = wfMessage( $message );
67 - if ( $this->language !== null ) {
68 - $message = $message->inLanguage( $this->language );
69 - }
70 - if ( !$message->exists() ) {
71 - global $wgLang;
72 - $lang = $this->language === null ? $wgLang : Language::factory( $this->language );
73 - $row['title'] = $lang->ucfirst( $category );
 64+ if ( $category === '' ) {
 65+ $row['title'] = wfMessage( 'gadgetmanager-uncategorized' )->plain();
7466 } else {
75 - $row['title'] = $message->plain();
 67+ $row['title'] = GadgetRepo::getCategoryTitle( $category, $this->language );
7668 }
7769 }
7870 if ( isset( $this->props['members'] ) ) {
Index: branches/RL2/extensions/Gadgets/SpecialGadgetManager.php
@@ -75,15 +75,15 @@
7676 // Avoid broken or empty headings. Fallback to a special message
7777 // for uncategorized gadgets (e.g. gadgets with category '' ).
7878 if ( $category !== '' ) {
79 - $categoryMsg = wfMessage( "gadgetcategory-$category" );
 79+ $categoryTitle = GadgetRepo::getCategoryTitle( $category );
8080 } else {
81 - $categoryMsg = wfMessage( 'gadgetmanager-uncategorized' );
 81+ $categoryTitle = wfMessage( 'gadgetmanager-uncategorized' )->plain();
8282 }
8383
8484 // Category header
8585 $html .= Html::element( 'h2',
8686 array( 'class' => 'mw-gadgetmanager-category' ),
87 - $categoryMsg->exists() ? $categoryMsg->plain() : $this->getLang()->ucfirst( $category )
 87+ $categoryTitle
8888 );
8989
9090 // Start per-category gadgets table

Follow-up revisions

RevisionCommit summaryAuthorDate
r97003[ResourceLoader 2]: Fix fatal r96863...krinkle21:13, 13 September 2011
r97055RL2: Per CR on r96863, moved getCategoryTitle() to LocalGadgetRepo. Also turn...catrope13:19, 14 September 2011

Comments

#Comment by Krinkle (talk | contribs)   17:42, 12 September 2011

Since ForeignGadgetRepos extends GadgetRepo, perhaps document that getCategoryTitle must only be used for local gadgets ? Or perhaps abstract it and implement in LocalGadgetRepo and return null/false in ForeignGadgetRepos.

#Comment by Catrope (talk | contribs)   17:43, 12 September 2011

Since getCategoryTitle() takes a string which might come from anywhere (but is usually obtained from Gadget::getCategory()), I don't see the point. Note it's a static function too, it's not really tied to the repo, that just seemed like a logical place to put the code to me.

#Comment by Krinkle (talk | contribs)   17:49, 12 September 2011

It takes a string that is used to build a message key, which is then fetched the value for from wfMessage. So it's local-only. I know it's a static function currently, but those can be re-implemented in an extending class too, right ?

#Comment by Catrope (talk | contribs)   10:15, 14 September 2011

It is local-only that's true. Re-implementing static functions in child classes only works 'properly' in PHP 5.3, using late static bindings. Also, given that the callers use the class name explicitly when calling the function (GadgetRepo::foo()), it's not a big deal.

But yeah it probably makes more sense to put it in LocalGadgetRepo, especially since that's a singleton now. I'll move it.

#Comment by Krinkle (talk | contribs)   21:14, 13 September 2011
+			global $wgLang;
+			$langObj = $lang === null ? $wgLang : Language::factory( $lang );
+			return $lang->ucfirst( $category );

Causes Fatal error: Call to a member function ucfirst() on a non-object in /Sites/local/mediawiki/branches/RL2/extensions/Gadgets/backend/GadgetRepo.php on line 110.

Fixed in r97003.

#Comment by Dantman (talk | contribs)   21:20, 13 September 2011

...time to join in on the feline-killing quote mimicry...

"Every time you use $wgLang in new code, I kill a translator's favourite pet cat."

#Comment by Krinkle (talk | contribs)   21:50, 13 September 2011

In this case it's somewhat justified in my opinion, as the line directly above this uses wfMessage(), which does new Message, which uses $wgLang right now by default (not RequestContext).

To avoid inconsitencies, this out-of-message construction should use the same default langauge as wfMessage itself.

Status & tagging log