r57749 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57748‎ | r57749 | r57750 >
Date:07:10, 15 October 2009
Author:nad
Status:deferred
Tags:
Comment:
Add an export link to the end of result tables
Modified paths:
  • /trunk/extensions/RecordAdmin/RecordAdmin.php (modified) (history)
  • /trunk/extensions/RecordAdmin/RecordAdmin_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RecordAdmin/RecordAdmin_body.php
@@ -13,6 +13,8 @@
1414 var $orderBy = '';
1515 var $desc = false;
1616 var $guid = '';
 17+ var $quid = '';
 18+ var $filter = array();
1719
1820 function __construct() {
1921 # Name to use for creating a new record either via RecordAdmin or a public form
@@ -48,6 +50,7 @@
4951 # Get posted form values if any
5052 $posted = array();
5153 foreach ( $_REQUEST as $k => $v ) if ( preg_match( '|^ra_(\\w+)|', $k, $m ) ) $posted[$m[1]] = is_array( $v ) ? join( "\n", $v ) : $v;
 54+ $this->filter = $posted;
5255
5356 # Read in and prepare the form for this record type if one has been selected
5457 if ( $type ) $this->preProcessForm( $type );
@@ -238,7 +241,14 @@
239242 * Return an array of records given type and other criteria
240243 */
241244 function getRecords( $type, $posted, $wpTitle = '', $invert = false, $orderby = 'created desc' ) {
 245+ global $wgRequest;
242246
 247+ # Generate a unique id for this set of parameters
 248+ $this->quid = md5( var_export( array( $type, $posted ), true ) );
 249+
 250+ # If an export has been requested but not for this query-id, then bail with empty set
 251+ if ( ( $export = $wgRequest->getText( 'quid' ) ) && $export != $this->quid ) return array();
 252+
243253 # First get all the articles using the type's template
244254 $records = array();
245255 $dbr = wfGetDB( DB_SLAVE );
@@ -294,7 +304,6 @@
295305 if ( $this->desc = eregi( ' +desc *$', $orderby ) ) $orderby = eregi_replace( ' +desc *$', '', $orderby );
296306 $this->orderBy = $orderby;
297307 usort( $records, array( $this, 'sortCallback' ) );
298 -
299308 return $records;
300309 }
301310
@@ -312,7 +321,7 @@
313322 * Render a set of records returned by getRecords() as an HTML table
314323 */
315324 function renderRecords( $records, $cols = false, $sortable = true, $template = false, $name = 'wpSelect' ) {
316 - global $wgParser, $wgTitle;
 325+ global $wgParser, $wgTitle, $wgRequest;
317326 if ( count( $records ) < 1 ) return wfMsg( 'recordadmin-nomatch' );
318327
319328 $special = Title::makeTitle( NS_SPECIAL, 'RecordAdmin' );
@@ -389,6 +398,39 @@
390399 $table .= "</tr>\n";
391400 }
392401 $table .= "</table>\n";
 402+
 403+ # If export requested convert the table to csv and disable output etc
 404+ if ( $quid = $wgRequest->getText( 'quid' ) ) {
 405+ global $wgOut;
 406+ $wgOut->disable();
 407+ header("Content-Type: text/html");
 408+ header("Content-Disposition: attachment; filename=\"$quid.csv\"");
 409+ preg_match_all( "|<td.*?>\s*(.*?)\s*</td>|s", $table, $data );
 410+ $cols = $cols ? $cols : array_keys( $th );
 411+ $csv = join( "\t", $cols );
 412+ foreach ( $data[1] as $i => $cell ) {
 413+ if ( $i % count( $cols ) == 0 ) {
 414+ $csv .= "\n";
 415+ $sep = "";
 416+ } else $sep = "\t";
 417+ $cell = preg_replace( "|<!--.+?-->|s", "", $cell );
 418+ if ( preg_match( "|<p>\s*(.+?)\s*</p>|s", $cell, $m ) ) $cell = $m[1];
 419+ $cell = preg_replace( "/[\\r\\n]+/m", "\\n", $cell );
 420+ $csv .= "$sep$cell";
 421+ }
 422+ print $csv;
 423+ $table = '';
 424+ }
 425+
 426+ # Otherwise add an export link
 427+ else {
 428+ $qs = "wpType=$type&wpFind=1&quid={$this->quid}";
 429+ foreach ( $this->filter as $k => $v ) $qs .= "&ra_$k=" . urlencode( $v );
 430+ $url = $wgTitle->getLocalURL( $qs );
 431+ $anchor = wfMsg( 'export-submit' );
 432+ $table .= "\n<a class=\"recordadmin-export\" href=\"$url\">$anchor</a>";
 433+ }
 434+
393435 return $table;
394436 }
395437
@@ -664,6 +706,7 @@
665707 * Render a record search in a parser-function
666708 */
667709 function expandTableMagic( &$parser, $type ) {
 710+ global $wgTitle;
668711 $parser->mOutput->mCacheTime = -1;
669712 $filter = array();
670713 $title = '';
@@ -688,11 +731,13 @@
689732 else $filter[$match[1]] = $match[2];
690733 }
691734 }
 735+ $this->filter = $filter;
692736 $this->preProcessForm( $type );
693737 $this->examineForm();
694738 $records = $this->getRecords( $type, $filter, $title, $invert, $orderby );
695739 if ( $count ) while ( count( $records ) > $count ) array_pop( $records );
696740 $table = $this->renderRecords( $records, $cols, $sortable, $template, $name );
 741+
697742 return array( $table, 'noparse' => true, 'isHTML' => true );
698743 }
699744
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.8.7, 2009-09-25' );
 15+define( 'RECORDADMIN_VERSION', '0.8.8, 2009-10-15' );
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