r62959 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62958‎ | r62959 | r62960 >
Date:16:00, 25 February 2010
Author:catrope
Status:ok (Comments)
Tags:
Comment:
DynamicSidebar: Convert to using the existing SkinBuildSidebar hook, so adding a new hook in core isn't necessary. Uses Skin::addToSidebarPlain(), which was added in r62958
Modified paths:
  • /trunk/extensions/DynamicSidebar/DynamicSidebar.body.php (modified) (history)
  • /trunk/extensions/DynamicSidebar/DynamicSidebar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DynamicSidebar/DynamicSidebar.body.php
@@ -26,27 +26,34 @@
2727 global $wgDynamicSidebarUseGroups, $wgDynamicSidebarUseUserpages;
2828 global $wgDynamicSidebarUseCategories;
2929
30 - if ( $wgDynamicSidebarUseGroups ) {
31 - $sidebar = preg_replace_callback( '/\* GROUP-SIDEBAR/', array( 'self', 'doGroupSidebar' ), $sidebar );
 30+ if ( $wgDynamicSidebarUseGroups && isset( $sidebar['GROUP-SIDEBAR'] ) ) {
 31+ // Replace the GROUP-SIDEBAR entry with the group's sidebar
 32+ $groupSB = array();
 33+ $skin->addToSidebarPlain( $groupSB, self::doGroupSidebar() );
 34+ array_splice( $sidebar, array_search( 'GROUP-SIDEBAR', $sidebar ), 1, $groupSB );
3235 }
33 - if ( $wgDynamicSidebarUseUserpages ) {
34 - $sidebar = preg_replace_callback( '/\* USER-SIDEBAR/', array( 'self', 'doUserSidebar' ), $sidebar );
 36+ if ( $wgDynamicSidebarUseUserpages && isset( $sidebar['USER-SIDEBAR'] ) ) {
 37+ // Replace the USER-SIDEBAR entry with the user's sidebar
 38+ $userSB = array();
 39+ $skin->addToSidebarPlain( $userSB, self::doUserSidebar() );
 40+ array_splice( $sidebar, array_search( 'USER-SIDEBAR', $sidebar ), 1, $userSB );
3541 }
36 - if ( $wgDynamicSidebarUseCategories ) {
37 - $sidebar = preg_replace_callback( '/\* CATEGORY-SIDEBAR/', array( 'self', 'doCategorySidebar' ), $sidebar );
 42+ if ( $wgDynamicSidebarUseCategories && isset( $sidebar['CATEGORY-SIDEBAR'] ) ) {
 43+ $catSB = array();
 44+ $skin->addToSidebarPlain( $catSB, self::doCategorySidebar() );
 45+ array_splice( $sidebar, array_search( 'CATEGORY-SIDEBAR', $sidebar ), 1, $catSB );
3846 }
3947 return true;
4048 }
4149
4250 /**
43 - * Callback function, replaces $matches with the contents of
 51+ * Grabs the sidebar for the current user
4452 * User:<username>/Sidebar
4553 *
46 - * @param array $matches unused
4754 * @access private
4855 * @return string
4956 */
50 - private static function doUserSidebar( $matches ) {
 57+ private static function doUserSidebar() {
5158 global $wgUser;
5259 $username = $wgUser->getName();
5360
@@ -62,11 +69,8 @@
6370 }
6471
6572 /**
66 - * Callback function, replaces $matches with the contents of
67 - * MediaWiki:Sidebar/<group>, based on the current logged in user's
68 - * groups.
 73+ * Grabs the sidebar for the current user's groups
6974 *
70 - * @param array $matches unused
7175 * @access private
7276 * @return string
7377 */
@@ -97,11 +101,8 @@
98102 }
99103
100104 /**
101 - * Callback function, replaces $matches with the contents of
102 - * MediaWiki:Sidebar/<category>, based on the current logged in user's
103 - * userpage categories.
 105+ * Grabs the sidebar for the current user's categories
104106 *
105 - * @param array $matches unused
106107 * @access private
107108 * @return string
108109 */
Index: trunk/extensions/DynamicSidebar/DynamicSidebar.php
@@ -39,7 +39,7 @@
4040 );
4141
4242 $wgExtensionFunctions[] = 'DynamicSidebar::setup';
43 -$wgHooks['SkinBeforeParseSidebar'][] = 'DynamicSidebar::modifySidebar';
 43+$wgHooks['SkinBuildSidebar'][] = 'DynamicSidebar::modifySidebar';
4444
4545 $dir = dirname( __FILE__ ) . '/';
4646 $wgAutoloadClasses['DynamicSidebar'] = $dir . 'DynamicSidebar.body.php';

Follow-up revisions

RevisionCommit summaryAuthorDate
r62974DynamicSidebar: Fix usage of array_search n r62959catrope20:44, 25 February 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r62958Rename Skin::addToSidebar() to Skin::addToSidebarPlain() and have it take pla...catrope15:58, 25 February 2010

Comments

#Comment by Ryan lane (talk | contribs)   19:29, 25 February 2010

$matches also needs to be removed from doGroupSidebar() and doCategorySidebar() as well.

array_search isn't finding the keys in the sidebar array. A comment on php.net's array_search page mentions:

array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings.

#Comment by Catrope (talk | contribs)   20:02, 25 February 2010

Presumably this can be fixed by passing true as the third argument to array_search()

#Comment by Ryan lane (talk | contribs)   20:41, 25 February 2010

"* USER-SIDEBAR" is turned into a key in the array, so array_search() doesn't work. The following works:

Index: DynamicSidebar.body.php

=======================================================

--- DynamicSidebar.body.php (revision 62973) +++ DynamicSidebar.body.php (working copy) @@ -41,7 +41,9 @@

               if ( $wgDynamicSidebarUseCategories && isset( $sidebar['CATEGORY-SIDEBAR'] ) ) {
                       $catSB = array();
                       $skin->addToSidebarPlain( $catSB, self::doCategorySidebar() );

- array_splice( $sidebar, array_search( 'CATEGORY-SIDEBAR', $sidebar ), 1, $catSB ); + $keys = array_keys( $sidebar ); + $key = array_search( 'CATEGORY-SIDEBAR', $keys, true ); + array_splice( $sidebar, $key, 1, $catSB );

#Comment by Ryan lane (talk | contribs)   20:42, 25 February 2010

Let's try this again:

Index: DynamicSidebar.body.php
===================================================================
--- DynamicSidebar.body.php     (revision 62973)
+++ DynamicSidebar.body.php     (working copy)
@@ -41,7 +41,9 @@
                if ( $wgDynamicSidebarUseCategories && isset( $sidebar['CATEGORY-SIDEBAR'] ) ) {
                        $catSB = array();
                        $skin->addToSidebarPlain( $catSB, self::doCategorySidebar() );
-                       array_splice( $sidebar, array_search( 'CATEGORY-SIDEBAR', $sidebar ), 1, $catSB );
+                       $keys = array_keys( $sidebar );
+                       $key = array_search( 'CATEGORY-SIDEBAR', $keys, true );
+                       array_splice( $sidebar, $key, 1, $catSB );
#Comment by Catrope (talk | contribs)   20:45, 25 February 2010

Did this, but a bit more concisely, in r62974

Status & tagging log