Index: trunk/extensions/RecordAdmin/RecordAdmin_body.php |
— | — | @@ -13,6 +13,8 @@ |
14 | 14 | var $orderBy = ''; |
15 | 15 | var $desc = false; |
16 | 16 | var $guid = ''; |
| 17 | + var $quid = ''; |
| 18 | + var $filter = array(); |
17 | 19 | |
18 | 20 | function __construct() { |
19 | 21 | # Name to use for creating a new record either via RecordAdmin or a public form |
— | — | @@ -48,6 +50,7 @@ |
49 | 51 | # Get posted form values if any |
50 | 52 | $posted = array(); |
51 | 53 | 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; |
52 | 55 | |
53 | 56 | # Read in and prepare the form for this record type if one has been selected |
54 | 57 | if ( $type ) $this->preProcessForm( $type ); |
— | — | @@ -238,7 +241,14 @@ |
239 | 242 | * Return an array of records given type and other criteria |
240 | 243 | */ |
241 | 244 | function getRecords( $type, $posted, $wpTitle = '', $invert = false, $orderby = 'created desc' ) { |
| 245 | + global $wgRequest; |
242 | 246 | |
| 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 | + |
243 | 253 | # First get all the articles using the type's template |
244 | 254 | $records = array(); |
245 | 255 | $dbr = wfGetDB( DB_SLAVE ); |
— | — | @@ -294,7 +304,6 @@ |
295 | 305 | if ( $this->desc = eregi( ' +desc *$', $orderby ) ) $orderby = eregi_replace( ' +desc *$', '', $orderby ); |
296 | 306 | $this->orderBy = $orderby; |
297 | 307 | usort( $records, array( $this, 'sortCallback' ) ); |
298 | | - |
299 | 308 | return $records; |
300 | 309 | } |
301 | 310 | |
— | — | @@ -312,7 +321,7 @@ |
313 | 322 | * Render a set of records returned by getRecords() as an HTML table |
314 | 323 | */ |
315 | 324 | function renderRecords( $records, $cols = false, $sortable = true, $template = false, $name = 'wpSelect' ) { |
316 | | - global $wgParser, $wgTitle; |
| 325 | + global $wgParser, $wgTitle, $wgRequest; |
317 | 326 | if ( count( $records ) < 1 ) return wfMsg( 'recordadmin-nomatch' ); |
318 | 327 | |
319 | 328 | $special = Title::makeTitle( NS_SPECIAL, 'RecordAdmin' ); |
— | — | @@ -389,6 +398,39 @@ |
390 | 399 | $table .= "</tr>\n"; |
391 | 400 | } |
392 | 401 | $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 | + |
393 | 435 | return $table; |
394 | 436 | } |
395 | 437 | |
— | — | @@ -664,6 +706,7 @@ |
665 | 707 | * Render a record search in a parser-function |
666 | 708 | */ |
667 | 709 | function expandTableMagic( &$parser, $type ) { |
| 710 | + global $wgTitle; |
668 | 711 | $parser->mOutput->mCacheTime = -1; |
669 | 712 | $filter = array(); |
670 | 713 | $title = ''; |
— | — | @@ -688,11 +731,13 @@ |
689 | 732 | else $filter[$match[1]] = $match[2]; |
690 | 733 | } |
691 | 734 | } |
| 735 | + $this->filter = $filter; |
692 | 736 | $this->preProcessForm( $type ); |
693 | 737 | $this->examineForm(); |
694 | 738 | $records = $this->getRecords( $type, $filter, $title, $invert, $orderby ); |
695 | 739 | if ( $count ) while ( count( $records ) > $count ) array_pop( $records ); |
696 | 740 | $table = $this->renderRecords( $records, $cols, $sortable, $template, $name ); |
| 741 | + |
697 | 742 | return array( $table, 'noparse' => true, 'isHTML' => true ); |
698 | 743 | } |
699 | 744 | |
Index: trunk/extensions/RecordAdmin/RecordAdmin.php |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * @licence GNU General Public Licence 2.0 or later |
13 | 13 | */ |
14 | 14 | |
15 | | -define( 'RECORDADMIN_VERSION', '0.8.7, 2009-09-25' ); |
| 15 | +define( 'RECORDADMIN_VERSION', '0.8.8, 2009-10-15' ); |
16 | 16 | |
17 | 17 | $wgRecordAdminUseNamespaces = false; # Whether record articles should be in a namespace of the same name as their type |
18 | 18 | $wgRecordAdminCategory = 'Records'; # Category containing record types |