r82966 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82965‎ | r82966 | r82967 >
Date:21:32, 28 February 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added DSV printer, unfinished, but basic stuff working
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/SMW_Settings.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_DSV.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_DSV.php
@@ -0,0 +1,186 @@
 2+<?php
 3+
 4+/**
 5+ * Result printer to print results in UNIX-style DSV (deliminter separated value) format.
 6+ *
 7+ * @file SMW_QP_DSV.php
 8+ * @ingroup SMWQuery
 9+ * @since 1.5.7
 10+ *
 11+ * @licence GNU GPL v3
 12+ *
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ * Based on the SMWCsvResultPrinter class.
 15+ */
 16+class SMWDSVResultPrinter extends SMWResultPrinter {
 17+
 18+ protected $separator = ':';
 19+
 20+ protected function readParameters( $params, $outputmode ) {
 21+ SMWResultPrinter::readParameters( $params, $outputmode );
 22+
 23+ if ( array_key_exists( 'separator', $this->m_params ) ) {
 24+ $this->separator = trim( $this->m_params['separator'] );
 25+ // Also support 'sep' as alias, since this is the param name for the CSV format.
 26+ } elseif ( array_key_exists( 'sep', $this->m_params ) ) {
 27+ $this->separator = trim( $this->m_params['sep'] );
 28+ }
 29+ }
 30+
 31+ public function getMimeType( $res ) {
 32+ return 'text/dsv';
 33+ }
 34+
 35+ public function getFileName( $res ) {
 36+ if ( isset( $this->m_params['filename'] ) ) {
 37+ return str_replace( ' ', '_', $this->m_params['filename'] ) . '.dsv';
 38+ } else {
 39+ return 'result.dsv';
 40+ }
 41+ }
 42+
 43+ public function getQueryMode( $context ) {
 44+ return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
 45+ }
 46+
 47+ public function getName() {
 48+ smwfLoadExtensionMessages( 'SemanticMediaWiki' );
 49+ return wfMsg( 'smw_printername_dsv' );
 50+ }
 51+
 52+ protected function getResultText( /* SMWQueryResult */ $res, $outputmode ) {
 53+ if ( $outputmode == SMW_OUTPUT_FILE ) { // Make the DSV file.
 54+ return $this->getResultFileContents( $res );
 55+ }
 56+ else { // Create a link pointing to the DSV file.
 57+ return $this->getLinkToFile( $res, $outputmode );
 58+ }
 59+ }
 60+
 61+ /**
 62+ * Returns the query result in DSV.
 63+ *
 64+ * @since 1.5.7
 65+ *
 66+ * @param SMWQueryResult $res
 67+ *
 68+ * @return string
 69+ */
 70+ protected function getResultFileContents( SMWQueryResult $res ) {
 71+ $lines = array();
 72+
 73+ if ( $this->mShowHeaders ) {
 74+ $headerItems = array();
 75+
 76+ foreach ( $res->getPrintRequests() as $pr ) {
 77+ $headerItems[] = $pr->getLabel();
 78+ }
 79+
 80+ $lines[] = $this->getDSVLine( $headerItems );
 81+ }
 82+
 83+ // Loop over the result objects (pages).
 84+ while ( $row = $res->getNext() ) {
 85+ $rowItems = array();
 86+
 87+ // Loop over their fields (properties).
 88+ foreach ( $row as $field ) {
 89+ $itemSegments = array();
 90+
 91+ // Loop over all values for the property.
 92+ while ( ( $object = $field->getNextObject() ) !== false ) {
 93+ $itemSegments[] = Sanitizer::decodeCharReferences( $object->getWikiValue() );
 94+ }
 95+
 96+ // Join all values into a single string, separating them with comma's.
 97+ $rowItems[] = implode( ',', $itemSegments );
 98+ }
 99+
 100+ $lines[] = $this->getDSVLine( $rowItems );
 101+ }
 102+
 103+ return implode( "\n", $lines );
 104+ }
 105+
 106+ /**
 107+ * Returns a single DSV line.
 108+ *
 109+ * @since 1.5.7
 110+ *
 111+ * @param array $fields
 112+ *
 113+ * @return string
 114+ */
 115+ protected function getDSVLine( array $fields ) {
 116+ return implode( $this->separator, array_map( array( $this, 'encodeDSV' ), $fields ) );
 117+ }
 118+
 119+ /**
 120+ * Encodes a single DSV.
 121+ *
 122+ * @since 1.5.7
 123+ *
 124+ * @param string $value
 125+ *
 126+ * @return string
 127+ */
 128+ protected function encodeDSV( $value ) {
 129+ // TODO
 130+ // \n for a newline, \r for a carriage return, \t for a tab, \b for backspace, \f for formfeed
 131+ // \e for ASCII escape
 132+ // \nnn or \onnn or \0nnn for the character with octal value nnn
 133+ // \xnn for the character with hexadecimal value nn
 134+ // \dnnn for the character with decimal value nnn
 135+ // \unnnn for a hexadecimal Unicode literal.
 136+ return str_replace( array( '\\', $this->separator ), array( '\\\\', "\\$this->separator" ), $value );
 137+ }
 138+
 139+ /**
 140+ * Returns html for a link to a query that returns the DSV file.
 141+ *
 142+ * @since 1.5.7
 143+ *
 144+ * @param SMWQueryResult $res
 145+ * @param $outputmode
 146+ *
 147+ * @return string
 148+ */
 149+ protected function getLinkToFile( SMWQueryResult $res, $outputmode ) {
 150+ if ( $this->getSearchLabel( $outputmode ) ) {
 151+ $label = $this->getSearchLabel( $outputmode );
 152+ } else {
 153+ smwfLoadExtensionMessages( 'SemanticMediaWiki' );
 154+ $label = wfMsgForContent( 'smw_dsv_link' );
 155+ }
 156+
 157+ $link = $res->getQueryLink( $label );
 158+ $link->setParameter( 'dsv', 'format' );
 159+ $link->setParameter( $this->separator, 'sep' );
 160+
 161+ if ( array_key_exists( 'mainlabel', $this->m_params ) ) {
 162+ $link->setParameter( $this->m_params['mainlabel'], 'mainlabel' );
 163+ }
 164+
 165+ $link->setParameter( $this->mShowHeaders ? 'show' : 'hide', 'headers' );
 166+
 167+ if ( array_key_exists( 'limit', $this->m_params ) ) {
 168+ $link->setParameter( $this->m_params['limit'], 'limit' );
 169+ } else { // Use a reasonable default limit
 170+ $link->setParameter( 100, 'limit' );
 171+ }
 172+
 173+ // yes, our code can be viewed as HTML if requested, no more parsing needed
 174+ $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
 175+ return $link->getText( $outputmode, $this->mLinker );
 176+ }
 177+
 178+ public function getParameters() {
 179+ $params = parent::exportFormatParameters();
 180+
 181+ $params[] = array( 'name' => 'separator', 'type' => 'string', 'description' => wfMsg( 'smw-paramdesc-dsv-separator' ) );
 182+ $params[] = array( 'name' => 'filename', 'type' => 'string', 'description' => wfMsg( 'smw-paramdesc-dsv-filename' ) );
 183+
 184+ return $params;
 185+ }
 186+
 187+}
