示例#1
0
 /**
  * @param \OCA\News\Db\Item $item
  * @return \OCA\News\Db\Item enhanced item
  */
 public function enhance(Item $item)
 {
     if (preg_match($this->matchArticleUrl, $item->getUrl())) {
         $body = $item->getBody();
         foreach ($this->regexPair as $search => $replaceWith) {
             $body = preg_replace($search, $replaceWith, $body);
         }
         $item->setBody($body);
     }
     return $item;
 }
    /**
     * This method is run after all enhancers and for every item
     */
    public function enhance(Item $item) {

        $dom = new DOMDocument();

        // wrap it inside a div if there is none to prevent invalid wrapping
        // inside <p> tags
        $body = '<div>' . $item->getBody() . '</div>';

        @$dom->loadHTML($body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

        $xpath = new DOMXpath($dom);

        // remove youtube autoplay
        // NOTE: PHP supports only XPath 1.0 so no matches() function :(
        $youtubeIframes = "//iframe[contains(@src, 'youtube.com')]";

        $elements = $xpath->query($youtubeIframes);
        foreach ($elements as $element) {

            // src needs to be matched against regex to prevent false positives
            // and because theres no XPath matches function available
            $src = $element->getAttribute('src');
            $regex = '%^(http://|https://|//)(www\.)?youtube.com/' .
                     '.*\?.*autoplay=1.*%i';

            if (preg_match($regex, $src)) {
                $replaced = str_replace('autoplay=1', 'autoplay=0', $src);
                $element->setAttribute('src', $replaced);
            }
        }

        // save all changes back to the item
        $item->setBody(trim($dom->saveHTML()));

        return $item;
    }
示例#3
0
 public function testImportArticlesCreatesOwnFeedWhenNotFound()
 {
     $url = 'http://owncloud/args';
     $feed = new Feed();
     $feed->setId(3);
     $feed->setUserId($this->user);
     $feed->setUrl($url);
     $feed->setLink($url);
     $feed->setTitle('Articles without feed');
     $feed->setAdded($this->time);
     $feed->setFolderId(0);
     $feed->setPreventUpdate(true);
     $feeds = [$feed];
     $item = new Item();
     $item->setFeedId(3);
     $item->setAuthor('john');
     $item->setGuid('s');
     $item->setGuidHash('s');
     $item->setTitle('hey');
     $item->setPubDate(333);
     $item->setBody('come over');
     $item->setEnclosureMime('mime');
     $item->setEnclosureLink('lin');
     $item->setUnread();
     $item->setUnstarred();
     $item->setLastModified($this->time);
     $json = $item->toExport(['feed3' => $feed]);
     $json2 = $json;
     // believe it or not this copies stuff :D
     $json2['feedLink'] = 'http://test.com';
     $items = [$json, $json2];
     $insertFeed = new Feed();
     $insertFeed->setLink('http://owncloud/nofeed');
     $insertFeed->setUrl('http://owncloud/nofeed');
     $insertFeed->setUserId($this->user);
     $insertFeed->setTitle('Articles without feed');
     $insertFeed->setAdded($this->time);
     $insertFeed->setPreventUpdate(true);
     $insertFeed->setFolderId(0);
     $this->l10n->expects($this->once())->method('t')->will($this->returnValue('Articles without feed'));
     $this->feedMapper->expects($this->once())->method('findAllFromUser')->with($this->equalTo($this->user))->will($this->returnValue($feeds));
     $this->feedMapper->expects($this->once())->method('insert')->with($this->equalTo($insertFeed))->will($this->returnValue($insertFeed));
     $this->itemMapper->expects($this->at(0))->method('findByGuidHash')->will($this->throwException(new DoesNotExistException('yo')));
     $this->purifier->expects($this->once())->method('purify')->with($this->equalTo($item->getBody()))->will($this->returnValue($item->getBody()));
     $this->itemMapper->expects($this->at(1))->method('insert')->with($this->equalTo($item));
     $this->itemMapper->expects($this->at(2))->method('findByGuidHash')->will($this->returnValue($item));
     $this->itemMapper->expects($this->at(3))->method('update')->with($this->equalTo($item));
     $this->feedMapper->expects($this->once())->method('findByUrlHash')->will($this->returnValue($feed));
     $result = $this->feedService->importArticles($items, $this->user);
     $this->assertEquals($feed, $result);
 }
示例#4
0
 public function enhance(Item $item)
 {
     $body = $item->getBody();
     $item->setBody($body += 1);
     return $item;
 }