00001 <?php
00016 class SMWRSSResultPrinter extends SMWResultPrinter {
00017
00018 protected $m_title = '';
00019 protected $m_description = '';
00020
00029 protected function handleParameters( array $params, $outputmode ) {
00030 parent::handleParameters( $params, $outputmode );
00031
00032 $this->m_title = trim( $params['title'] );
00033 $this->m_description = trim( $params['description'] );
00034 }
00035
00036 public function getMimeType( $res ) {
00037
00038 return 'application/rss+xml';
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( 'smw_printername_rss' );
00047 }
00048
00049 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00050 global $smwgIQRunningNumber, $wgSitename, $wgServer, $smwgRSSEnabled, $wgRequest;
00051 $result = '';
00052 if ( $outputmode == SMW_OUTPUT_FILE ) {
00053 if ( !$smwgRSSEnabled ) return '';
00054 if ( $this->m_title === '' ) {
00055 $this->m_title = $wgSitename;
00056 }
00057 if ( $this->m_description === '' ) {
00058 $this->m_description = wfMsg( 'smw_rss_description', $wgSitename );
00059 }
00060
00061
00062 $items = array();
00063 $row = $res->getNext();
00064 while ( $row !== false ) {
00065 $creators = array();
00066 $dates = array();
00067 $wikipage = $row[0]->getNextDataValue();
00068 foreach ( $row as $field ) {
00069
00070
00071
00072 $req = $field->getPrintRequest();
00073 if ( strtolower( $req->getLabel() ) == 'creator' ) {
00074 while ( $entry = $field->getNextDataValue() ) {
00075 $creators[] = $entry->getShortWikiText();
00076 }
00077 } elseif ( ( strtolower( $req->getLabel() ) == 'date' ) && ( $req->getTypeID() == '_dat' ) ) {
00078 while ( $entry = $field->getNextDataValue() ) {
00079 $dates[] = $entry->getXMLSchemaDate();
00080 }
00081 }
00082 }
00083 if ( $wikipage instanceof SMWWikiPageValue ) {
00085 $items[] = new SMWRSSItem( $wikipage->getTitle(), $creators, $dates );
00086 }
00087 $row = $res->getNext();
00088 }
00089
00090 $result .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
00091 $result .= "<rdf:RDF\n";
00092 $result .= "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
00093 $result .= "\txmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n";
00094 $result .= "\txmlns:admin=\"http://webns.net/mvcb/\"\n";
00095 $result .= "\txmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n";
00096 $result .= "\txmlns=\"http://purl.org/rss/1.0/\">\n";
00097 $result .= "\t<channel rdf:about=\"" . str_replace( '&', '&', $wgRequest->getFullRequestURL() ) . "\">\n";
00098 $result .= "\t\t<admin:generatorAgent rdf:resource=\"http://semantic-mediawiki.org/wiki/Special:URIResolver/Semantic_MediaWiki\"/>\n";
00099 $result .= "\t\t<title>" . smwfXMLContentEncode( $this->m_title ) . "</title>\n";
00100 $result .= "\t\t<link>$wgServer</link>\n";
00101 $result .= "\t\t<description>" . smwfXMLContentEncode( $this->m_description ) . "</description>\n";
00102 if ( count( $items ) > 0 ) {
00103 $result .= "\t\t<items>\n";
00104 $result .= "\t\t\t<rdf:Seq>\n";
00105 foreach ( $items as $item ) {
00106 $result .= "\t\t\t\t<rdf:li rdf:resource=\"" . $item->uri() . "\"/>\n";
00107 }
00108 $result .= "\t\t\t</rdf:Seq>\n";
00109 $result .= "\t\t</items>\n";
00110 }
00111 $result .= "\t</channel>\n";
00112 foreach ( $items as $item ) {
00113 $result .= $item->text();
00114 }
00115 $result .= '</rdf:RDF>';
00116 } else {
00117 if ( $this->getSearchLabel( $outputmode ) ) {
00118 $label = $this->getSearchLabel( $outputmode );
00119 } else {
00120 $label = wfMsgForContent( 'smw_rss_link' );
00121 }
00122 $link = $res->getQueryLink( $label );
00123 $link->setParameter( 'rss', 'format' );
00124 if ( $this->m_title !== '' ) {
00125 $link->setParameter( $this->m_title, 'title' );
00126 }
00127 if ( $this->m_description !== '' ) {
00128 $link->setParameter( $this->m_description, 'description' );
00129 }
00130 if ( array_key_exists( 'limit', $this->m_params ) ) {
00131 $link->setParameter( $this->m_params['limit'], 'limit' );
00132 } else {
00133 $link->setParameter( 10, 'limit' );
00134 }
00135
00136 foreach ( $res->getPrintRequests() as $printout ) {
00137 if ( ( $printout->getMode() == SMWPrintRequest::PRINT_PROP ) && ( strtolower( $printout->getLabel() ) == "date" ) && ( $printout->getTypeID() == "_dat" ) ) {
00138 $link->setParameter( $printout->getData()->getWikiValue(), 'sort' );
00139 }
00140 }
00141
00142 $result .= $link->getText( $outputmode, $this->mLinker );
00143 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00144 SMWOutputs::requireHeadItem( 'rss' . $smwgIQRunningNumber, '<link rel="alternate" type="application/rss+xml" title="' . $this->m_title . '" href="' . $link->getURL() . '" />' );
00145 }
00146
00147 return $result;
00148 }
00149
00150 public function getParameters() {
00151 $params = array_merge( parent::getParameters(), $this->exportFormatParameters() );
00152
00153 $params['title'] = new Parameter( 'title' );
00154 $params['title']->setMessage( 'smw_paramdesc_rsstitle' );
00155 $params['title']->setDefault( '' );
00156
00157 $params['description'] = new Parameter( 'description' );
00158 $params['description']->setMessage( 'smw_paramdesc_rssdescription' );
00159 $params['description']->setDefault( '' );
00160
00161 return $params;
00162 }
00163
00164 }
00165
00166
00173 class SMWRSSItem {
00174
00175 private $uri;
00176 private $label;
00177 private $creator;
00178 private $date;
00179 private $articlename;
00180 private $title;
00181
00185 public function __construct( Title $t, $c, $d ) {
00186 $this->title = $t;
00187 $this->uri = $t->getFullURL();
00188 $this->label = $t->getText();
00189 $article = null;
00190 if ( count( $c ) == 0 ) {
00191 $article = new Article( $t );
00192 $this->creator = array();
00193 $this->creator[] = $article->getUserText();
00194 } else {
00195 $this->creator = $c;
00196 }
00197 $this->date = array();
00198 if ( count( $d ) == 0 ) {
00199 if ( is_null( $article ) ) {
00200 $article = new Article( $t );
00201 }
00202 $this->date[] = date( "c", strtotime( $article->getTimestamp() ) );
00203 } else {
00204 foreach ( $d as $date ) {
00205 $this->date[] = $date;
00206 }
00207 }
00208
00209
00210 if ( $t->getNamespace() == NS_MAIN ) {
00211 $this->articlename = ':' . $t->getDBkey();
00212 } else {
00213 $this->articlename = $t->getPrefixedDBKey();
00214 }
00215 }
00216
00220 public function uri() {
00221 return $this->uri;
00222 }
00223
00227 public function text() {
00228 global $wgServer, $wgParser, $smwgShowFactbox, $smwgRSSWithPages;
00229 static $parser_options = null;
00230 $smwgShowFactbox = SMW_FACTBOX_HIDDEN;
00231
00232 $text = "\t<item rdf:about=\"$this->uri\">\n";
00233 $text .= "\t\t<title>" . smwfXMLContentEncode( $this->label ) . "</title>\n";
00234 $text .= "\t\t<link>" . smwfXMLContentEncode( $this->uri ) . "</link>\n";
00235 foreach ( $this->date as $date )
00236 $text .= "\t\t<dc:date>$date</dc:date>\n";
00237 foreach ( $this->creator as $creator )
00238 $text .= "\t\t<dc:creator>" . smwfXMLContentEncode( $creator ) . "</dc:creator>\n";
00239 if ( $smwgRSSWithPages ) {
00240 $parser_options = new ParserOptions();
00241 $parser_options->setEditSection( false );
00242 $parserOutput = $wgParser->parse( '{{' . $this->articlename . '}}', $this->title, $parser_options );
00243 $content = $parserOutput->getText();
00244
00246 $content = str_replace( '<a href="/', '<a href="' . $wgServer . '/', $content );
00247 $text .= "\t\t<description>" . smwfXMLContentEncode( $content ) . "</description>\n";
00248 $text .= "\t\t<content:encoded rdf:datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\"><![CDATA[$content]]></content:encoded>\n";
00249 }
00250 $text .= "\t</item>\n";
00251 return $text;
00252 }
00253
00254 }