Property changes on: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_DSV.php
___________________________________________________________________
Added: svn:eol-style
1188 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -36,10 +36,10 @@
3737 global $smwgIP, $smwgNamespace, $wgJobClasses, $wgExtensionAliasesFiles, $wgServer;
3838 global $wgResourceModules, $smwgScriptPath;
3939
40 - $wgFooterIcons["poweredby"]["semanticmediawiki"] = array(
41 - "src" => null,
42 - "url" => "http://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki",
43 - "alt" => "Powered by Semantic MediaWiki",
 40+ $wgFooterIcons['poweredby']['semanticmediawiki'] = array(
 41+ 'src' => null,
 42+ 'url' => 'http://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki',
 43+ 'alt' => 'Powered by Semantic MediaWiki',
4444 );
4545
4646 // The dot tells that the domain is not complete. It will be completed
@@ -126,6 +126,7 @@
127127 $wgAutoloadClasses['SMWEmbeddedResultPrinter'] = $qpDir . 'SMW_QP_Embedded.php';
128128 $wgAutoloadClasses['SMWRSSResultPrinter'] = $qpDir . 'SMW_QP_RSSlink.php';
129129 $wgAutoloadClasses['SMWCsvResultPrinter'] = $qpDir . 'SMW_QP_CSV.php';
 130+ $wgAutoloadClasses['SMWDSVResultPrinter'] = $qpDir . 'SMW_QP_DSV.php';
130131 $wgAutoloadClasses['SMWJSONResultPrinter'] = $qpDir . 'SMW_QP_JSONlink.php';
131132 $wgAutoloadClasses['SMWRDFResultPrinter'] = $qpDir . 'SMW_QP_RDF.php';
132133
Index: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
@@ -88,7 +88,7 @@
8989 'smw_paramdesc_rdfsyntax' => 'The RDF syntax to be used',
9090 'smw-paramdesc-csv-sep' => 'The separator to use',
9191 'smw-paramdesc-dsv-separator' => 'The separator to use',
92 - 'smw-paramdesc-dsv-title' => 'The name for the DSV file (without the .dsv part)',
 92+ 'smw-paramdesc-dsv-filename' => 'The name for the DSV file (without the .dsv part)',
9393
9494 // Messages and strings for inline queries
9595 'smw_iq_disabled' => "Semantic queries have been disabled for this wiki.",
Index: trunk/extensions/SemanticMediaWiki/SMW_Settings.php
@@ -295,6 +295,7 @@
296296 'debug' => 'SMWListResultPrinter',
297297 'rss' => 'SMWRSSResultPrinter',
298298 'csv' => 'SMWCsvResultPrinter',
 299+ 'dsv' => 'SMWDSVResultPrinter',
299300 'json' => 'SMWJSONResultPrinter',
300301 'rdf' => 'SMWRDFResultPrinter'
301302 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r82970follow up to r82966jeroendedauw22:10, 28 February 2011

Status & tagging log