2016-06-19 01:41:02 +03:00
|
|
|
<?php
|
|
|
|
class LWNprevBridge extends BridgeAbstract{
|
2017-02-11 18:16:56 +03:00
|
|
|
const MAINTAINER = 'Pierre Mazière';
|
|
|
|
const NAME = 'LWN Free Weekly Edition';
|
|
|
|
const URI = 'https://lwn.net/';
|
|
|
|
const CACHE_TIMEOUT = 604800; // 1 week
|
|
|
|
const DESCRIPTION = 'LWN Free Weekly Edition available one week late';
|
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
private $editionTimeStamp;
|
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
function getURI(){
|
2018-11-05 14:55:58 +03:00
|
|
|
return self::URI . 'free/bigpage';
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function jumpToNextTag(&$node){
|
2017-07-29 20:28:00 +03:00
|
|
|
while($node && $node->nodeType === XML_TEXT_NODE) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$nextNode = $node->nextSibling;
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!$nextNode) {
|
2017-02-11 18:16:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$node = $nextNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function jumpToPreviousTag(&$node){
|
2017-07-29 20:28:00 +03:00
|
|
|
while($node && $node->nodeType === XML_TEXT_NODE) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$previousNode = $node->previousSibling;
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!$previousNode) {
|
2017-02-11 18:16:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$node = $previousNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function collectData(){
|
|
|
|
// Because the LWN page is written in loose HTML and not XHTML,
|
|
|
|
// Simple HTML Dom is not accurate enough for the job
|
|
|
|
$content = getContents($this->getURI())
|
|
|
|
or returnServerError('No results for LWNprev');
|
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
$contents = explode('<b>Page editor</b>', $content);
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
foreach($contents as $content) {
|
|
|
|
if(strpos($content, '<html>') === false) {
|
|
|
|
$content = <<<EOD
|
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
|
|
<html><head><title>LWN</title></head><body>{$content}</body></html>
|
|
|
|
EOD;
|
|
|
|
} else {
|
2018-11-05 14:55:58 +03:00
|
|
|
$content = $content . '</body></html>';
|
2017-08-03 01:15:55 +03:00
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
$html = new DOMDocument();
|
|
|
|
$html->loadHTML($content);
|
|
|
|
libxml_clear_errors();
|
|
|
|
|
|
|
|
$edition = $html->getElementsByTagName('h1');
|
|
|
|
if($edition->length !== 0) {
|
|
|
|
$text = $edition->item(0)->textContent;
|
|
|
|
$this->editionTimeStamp = strtotime(
|
|
|
|
substr($text, strpos($text, 'for ') + strlen('for '))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strpos($content, 'Cat1HL') === false) {
|
|
|
|
$items = $this->getFeatureContents($html);
|
|
|
|
} elseif(strpos($content, 'Cat3HL') === false) {
|
|
|
|
$items = $this->getBriefItems($html);
|
|
|
|
} else {
|
|
|
|
$items = $this->getAnnouncements($html);
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2017-08-03 01:15:55 +03:00
|
|
|
|
|
|
|
$this->items = array_merge($this->items, $items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getArticleContent(&$title){
|
|
|
|
$link = $title->firstChild;
|
|
|
|
$this->jumpToNextTag($link);
|
|
|
|
$item['uri'] = self::URI;
|
|
|
|
if($link->nodeName === 'a') {
|
|
|
|
$item['uri'] .= $link->getAttribute('href');
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
$item['timestamp'] = $this->editionTimeStamp;
|
|
|
|
|
|
|
|
$node = $title;
|
|
|
|
$content = '';
|
|
|
|
$contentEnd = false;
|
|
|
|
while(!$contentEnd) {
|
|
|
|
$node = $node->nextSibling;
|
|
|
|
if(!$node || (
|
|
|
|
$node->nodeType !== XML_TEXT_NODE &&
|
|
|
|
$node->nodeName === 'h2' || (
|
|
|
|
!is_null($node->attributes) &&
|
|
|
|
!is_null($class = $node->attributes->getNamedItem('class')) &&
|
|
|
|
in_array($class->nodeValue, array('Cat1HL','Cat2HL'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$contentEnd = true;
|
|
|
|
} else {
|
|
|
|
$content .= $node->C14N();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$item['content'] = $content;
|
|
|
|
return $item;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
private function getFeatureContents(&$html){
|
|
|
|
$items = array();
|
|
|
|
foreach($html->getElementsByTagName('h2') as $title) {
|
|
|
|
if($title->getAttribute('class') !== 'SummaryHL') {
|
2017-02-11 18:16:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = array();
|
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
$author = $title->nextSibling;
|
|
|
|
$this->jumpToNextTag($author);
|
|
|
|
if($author->getAttribute('class') === 'FeatureByline') {
|
|
|
|
$item['author'] = $author->getElementsByTagName('b')->item(0)->textContent;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
$item['title'] = $title->textContent;
|
|
|
|
|
|
|
|
$items[] = array_merge($item, $this->getArticleContent($title));
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getItemPrefix(&$cat, &$cats){
|
|
|
|
$cat1 = '';
|
|
|
|
$cat2 = '';
|
|
|
|
$cat3 = '';
|
|
|
|
switch($cat->getAttribute('class')) {
|
|
|
|
case 'Cat3HL':
|
|
|
|
$cat3 = $cat->textContent;
|
|
|
|
$cat = $cat->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$cats[2] = $cat3;
|
|
|
|
if($cat->getAttribute('class') !== 'Cat2HL') {
|
2017-02-11 18:16:56 +03:00
|
|
|
break;
|
2017-08-03 01:15:55 +03:00
|
|
|
}
|
|
|
|
case 'Cat2HL':
|
|
|
|
$cat2 = $cat->textContent;
|
|
|
|
$cat = $cat->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$cats[1] = $cat2;
|
|
|
|
if(empty($cat3)) {
|
|
|
|
$cats[2] = '';
|
|
|
|
}
|
|
|
|
if($cat->getAttribute('class') !== 'Cat1HL') {
|
2017-02-11 18:16:56 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-08-03 01:15:55 +03:00
|
|
|
case 'Cat1HL':
|
|
|
|
$cat1 = $cat->textContent;
|
|
|
|
$cats[0] = $cat1;
|
|
|
|
if(empty($cat3)) {
|
|
|
|
$cats[2] = '';
|
|
|
|
}
|
|
|
|
if(empty($cat2)) {
|
|
|
|
$cats[1] = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$prefix = '';
|
|
|
|
if(!empty($cats[0])) {
|
2018-11-05 14:55:58 +03:00
|
|
|
$prefix .= '[' . $cats[0] . ($cats[1] ? '/' . $cats[1] : '') . '] ';
|
2017-08-03 01:15:55 +03:00
|
|
|
}
|
|
|
|
return $prefix;
|
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
private function getAnnouncements(&$html){
|
|
|
|
$items = array();
|
|
|
|
$cats = array('','','');
|
|
|
|
|
|
|
|
foreach($html->getElementsByTagName('p') as $newsletters) {
|
|
|
|
if($newsletters->getAttribute('class') !== 'Cat3HL') {
|
|
|
|
continue;
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 01:15:55 +03:00
|
|
|
$item = array();
|
|
|
|
|
2018-11-05 14:55:58 +03:00
|
|
|
$item['uri'] = self::URI . '#' . count($items);
|
2017-08-03 01:15:55 +03:00
|
|
|
|
2017-08-05 16:24:21 +03:00
|
|
|
$item['timestamp'] = $this->editionTimeStamp;
|
2017-08-03 01:15:55 +03:00
|
|
|
|
|
|
|
$item['author'] = 'LWN';
|
|
|
|
|
|
|
|
$cat = $newsletters->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$prefix = $this->getItemPrefix($cat, $cats);
|
2018-11-05 14:55:58 +03:00
|
|
|
$item['title'] = $prefix . ' ' . $newsletters->textContent;
|
2017-08-03 01:15:55 +03:00
|
|
|
|
|
|
|
$node = $newsletters;
|
2017-02-11 18:16:56 +03:00
|
|
|
$content = '';
|
|
|
|
$contentEnd = false;
|
2017-07-29 20:28:00 +03:00
|
|
|
while(!$contentEnd) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$node = $node->nextSibling;
|
|
|
|
if(!$node || (
|
|
|
|
$node->nodeType !== XML_TEXT_NODE && (
|
2017-08-03 01:15:55 +03:00
|
|
|
!is_null($node->attributes) &&
|
|
|
|
!is_null($class = $node->attributes->getNamedItem('class')) &&
|
|
|
|
in_array($class->nodeValue, array('Cat1HL','Cat2HL','Cat3HL'))
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
)
|
2017-07-29 20:28:00 +03:00
|
|
|
) {
|
2017-02-11 18:16:56 +03:00
|
|
|
$contentEnd = true;
|
2017-08-03 01:15:55 +03:00
|
|
|
} else {
|
2017-02-11 18:16:56 +03:00
|
|
|
$content .= $node->C14N();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$item['content'] = $content;
|
2017-08-03 01:15:55 +03:00
|
|
|
$items[] = $item;
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2017-08-03 01:15:55 +03:00
|
|
|
|
|
|
|
foreach($html->getElementsByTagName('h2') as $title) {
|
|
|
|
if($title->getAttribute('class') !== 'SummaryHL') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = array();
|
|
|
|
|
|
|
|
$cat = $title->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$cat = $cat->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$prefix = $this->getItemPrefix($cat, $cats);
|
2018-11-05 14:55:58 +03:00
|
|
|
$item['title'] = $prefix . ' ' . $title->textContent;
|
2017-08-03 01:15:55 +03:00
|
|
|
$items[] = array_merge($item, $this->getArticleContent($title));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBriefItems(&$html){
|
|
|
|
$items = array();
|
|
|
|
$cats = array('','','');
|
|
|
|
foreach($html->getElementsByTagName('h2') as $title) {
|
|
|
|
if($title->getAttribute('class') !== 'SummaryHL') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = array();
|
|
|
|
|
|
|
|
$cat = $title->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$cat = $cat->previousSibling;
|
|
|
|
$this->jumpToPreviousTag($cat);
|
|
|
|
$prefix = $this->getItemPrefix($cat, $cats);
|
2018-11-05 14:55:58 +03:00
|
|
|
$item['title'] = $prefix . ' ' . $title->textContent;
|
2017-08-03 01:15:55 +03:00
|
|
|
$items[] = array_merge($item, $this->getArticleContent($title));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
2016-06-19 01:41:02 +03:00
|
|
|
}
|
2017-08-03 01:15:55 +03:00
|
|
|
?>
|