r62947 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62946‎ | r62947 | r62948 >
Date:02:44, 25 February 2010
Author:laner
Status:deferred (Comments)
Tags:
Comment:
Initial commit of DynamicSidebar; see documentation at:

http://www.mediawiki.org/wiki/Extension:DynamicSidebar
Modified paths:
  • /trunk/extensions/DynamicSidebar/DynamicSidebar.body.php (added) (history)
  • /trunk/extensions/DynamicSidebar/DynamicSidebar.php (added) (history)

Diff [purge]

Index: trunk/extensions/DynamicSidebar/DynamicSidebar.body.php
@@ -0,0 +1,187 @@
 2+<?php
 3+
 4+if (!defined('MEDIAWIKI')) die();
 5+
 6+class DynamicSidebar {
 7+
 8+ /**
 9+ * Called from SkinBeforeParseSidebar hook. Modifies the sidebar
 10+ * via callbacks.
 11+ *
 12+ * @param Skin $skin
 13+ * @param string $sidebar
 14+ * @access public
 15+ */
 16+ public function modifySidebarContent( $skin, &$sidebar ) {
 17+ $dynamicsidebar = new DynamicSidebar();
 18+ $sidebar = $dynamicsidebar->modifySidebar( $skin, $sidebar );
 19+
 20+ return true;
 21+ }
 22+
 23+ /**
 24+ * Internal function called to modify the sidebar via callbacks.
 25+ *
 26+ * @param Skin $skin
 27+ * @param string $sidebar
 28+ * @access private
 29+ * @return string
 30+ */
 31+ private function modifySidebar( $skin, $sidebar ) {
 32+ global $egDynamicSidebarUseGroups, $egDynamicSidebarUseUserpages;
 33+ global $egDynamicSidebarUseCategories;
 34+
 35+ if ( $egDynamicSidebarUseGroups ) {
 36+ $sidebar = preg_replace_callback( "/\* GROUP-SIDEBAR/", array( &$this, 'doGroupSidebar' ), $sidebar );
 37+ }
 38+ if ( $egDynamicSidebarUseUserpages ) {
 39+ $sidebar = preg_replace_callback( "/\* USER-SIDEBAR/", array( &$this, 'doUserSidebar' ), $sidebar );
 40+ }
 41+ if ( $egDynamicSidebarUseCategories ) {
 42+ $sidebar = preg_replace_callback( "/\* CATEGORY-SIDEBAR/", array( &$this, 'doCategorySidebar' ), $sidebar );
 43+ }
 44+
 45+ return $sidebar;
 46+ }
 47+
 48+ /**
 49+ * Callback function, replaces $matches with the contents of
 50+ * User:<username>/Sidebar
 51+ *
 52+ * @param array $matches
 53+ * @access private
 54+ * @return string
 55+ */
 56+ private function doUserSidebar( $matches ) {
 57+ global $wgUser;
 58+
 59+ $username = $wgUser->getName();
 60+
 61+ $title = Title::makeTitle( NS_USER, $username . '/Sidebar' );
 62+ $a = new Article( $title );
 63+
 64+ // does '<username>/Sidebar' page exist?
 65+ if ( ( $a === null ) || ( $a->getID() === 0 ) ) {
 66+ // Remove this sidebar if not
 67+ return '';
 68+ }
 69+
 70+ $text = $a->getContent();
 71+
 72+ return $text;
 73+ }
 74+
 75+ /**
 76+ * Callback function, replaces $matches with the contents of
 77+ * MediaWiki:Sidebar/<group>, based on the current logged in user's
 78+ * groups.
 79+ *
 80+ * @param array $matches
 81+ * @access private
 82+ * @return string
 83+ */
 84+ private function doGroupSidebar( $matches ) {
 85+ global $wgUser;
 86+
 87+ // Get group membership array.
 88+ $groups = $wgUser->getEffectiveGroups();
 89+
 90+ // Did we find any groups?
 91+ if ( count( $groups ) == 0 ) {
 92+ // Remove this sidebar if not
 93+ return '';
 94+ }
 95+
 96+ $text = '';
 97+
 98+ foreach ( $groups as $group ) {
 99+ // Form the path to the article:
 100+ // MediaWiki:Sidebar/<group>
 101+ $title = Title::makeTitle( NS_MEDIAWIKI, 'Sidebar/' . $group );
 102+ $a = new Article( $title );
 103+
 104+ // Is the corresponding page found?
 105+ if ( ( $a === null ) || ( $a->getID() === 0 ) ) {
 106+ continue;
 107+ }
 108+
 109+ $text .= $a->getContent() . "\n";
 110+
 111+ }
 112+
 113+ return $text;
 114+ }
 115+
 116+ /**
 117+ * Callback function, replaces $matches with the contents of
 118+ * MediaWiki:Sidebar/<category>, based on the current logged in user's
 119+ * userpage categories.
 120+ *
 121+ * @param array $matches
 122+ * @access private
 123+ * @return string
 124+ */
 125+ private function doCategorySidebar( $matches ) {
 126+ global $wgUser;
 127+
 128+ $username = $wgUser->getName();
 129+ self::printDebug( "User name: $username" );
 130+ $userpage = Title::makeTitle( NS_USER, $username );
 131+ $categories = $userpage->getParentCategories();
 132+
 133+ // Did we find any categories?
 134+ if ( count( $categories ) == 0 ) {
 135+ // Remove this sidebar if not.
 136+ return '';
 137+ }
 138+
 139+ $text = '';
 140+
 141+ // getParentCategories() returns categories in the form:
 142+ // [ParentCategory] => page
 143+ // We only care about the parent category
 144+ foreach ( $categories as $category => $userpage ) {
 145+ // $category is in form Category:<category>
 146+ // We need <category>.
 147+ $category = explode( ":", $category );
 148+ $category = $category[1];
 149+ self::printDebug( "Checking category: $category" );
 150+
 151+ // Form the path to the article:
 152+ // MediaWiki:Sidebar/<category>
 153+ $title = Title::makeTitle( NS_MEDIAWIKI, 'Sidebar/' . $category );
 154+ $a = new Article( $title );
 155+
 156+ // Is the corresponding page found?
 157+ if ( ( $a === null ) || ( $a->getID() === 0 ) ) {
 158+ continue;
 159+ }
 160+
 161+ $text .= $a->getContent() . "\n";
 162+ }
 163+
 164+ return $text;
 165+ }
 166+
 167+ /**
 168+ * Prints debugging information. $debugText is what you want to print, $debugArr
 169+ * will expand into arrItem::arrItem2::arrItem3::... and is appended to $debugText
 170+ *
 171+ * @param string $debugText
 172+ * @param array $debugArr
 173+ * @access private
 174+ */
 175+ private static function printDebug( $debugText, $debugArr = null ) {
 176+ global $egDynamicSidebarDebug;
 177+
 178+ if ( $egDynamicSidebarDebug ) {
 179+ if ( isset( $debugArr ) ) {
 180+ $text = $debugText . " " . implode( "::", $debugArr );
 181+ wfDebugLog( 'dynamic-sidebar', $text, false );
 182+ } else {
 183+ wfDebugLog( 'dynamic-sidebar', $debugText, false );
 184+ }
 185+ }
 186+ }
 187+
 188+}
