AdSvS has uploaded a new change for review. https://gerrit.wikimedia.org/r/199859
Change subject: Added the Excel export format. ...................................................................... Added the Excel export format. Change-Id: Ic3d0d21398eaac9c70de8d21606a3d17ca59cd1a --- M Cargo.php M CargoQueryDisplayer.php A formats/CargoExcelFormat.php M specials/CargoExport.php 4 files changed, 68 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo refs/changes/59/199859/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..f3b97af --- /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( 'cargo-viewcsv' )->text(); + } + $linkAttrs = array( + 'href' => $ce->getFullURL( $queryParams ), + ); + $text = Html::rawElement( 'a', $linkAttrs, $linkText ); + + return $text; + } + +} diff --git a/specials/CargoExport.php b/specials/CargoExport.php index 08ab751..67034c1 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 ); } @@ -226,8 +232,9 @@ } function displayCSVData( $sqlQueries, $delimiter, $filename ) { - header( "Content-Type: text/csv" ); + header( "Content-Type: text/csv;charset=UTF-8" ); header( "Content-Disposition: attachment; filename=$filename" ); + header( "Content-Transfer-Encoding: quoted-printable" ); // We'll only use the first query, if there's more than one. $sqlQuery = $sqlQueries[0]; @@ -240,7 +247,25 @@ } 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); + $file->getActiveSheet()->fromArray($queryResults, 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/199859 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic3d0d21398eaac9c70de8d21606a3d17ca59cd1a 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