r107077 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107076‎ | r107077 | r107078 >
Date:17:16, 22 December 2011
Author:robin
Status:ok
Tags:
Comment:
Follow-up r105078: per Brion, make a general function to divide a list over several columns (it if is useful enough it could be moved to core).
Make it 3 instead of 2 columns.
Modified paths:
  • /trunk/extensions/CentralAuth/specials/SpecialWikiSets.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralAuth/specials/SpecialWikiSets.php
@@ -106,7 +106,7 @@
107107 * @param $reason
108108 */
109109 function buildSetView( $subpage, $error = false, $name = null, $type = null, $wikis = null, $reason = null ) {
110 - global $wgOut, $wgUser;
 110+ global $wgOut, $wgUser, $wgLocalDatabases;
111111
112112 $wgOut->setSubtitle( wfMsgExt( 'centralauth-editset-subtitle', 'parseinline' ) );
113113
@@ -139,6 +139,19 @@
140140 $usage = '';
141141 }
142142
 143+ $sortedWikis = $set->getWikisRaw();
 144+ sort( $sortedWikis );
 145+
 146+ # Make an array of the opposite list of wikis
 147+ # (all databases *excluding* the defined ones)
 148+ $restWikis = array();
 149+ foreach( $wgLocalDatabases as $wiki ) {
 150+ if( !in_array( $wiki, $sortedWikis ) ) {
 151+ $restWikis[] = $wiki;
 152+ }
 153+ }
 154+ sort( $restWikis );
 155+
143156 if ( $this->mCanEdit ) {
144157 if ( $error ) {
145158 $wgOut->addHTML( "<strong class='error'>{$error}</strong>" );
@@ -152,7 +165,8 @@
153166 }
154167 $form['centralauth-editset-type'] = $this->buildTypeSelector( 'wpType', $type );
155168 $form['centralauth-editset-wikis'] = Xml::textarea( 'wpWikis', $wikis );
156 - $form['centralauth-editset-restwikis'] = $this->buildRestWikiList( $set->getWikisRaw() );
 169+ $form['centralauth-editset-restwikis'] = Xml::textarea( 'wpRestWikis',
 170+ implode( "\n", $restWikis ), 40, 5, array( 'readonly' => true ) );
157171 $form['centralauth-editset-reason'] = Xml::input( 'wpReason', 50, $reason );
158172
159173 $wgOut->addHTML( Xml::buildForm( $form, 'centralauth-editset-submit' ) );
@@ -164,8 +178,8 @@
165179 $form['centralauth-editset-name'] = htmlspecialchars( $name );
166180 $form['centralauth-editset-usage'] = $usage;
167181 $form['centralauth-editset-type'] = wfMsg( "centralauth-editset-{$type}" );
168 - $form['centralauth-editset-wikis'] = $this->buildWikiList( $set->getWikisRaw() );
169 - $form['centralauth-editset-restwikis'] = $this->buildRestWikiList( $set->getWikisRaw() );
 182+ $form['centralauth-editset-wikis'] = self::buildTableByList( $sortedWikis, 3, array( 'width' => '100%' ) );
 183+ $form['centralauth-editset-restwikis'] = self::buildTableByList( $restWikis, 3, array( 'width' => '100%' ) );
170184
171185 $wgOut->addHTML( Xml::buildForm( $form ) );
172186 }
@@ -185,64 +199,43 @@
186200 }
187201
188202 /**
189 - * @param $list array List of wikis defined in the wiki set (either opt-in or opt-out)
190 - * @return string
 203+ * Builds a table of several columns, and divides the items of
 204+ * $list equally among each column. All items are escaped.
 205+ *
 206+ * Could in the future be replaced by CSS column-count.
 207+ *
 208+ * @param $list array
 209+ * @param $columns int: number of columns
 210+ * @param $tableAttribs array: <table> attributes
 211+ * @return string Table
191212 */
192 - function buildWikiList( $list ) {
193 - sort( $list );
194 -
195 - $firstCol = round( count( $list ) / 2 );
196 - $list1 = array_slice( $list, 0, $firstCol );
197 - $list2 = array_slice( $list, $firstCol );
198 -
199 - $html = '<table><tbody><tr style="vertical-align:top;"><td><ul>';
200 - foreach ( $list1 as $wiki ) {
201 - $escWiki = htmlspecialchars( $wiki );
202 - $html .= "<li>{$escWiki}</li>";
 213+ function buildTableByList( $list, $columns = 2, $tableAttribs = array() ) {
 214+ if( !is_array( $list ) ) {
 215+ return '';
203216 }
204 - $html .= '</ul></td><td>&nbsp;</td><td><ul>';
205 - foreach ( $list2 as $wiki ) {
206 - $escWiki = htmlspecialchars( $wiki );
207 - $html .= "<li>{$escWiki}</li>";
 217+ $count = count( $list );
 218+ # If there are less items than columns, limit the number of columns
 219+ $columns = $count < $columns ? $count : $columns;
 220+ $itemsPerCol = round( $count / $columns );
 221+ $i = 0;
 222+ $splitLists = array();
 223+ while( $i < $columns ) {
 224+ $splitLists[$i] = array_slice( $list, $itemsPerCol*$i, $itemsPerCol );
 225+ $i++;
208226 }
209 - $html .= '</ul></td></tr></tbody></table>';
210 -
211 - return $html;
212 - }
213 -
214 - /**
215 - * This list shows all databases *excluding* the defined ones
216 - * So for opt-out, it shows the databases on which the wiki set is enabled
217 - * And for opt-in, it shows the databases on which the wiki set is disabled
218 - *
219 - * @param $list array List of wikis defined in the wiki set (either opt-in or opt-out)
220 - * @return string
221 - */
222 - function buildRestWikiList( $list ) {
223 - global $wgLocalDatabases;
224 -
225 - sort( $wgLocalDatabases );
226 - foreach( $wgLocalDatabases as $wiki ) {
227 - if( !in_array( $wiki, $list ) ) {
228 - $restWikis[] = htmlspecialchars( $wiki );
 227+ $body = '';
 228+ foreach( $splitLists as $column => $splitList ) {
 229+ $body .= '<td width="' . round( 100 / $columns ) . '%"><ul>';
 230+ foreach( $splitList as $listitem ) {
 231+ $body .= Html::element( 'li', array(), $listitem );
229232 }
 233+ $body .= '</ul></td>';
230234 }
231 -
232 - if( $this->mCanEdit ) {
233 - $html = Xml::textarea( 'wpWikis', implode( "\n", $restWikis ), 40, 5, array( 'readonly' => true ) );
234 - } else {
235 - $firstCol = round( count( $restWikis ) / 2 );
236 - $list1 = array_slice( $restWikis, 0, $firstCol );
237 - $list2 = array_slice( $restWikis, $firstCol );
238 -
239 - $html = '<table><tbody><tr style="vertical-align:top;"><td><ul>' .
240 - '<li>' . implode( '</li><li>', $list2 ) . '</li>' .
241 - '</ul></td><td>&nbsp;</td><td><ul>' .
242 - '<li>' . implode( '</li><li>', $list2 ) . '</li>' .
243 - '</ul></td></tr></tbody></table>';
244 - }
245 -
246 - return $html;
 235+ return Html::rawElement( 'table', $tableAttribs,
 236+ '<tbody>' .
 237+ Html::rawElement( 'tr', array( 'style' => 'vertical-align:top;' ), $body ) .
 238+ '</tbody>'
 239+ );
247240 }
248241
249242 /**

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r105078* Show the list of wikis in two columns, to save space...robin19:47, 3 December 2011

Status & tagging log