Property changes on: trunk/extensions/DynamicSidebar/DynamicSidebar.body.php
___________________________________________________________________
Name: svn:eol-style
1189 + native
Index: trunk/extensions/DynamicSidebar/DynamicSidebar.php
@@ -0,0 +1,63 @@
 2+<?php
 3+# Copyright (C) 2010 Ryan Lane <http://www.mediawiki.org/wiki/User:Ryan_lane>
 4+#
 5+# This program is free software; you can redistribute it and/or modify
 6+# it under the terms of the GNU General Public License as published by
 7+# the Free Software Foundation; either version 2 of the License, or
 8+# (at your option) any later version.
 9+#
 10+# This program is distributed in the hope that it will be useful,
 11+# but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13+# GNU General Public License for more details.
 14+#
 15+# You should have received a copy of the GNU General Public License along
 16+# with this program; if not, write to the Free Software Foundation, Inc.,
 17+# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 18+# http://www.gnu.org/copyleft/gpl.html
 19+
 20+# This extension is loosely based on the SidebarEx extension by Jean-Lou Dupont;
 21+# See: http://www.mediawiki.org/wiki/Extension:SidebarEx
 22+
 23+if ( !defined( 'MEDIAWIKI' ) ) {
 24+ echo "Not a valid entry point";
 25+ exit( 1 );
 26+}
 27+
 28+// Set defaults
 29+global $egDynamicSidebarDebug, $egDynamicSidebarUseGroups, $egDynamicSidebarUseUserpages;
 30+global $egDynamicSidebarUseCategories;
 31+
 32+$egDynamicSidebarDebug = false;
 33+$egDynamicSidebarUseUserpages = true;
 34+$egDynamicSidebarUseGroups = true;
 35+$egDynamicSidebarUseCategories = true;
 36+
 37+global $wgExtensionCredits;
 38+$wgExtensionCredits['other'][] = array(
 39+ 'name' => 'DynamicSidebar',
 40+ 'version' => '1.0a',
 41+ 'author' => 'Ryan Lane',
 42+ 'url' => 'http://www.mediawiki.org/wiki/Extension:DynamicSidebar',
 43+ 'description' => "Provides dynamic sidebars based on user pages, groups, and categories.",
 44+ );
 45+
 46+$wgExtensionFunctions[] = 'DynamicSidebarSetupExtension';
 47+
 48+$dir = dirname( __FILE__ ) . '/';
 49+$wgAutoloadClasses['DynamicSidebar'] = $dir . 'DynamicSidebar.body.php';
 50+
 51+function DynamicSidebarSetupExtension() {
 52+ global $wgHooks;
 53+ global $wgUser, $wgEnableSidebarCache;
 54+
 55+ // Don't pollute the sidebar cache for non-loggedin users
 56+ // Also ensure that loggedin users are getting dynamic content
 57+ if ( $wgUser->isLoggedIn() ) {
 58+ $wgEnableSidebarCache = false;
 59+ }
 60+
 61+ $wgHooks['SkinBeforeParseSidebar'][] = 'DynamicSidebar::modifySidebarContent';
 62+
 63+ return true;
 64+}
