r19886 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r19885‎ | r19886 | r19887 >
Date:17:27, 11 February 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Enabled template-based output formatting for inline queries.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_InlineQueries.php
@@ -123,7 +123,7 @@
124124
125125 /**
126126 * A simple data container for storing the essential components of SQL queries.
127 - * Used to have a struct as convenient reutrn value to functions that construct queries.
 127+ * Used to have a struct as convenient return value to functions that construct queries.
128128 */
129129 class SMWSQLQuery {
130130 public $mConditions; // SQL conditions as a string
@@ -132,9 +132,9 @@
133133 public $mOrderBy; // NULL or name of a single field by which the result should be ordered
134134 public $mFixedSubject; // true if the subject of the query has been given explicitly
135135 public $mPrint; // array of things to print; format: id=>array(label,mode[,namestring[,datavalue]])
136 -
 136+
137137 public $mDebug; // some field for debugging //DEBUG
138 -
 138+
139139 function SMWSQLQuery() {
140140 $this->mConditions = '';
141141 $this->mTables = '';
@@ -167,7 +167,7 @@
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', or 'auto' (default).
 171+ * 'table', 'broadtable', 'timeline', 'eventline', 'embedded', 'template' or 'auto' (default).
172172 * Some formats have additional parameters:
173173 * sep (list only) -- Customized separator string.
174174 */
@@ -178,7 +178,7 @@
179179 * formats. The formats 'table' and 'list' are defaults that cannot be disabled. The format 'broadtable'
180180 * should not be disabled either in order not to break Special:ask.
181181 */
182 - static $formats = array('table','list','ol','ul','broadtable','embedded','timeline','eventline');
 182+ static $formats = array('table','list','ol','ul','broadtable','embedded','timeline','eventline','template');
183183
184184 private $mInline; // is this really an inline query, i.e. are results used in an article or not? (bool)
185185
@@ -214,6 +214,7 @@
215215 private $mQueryResult; // retrieved query result
216216
217217 // other stuff
 218+ private $mHTMLPrinter; // is the current printer expecting HTML (true) or wikitext (false) for labels?
218219 private $mLinker; // we make our own linker for creating the links -- TODO: is this bad?
219220
220221 public function SMWInlineQuery($param = array(), $inline = true) {
@@ -274,6 +275,15 @@
275276 if ( !in_array($this->mFormat,SMWInlineQuery::$formats) )
276277 $this->mFormat = 'auto'; // If it is an unknown format, default to list again
277278 }
 279+ // TODO: of course the printer should specify the following, but links are currently
 280+ // created during query parsing, while printers might be selected only after this.
 281+ // This will be fixed by cleaning up the query parsing code to not create full links
 282+ // at a later stage.
 283+ if ($this->mFormat == 'template') {
 284+ $this->mHTMLPrinter = false;
 285+ } else {
 286+ $this->mHTMLPrinter = true;
 287+ }
278288 if (array_key_exists('intro', $param)) {
279289 $this->mIntro = htmlspecialchars(str_replace('_', ' ', $param['intro']));
280290 }
@@ -540,6 +550,9 @@
541551 case 'embedded':
542552 $printer = new SMWEmbeddedPrinter($this,$sq);
543553 break;
 554+ case 'template':
 555+ $printer = new SMWTemplatePrinter($this,$sq);
 556+ break;
544557 default: $printer = new SMWListPrinter($this,$sq);
545558 }
546559 $result = $printer->printResult();
@@ -901,14 +914,14 @@
902915
903916 /**
904917 * Create output string for an article title (possibly including namespace)
905 - * as given by $text.
 918+ * as given by $text.
906919 *
907920 * $subject states whether the given title is the subject (to which special
908921 * settings for linking apply).
909922 * If $label is null the standard label of the given article will be used.
910923 * If $label is the empty string, an empty string is returned.
911924 * $linked states whether the result should be a hyperlink
912 - * $exists states whether $text is known to be anexisting article, in which
 925+ * $exists states whether $text is known to be an existing article, in which
913926 * case we can save a DB lookup when creating links.
914927 */
915928 public function makeTitleString($text,$label,$linked,$exists=false) {
@@ -918,9 +931,13 @@
919932 return $text; // TODO maybe report an error here?
920933 } elseif ( $linked ) {
921934 if ( NULL === $label ) $label = $title->getText();
922 - if ($exists)
923 - return $this->mLinker->makeKnownLinkObj($title, $label);
924 - else return $this->mLinker->makeLinkObj($title, $label);
 935+ if ($this->mHTMLPrinter) {
 936+ if ($exists)
 937+ return $this->mLinker->makeKnownLinkObj($title, $label);
 938+ else return $this->mLinker->makeLinkObj($title, $label);
 939+ } else {
 940+ return '[[' . $title->getPrefixedText() . '|' . $label . ']]';
 941+ }
925942 } else {
926943 return $title->getText(); // TODO: shouldn't this default to $label?
927944 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryPrinters.php
@@ -419,7 +419,7 @@
420420 }
421421 $article_namespace = $cur[1];
422422 $article_title = $cur[2]; // TODO: ouch .. we need to check whether this exists
423 - if ((NULL != $article_title) and ($article_title != '')) {
 423+ if ( (NULL != $article_title) and ('' != $article_title) ) {
424424 $title_text = $article_namespace;
425425 if ($title_text != '') $title_text .= ':';
426426 $title_text .= $article_title;
@@ -453,4 +453,67 @@
454454 }
455455 }
456456
 457+/**
 458+ * Printer for template data. Passes a result a result row as anonymous parameters to
 459+ * a given template (which might ignore them or not) and prints the result. Values
 460+ */
 461+class SMWTemplatePrinter implements SMWQueryPrinter {
 462+ private $mIQ; // the querying object that called the printer
 463+ private $mQuery; // the query that was executed and whose results are to be printed
 464+
 465+ public function SMWTemplatePrinter($iq, $query) {
 466+ $this->mIQ = $iq;
 467+ $this->mQuery = $query;
 468+ }
 469+
 470+ public function printResult() {
 471+ // handle factbox
 472+ global $smwgStoreActive, $wgTitle;
 473+ $old_smwgStoreActive = $smwgStoreActive;
 474+ $smwgStoreActive = false; // no annotations stored, no factbox printed
 475+
 476+ // print all result rows
 477+ $result = $this->mIQ->getIntro();
 478+ $params = $this->mIQ->getParameters();
 479+ if (array_key_exists('template', $params)) {
 480+ $templatename = $params['template'];
 481+ } else {
 482+ return 'Please set parameter "template" for <ask> to work.'; // TODO: internationalise
 483+ }
 484+
 485+ $parser_options = new ParserOptions();
 486+ $parser_options->setEditSection(false); // embedded sections should not have edit links
 487+ $parser = new Parser();
 488+ while ( $row = $this->mIQ->getNextRow() ) {
 489+ $wikitext = '';
 490+ $firstcol = true;
 491+ foreach ($this->mQuery->mPrint as $print_data) {
 492+ $iterator = $this->mIQ->getIterator($print_data,$row,$firstcol);
 493+ $wikitext .= "|";
 494+ $first = true;
 495+ while ($cur = $iterator->getNext()) {
 496+ if ($first) $first = false; else $wikitext .= ', ';
 497+ $wikitext .= $cur[0];
 498+ }
 499+ $firstcol = false;
 500+ }
 501+ $parserOutput = $parser->parse('{{' . $templatename . $wikitext . '}}', $wgTitle, $parser_options);
 502+ $result .= $parserOutput->getText();
 503+ }
 504+ // show link to more results
 505+ if ($this->mIQ->isInline() && $this->mIQ->hasFurtherResults()) {
 506+ $label = $this->mIQ->getSearchLabel();
 507+ if ($label === NULL) { //apply defaults
 508+ $label = wfMsgForContent('smw_iq_moreresults');
 509+ }
 510+ if ($label != '') {
 511+ $result .= '<a href="' . $this->mIQ->getQueryURL() . '">' . $label . '</a>';
 512+ }
 513+ }
 514+
 515+ $smwgStoreActive = $old_smwgStoreActive;
 516+ return $result;
 517+ }
 518+}
 519+
457520 ?>
\ No newline at end of file

Status & tagging log