'Breaking News: Nothing Happening' * description --> 'A description of the item.' * * @return string */ protected function getItemTitle($item) { return empty($item) ? '' : html::escape(strval($item)); } protected function getItemDescription($item) { return empty($item) ? '' : strval($item); } /** * LINK -- Required. The URL of the item. * * Takes an item as returned by getItems() and returns the * item's URL as a normal PHP string. * * @return string */ protected function getItemLink($item) { return null; } /** * AUTHOR NAME -- Optional. The item's author name. * * Takes an item as returned by getItems() and returns the * item's author name as a normal PHP string. * * @return string */ protected function getItemAuthorName($item) { return null; } /** * AUTHOR EMAIL -- Optional. The item's author email. * * Takes an item as returned by getItems() and returns the * item's author email as a normal PHP string. * * If you specify this, you must specify getItemAuthorName(). * * @return string */ protected function getItemAuthorEmail($item) { return null; } /** * AUTHOR LINK -- Optional. The item's author email. * * Takes an item as returned by getItems() and returns the * item's author email as a normal PHP string. * * If you specify this, you must specify getItemAuthorName(). * * @return string */ protected function getItemAuthorLink($item) { return null; } /** * ENCLOSURE URL -- Required if you're publishing enclosures. * * Takes an item as returned by getItems() and returns the * item's enclosure URL. * * Eg.: '/foo/bar.mp3' * * @return string */ protected function getItemEnclosureURL($item) { return null; } /** * ENCLOSURE LENGTH -- Required if you're publishing enclosures. * * Takes an item as returned by getItems() and returns the * item's enclosure length. * * The returned value should be either an integer, or a string * representation of the integer, in bytes. * * Eg.: 32000 * * @return int */ protected function getItemEnclosureLength($item) { return null; } /** * ENCLOSURE MIME TYPE -- Required if you're publishing enclosures. * * Takes an item as returned by getItems() and returns the * item's enclosure MIME type. * * Eg.: 'audio/mpeg' * * @return string */ protected function getItemEnclosureMIMEType($item) { return null; } /** * PUB DATE -- Optional. * * Takes an item as returned by getItems() and returns the * item's pubdate. * * @return \DateTime */ protected function getItemPubDate($item) { return null; } /** * CATEGORIES -- Optional. * * Takes an item as returned by getItems() and returns the * item's categories as an iterable on strings. * * Eg.: array('php', 'bjork') * * @return string[] */ protected function getItemCategories($item) { return null; } /** * COPYRIGHT NOTICE (only applicable to Atom feeds) -- Optional. * * Takes an item as returned by getItems() and returns the * item's copyright notice as a normal PHP string. * * Eg.: 'Copyright (c) 2007, Sally Smith' * * @return string */ protected function getItemCopyright($item) { return null; } /** * GUID -- Optional. * * @return string */ protected function getItemGUID($item) { return null; } // // Feed generation ------------------------------------------------------- // protected function getFeedExtraOptions($obj) { return array(); } protected function getItemExtraOptions($item) { return array(); } public function getFeed($obj, $request) { $domain = $request->getHost(); $isHttps = $request->isSecure(); // feed $link = $this->getFeedLink($obj); $link = add_domain($domain, $link, $isHttps); $feedURL = $this->getFeedURL($obj); $feedURL = add_domain($domain, $feedURL ? $feedURL : $request->getPath(), $isHttps); $options = array_merge(array( 'url' => $feedURL, 'language' => translation::get_language(), 'subtitle' => $this->getFeedSubtitle($obj), 'authorName' => $this->getFeedAuthorName($obj), 'authorEmail' => $this->getFeedAuthorEmail($obj), 'authorLink' => $this->getFeedAuthorLink($obj), 'categories' => $this->getFeedCategories($obj), 'copyright' => $this->getFeedCopyright($obj), 'guid' => $this->getFeedGUID($obj), 'ttl' => $this->getFeedTTL($obj), ), $this->getFeedExtraOptions($obj)); $feedType = static::getFeedType(); $feed = new $feedType( $this->getFeedTitle($obj), $this->getFeedDescription($obj), $link, $options); // items $titleTpl = null; $titleTplPath = static::getTitleTemplate(); if (!empty($titleTplPath)) { try { list($path, $name) = loader::find_template($titleTplPath); $titleTpl = new Template($path); } catch (TemplateDoesNotExist $e) { // pass } } $descriptionTpl = null; $descriptionTplPath = static::getDescriptionTemplate(); if (!empty($descriptionTplPath)) { try { list($path, $name) = loader::find_template($descriptionTplPath); $descriptionTpl = new Template($path); } catch (TemplateDoesNotExist $e) { // pass } } foreach ($this->getItems($obj) as $item) { if (!empty($titleTpl) || !empty($descriptionTpl)) { $context = new RequestContext($request, array( 'obj' => $item, 'site' => $domain, )); } // title if (!empty($titleTpl)) $title = $titleTpl->render($context); else $title = $this->getItemTitle($item); // description if (!empty($descriptionTpl)) $description = $descriptionTpl->render($context); else $description = $this->getItemDescription($item); // link $link = add_domain($domain, $this->getItemLink($item), $isHttps); // enclosure $enc = null; $encURL = $this->getItemEnclosureURL($item); if (!empty($encURL)) { $enc = new Enclosure($encURL, $this->getItemEnclosureLength($item), $this->getItemEnclosureMIMEType($item) ); } // author $authorName = $this->getItemAuthorName($item); if (!empty($authorName)) { $authorEmail = $this->getItemAuthorEmail($item); $authorLink = $this->getItemAuthorLink($item); } else { $authorEmail = null; $authorLink = null; } // guid $guid = $this->getItemGUID($item); if (!empty($guid)) $guid = $link; $options = array_merge(array( 'guid' => $guid, 'enclosure' => $enc, 'authorName' => $authorName, 'authorEmail' => $authorEmail, 'authorLink' => $authorLink, 'pubdate' => $this->getItemPubDate($item), 'categories' => $this->getItemCategories($item), 'copyright' => $this->getItemCopyright($item) ), $this->getItemExtraOptions($item)); $feed->addItem($title, $description, $link, $options); } return $feed; } final public static function asView($request) { $args = func_get_args(); array_shift($args); $self = new static(); try { $obj = call_user_func_array(array($self, 'getObject'), array_merge( array($request), array($args))); } catch (ObjectDoesNotExist $e) { throw new Http404('Feed object does not exist.'); } $feedgen = $self->getFeed($obj, $request); $response = new HttpResponse('', $feedgen->getMIMEType()); $feedgen->write($response, 'utf-8'); return $response; } }