r49213 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49212‎ | r49213 | r49214 >
Date:15:25, 5 April 2009
Author:mkroetzsch
Status:deferred
Tags:
Comment:
Updated mechanims for selecting result printers; selected printer no longer depending on eventual query result: a new printer for format="auto" makes
this choice later if needed
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Auto.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_CSV.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_RSSlink.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinter.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialAsk.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_SpecialAsk.php
@@ -167,8 +167,7 @@
168168 if ('' != $this->m_params['order']) $urltail .= '&order=' . $this->m_params['order'];
169169
170170 if ($this->m_querystring != '') {
171 - $queryobj = SMWQueryProcessor::createQuery($this->m_querystring, $this->m_params, false, '', $this->m_printouts);
172 - $queryobj->querymode = SMWQuery::MODE_INSTANCES; ///TODO: Somewhat hacky (just as the query mode computation in SMWQueryProcessor::createQuery!)
 171+ $queryobj = SMWQueryProcessor::createQuery($this->m_querystring, $this->m_params, SMWQueryProcessor::SPECIAL_PAGE , $this->m_params['format'], $this->m_printouts);
173172 $res = smwfGetStore()->getQueryResult($queryobj);
174173 // try to be smart for rss/ical if no description/title is given and we have a concept query:
175174 if ($this->m_params['format'] == 'rss') {
@@ -191,7 +190,7 @@
192191 }
193192 }
194193 }
195 - $printer = SMWQueryProcessor::getResultPrinter($this->m_params['format'], SMWQueryProcessor::SPECIAL_PAGE, $res);
 194+ $printer = SMWQueryProcessor::getResultPrinter($this->m_params['format'], SMWQueryProcessor::SPECIAL_PAGE);
