00001 <?php
00016 class SMWCsvResultPrinter extends SMWResultPrinter {
00017
00018 protected $m_sep;
00019
00028 protected function handleParameters( array $params, $outputmode ) {
00029 parent::handleParameters( $params, $outputmode );
00030
00031 $this->m_sep = str_replace( '_', ' ', $params['sep'] );
00032 }
00033
00034 public function getMimeType( $res ) {
00035 return 'text/csv';
00036 }
00037
00038 public function getFileName( $res ) {
00039 return 'result.csv';
00040 }
00041
00042 public function getQueryMode( $context ) {
00043 return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
00044 }
00045
00046 public function getName() {
00047 return wfMsg( 'smw_printername_csv' );
00048 }
00049
00050 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00051 $result = '';
00052
00053 if ( $outputmode == SMW_OUTPUT_FILE ) {
00054 $csv = fopen( 'php://temp', 'r+' );
00055
00056 if ( $this->mShowHeaders ) {
00057 $header_items = array();
00058
00059 foreach ( $res->getPrintRequests() as $pr ) {
00060 $header_items[] = $pr->getLabel();
00061 }
00062
00063 fputcsv( $csv, $header_items, $this->m_sep );
00064 }
00065
00066 while ( $row = $res->getNext() ) {
00067 $row_items = array();
00068
00069 foreach ( $row as $field ) {
00070 $growing = array();
00071
00072 while ( ( $object = $field->getNextDataValue() ) !== false ) {
00073 $growing[] = Sanitizer::decodeCharReferences( $object->getWikiValue() );
00074 }
00075
00076 $row_items[] = implode( ',', $growing );
00077 }
00078
00079 fputcsv( $csv, $row_items, $this->m_sep );
00080 }
00081
00082 rewind( $csv );
00083 $result .= stream_get_contents( $csv );
00084 } else {
00085 if ( $this->getSearchLabel( $outputmode ) ) {
00086 $label = $this->getSearchLabel( $outputmode );
00087 } else {
00088 $label = wfMsgForContent( 'smw_csv_link' );
00089 }
00090
00091 $link = $res->getQueryLink( $label );
00092 $link->setParameter( 'csv', 'format' );
00093 $link->setParameter( $this->m_sep, 'sep' );
00094
00095 if ( array_key_exists( 'mainlabel', $this->params ) && $this->params['mainlabel'] !== false ) {
00096 $link->setParameter( $this->params['mainlabel'], 'mainlabel' );
00097 }
00098
00099 $link->setParameter( $this->mShowHeaders ? 'show' : 'hide', 'headers' );
00100
00101 if ( array_key_exists( 'limit', $this->params ) ) {
00102 $link->setParameter( $this->params['limit'], 'limit' );
00103 } else {
00104 $link->setParameter( 100, 'limit' );
00105 }
00106
00107 $result .= $link->getText( $outputmode, $this->mLinker );
00108 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00109 }
00110 return $result;
00111 }
00112
00113 public function getParameters() {
00114 $params = array_merge( parent::getParameters(), $this->exportFormatParameters() );
00115
00116 $params['sep'] = new Parameter( 'sep' );
00117 $params['sep']->setMessage( 'smw-paramdesc-csv-sep' );
00118 $params['sep']->setDefault( ',' );
00119
00120 return $params;
00121 }
00122
00123 }