Index: trunk/extensions/DynamicSidebar/DynamicSidebar.body.php |
— | — | @@ -1,74 +1,64 @@ |
2 | 2 | <?php |
3 | | - |
4 | | -if (!defined('MEDIAWIKI')) die(); |
5 | | - |
6 | 3 | class DynamicSidebar { |
7 | | - |
8 | 4 | /** |
9 | | - * Called from SkinBeforeParseSidebar hook. Modifies the sidebar |
10 | | - * via callbacks. |
11 | | - * |
12 | | - * @param Skin $skin |
13 | | - * @param string $sidebar |
14 | | - * @access public |
| 5 | + * Called through $wgExtensionFunctions. Disables sidebar cache if necessary |
15 | 6 | */ |
16 | | - public function modifySidebarContent( $skin, &$sidebar ) { |
17 | | - $dynamicsidebar = new DynamicSidebar(); |
18 | | - $sidebar = $dynamicsidebar->modifySidebar( $skin, $sidebar ); |
| 7 | + public static function setup() { |
| 8 | + global $wgUser, $wgEnableSidebarCache; |
19 | 9 | |
| 10 | + // Don't pollute the sidebar cache for non-logged-in users |
| 11 | + // Also ensure that logged-in users are getting dynamic content |
| 12 | + // FIXME: Only do this for users who should actually get the non-standard sidebar |
| 13 | + if ( $wgUser->isLoggedIn() ) { |
| 14 | + $wgEnableSidebarCache = false; |
| 15 | + } |
20 | 16 | return true; |
21 | 17 | } |
22 | 18 | |
23 | 19 | /** |
24 | | - * Internal function called to modify the sidebar via callbacks. |
| 20 | + * Called from SkinBeforeParseSidebar hook. Modifies the sidebar |
| 21 | + * via callbacks. |
25 | 22 | * |
26 | 23 | * @param Skin $skin |
27 | 24 | * @param string $sidebar |
28 | | - * @access private |
29 | | - * @return string |
30 | 25 | */ |
31 | | - private function modifySidebar( $skin, $sidebar ) { |
32 | | - global $egDynamicSidebarUseGroups, $egDynamicSidebarUseUserpages; |
33 | | - global $egDynamicSidebarUseCategories; |
| 26 | + private static function modifySidebar( $skin, &$sidebar ) { |
| 27 | + global $wgDynamicSidebarUseGroups, $wgDynamicSidebarUseUserpages; |
| 28 | + global $wgDynamicSidebarUseCategories; |
34 | 29 | |
35 | | - if ( $egDynamicSidebarUseGroups ) { |
36 | | - $sidebar = preg_replace_callback( "/\* GROUP-SIDEBAR/", array( &$this, 'doGroupSidebar' ), $sidebar ); |
| 30 | + if ( $wgDynamicSidebarUseGroups ) { |
| 31 | + $sidebar = preg_replace_callback( '/\* GROUP-SIDEBAR/', array( 'self', 'doGroupSidebar' ), $sidebar ); |
37 | 32 | } |
38 | | - if ( $egDynamicSidebarUseUserpages ) { |
39 | | - $sidebar = preg_replace_callback( "/\* USER-SIDEBAR/", array( &$this, 'doUserSidebar' ), $sidebar ); |
| 33 | + if ( $wgDynamicSidebarUseUserpages ) { |
| 34 | + $sidebar = preg_replace_callback( '/\* USER-SIDEBAR/', array( 'self', 'doUserSidebar' ), $sidebar ); |
40 | 35 | } |
41 | | - if ( $egDynamicSidebarUseCategories ) { |
42 | | - $sidebar = preg_replace_callback( "/\* CATEGORY-SIDEBAR/", array( &$this, 'doCategorySidebar' ), $sidebar ); |
| 36 | + if ( $wgDynamicSidebarUseCategories ) { |
| 37 | + $sidebar = preg_replace_callback( '/\* CATEGORY-SIDEBAR/', array( 'self', 'doCategorySidebar' ), $sidebar ); |
43 | 38 | } |
44 | | - |
45 | | - return $sidebar; |
| 39 | + return true; |
46 | 40 | } |
47 | 41 | |
48 | 42 | /** |
49 | 43 | * Callback function, replaces $matches with the contents of |
50 | 44 | * User:<username>/Sidebar |
51 | 45 | * |
52 | | - * @param array $matches |
| 46 | + * @param array $matches unused |
53 | 47 | * @access private |
54 | 48 | * @return string |
55 | 49 | */ |
56 | | - private function doUserSidebar( $matches ) { |
| 50 | + private static function doUserSidebar( $matches ) { |
57 | 51 | global $wgUser; |
58 | | - |
59 | 52 | $username = $wgUser->getName(); |
60 | | - |
61 | | - $title = Title::makeTitle( NS_USER, $username . '/Sidebar' ); |
62 | | - $a = new Article( $title ); |
63 | 53 | |
64 | | - // does '<username>/Sidebar' page exist? |
65 | | - if ( ( $a === null ) || ( $a->getID() === 0 ) ) { |
| 54 | + // does 'User:<username>/Sidebar' page exist? |
| 55 | + $title = Title::makeTitle( NS_USER, $username . '/Sidebar' ); |
| 56 | + if ( !$title->exists() ) { |
66 | 57 | // Remove this sidebar if not |
67 | 58 | return ''; |
68 | 59 | } |
69 | 60 | |
70 | | - $text = $a->getContent(); |
71 | | - |
72 | | - return $text; |
| 61 | + $a = new Article( $title ); |
| 62 | + return $a->getContent(); |
73 | 63 | } |
74 | 64 | |
75 | 65 | /** |
— | — | @@ -76,16 +66,15 @@ |
77 | 67 | * MediaWiki:Sidebar/<group>, based on the current logged in user's |
78 | 68 | * groups. |
79 | 69 | * |
80 | | - * @param array $matches |
| 70 | + * @param array $matches unused |
81 | 71 | * @access private |
82 | 72 | * @return string |
83 | 73 | */ |
84 | | - private function doGroupSidebar( $matches ) { |
| 74 | + private static function doGroupSidebar( $matches ) { |
85 | 75 | global $wgUser; |
86 | | - |
| 76 | + |
87 | 77 | // Get group membership array. |
88 | 78 | $groups = $wgUser->getEffectiveGroups(); |
89 | | - |
90 | 79 | // Did we find any groups? |
91 | 80 | if ( count( $groups ) == 0 ) { |
92 | 81 | // Remove this sidebar if not |
— | — | @@ -93,22 +82,17 @@ |
94 | 83 | } |
95 | 84 | |
96 | 85 | $text = ''; |
97 | | - |
98 | 86 | foreach ( $groups as $group ) { |
99 | 87 | // Form the path to the article: |
100 | 88 | // MediaWiki:Sidebar/<group> |
101 | 89 | $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 ) ) { |
| 90 | + if ( !$title->exists() ) { |
106 | 91 | continue; |
107 | 92 | } |
108 | | - |
| 93 | + $a = new Article( $title ); |
109 | 94 | $text .= $a->getContent() . "\n"; |
110 | 95 | |
111 | 96 | } |
112 | | - |
113 | 97 | return $text; |
114 | 98 | } |
115 | 99 | |
— | — | @@ -117,17 +101,15 @@ |
118 | 102 | * MediaWiki:Sidebar/<category>, based on the current logged in user's |
119 | 103 | * userpage categories. |
120 | 104 | * |
121 | | - * @param array $matches |
| 105 | + * @param array $matches unused |
122 | 106 | * @access private |
123 | 107 | * @return string |
124 | 108 | */ |
125 | | - private function doCategorySidebar( $matches ) { |
| 109 | + private static function doCategorySidebar( $matches ) { |
126 | 110 | global $wgUser; |
127 | 111 | |
128 | | - $username = $wgUser->getName(); |
129 | | - self::printDebug( "User name: $username" ); |
130 | | - $userpage = Title::makeTitle( NS_USER, $username ); |
131 | | - $categories = $userpage->getParentCategories(); |
| 112 | + self::printDebug( "User name: {$wgUser->getName()}" ); |
| 113 | + $categories = $wgUser->getUserPage()->getParentCategories(); |
132 | 114 | |
133 | 115 | // Did we find any categories? |
134 | 116 | if ( count( $categories ) == 0 ) { |
— | — | @@ -136,11 +118,10 @@ |
137 | 119 | } |
138 | 120 | |
139 | 121 | $text = ''; |
140 | | - |
141 | 122 | // getParentCategories() returns categories in the form: |
142 | 123 | // [ParentCategory] => page |
143 | 124 | // We only care about the parent category |
144 | | - foreach ( $categories as $category => $userpage ) { |
| 125 | + foreach ( $categories as $category => $unused ) { |
145 | 126 | // $category is in form Category:<category> |
146 | 127 | // We need <category>. |
147 | 128 | $category = explode( ":", $category ); |
— | — | @@ -150,16 +131,12 @@ |
151 | 132 | // Form the path to the article: |
152 | 133 | // MediaWiki:Sidebar/<category> |
153 | 134 | $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 ) ) { |
| 135 | + if ( !$title->exists() ) { |
158 | 136 | continue; |
159 | 137 | } |
160 | | - |
| 138 | + $a = new Article( $title ); |
161 | 139 | $text .= $a->getContent() . "\n"; |
162 | 140 | } |
163 | | - |
164 | 141 | return $text; |
165 | 142 | } |
166 | 143 | |
— | — | @@ -172,9 +149,9 @@ |
173 | 150 | * @access private |
174 | 151 | */ |
175 | 152 | private static function printDebug( $debugText, $debugArr = null ) { |
176 | | - global $egDynamicSidebarDebug; |
| 153 | + global $wgDynamicSidebarDebug; |
177 | 154 | |
178 | | - if ( $egDynamicSidebarDebug ) { |
| 155 | + if ( $wgDynamicSidebarDebug ) { |
179 | 156 | if ( isset( $debugArr ) ) { |
180 | 157 | $text = $debugText . " " . implode( "::", $debugArr ); |
181 | 158 | wfDebugLog( 'dynamic-sidebar', $text, false ); |
— | — | @@ -183,5 +160,4 @@ |
184 | 161 | } |
185 | 162 | } |
186 | 163 | } |
187 | | - |
188 | 164 | } |
Index: trunk/extensions/DynamicSidebar/DynamicSidebar.php |
— | — | @@ -25,15 +25,11 @@ |
26 | 26 | } |
27 | 27 | |
28 | 28 | // Set defaults |
29 | | -global $egDynamicSidebarDebug, $egDynamicSidebarUseGroups, $egDynamicSidebarUseUserpages; |
30 | | -global $egDynamicSidebarUseCategories; |
| 29 | +$wgDynamicSidebarDebug = false; |
| 30 | +$wgDynamicSidebarUseUserpages = true; |
| 31 | +$wgDynamicSidebarUseGroups = true; |
| 32 | +$wgDynamicSidebarUseCategories = true; |
31 | 33 | |
32 | | -$egDynamicSidebarDebug = false; |
33 | | -$egDynamicSidebarUseUserpages = true; |
34 | | -$egDynamicSidebarUseGroups = true; |
35 | | -$egDynamicSidebarUseCategories = true; |
36 | | - |
37 | | -global $wgExtensionCredits; |
38 | 34 | $wgExtensionCredits['other'][] = array( |
39 | 35 | 'name' => 'DynamicSidebar', |
40 | 36 | 'version' => '1.0a', |
— | — | @@ -41,23 +37,9 @@ |
42 | 38 | 'url' => 'http://www.mediawiki.org/wiki/Extension:DynamicSidebar', |
43 | 39 | 'description' => "Provides dynamic sidebars based on user pages, groups, and categories.", |
44 | 40 | ); |
45 | | - |
46 | | -$wgExtensionFunctions[] = 'DynamicSidebarSetupExtension'; |
47 | 41 | |
| 42 | +$wgExtensionFunctions[] = 'DynamicSidebar::setup'; |
| 43 | +$wgHooks['SkinBeforeParseSidebar'][] = 'DynamicSidebar::modifySidebar'; |
| 44 | + |
48 | 45 | $dir = dirname( __FILE__ ) . '/'; |
49 | 46 | $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 | | -} |