AdSvS has uploaded a new change for review.

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

Change subject: Added Excel export format
......................................................................

Added Excel export format

Change-Id: I0b053d5ef0decbf0b3b70a5e50c8780a8eb06e41
---
M Cargo.php
M CargoQueryDisplayer.php
A formats/CargoExcelFormat.php
M i18n/en.json
M i18n/nl.json
M specials/CargoExport.php
6 files changed, 73 insertions(+), 0 deletions(-)


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

diff --git a/Cargo.php b/Cargo.php
index 7f36145..73944e5 100644
--- a/Cargo.php
+++ b/Cargo.php
@@ -100,6 +100,7 @@
 $wgAutoloadClasses['CargoTreeFormatTree'] = $dir . 
'/formats/CargoTreeFormat.php';
 $wgAutoloadClasses['CargoEmbeddedFormat'] = $dir . 
'/formats/CargoEmbeddedFormat.php';
 $wgAutoloadClasses['CargoCSVFormat'] = $dir . '/formats/CargoCSVFormat.php';
+$wgAutoloadClasses['CargoExcelFormat'] = $dir . 
'/formats/CargoExcelFormat.php';
 $wgAutoloadClasses['CargoJSONFormat'] = $dir . '/formats/CargoJSONFormat.php';
 $wgAutoloadClasses['CargoTableFormat'] = $dir . 
'/formats/CargoTableFormat.php';
 $wgAutoloadClasses['CargoDynamicTableFormat'] = $dir . 
'/formats/CargoDynamicTableFormat.php';
diff --git a/CargoQueryDisplayer.php b/CargoQueryDisplayer.php
index d5d5fa0..460dd8e 100644
--- a/CargoQueryDisplayer.php
+++ b/CargoQueryDisplayer.php
@@ -35,6 +35,7 @@
                        'template' => 'CargoTemplateFormat',
                        'embedded' => 'CargoEmbeddedFormat',
                        'csv' => 'CargoCSVFormat',
+                       'excel' => 'CargoExcelFormat',
                        'json' => 'CargoJSONFormat',
                        'outline' => 'CargoOutlineFormat',
                        'tree' => 'CargoTreeFormat',
diff --git a/formats/CargoExcelFormat.php b/formats/CargoExcelFormat.php
new file mode 100644
index 0000000..16064d3
--- /dev/null
+++ b/formats/CargoExcelFormat.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @author Yaron Koren
+ * @ingroup Cargo
+ */
+
+class CargoExcelFormat extends CargoDeferredFormat {
+
+       function allowedParameters() {
+               return array( 'filename' );
+       }
+
+       /**
+        *
+        * @param array $sqlQueries
+        * @param array $displayParams Unused
+        * @param array $querySpecificParams Unused
+        * @return string
+        */
+       function queryAndDisplay( $sqlQueries, $displayParams, 
$querySpecificParams = null ) {
+               $ce = SpecialPage::getTitleFor( 'CargoExport' );
+               $queryParams = $this->sqlQueriesToQueryParams( $sqlQueries );
+               $queryParams['format'] = 'excel';
+               if ( array_key_exists( 'filename', $displayParams ) ) {
+                       $queryParams['filename'] = $displayParams['filename'];
+               }
+               if ( array_key_exists( 'link text', $displayParams ) ) {
+                       $linkText = $displayParams['link text'];
+               } else {
+                       $linkText = wfMessage( 'results.xls' )->text();
+               }
+               $linkAttrs = array(
+                       'href' => $ce->getFullURL( $queryParams ),
+               );
+               $text = Html::rawElement( 'a', $linkAttrs, $linkText );
+
+               return $text;
+       }
+
+}
diff --git a/i18n/en.json b/i18n/en.json
index 308664e..b7c0fc8 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -38,6 +38,7 @@
        "cargo-drilldown-novalues": "There are no values for this filter",
        "cargo-drilldown-toomanyvalues": "Too many values; not displaying.",
        "cargo-viewcsv": "View CSV",
+       "cargo-viewxls": "View XLS",
        "cargo-viewjson": "View JSON",
        "cargo-purgecache": "Purge cache",
        "specialpages-group-cargo": "Cargo",
diff --git a/i18n/nl.json b/i18n/nl.json
index 8758974..5451e5b 100644
--- a/i18n/nl.json
+++ b/i18n/nl.json
@@ -9,5 +9,6 @@
        "cargo-pagevalues-tablevalues": "\"$1\" waarden",
        "cargo-drilldown-or": "of",
        "cargo-viewcsv": "CSV bekijken",
+       "cargo-viewxls": "XLS bekijken",
        "cargo-viewjson": "JSON bekijken"
 }
diff --git a/specials/CargoExport.php b/specials/CargoExport.php
index 43aa0de..5356553 100644
--- a/specials/CargoExport.php
+++ b/specials/CargoExport.php
@@ -56,6 +56,12 @@
                                $filename = 'results.csv';
                        }
                        $this->displayCSVData( $sqlQueries, $delimiter, 
$filename );
+               } elseif ( $format == 'excel' ) {
+                       $filename = $req->getVal( 'filename' );
+                       if ( $filename == '' ) {
+                               $filename = 'results.xls';
+                       }
+                       $this->displayExcelData( $sqlQueries, $filename );
                } elseif ( $format == 'json' ) {
                        $this->displayJSONData( $sqlQueries );
                }
@@ -247,6 +253,29 @@
                fclose( $out );
        }
 
+       function displayExcelData( $sqlQueries, $filename ) {
+
+               // We'll only use the first query, if there's more than one.
+               $sqlQuery = $sqlQueries[0];
+               $queryResults = $sqlQuery->run();
+
+               $file = new PHPExcel();
+               $file->setActiveSheetIndex(0);
+
+               // Create array with header row and query results.
+               $header[] = array_keys( reset( $queryResults ) );
+               $rows = array_merge($header, $queryResults);
+
+               $file->getActiveSheet()->fromArray($rows, null, 'A1');
+               header("Content-Type: 
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+               header("Content-Disposition: attachment;filename=$filename");
+               header("Cache-Control: max-age=0");
+
+               $writer = PHPExcel_IOFactory::createWriter($file, 'Excel5');
+
+               $writer->save('php://output');
+       }
+
        function displayJSONData( $sqlQueries ) {
                header( "Content-Type: application/json" );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b053d5ef0decbf0b3b70a5e50c8780a8eb06e41
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