Property changes on: trunk/extensions/DynamicSidebar/DynamicSidebar.php
___________________________________________________________________
Name: svn:eol-style
165 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r62961DynamicSidebar: Kill $wgDynamicSidebarDebug per CR on r62947catrope16:05, 25 February 2010

Comments

#Comment by Nikerabbit (talk | contribs)   10:59, 25 February 2010
+		if ( ( $a === null ) || ( $a->getID() === 0 ) ) {
  • Why don't you check the title instead. if ( !$title || !$title->exists() )? Should work fine for NS_USER, but NS_MEDIAWIKI is special.
+			// MediaWiki:Sidebar/<group>
  • Message cache doesn't like that, it expects only language codes as subpages. I'm not sure what does it actually do on this case, though.
+// Set defaults
  • What are these for?
+		if ( $egDynamicSidebarDebug ) {
  • Feels a bit redundant, since one has to configure where given group in wfDebugLog goes into first place
#Comment by Catrope (talk | contribs)   16:06, 25 February 2010
  • The $a === null thing was fixed in r62957.
  • Yes, the MediaWiki:Sidebar/foo syntax is fragile due to possible collisions with message keys and possible collisions between user, group and category names. I suggest using MediaWiki:Sidebar/Group:foo , MediaWiki:Sidebar/User:Bar and MediaWiki:Sidebar/Category:Baz instead so they can't conflict and for sure aren't valid message keys
  • The $wg vars need to be documented with comments, yes.
  • $wgDynamicSidebarDebug was killed in r62961.
#Comment by Ryan lane (talk | contribs)   16:28, 25 February 2010

Ok. Will make the change to MediaWiki:Sidebar/Category:<category> and MediaWiki:Sidebar/Group:<group>. I'll also document the globals. Notice the User sidebars are in the User namespace, so they shouldn't interfere.

Status & tagging log