r56360 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56359‎ | r56360 | r56361 >
Date:15:13, 15 September 2009
Author:vrandezo
Status:deferred
Tags:
Comment:
added the print request parameter infrastructure
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Table.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_PrintRequest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QP_Table.php
@@ -22,6 +22,10 @@
2323 global $smwgIQRunningNumber;
2424 SMWOutputs::requireHeadItem(SMW_HEADER_SORTTABLE);
2525
 26+ $printrequestparameters = array();
 27+ foreach ($res->getPrintRequests() as $pr)
 28+ $printrequestparameters[] = $pr->getParams();
 29+
2630 // print header
2731 $result = '<table class="smwtable"' .
2832 ('broadtable' == $this->mFormat?' width="100%"':'') .
@@ -38,8 +42,19 @@
3943 while ( $row = $res->getNext() ) {
4044 $result .= "\t<tr>\n";
4145 $firstcol = true;
 46+ $fieldcount = -1;
4247 foreach ($row as $field) {
43 - $result .= "\t\t<td>";
 48+ $fieldcount = $fieldcount + 1;
 49+
 50+ $result .= "\t\t<td";
 51+ if (array_key_exists('align', $printrequestparameters[$fieldcount])) {
 52+ $alignment = $printrequestparameters[$fieldcount]['align'];
 53+ // check the content, otherwise evil people could inject here anything they wanted
 54+ if (($alignment == 'right') || ($alignment == 'left'))
 55+ $result .= " style=\"text-align:" . $printrequestparameters[$fieldcount]['align'] . ";\"";
 56+ }
 57+ $result .= ">";
 58+
4459 $first = true;
4560 while ( ($object = $field->getNextObject()) !== false ) {
4661 if ($first) {
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -181,6 +181,7 @@
182182 global $wgContLang;
183183 $querystring = '';
184184 $printouts = array();
 185+ $lastprintout = NULL;
185186 $params = array();
186187 foreach ($rawparams as $name => $param) {
187188 if ( is_string($name) && ($name != '') ) { // accept 'name' => 'value' just as '' => 'name=value'
@@ -225,7 +226,14 @@
226227 if (count($parts) > 1) { // label found, use this instead of default
227228 $label = trim($parts[1]);
228229 }
229 - $printouts[] = new SMWPrintRequest($printmode, $label, $data, trim($propparts[1]));
 230+ $lastprintout = new SMWPrintRequest($printmode, $label, $data, trim($propparts[1]));
 231+ $printouts[] = $lastprintout;
 232+ } elseif ($param[0] == '+') { // print request parameter
 233+ if ($lastprintout != NULL) {
 234+ $param = substr($param,1);
 235+ $parts = explode('=',$param,2);
 236+ $lastprintout->setParam($parts[0], $parts[1]);
 237+ }
230238 } else { // parameter or query
231239 $parts = explode('=',$param,2);
232240 if (count($parts) >= 2) {
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_PrintRequest.php
@@ -28,15 +28,17 @@
2929 protected $m_typeid = false; // id of the datatype of the printed objects, if applicable
3030 protected $m_outputformat; // output format string for formatting results, if applicable
3131 protected $m_hash = false; // cache your hash (currently useful since SMWQueryResult accesses the hash many times, might be dropped at some point)
32 -
 32+ protected $m_params = array();
 33+
3334 /**
3435 * Create a print request.
3536 * @param $mode a constant defining what to printout
3637 * @param $label the string label to describe this printout
3738 * @param $data optional data for specifying some request, might be a property object, title, or something else; interpretation depends on $mode
3839 * @param $outputformat optional string for specifying an output format, e.g. an output unit
 40+ * @param $params optional array of further, named parameters for the print request
3941 */
40 - public function __construct($mode, $label, $data = NULL, $outputformat = false) {
 42+ public function __construct($mode, $label, $data = NULL, $outputformat = false, $params = NULL) {
4143 $this->m_mode = $mode;
4244 $this->m_label = $label;
4345 $this->m_data = $data;
@@ -47,6 +49,7 @@
4850 if ($this->m_data instanceof SMWDataValue) {
4951 $this->m_data->setCaption($label);
5052 }
 53+ if (NULL != $params) $m_params = $params;
5154 }
5255
5356 public function getMode() {
@@ -139,6 +142,9 @@
140143 * print requests. The hash also includes the chosen label,
141144 * so it is possible to print the same date with different
142145 * labels.
 146+ * TODO: For now, the params are not part of the Hash, but
 147+ * maybe they should? But the hash is used for some things,
 148+ * check that first!
143149 */
144150 public function getHash() {
145151 if ($this->m_hash === false) {
@@ -155,8 +161,14 @@
156162
157163 /**
158164 * Serialise this object like print requests given in \#ask.
 165+ * @param $params boolean that sets if the serialization should
 166+ * include the extra print request parameters
159167 */
160 - public function getSerialisation() {
 168+ public function getSerialisation($showparams = false) {
 169+ $parameters = '';
 170+ if ($showparams) foreach ( $this->m_params as $key => $value ) {
 171+ $parameters .= "|+" . $key . "=" . $value;
 172+ }
161173 switch ($this->m_mode) {
162174 case SMWPrintRequest::PRINT_CATS:
163175 global $wgContLang;
@@ -165,7 +177,7 @@
166178 if ($this->m_label != $catlabel) {
167179 $result .= '=' . $this->m_label;
168180 }
169 - return $result;
 181+ return $result . $parameters;
170182 case SMWPrintRequest::PRINT_PROP: case SMWPrintRequest::PRINT_CCAT:
171183 if ($this->m_mode == SMWPrintRequest::PRINT_CCAT) {
172184 $printname = $this->m_data->getPrefixedText();
@@ -183,9 +195,46 @@
184196 if ( $printname != $this->m_label ) {
185197 $result .= '=' . $this->m_label;
186198 }
187 - return $result;
 199+ return $result . $parameters;
188200 case SMWPrintRequest::PRINT_THIS: default: return ''; // no current serialisation
189201 }
190202 }
191203
 204+ /**
 205+ * Returns the value of a named parameter.
 206+ * @param $key string the name of the parameter key
 207+ * @return string Value of the paramer, if set (else '')
 208+ */
 209+ public function getParam($key) {
 210+ if (array_key_exists($key, $this->m_params))
 211+ return $this->m_params[$key];
 212+ else
 213+ return '';
 214+ }
 215+
 216+ /**
 217+ * Returns if a named parameter is set.
 218+ * @param $key string the name of the parameter
 219+ * @return boolean True if the parameter is set, false otherwise
 220+ */
 221+ public function hasParam($key) {
 222+ return array_key_exists($key, $this->m_params);
 223+ }
 224+
 225+ /**
 226+ * Returns the array of parameters, where a string is mapped to a string.
 227+ * @return array Map of parameter names to values.
 228+ */
 229+ public function getParams() {
 230+ return $this->m_params;
 231+ }
 232+
 233+ /**
 234+ * Sets a print request parameter.
 235+ * @param $key string Name of the parameter
 236+ * @param $value string Value for the parameter
 237+ */
 238+ public function setParam($key, $value) {
 239+ $this->m_params[$key] = $value;
 240+ }
192241 }
\ No newline at end of file

Status & tagging log