r19887 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r19886‎ | r19887 | r19888 >
Date:18:04, 11 February 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Support for counting results of inline queries (format="count"); can be disabled like any other format if
performance is critical.
Query parameter debug="true" replaced by format="debug"
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php
@@ -167,7 +167,8 @@
168168 * mainlabel -- Label to use for the column that shows the main subjects. Also used to indicate that
169169 * the subject should be displayed in cases where it would normally be hidden.
170170 * format -- Either 'list', 'ul' (for unordered bullet list), 'ol' (ordered and numbered list),
171 - * 'table', 'broadtable', 'timeline', 'eventline', 'embedded', 'template' or 'auto' (default).
 171+ * 'table', 'broadtable', 'timeline', 'eventline', 'embedded', 'template', 'count',
 172+ * 'debug', or 'auto' (default).
172173 * Some formats have additional parameters:
173174 * sep (list only) -- Customized separator string.
174175 */
@@ -178,7 +179,7 @@
179180 * formats. The formats 'table' and 'list' are defaults that cannot be disabled. The format 'broadtable'
180181 * should not be disabled either in order not to break Special:ask.
181182 */
182 - static $formats = array('table','list','ol','ul','broadtable','embedded','timeline','eventline','template');
 183+ static $formats = array('table', 'list', 'ol', 'ul', 'broadtable', 'embedded', 'timeline', 'eventline', 'template', 'count', 'debug');
183184
184185 private $mInline; // is this really an inline query, i.e. are results used in an article or not? (bool)
185186
@@ -197,7 +198,6 @@
198199 private $mDefault; // default return value for empty queries
199200 private $mShowHeaders; // should the headers (property names) be printed?
200201 private $mMainLabel; // label used for displaying the subject, or NULL if none was given
201 - private $mShowDebug; // should debug output be generated?
202202
203203 // fields used during query processing:
204204 private $mQueryText; // the original query text for future reference
@@ -235,7 +235,6 @@
236236 $this->mDefault = '';
237237 $this->mShowHeaders = true;
238238 $this->mMainLabel = NULL;
239 - $this->mShowDebug = false;
240239
241240 $this->mLinker = new Linker();
242241
@@ -319,9 +318,6 @@
320319 if (array_key_exists('mainlabel', $param)) {
321320 $this->mMainLabel = htmlspecialchars($param['mainlabel']);
322321 }
323 - if (array_key_exists('debug', $param)) {
324 - $this->mShowDebug = true;
325 - }
326322 }
327323
328324 /**
@@ -489,20 +485,26 @@
490486 $this->mPrintoutCount = 0;
491487 $sq = $this->parseQuery($text);
492488
493 - $sq->mSelect[0] .= ' AS page_id';
494 - $sq->mSelect[1] .= ' AS page_title';
495 - $sq->mSelect[2] .= ' AS page_namespace';
496 -
497 - $sql_options = array('LIMIT' => $this->mLimit + 1, 'OFFSET' => $this->mOffset); // additional options (order by, limit)
498 - if ( $smwgIQSortingEnabled ) {
499 - if ( NULL == $sq->mOrderBy ) {
500 - $sql_options['ORDER BY'] = "page_title $this->mOrder "; // default
501 - } else {
502 - $sql_options['ORDER BY'] = "$sq->mOrderBy $this->mOrder ";
 489+ $sql_options = array();
 490+ if ($this->mFormat == 'count') {
 491+ $sq->mSelect = array('COUNT(' . $sq->mSelect[0] . ') AS count'); // just count
 492+ } else {
 493+ $sq->mSelect[0] .= ' AS page_id';
 494+ $sq->mSelect[1] .= ' AS page_title';
 495+ $sq->mSelect[2] .= ' AS page_namespace';
 496+
 497+ $sql_options['LIMIT'] = $this->mLimit + 1;
 498+ $sql_options['OFFSET'] = $this->mOffset;
 499+ if ( $smwgIQSortingEnabled ) {
 500+ if ( NULL == $sq->mOrderBy ) {
 501+ $sql_options['ORDER BY'] = "page_title $this->mOrder "; // default
 502+ } else {
 503+ $sql_options['ORDER BY'] = "$sq->mOrderBy $this->mOrder ";
 504+ }
503505 }
504506 }
505507
506 - if ($this->mShowDebug) {
 508+ if ($this->mFormat == 'debug') {
507509 return $sq->mDebug; // DEBUG
508510 }
509511
@@ -519,6 +521,16 @@
520522 //No results, TODO: is there a better way than calling numRows (which counts all results)?
521523 if ( (!$this->mQueryResult) || (0 == $this->dbr->numRows( $this->mQueryResult )) ) return $this->mDefault;
522524
 525+ if ($this->mFormat == 'count') {
 526+ $row = $this->dbr->fetchRow( $this->mQueryResult );
 527+ if ($row['count'] !== '0') {
 528+ return $this->mIntro . $row['count'];
 529+ } else {
 530+ if ($this->mDefault === '') $this->mDefault = '0';
 531+ return $this->mDefault;
 532+ }
 533+ }
 534+
523535 $this->mDisplayCount = min($this->mLimit, $this->dbr->numRows( $this->mQueryResult ));
524536
525537 // Cases in which to print the subject:
@@ -530,7 +542,7 @@
531543 }
532544 }
533545
534 - //Determine format if 'auto', also for backwards compatibility
 546+ // Guess suitable format if 'auto' is selected
535547 if ( 'auto' == $this->mFormat ) {
536548 if ( (count($sq->mPrint)>1) && ($this->mLimit > 0) )
537549 $this->mFormat = 'table';

Status & tagging log