r47803 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47802‎ | r47803 | r47804 >
Date:16:05, 25 February 2009
Author:yaron
Status:deferred
Tags:
Comment:
Handling of 'template' and 'userparam' parameters added by David Loomer
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Calendar/SRF_Calendar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Calendar/SRF_Calendar.php
@@ -2,8 +2,6 @@
33
44 /**
55 * A class to print query results in a monthly calendar.
6 - *
7 - * @author Yaron Koren
86 */
97
108 if (!defined('MEDIAWIKI')) die();
@@ -13,7 +11,24 @@
1412
1513 class SRFCalendar extends SMWResultPrinter {
1614
 15+ protected $mTemplate = '';
 16+ protected $mUserParam = '';
 17+
 18+ protected function readParameters($params,$outputmode) {
 19+ SMWResultPrinter::readParameters($params,$outputmode);
 20+
 21+ if (array_key_exists('template', $params)) {
 22+ $this->mTemplate = trim($params['template']);
 23+ }
 24+ if (array_key_exists('userparam', $params)) {
 25+ $this->mUserParam = trim($params['userparam']);
 26+ }
 27+ }
 28+
1729 public function getResult($results, $params, $outputmode) {
 30+ $this->isHTML = false;
 31+ $this->hasTemplates = false;
 32+
1833 // skip checks, results with 0 entries are normal
1934 $this->readParameters($params, $outputmode);
2035 return $this->getResultText($results, SMW_OUTPUT_HTML);
@@ -28,38 +43,54 @@
2944 // print all result rows
3045 while ( $row = $res->getNext() ) {
3146 $date = $title = $text = $color = "";
32 - foreach ($row as $i => $field) {
33 - $pr = $field->getPrintRequest();
34 - if ($i == 2)
35 - $text .= " (";
36 - elseif ($i > 2)
37 - $text .= ", ";
38 - while ( ($object = $field->getNextObject()) !== false ) {
39 - if ($object->getTypeID() == '_dat') { // use shorter "LongText" for wikipage
40 - // don't add date values to the display
41 - } elseif ($object->getTypeID() == '_wpg') { // use shorter "LongText" for wikipage
42 - if ($i == 0) {
43 - $title = Title::newFromText($object->getShortWikiText(false));
 47+
 48+ if ($this->mTemplate != '') { // build template code
 49+ $this->hasTemplates = true;
 50+ if ($this->mUserParam)
 51+ $text = "|userparam=$this->mUserParam";
 52+ foreach ($row as $i => $field) {
 53+ $pr = $field->getPrintRequest();
 54+ $text .= '|' . ($i + 1) . '=';
 55+ while ( ($object = $field->getNextObject()) !== false ) {
 56+ if ($object->getTypeID() == '_dat') {
 57+ $test .= SRFCalendar::formatDateStr($object);
 58+ } elseif ($object->getTypeID() == '_wpg') { // use shorter "LongText" for wikipage
 59+ $text .= $object->getLongText($outputmode, NULL);
4460 } else {
45 - $text .= $pr->getHTMLText($skin) . " " . $object->getLongText($outputmode, $skin);
 61+ $text .= $object->getShortText($outputmode, NULL);
4662 }
47 - } else {
48 - $text .= $pr->getHTMLText($skin) . " " . $object->getShortText($outputmode, $skin);
 63+ if ($pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_dat') {
 64+ $date = SRFCalendar::formatDateStr($object);
 65+ }
4966 }
50 - if ($pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_dat') {
51 - if (method_exists('SMWTimeValue', 'getYear')) { // SMW 1.4 and higher
52 - // for some reason, getMonth() and getDay() sometimes return a number with a leading zero -
53 - // get rid of it using (int)
54 - $date = $object->getYear() . '-' . (int)$object->getMonth() . '-' . (int)$object->getDay();
 67+ }
 68+ } else { // build simple text
 69+ foreach ($row as $i => $field) {
 70+ $pr = $field->getPrintRequest();
 71+ if ($i == 2)
 72+ $text .= " (";
 73+ elseif ($i > 2)
 74+ $text .= ", ";
 75+ while ( ($object = $field->getNextObject()) !== false ) {
 76+ if ($object->getTypeID() == '_dat') { // use shorter "LongText" for wikipage
 77+ // don't add date values to the display
 78+ } elseif ($object->getTypeID() == '_wpg') { // use shorter "LongText" for wikipage
 79+ if ($i == 0) {
 80+ $title = Title::newFromText($object->getShortWikiText(false));
 81+ } else {
 82+ $text .= $pr->getHTMLText($skin) . " " . $object->getLongText($outputmode, $skin);
 83+ }
5584 } else {
56 - $date = date("Y-n-j", $event_date->getNumericValue());
 85+ $text .= $pr->getHTMLText($skin) . " " . $object->getShortText($outputmode, $skin);
5786 }
58 -
 87+ if ($pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_dat') {
 88+ $date = SRFCalendar::formatDateStr($object);
 89+ }
5990 }
6091 }
 92+ if ($i > 1)
 93+ $text .= ")";
6194 }
62 - if ($i > 1)
63 - $text .= ")";
6495 if ($date != '') {
6596 // handle the 'color=' value, whether it came
6697 // from a compound query or a regular one
@@ -94,6 +125,17 @@
95126 return wfMsg('january');
96127 }
97128
 129+
 130+ function formatDateStr($object) {
 131+ if (method_exists('SMWTimeValue', 'getYear')) { // SMW 1.4 and higher
 132+ // for some reason, getMonth() and getDay() sometimes return a number with a leading zero -
 133+ // get rid of it using (int)
 134+ return $object->getYear() . '-' . (int)$object->getMonth() . '-' . (int)$object->getDay();
 135+ } else {
 136+ return date("Y-n-j", $event_date->getNumericValue());
 137+ }
 138+ }
 139+
98140 function displayCalendar($events) {
99141 global $wgOut, $srfgScriptPath, $wgParser, $wgRequest;
100142
@@ -253,14 +295,25 @@
254296 foreach ($events as $event) {
255297 list($event_title, $other_text, $event_date, $color) = $event;
256298 if ($event_date == $date_str) {
257 - $event_name = str_replace('_', ' ', $event_title->getPrefixedDbKey());
258 - if ($color != '') {
259 - $event_url = $event_title->getPrefixedDbKey();
260 - $event_str = '<a href="' . $event_url . '" style="color: ' . $color . '">' . $event_name . "</a>";
 299+ if ($this->mTemplate != '') {
 300+ $templatetext = '{{' . $this->mTemplate . $other_text . '}}';
 301+ $templatetext = $wgParser->replaceVariables($templatetext);
 302+ $templatetext = $wgParser->recursiveTagParse($templatetext);
 303+ if ($color != '') {
 304+ $text .= '<span style="color: ' . $color . '">' . $templatetext . '</span>';
 305+ } else {
 306+ $text .= $templatetext;
 307+ }
261308 } else {
262 - $event_str = $skin->makeLinkObj($event_title, $event_name);
 309+ $event_name = str_replace('_', ' ', $event_title->getPrefixedDbKey());
 310+ if ($color != '') {
 311+ $event_url = $event_title->getPrefixedDbKey();
 312+ $event_str = '<a href="' . $event_url . '" style="color: ' . $color . '">' . $event_name . "</a>";
 313+ } else {
 314+ $event_str = $skin->makeLinkObj($event_title, $event_name);
 315+ }
 316+ $text .= "$event_str $other_text\n\n";
263317 }
264 - $text .= "$event_str $other_text\n\n";
265318 }
266319 }
267320 $text .=<<<END

Status & tagging log