196195 $result_mime = $printer->getMimeType($res);
197196 if ($result_mime == false) {
198197 if ($res->getCount() > 0) {
@@ -220,7 +219,7 @@
221220 $wgOut->disable();
222221 header( "Content-type: $result_mime; charset=UTF-8" );
223222 if ($result_name !== false) {
224 - header( "Content-Disposition: attachment; filename=$result_name");
 223+ header( "content-disposition: attachment; filename=$result_name");
225224 }
226225 print $result;
227226 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -31,10 +31,6 @@
3232 * The format string is used to specify the output format if already
3333 * known. Otherwise it will be determined from the parameters when
3434 * needed. This parameter is just for optimisation in a common case.
35 - *
36 - * @todo This method contains too many special cases for certain
37 - * printouts. Especially the case of rss, icalendar, etc. (no query)
38 - * should be specified differently.
3935 */
4036 static public function createQuery($querystring, $params, $context = SMWQueryProcessor::INLINE_QUERY, $format = '', $extraprintouts = array()) {
4137 global $smwgQDefaultNamespaces, $smwgQFeatures, $smwgQConceptFeatures;
@@ -56,10 +52,9 @@
5753 $querymode = SMWQuery::MODE_COUNT;
5854 } elseif ($format == 'debug') {
5955 $querymode = SMWQuery::MODE_DEBUG;
60 - } elseif (in_array($format, array('rss','icalendar','vcard','csv','json'))) {
61 - $querymode = SMWQuery::MODE_NONE;
6256 } else {
63 - $querymode = SMWQuery::MODE_INSTANCES;
 57+ $printer = SMWQueryProcessor::getResultPrinter($format, $context);
 58+ $querymode = $printer->getQueryMode($context);
6459 }
6560
6661 if (array_key_exists('mainlabel', $params)) {
@@ -282,12 +277,12 @@
283278
284279 static public function getResultFromQuery($query, $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '') {
285280 wfProfileIn('SMWQueryProcessor::getResultFromQuery (SMW)');
286 - if ($format == '') {
287 - $format = SMWQueryProcessor::getResultFormat($params);
288 - }
289281 $res = smwfGetStore()->getQueryResult($query);
290282 if ( ($query->querymode == SMWQuery::MODE_INSTANCES) || ($query->querymode == SMWQuery::MODE_NONE) ) {
291283 wfProfileIn('SMWQueryProcessor::getResultFromQuery-printout (SMW)');
 284+ if ($format == '') {
 285+ $format = SMWQueryProcessor::getResultFormat($params);
 286+ }
292287 $printer = SMWQueryProcessor::getResultPrinter($format, $context, $res);
293288 $result = $printer->getResult($res, $params, $outputmode);
294289 wfProfileOut('SMWQueryProcessor::getResultFromQuery-printout (SMW)');
@@ -315,19 +310,13 @@
316311 }
317312
318313 /**
319 - * Find suitable SMWResultPrinter for the given format.
 314+ * Find suitable SMWResultPrinter for the given format. The context in which the query is to be
 315+ * used determines some basic settings of the returned printer object. Possible contexts are
 316+ * SMWQueryProcessor::SPECIAL_PAGE, SMWQueryProcessor::INLINE_QUERY, SMWQueryProcessor::CONCEPT_DESC.
320317 */
321 - static public function getResultPrinter($format,$context,$res) {
322 - if ( 'auto' == $format ) {
323 - if ( ($res->getColumnCount()>1) && ($res->getColumnCount()>0) )
324 - $format = 'table';
325 - else $format = 'list';
326 - }
 318+ static public function getResultPrinter($format, $context = SMWQueryProcessor::SPECIAL_PAGE) {
327319 global $smwgResultFormats;
328 - if (array_key_exists($format, $smwgResultFormats))
329 - $formatclass = $smwgResultFormats[$format];
330 - else
331 - $formatclass = "SMWListResultPrinter";
 320+ $formatclass = (array_key_exists($format, $smwgResultFormats))?$smwgResultFormats[$format]:'SMWAutoResultPrinter';
332321 return new $formatclass($format, ($context != SMWQueryProcessor::SPECIAL_PAGE));
333322 }
334323
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Auto.php
@@ -0,0 +1,33 @@
 2+<?php
 3+/**
 4+ * Print query results in tables or lists, depending on their shape.
 5+ * This implements the automatic printer selection used in SMW if no
 6+ * query format is specified.
 7+ * @author Markus Krötzsch
 8+ * @file
 9+ * @ingroup SMWQuery
 10+ */
 11+
 12+/**
 13+ * New implementation of SMW's printer for automatically selecting the
 14+ * format for printing a result.
 15+ *
 16+ * @ingroup SMWQuery
 17+ */
 18+class SMWAutoResultPrinter extends SMWResultPrinter {
 19+
 20+ public function getResult($results, $params, $outputmode) {
 21+ if ( ($results->getColumnCount()>1) && ($results->getColumnCount()>0) ) {
 22+ $format = 'table';
 23+ } else {
 24+ $format = 'list';
 25+ }
 26+ $printer = SMWQueryProcessor::getResultPrinter($format, ($this->mInline?SMWQueryProcessor::INLINE_QUERY:SMWQueryProcessor::SPECIAL_PAGE));
 27+ return $printer->getResult($results, $params, $outputmode);
 28+ }
 29+
 30+ protected function getResultText($res, $outputmode) {
 31+ return ''; // acutally not needed in this implementation
 32+ }
 33+
 34+}
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Auto.php
___________________________________________________________________
Added: svn:eol-style
135 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_JSONlink.php
@@ -26,6 +26,10 @@
2727 }
2828 }
2929
 30+ public function getQueryMode($context) {
 31+ return ($context==SMWQueryProcessor::SPECIAL_PAGE)?SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
 32+ }
 33+
3034 protected function getResultText($res, $outputmode) {
3135 global $smwgIQRunningNumber, $wgSitename, $wgServer, $wgScriptPath;
3236 $result = '';
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinter.php
@@ -267,16 +267,31 @@
268268 * standalone files are produced.
269269 *
270270 * If this function returns something other than FALSE, then the printer will
271 - * not be regarded as a printer that displays in-line results. In in-line mode,
272 - * queries to that printer will not be executed, but behave as if the user
273 - * would have set limit=-1. This saves effort for printers that do not show
274 - * results in-line anyway, even if they would be part of the result.
 271+ * not be regarded as a printer that displays in-line results. This is used to
 272+ * determine if a file output should be generated in Special:Ask.
275273 */
276274 public function getMimeType($res) {
277275 return false;
278276 }
279277
280278 /**
 279+ * This function determines the query mode that is to be used for this printer in
 280+ * various contexts. The query mode influences how queries to that printer should
 281+ * be processed to obtain a result. Possible values are SMWQuery::MODE_INSTANCES
 282+ * (retrieve instances), SMWQuery::MODE_NONE (do nothing), SMWQuery::MODE_COUNT
 283+ * (get number of results), SMWQuery::MODE_DEBUG (return debugging text).
 284+ * Possible values for context are SMWQueryProcessor::SPECIAL_PAGE,
 285+ * SMWQueryProcessor::INLINE_QUERY, SMWQueryProcessor::CONCEPT_DESC.
 286+ *
 287+ * The default implementation always returns SMWQuery::MODE_INSTANCES. File exports
 288+ * like RSS will use MODE_INSTANCES on special pages (so that instances are
 289+ * retrieved for the export) and MODE_NONE otherwise (displaying just a download link).
 290+ */
 291+ public function getQueryMode($context) {
 292+ return SMWQuery::MODE_INSTANCES;
 293+ }
 294+
 295+ /**
281296 * Some printers can produce not only embeddable HTML or Wikitext, but
282297 * can also produce stand-alone files. An example is RSS or iCalendar.
283298 * This function returns a filename that is to be sent to the caller
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_RSSlink.php
@@ -36,6 +36,10 @@
3737 return 'application/rss+xml'; // or is rdf+xml better? Might be confused in either case (with RSS2.0 or RDF)
3838 }
3939
 40+ public function getQueryMode($context) {
 41+ return ($context==SMWQueryProcessor::SPECIAL_PAGE)?SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
 42+ }
 43+
4044 protected function getResultText($res, $outputmode) {
4145 global $smwgIQRunningNumber, $wgSitename, $wgServer, $smwgRSSEnabled, $wgRequest;
4246 $result = '';
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_GlobalFunctions.php
@@ -117,6 +117,7 @@
118118 $wgAutoloadClasses['SMWConceptPage'] = $smwgIP . '/includes/articlepages/SMW_ConceptPage.php';
119119 //// printers
120120 $wgAutoloadClasses['SMWResultPrinter'] = $smwgIP . '/includes/SMW_QueryPrinter.php';
 121+ $wgAutoloadClasses['SMWAutoResultPrinter'] = $smwgIP . '/includes/SMW_QP_Auto.php';
121122 $wgAutoloadClasses['SMWTableResultPrinter'] = $smwgIP . '/includes/SMW_QP_Table.php';
122123 $wgAutoloadClasses['SMWListResultPrinter'] = $smwgIP . '/includes/SMW_QP_List.php';
123124 $wgAutoloadClasses['SMWEmbeddedResultPrinter'] = $smwgIP . '/includes/SMW_QP_Embedded.php';
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_CSV.php
@@ -31,6 +31,10 @@
3232 return 'result.csv';
3333 }
3434
 35+ public function getQueryMode($context) {
 36+ return ($context==SMWQueryProcessor::SPECIAL_PAGE)?SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
 37+ }
 38+
3539 protected function getResultText($res, $outputmode) {
3640 $result = '';
3741 if ($outputmode == SMW_OUTPUT_FILE) { // make CSV file

Status & tagging log