r70731 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:< r70730‎ | r70731 | r70732 >
Date:01:12, 9 August 2010
Author:jeroendedauw
Status:ok (Comments)
Tags:
Comment:
This change puts extensions that have an unknown type into the 'other' category, instead of not displaying them at all, and ensures this category is displayed as the last one (not careful use of SpecialVersionExtensionTypes broke this).
Modified paths:
  • /trunk/phase3/includes/specials/SpecialVersion.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialVersion.php
——@@ -201,11 +201,15 @@
202202 * Returns an array with the base extension types.
203203 * Type is stored as array key, the message as array value.
204204 *
 205+ * TODO: ideally this would return all extension types, including
 206+ * those added by SpecialVersionExtensionTypes. This is not possible
 207+ * since this hook is passing along $this though.
 208+ *
205209 * @since 1.17
206210 *
207211 * @return array
208212 */
209 - public static function getBaseExtensionTypes() {
 213+ public static function getExtensionTypes() {
210214 return array(
211215 'specialpage' => wfMsg( 'version-specialpages' ),
212216 'parserhook' => wfMsg( 'version-parserhooks' ),
——@@ -227,24 +231,34 @@
228232 return '';
229233 }
230234
231 - $extensionTypes = self::getBaseExtensionTypes();
 235+ $extensionTypes = self::getExtensionTypes();
232236
233237 wfRunHooks( 'SpecialVersionExtensionTypes', array( &$this, &$extensionTypes ) );
234238
235239 $out = Xml::element( 'h2', array( 'id' => 'mw-version-ext' ), wfMsg( 'version-extensions' ) ) .
236240 Xml::openElement( 'table', array( 'class' => 'wikitable', 'id' => 'sv-ext' ) );
237241
238 - foreach ( $extensionTypes as $type => $text ) {
239 - if ( isset ( $wgExtensionCredits[$type] ) && count ( $wgExtensionCredits[$type] ) ) {
240 - $out .= $this->openExtType( $text, 'credits-' . $type );
241 -
242 - usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
243 -
244 - foreach ( $wgExtensionCredits[$type] as $extension ) {
245 - $out .= $this->getCreditsForExtension( $extension );
246 - }
 242+ // Make sure the 'other' type is set to an array.
 243+ if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {
 244+ $wgExtensionCredits['other'] = array();
 245+ }
 246+
 247+ // Find all extensions that do not have a valid type and give them the type 'other'.
 248+ foreach ( $wgExtensionCredits as $type => $extensions ) {
 249+ if ( !array_key_exists( $type, $extensionTypes ) ) {
 250+ $wgExtensionCredits['other'] = array_merge( $wgExtensionCredits['other'], $extensions );
247251 }
248252 }
 253+
 254+ // Loop through the extension categories to display their extensions in the list.
 255+ foreach ( $extensionTypes as $type => $message ) {
 256+ if ( $type != 'other' ) {
 257+ $out .= $this->getExtensionCategory( $type, $message );
 258+ }
 259+ }
 260+
 261+ // We want the 'other' type to be last in the list.
 262+ $out .= $this->getExtensionCategory( 'other', $extensionTypes['other'] );
249263
250264 if ( count( $wgExtensionFunctions ) ) {
251265 $out .= $this->openExtType( wfMsg( 'version-extension-functions' ), 'extension-functions' );
——@@ -272,7 +286,35 @@
273287
274288 return $out;
275289 }
 290+
 291+ /**
 292+ * Creates and returns the HTML for a single extension category.
 293+ *
 294+ * @since 1.17
 295+ *
 296+ * @param $type String
 297+ * @param $message String
 298+ *
 299+ * @return string
 300+ */
 301+ protected function getExtensionCategory( $type, $message ) {
 302+ global $wgExtensionCredits;
 303+
 304+ $out = '';
 305+
 306+ if ( array_key_exists( $type, $wgExtensionCredits ) && count( $wgExtensionCredits[$type] ) > 0 ) {
 307+ $out .= $this->openExtType( $message, 'credits-' . $type );
276308
 309+ usort( $wgExtensionCredits[$type], array( $this, 'compare' ) );
 310+
 311+ foreach ( $wgExtensionCredits[$type] as $extension ) {
 312+ $out .= $this->getCreditsForExtension( $extension );
 313+ }
 314+ }
 315+
 316+ return $out;
 317+ }
 318+
277319 /**
278320 * Callback to sort extensions by type.
279321 */

Comments

#Comment by 😂 (talk | contribs)   15:57, 21 January 2011
	if ( !array_key_exists( 'other', $wgExtensionCredits ) ) {

You should use

isset( $wgExtensionCredits['other'] )

, it's faster. Same in a few other places.

#Comment by Jeroen De Dauw (talk | contribs)   16:09, 21 January 2011

I think using the array_key_exists function is more correct from a logic point of view. The PHP docs also do not anything about using isset instead, so I'm sticking with using this one.

#Comment by 😂 (talk | contribs)   16:19, 21 January 2011

I'm not surprised the docs don't say anything, I'm just saying it's faster

#Comment by Jeroen De Dauw (talk | contribs)   16:37, 21 January 2011

If you can link some credible source stating the performance difference of the current PHP version, then I'll consider it. However, I really think PHP should take care of this, not everyone using the function.

#Comment by 😂 (talk | contribs)   17:30, 21 January 2011

It doesn't seem to be as big a concern as it used to be (The array isn't passed by reference in current PHP, as mentioned in [1]). Doing some profiling, array_key_exists() is only slightly slower (10mil iterations). isset() will always be at least marginally faster, since it is a language construct and array_key_exists() is a function.

In a high-traffic code path like the Parser or Database, isset would probably be best due to the (slight) increase. However, changing it here is just micro optimizing and can be left as it is.

  1. http://us3.php.net/manual/en/function.array-key-exists.php#68619
#Comment by 😂 (talk | contribs)   17:30, 21 January 2011

It doesn't seem to be as big a concern as it used to be (The array isn't passed by reference in current PHP, as mentioned in [1]). Doing some profiling, array_key_exists() is only slightly slower (10mil iterations). isset() will always be at least marginally faster, since it is a language construct and array_key_exists() is a function.

In a high-traffic code path like the Parser or Database, isset would probably be best due to the (slight) increase. However, changing it here is just micro optimizing and can be left as it is.

  1. http://us3.php.net/manual/en/function.array-key-exists.php#68619
#Comment by Jeroen De Dauw (talk | contribs)   17:32, 21 January 2011

Cool, thanks for looking into it further :)

#Comment by Nikerabbit (talk | contribs)   18:30, 21 January 2011

Isset and array_key_exists differ how they handle keys with null values, unless I'm mistaken.

#Comment by 😂 (talk | contribs)   21:25, 21 January 2011

Yes.

Status & tagging log