AdSvS has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/195893

Change subject: Additional params 'filename' and 'link text'
......................................................................

Additional params 'filename' and 'link text'

Support has been added for two additional parameters. Filename sets the
name of the file to be downloaded, instead of 'results.csv'. Link text
sets the text of the text shown on the page, instead of the
'cargo-viewcsv' message ('View CSV' in English).

Change-Id: If85be4133c294e71767b58340b6907a829181bbb
---
M formats/CargoCSVFormat.php
M specials/CargoExport.php
2 files changed, 27 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo 
refs/changes/93/195893/1

diff --git a/formats/CargoCSVFormat.php b/formats/CargoCSVFormat.php
index 90e4a48..beab6be 100644
--- a/formats/CargoCSVFormat.php
+++ b/formats/CargoCSVFormat.php
@@ -24,13 +24,20 @@
                if ( array_key_exists( 'delimiter', $displayParams ) ) {
                        $queryParams['delimiter'] = $displayParams['delimiter'];
                }
-
+               if ( array_key_exists( 'filename', $displayParams ) ) {
+                       $queryParams['filename'] = $displayParams['filename'];
+               }
+               if ( array_key_exists( 'link text', $displayParams ) ) {
+                       $linktext = $displayParams['link text'];
+               } else { 
+                       $linktext = wfMessage( 'cargo-viewcsv' )->text();
+               }
                $linkAttrs = array(
                        'href' => $ce->getFullURL( $queryParams ),
                );
-               $text = Html::rawElement( 'a', $linkAttrs, wfMessage( 
'cargo-viewcsv' )->text() );
+               $text = Html::rawElement( 'a', $linkAttrs, $linktext );
 
-               return $text;
+               return $text; 
        }
 
 }
diff --git a/specials/CargoExport.php b/specials/CargoExport.php
index 9295289..034e8f5 100644
--- a/specials/CargoExport.php
+++ b/specials/CargoExport.php
@@ -51,7 +51,12 @@
                        } elseif ( $delimiter == '\t' ) {
                                $delimiter = "\t";
                        }
-                       $this->displayCSVData( $sqlQueries, $delimiter );
+                       $filename = $req->getVal( 'filename' );
+                       if ( $filename == '' ) {
+                               $filename = 'results.csv';
+                       }
+                       $default = $req->getVal( 'default' );
+                       $this->displayCSVData( $sqlQueries, $delimiter, 
$filename, $default );
                } elseif ( $format == 'json' ) {
                        $this->displayJSONData( $sqlQueries );
                }
@@ -216,13 +221,22 @@
                print json_encode( $displayedArray, JSON_NUMERIC_CHECK | 
JSON_HEX_TAG );
        }
 
-       function displayCSVData( $sqlQueries, $delimiter ) {
+       function displayCSVData( $sqlQueries, $delimiter, $filename, $default ) 
{
+               $attachmenttext = "Content-Disposition: attachment; filename=";
+               $attachmenttext .= $filename;
                header( "Content-Type: text/csv" );
-               header( "Content-Disposition: attachment; filename=results.csv" 
);
+               header( $attachmenttext );
 
                // We'll only use the first query, if there's more than one.
                $sqlQuery = $sqlQueries[0];
                $queryResults = $sqlQuery->run();
+               if ( count( $queryResults ) == 0 ) {
+                       if ( $default == '' ) {
+                               return '<em>' . wfMessage( 'table_pager_empty' 
)->text() . '</em>'; //default
+                       } else {
+                               return $default;
+                       }
+               }
                $out = fopen('php://output', 'w');
                // Display header row.
                fputcsv( $out, array_keys( reset( $queryResults ) ), $delimiter 
);

-- 
To view, visit https://gerrit.wikimedia.org/r/195893
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If85be4133c294e71767b58340b6907a829181bbb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: AdSvS <a...@wikibase.nl>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to