00001 <?php
00017 class SRFiCalendar extends SMWResultPrinter {
00018
00019 protected $m_title;
00020 protected $m_description;
00021
00022 protected function handleParameters( array $params, $outputmode ) {
00023 parent::handleParameters( $params, $outputmode );
00024
00025 $this->m_title = trim( $params['title'] );
00026 $this->m_description = trim( $params['description'] );
00027 }
00028
00029 public function getMimeType( $res ) {
00030 return 'text/calendar';
00031 }
00032
00033 public function getFileName( $res ) {
00034 if ( $this->m_title != '' ) {
00035 return str_replace( ' ', '_', $this->m_title ) . '.ics';
00036 } else {
00037 return 'iCalendar.ics';
00038 }
00039 }
00040
00041 public function getQueryMode( $context ) {
00042 return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
00043 }
00044
00045 public function getName() {
00046 return wfMsg( 'srf_printername_icalendar' );
00047 }
00048
00049 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00050 return $outputmode == SMW_OUTPUT_FILE ? $this->getIcal( $res ) : $this->getIcalLink( $res, $outputmode );
00051 }
00052
00062 protected function getIcal( SMWQueryResult $res ) {
00063 $result = '';
00064
00065 if ( $this->m_title == '' ) {
00066 global $wgSitename;
00067 $this->m_title = $wgSitename;
00068 }
00069
00070 $result .= "BEGIN:VCALENDAR\r\n";
00071 $result .= "PRODID:-//SMW Project//Semantic Result Formats\r\n";
00072 $result .= "VERSION:2.0\r\n";
00073 $result .= "METHOD:PUBLISH\r\n";
00074 $result .= "X-WR-CALNAME:" . $this->m_title . "\r\n";
00075
00076 if ( $this->m_description !== '' ) {
00077 $result .= "X-WR-CALDESC:" . $this->m_description . "\r\n";
00078 }
00079
00080
00081
00082
00083
00084 $row = $res->getNext();
00085 while ( $row !== false ) {
00086 $result .= $this->getIcalForItem( $row );
00087 $row = $res->getNext();
00088 }
00089
00090
00091
00092 $result .= "END:VCALENDAR\r\n";
00093
00094 return $result;
00095 }
00096
00107 protected function getIcalLink( SMWQueryResult $res, $outputmode ) {
00108 if ( $this->getSearchLabel( $outputmode ) ) {
00109 $label = $this->getSearchLabel( $outputmode );
00110 } else {
00111 $label = wfMsgForContent( 'srf_icalendar_link' );
00112 }
00113
00114 $link = $res->getQueryLink( $label );
00115 $link->setParameter( 'icalendar', 'format' );
00116
00117 if ( $this->m_title !== '' ) {
00118 $link->setParameter( $this->m_title, 'title' );
00119 }
00120
00121 if ( $this->m_description !== '' ) {
00122 $link->setParameter( $this->m_description, 'description' );
00123 }
00124
00125 if ( array_key_exists( 'limit', $this->params ) ) {
00126 $link->setParameter( $this->params['limit'], 'limit' );
00127 } else {
00128 $link->setParameter( 20, 'limit' );
00129 }
00130
00131
00132 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00133 return $link->getText( $outputmode, $this->mLinker );
00134 }
00135
00145 protected function getIcalForItem( array $row ) {
00146 $result = '';
00147
00148 $wikipage = $row[0]->getResultSubject();
00149 $wikipage = SMWDataValueFactory::newDataItemValue( $wikipage, null );
00150
00151 $startdate = false;
00152 $enddate = false;
00153
00154 $params = array(
00155 'summary' => $wikipage->getShortWikiText()
00156 );
00157
00158 foreach ( $row as $field ) {
00159
00160
00161
00162 $req = $field->getPrintRequest();
00163 $label = strtolower( $req->getLabel() );
00164
00165 switch ( $label ) {
00166 case 'start': case 'end':
00167 if ( $req->getTypeID() == '_dat' ) {
00168 $params[$label] = $field->getNextDataValue();
00169 }
00170 break;
00171 case 'location': case 'description': case 'summary':
00172 $value = $field->getNextDataValue();
00173 if ( $value !== false ) {
00174 $params[$label] = $value->getShortWikiText();
00175 }
00176 break;
00177 }
00178 }
00179
00180 $title = $wikipage->getTitle();
00181 $article = new Article( $title );
00182 $url = $title->getFullURL();
00183
00184 $result .= "BEGIN:VEVENT\r\n";
00185 $result .= "SUMMARY:" . $params['summary'] . "\r\n";
00186 $result .= "URL:$url\r\n";
00187 $result .= "UID:$url\r\n";
00188
00189 if ( array_key_exists( 'start', $params ) ) $result .= "DTSTART:" . $this->parsedate( $params['start'] ) . "\r\n";
00190 if ( array_key_exists( 'end', $params ) ) $result .= "DTEND:" . $this->parsedate( $params['end'], true ) . "\r\n";
00191 if ( array_key_exists( 'location', $params ) ) $result .= "LOCATION:" . $params['location'] . "\r\n";
00192 if ( array_key_exists( 'description', $params ) ) $result .= "DESCRIPTION:" . $params['description'] . "\r\n";
00193
00194 $t = strtotime( str_replace( 'T', ' ', $article->getTimestamp() ) );
00195 $result .= "DTSTAMP:" . date( "Ymd", $t ) . "T" . date( "His", $t ) . "\r\n";
00196 $result .= "SEQUENCE:" . $title->getLatestRevID() . "\r\n";
00197 $result .= "END:VEVENT\r\n";
00198
00199 return $result;
00200 }
00201
00205 static private function parsedate( SMWTimeValue $dv, $isend = false ) {
00206 $year = $dv->getYear();
00207 if ( ( $year > 9999 ) || ( $year < -9998 ) ) return '';
00208
00209 $year = number_format( $year, 0, '.', '' );
00210 $time = str_replace( ':', '', $dv->getTimeString( false ) );
00211
00212 if ( ( $time == false ) && ( $isend ) ) {
00213 $dv = SMWDataValueFactory::newTypeIDValue( '_dat', $dv->getWikiValue() . 'T00:00:00-24:00' );
00214 }
00215
00216 $month = $dv->getMonth();
00217 if ( strlen( $month ) == 1 ) $month = '0' . $month;
00218
00219 $day = $dv->getDay();
00220 if ( strlen( $day ) == 1 ) $day = '0' . $day;
00221
00222 $result = $year . $month . $day;
00223
00224 if ( $time != false ) $result .= "T$time";
00225
00226 return $result;
00227 }
00228
00229 public function getParameters() {
00230 $params = array_merge( parent::getParameters(), $this->exportFormatParameters() );
00231
00232 $params['title'] = new Parameter( 'title' );
00233 $params['title']->setMessage( 'srf_paramdesc_icalendartitle' );
00234 $params['title']->setDefault( '' );
00235
00236 $params['description'] = new Parameter( 'description' );
00237 $params['description']->setMessage( 'srf_paramdesc_icalendardescription' );
00238 $params['description']->setDefault( '' );
00239
00240 return $params;
00241 }
00242
00243 }