r61210 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61209‎ | r61210 | r61211 >
Date:19:46, 18 January 2010
Author:nad
Status:deferred
Tags:
Comment:
started implementing groupby parameter in renderRecords()
Modified paths:
  • /trunk/extensions/RecordAdmin/RecordAdmin.i18n.php (modified) (history)
  • /trunk/extensions/RecordAdmin/RecordAdmin.php (modified) (history)
  • /trunk/extensions/RecordAdmin/RecordAdmin_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RecordAdmin/RecordAdmin_body.php
@@ -353,7 +353,7 @@
354354 $ek = str_replace( '|', '\|', $k );
355355 if ( !preg_match( "|\s*\|\s*$ek\s*=|", $text ) ) $text .= "\n|$k=\n|"; # Treat non-existent fields as existing but empty
356356 $i = preg_match( "|^\s*\|\s*$ek\s*= *(.*?) *(?=^\s*[\|\}])|sm", $text, $m );
357 - $r[$k] = isset( $m[1] ) ? $m[1] : '';
 357+ $r[$k] = trim( isset( $m[1] ) ? $m[1] : '' );
358358 if ( $v && !( $i && $this->cmpCallback( $r[$k], $v, $operator[$k] ) ) ) $match = false;
359359 }
360360 if ( $invert ) $match = !$match;
@@ -392,19 +392,18 @@
393393
394394 # Group the records according to the "groupby" parameter
395395 if ( $groupby ) {
396 - if ( !is_array( $groupby ) ) $groupby = split( '/\s*,\s*/', $groupby );
 396+ $groupby = preg_split( '/\s*,\s*/', $groupby );
397397 $tmp = array();
398398 foreach ( $records as $r ) {
399399 $v0 = $r[$groupby[0]];
400 - if ( $groupby[1] ) {
 400+ if ( isset( $groupby[1] ) ) {
401401 $v1 = $r[$groupby[1]];
402 - if ( !in_array( $tmp[$v0] ) ) $tmp[$v0] = array();
 402+ if ( !isset( $tmp[$v0] ) || !is_array( $tmp[$v0] ) ) $tmp[$v0] = array();
403403 $tmp[$v0][$v1] = $r;
404404 } else $tmp[$v0] = $r;
405405 }
406406 $records = $tmp;
407407 }
408 - $this->groupBy = $groupby;
409408 $this->format = $format;
410409
411410 return $records;
@@ -452,7 +451,7 @@
453452 /**
454453 * Render a set of records returned by getRecords() as an HTML table
455454 */
456 - function renderRecords( $records, $cols = false, $sortable = true, $template = false, $name = 'wpSelect', $export = true ) {
 455+ function renderRecords( $records, $cols = false, $sortable = true, $template = false, $name = 'wpSelect', $export = true, $groupby = false ) {
457456 global $wgParser, $wgTitle, $wgRequest;
458457 if ( count( $records ) < 1 ) return wfMsg( 'recordadmin-nomatch' );
459458
@@ -474,59 +473,81 @@
475474 $class = 'col' . preg_replace( '|\W|', '-', $col );
476475 $th[$col] = "<th class='$class'>$col$br</th>";
477476 }
 477+ $ncol = 0;
478478 foreach ( $cols ? $cols : array_keys( $th ) as $col ) {
479479 $html = isset( $th[$col] ) ? $th[$col] : "<th>$col</th>";
480480 $table .= "$html\n";
 481+ $ncol++;
481482 }
482483 $table .= "</tr>\n";
483484
 485+ # If using grouping, reconstruct the record tree as a list including headings
 486+ if ( $groupby ) {
 487+ $groupby = preg_split( '/\s*,\s*/', $groupby );
 488+ $td = "<td colspan=\"$ncol\">";
 489+ $tmp = array();
 490+ foreach( $records as $k1 => $v1 ) {
 491+ if ( empty( $k1 ) ) $k1 = wfMsg( 'recordadmin-notset', $groupby[0] );
 492+ $tmp[] = "$td<h2>$k1</h2></td>\n";
 493+ foreach( $v1 as $k2 => $v2 ) {
 494+ if ( isset( $groupby[1] ) ) {
 495+ if ( empty( $k2 ) ) $k2 = wfMsg( 'recordadmin-notset', $groupby[1] );
 496+ $tmp[] = "$td<h3>$k2</h3></td>\n";
 497+ foreach( $v2 as $v3 ) $tmp[] = $v3;
 498+ } else $tmp[] = $v2;
 499+ }
 500+ }
 501+ $records = $tmp;
 502+ }
 503+
484504 # Table rows
485505 $stripe = '';
486506 foreach ( $records as $r ) {
487 -
488 - # Create special values for this row
489 - $tsc = $this->formatDate( $r['created'] );
490 - $tsm = $this->formatDate( $r['modified'] );
491 - $t = $r[0];
492 - $u = $t->getLocalURL();
493 - $col = $r['title'];
494 - $ecol = urlencode( $col );
495 - $sel = "<input type='checkbox' name='{$name}[]' value='$col' checked />";
496507 $stripe = $stripe ? '' : ' class="stripe"';
497508 $table .= "<tr$stripe>";
 509+ if ( !is_array( $r ) ) $table .= $r; # Just add as HTML content if not a row
 510+ else {
498511
499 - # Render this row
500 - if ( $template ) {
501 - $text = '{'.'{'."$template|select=%SELECT%|title=$col|created=$tsc|modified=$tsm";
502 - foreach ( array_keys( $this->types ) as $col ) {
503 - $v = isset( $r[$col] ) ? $r[$col] : '';
504 - $text .= "|$col=$v";
505 - }
506 - $text .= '}}';
507 - $text = $wgParser->parse( $text, $wgTitle, $wgParser->mOptions, true, false )->getText();
508 - $text = preg_replace( "|&lt;(/?td.*?)&gt;|", "<$1>", $text );
509 - $text = str_replace( '%SELECT%', $sel, $text );
510 - $table .= "$text\n";
511 - }
512 - else {
513 - $row = array(
514 - 'select' => "<td class='col-select'>$sel</td>\n",
515 - 'title' => "<td class='col0 col-title'><a href='$u'>$col</a></td>",
516 - 'actions' => "<td class='col1 col-actions'><a href='" . $special->getLocalURL( "wpType=$type&wpRecord=$ecol" ) . "'>"
517 - . wfMsg( 'recordadmin-editlink' ) . "</a></td>",
518 - 'created' => "<td class='col2 col-created'>$tsc</td>\n",
519 - 'modified' => "<td class='col3 col-modified'>$tsm</td>\n",
520 - );
521 - foreach ( $cols ? $cols : array_keys( $th ) as $col ) {
522 - if ( !isset( $row[$col] ) ) {
523 - $v = isset( $r[$col] ) ? $wgParser->parse( $r[$col], $wgTitle, $wgParser->mOptions, true, false )->getText() : '&nbsp;';
524 - $class = 'col' . preg_replace( '|\W|', '-', $col );
525 - $row[$col] = "<td class='$class'>$v</td>";
 512+ # Create special values for this row
 513+ $tsc = $this->formatDate( $r['created'] );
 514+ $tsm = $this->formatDate( $r['modified'] );
 515+ $t = $r[0];
 516+ $u = $t->getLocalURL();
 517+ $col = $r['title'];
 518+ $ecol = urlencode( $col );
 519+ $sel = "<input type='checkbox' name='{$name}[]' value='$col' checked />";
 520+
 521+ # Render this row
 522+ if ( $template ) {
 523+ $text = '{'.'{'."$template|select=%SELECT%|title=$col|created=$tsc|modified=$tsm";
 524+ foreach ( array_keys( $this->types ) as $col ) {
 525+ $v = isset( $r[$col] ) ? $r[$col] : '';
 526+ $text .= "|$col=$v";
526527 }
527 - $table .= "$row[$col]\n";
 528+ $text .= '}}';
 529+ $text = $wgParser->parse( $text, $wgTitle, $wgParser->mOptions, true, false )->getText();
 530+ $text = preg_replace( "|&lt;(/?td.*?)&gt;|", "<$1>", $text );
 531+ $text = str_replace( '%SELECT%', $sel, $text );
 532+ $table .= "$text\n";
 533+ } else {
 534+ $row = array(
 535+ 'select' => "<td class='col-select'>$sel</td>\n",
 536+ 'title' => "<td class='col0 col-title'><a href='$u'>$col</a></td>",
 537+ 'actions' => "<td class='col1 col-actions'><a href='" . $special->getLocalURL( "wpType=$type&wpRecord=$ecol" ) . "'>"
 538+ . wfMsg( 'recordadmin-editlink' ) . "</a></td>",
 539+ 'created' => "<td class='col2 col-created'>$tsc</td>\n",
 540+ 'modified' => "<td class='col3 col-modified'>$tsm</td>\n",
 541+ );
 542+ foreach ( $cols ? $cols : array_keys( $th ) as $col ) {
 543+ if ( !isset( $row[$col] ) ) {
 544+ $v = isset( $r[$col] ) ? $wgParser->parse( $r[$col], $wgTitle, $wgParser->mOptions, true, false )->getText() : '&nbsp;';
 545+ $class = 'col' . preg_replace( '|\W|', '-', $col );
 546+ $row[$col] = "<td class='$class'>$v</td>";
 547+ }
 548+ $table .= "$row[$col]\n";
 549+ }
528550 }
529551 }
530 -
531552 $table .= "</tr>\n";
532553 }
533554 $table .= "</table>\n";
@@ -578,10 +599,10 @@
579600 foreach ( $this->filter as $k => $v ) $qs .= "&ra_$k=" . urlencode( $v );
580601 $url = $wgTitle->getLocalURL( $qs );
581602 if ( in_array( 'csv', $export ) ) {
582 - $table .= "\n<a class=\"recordadmin-export-csv\" href=\"$url\">" . wfMsg( 'recrodadmin-export-csv' ) . "</a>";
 603+ $table .= "\n<a class=\"recordadmin-export-csv\" href=\"$url\">" . wfMsg( 'recordadmin-export-csv' ) . "</a>";
583604 }
584605 if ( in_array( 'pdf', $export ) ) {
585 - $table .= "\n<a class=\"recordadmin-export-pdf\" href=\"$url&format=pdf\">" . wfMsg( 'recrodadmin-export-pdf' ) . "</a>";
 606+ $table .= "\n<a class=\"recordadmin-export-pdf\" href=\"$url&format=pdf\">" . wfMsg( 'recordadmin-export-pdf' ) . "</a>";
586607 }
587608 }
588609
@@ -913,7 +934,7 @@
914935 $this->examineForm();
915936 $records = $this->getRecords( $type, $filter, $op, $title, $invert, $orderby, $groupby, $format );
916937 if ( $count ) while ( count( $records ) > $count ) array_pop( $records );
917 - $table = $this->renderRecords( $records, $cols, $sortable, $template, $name, $export );
 938+ $table = $this->renderRecords( $records, $cols, $sortable, $template, $name, $export, $groupby, $format );
918939 $this->type = $tmp;
919940
920941 return array( $table, 'noparse' => true, 'isHTML' => true );
Index: trunk/extensions/RecordAdmin/RecordAdmin.i18n.php
@@ -55,8 +55,9 @@
5656 'recordadmin-editwithform' => 'Edit with form',
5757 'recordadmin-typeinfo' => '$1 record',
5858 'right-recordadmin' => 'Find and edit record pages',
59 - 'recrodadmin-export-csv' => 'CSV',
60 - 'recrodadmin-export-pdf' => 'PDF',
 59+ 'recordadmin-export-csv' => 'CSV',
 60+ 'recordadmin-export-pdf' => 'PDF',
 61+ 'recordadmin-notset' => 'No $1',
6162 );
6263
6364 /** Message documentation (Message documentation)
@@ -84,8 +85,8 @@
8586 'recordadmin-actions' => '{{Identical|Action}}',
8687 'recordadmin-typeinfo' => '$1 is a record type. Could be read as "A record of type $1"',
8788 'right-recordadmin' => '{{doc-right|recordadmin}}',
88 - 'recrodadmin-export-csv' => '{{Optional}}',
89 - 'recrodadmin-export-pdf' => '{{Optional}}',
 89+ 'recordadmin-export-csv' => '{{Optional}}',
 90+ 'recordadmin-export-pdf' => '{{Optional}}',
9091 );
9192
9293 /** Afrikaans (Afrikaans)
@@ -153,8 +154,8 @@
154155 'recordadmin-editwithform' => 'عدل مع الاستمارة',
155156 'recordadmin-typeinfo' => '$1 تسجيل',
156157 'right-recordadmin' => 'أبحث و عدل صفحات السجل',
157 - 'recrodadmin-export-csv' => 'سي في إس',
158 - 'recrodadmin-export-pdf' => 'بي دي إف',
 158+ 'recordadmin-export-csv' => 'سي في إس',
 159+ 'recordadmin-export-pdf' => 'بي دي إف',
159160 );
160161
161162 /** Aramaic (ܐܪܡܝܐ)
Index: trunk/extensions/RecordAdmin/RecordAdmin.php
@@ -11,7 +11,7 @@
1212 * @licence GNU General Public Licence 2.0 or later
1313 */
1414
15 -define( 'RECORDADMIN_VERSION', '0.9.10, 2010-01-18' );
 15+define( 'RECORDADMIN_VERSION', '0.10.0, 2010-01-18' );
1616
1717 $wgRecordAdminUseNamespaces = false; # Whether record articles should be in a namespace of the same name as their type
1818 $wgRecordAdminCategory = 'Records'; # Category containing record types

Status & tagging log