update external libs

This commit is contained in:
Nicolas Lœuillet 2013-07-31 19:09:06 +02:00
parent 95a7596cb4
commit 3db95a85de
2 changed files with 105 additions and 105 deletions

View file

@ -4,7 +4,7 @@
* *
* This class extends PHP's DOMElement to allow * This class extends PHP's DOMElement to allow
* users to get and set the innerHTML property of * users to get and set the innerHTML property of
* HTML elements in the same way it's done in * HTML elements in the same way it's done in
* JavaScript. * JavaScript.
* *
* Example usage: * Example usage:
@ -15,16 +15,16 @@
* $doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement'); * $doc->registerNodeClass('DOMElement', 'JSLikeHTMLElement');
* $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>'); * $doc->loadHTML('<div><p>Para 1</p><p>Para 2</p></div>');
* $elem = $doc->getElementsByTagName('div')->item(0); * $elem = $doc->getElementsByTagName('div')->item(0);
* *
* // print innerHTML * // print innerHTML
* echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>' * echo $elem->innerHTML; // prints '<p>Para 1</p><p>Para 2</p>'
* echo "\n\n"; * echo "\n\n";
* *
* // set innerHTML * // set innerHTML
* $elem->innerHTML = '<a href="http://fivefilters.org">FiveFilters.org</a>'; * $elem->innerHTML = '<a href="http://fivefilters.org">FiveFilters.org</a>';
* echo $elem->innerHTML; // prints '<a href="http://fivefilters.org">FiveFilters.org</a>' * echo $elem->innerHTML; // prints '<a href="http://fivefilters.org">FiveFilters.org</a>'
* echo "\n\n"; * echo "\n\n";
* *
* // print document (with our changes) * // print document (with our changes)
* echo $doc->saveXML(); * echo $doc->saveXML();
* @endcode * @endcode
@ -59,7 +59,7 @@ class JSLikeHTMLElement extends DOMElement
$value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8'); $value = mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');
// Using <htmlfragment> will generate a warning, but so will bad HTML // Using <htmlfragment> will generate a warning, but so will bad HTML
// (and by this point, bad HTML is what we've got). // (and by this point, bad HTML is what we've got).
// We use it (and suppress the warning) because an HTML fragment will // We use it (and suppress the warning) because an HTML fragment will
// be wrapped around <html><body> tags which we don't really want to keep. // be wrapped around <html><body> tags which we don't really want to keep.
// Note: despite the warning, if loadHTML succeeds it will return true. // Note: despite the warning, if loadHTML succeeds it will return true.
$result = @$f->loadHTML('<htmlfragment>'.$value.'</htmlfragment>'); $result = @$f->loadHTML('<htmlfragment>'.$value.'</htmlfragment>');
@ -86,7 +86,7 @@ class JSLikeHTMLElement extends DOMElement
* @code * @code
* $string = $div->innerHTML; * $string = $div->innerHTML;
* @endcode * @endcode
*/ */
public function __get($name) public function __get($name)
{ {
if ($name == 'innerHTML') { if ($name == 'innerHTML') {

View file

@ -1,5 +1,5 @@
<?php <?php
/** /**
* Arc90's Readability ported to PHP for FiveFilters.org * Arc90's Readability ported to PHP for FiveFilters.org
* Based on readability.js version 1.7.1 (without multi-page support) * Based on readability.js version 1.7.1 (without multi-page support)
* Updated to allow HTML5 parsing with html5lib * Updated to allow HTML5 parsing with html5lib
@ -13,34 +13,34 @@
* License: Apache License, Version 2.0 * License: Apache License, Version 2.0
* Requires: PHP5 * Requires: PHP5
* Date: 2012-09-19 * Date: 2012-09-19
* *
* Differences between the PHP port and the original * Differences between the PHP port and the original
* ------------------------------------------------------ * ------------------------------------------------------
* Arc90's Readability is designed to run in the browser. It works on the DOM * Arc90's Readability is designed to run in the browser. It works on the DOM
* tree (the parsed HTML) after the page's CSS styles have been applied and * tree (the parsed HTML) after the page's CSS styles have been applied and
* Javascript code executed. This PHP port does not run inside a browser. * Javascript code executed. This PHP port does not run inside a browser.
* We use PHP's ability to parse HTML to build our DOM tree, but we cannot * We use PHP's ability to parse HTML to build our DOM tree, but we cannot
* rely on CSS or Javascript support. As such, the results will not always * rely on CSS or Javascript support. As such, the results will not always
* match Arc90's Readability. (For example, if a web page contains CSS style * match Arc90's Readability. (For example, if a web page contains CSS style
* rules or Javascript code which hide certain HTML elements from display, * rules or Javascript code which hide certain HTML elements from display,
* Arc90's Readability will dismiss those from consideration but our PHP port, * Arc90's Readability will dismiss those from consideration but our PHP port,
* unable to understand CSS or Javascript, will not know any better.) * unable to understand CSS or Javascript, will not know any better.)
* *
* Another significant difference is that the aim of Arc90's Readability is * Another significant difference is that the aim of Arc90's Readability is
* to re-present the main content block of a given web page so users can * to re-present the main content block of a given web page so users can
* read it more easily in their browsers. Correct identification, clean up, * read it more easily in their browsers. Correct identification, clean up,
* and separation of the content block is only a part of this process. * and separation of the content block is only a part of this process.
* This PHP port is only concerned with this part, it does not include code * This PHP port is only concerned with this part, it does not include code
* that relates to presentation in the browser - Arc90 already do * that relates to presentation in the browser - Arc90 already do
* that extremely well, and for PDF output there's FiveFilters.org's * that extremely well, and for PDF output there's FiveFilters.org's
* PDF Newspaper: http://fivefilters.org/pdf-newspaper/. * PDF Newspaper: http://fivefilters.org/pdf-newspaper/.
* *
* Finally, this class contains methods that might be useful for developers * Finally, this class contains methods that might be useful for developers
* working on HTML document fragments. So without deviating too much from * working on HTML document fragments. So without deviating too much from
* the original code (which I don't want to do because it makes debugging * the original code (which I don't want to do because it makes debugging
* and updating more difficult), I've tried to make it a little more * and updating more difficult), I've tried to make it a little more
* developer friendly. You should be able to use the methods here on * developer friendly. You should be able to use the methods here on
* existing DOMElement objects without passing an entire HTML document to * existing DOMElement objects without passing an entire HTML document to
* be parsed. * be parsed.
*/ */
@ -48,7 +48,7 @@
require_once(dirname(__FILE__).'/JSLikeHTMLElement.php'); require_once(dirname(__FILE__).'/JSLikeHTMLElement.php');
// Alternative usage (for testing only!) // Alternative usage (for testing only!)
// uncomment the lines below and call Readability.php in your browser // uncomment the lines below and call Readability.php in your browser
// passing it the URL of the page you'd like content from, e.g.: // passing it the URL of the page you'd like content from, e.g.:
// Readability.php?url=http://medialens.org/alerts/09/090615_the_guardian_climate.php // Readability.php?url=http://medialens.org/alerts/09/090615_the_guardian_climate.php
@ -75,11 +75,11 @@ class Readability
public $url = null; // optional - URL where HTML was retrieved public $url = null; // optional - URL where HTML was retrieved
public $debug = false; public $debug = false;
public $lightClean = true; // preserves more content (experimental) added 2012-09-19 public $lightClean = true; // preserves more content (experimental) added 2012-09-19
protected $body = null; // protected $body = null; //
protected $bodyCache = null; // Cache the body HTML in case we need to re-use it later protected $bodyCache = null; // Cache the body HTML in case we need to re-use it later
protected $flags = 7; // 1 | 2 | 4; // Start with all flags set. protected $flags = 7; // 1 | 2 | 4; // Start with all flags set.
protected $success = false; // indicates whether we were able to extract or not protected $success = false; // indicates whether we were able to extract or not
/** /**
* All of the regular expressions in use within readability. * All of the regular expressions in use within readability.
* Defined up here so we don't instantiate them repeatedly in loops. * Defined up here so we don't instantiate them repeatedly in loops.
@ -97,19 +97,19 @@ class Readability
'killBreaks' => '/(<br\s*\/?>(\s|&nbsp;?)*){1,}/', 'killBreaks' => '/(<br\s*\/?>(\s|&nbsp;?)*){1,}/',
'video' => '!//(player\.|www\.)?(youtube|vimeo|viddler)\.com!i', 'video' => '!//(player\.|www\.)?(youtube|vimeo|viddler)\.com!i',
'skipFootnoteLink' => '/^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i' 'skipFootnoteLink' => '/^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i'
); );
/* constants */ /* constants */
const FLAG_STRIP_UNLIKELYS = 1; const FLAG_STRIP_UNLIKELYS = 1;
const FLAG_WEIGHT_CLASSES = 2; const FLAG_WEIGHT_CLASSES = 2;
const FLAG_CLEAN_CONDITIONALLY = 4; const FLAG_CLEAN_CONDITIONALLY = 4;
/** /**
* Create instance of Readability * Create instance of Readability
* @param string UTF-8 encoded string * @param string UTF-8 encoded string
* @param string (optional) URL associated with HTML (used for footnotes) * @param string (optional) URL associated with HTML (used for footnotes)
* @param string which parser to use for turning raw HTML into a DOMDocument (either 'libxml' or 'html5lib') * @param string which parser to use for turning raw HTML into a DOMDocument (either 'libxml' or 'html5lib')
*/ */
function __construct($html, $url=null, $parser='libxml') function __construct($html, $url=null, $parser='libxml')
{ {
$this->url = $url; $this->url = $url;
@ -135,18 +135,18 @@ class Readability
public function getTitle() { public function getTitle() {
return $this->articleTitle; return $this->articleTitle;
} }
/** /**
* Get article content element * Get article content element
* @return DOMElement * @return DOMElement
*/ */
public function getContent() { public function getContent() {
return $this->articleContent; return $this->articleContent;
} }
/** /**
* Runs readability. * Runs readability.
* *
* Workflow: * Workflow:
* 1. Prep the document by removing script tags, css, etc. * 1. Prep the document by removing script tags, css, etc.
* 2. Build readability's DOM tree. * 2. Build readability's DOM tree.
@ -161,7 +161,7 @@ class Readability
if (!isset($this->dom->documentElement)) return false; if (!isset($this->dom->documentElement)) return false;
$this->removeScripts($this->dom); $this->removeScripts($this->dom);
//die($this->getInnerHTML($this->dom->documentElement)); //die($this->getInnerHTML($this->dom->documentElement));
// Assume successful outcome // Assume successful outcome
$this->success = true; $this->success = true;
@ -176,7 +176,7 @@ class Readability
} }
$this->prepDocument(); $this->prepDocument();
//die($this->dom->documentElement->parentNode->nodeType); //die($this->dom->documentElement->parentNode->nodeType);
//$this->setInnerHTML($this->dom->documentElement, $this->getInnerHTML($this->dom->documentElement)); //$this->setInnerHTML($this->dom->documentElement, $this->getInnerHTML($this->dom->documentElement));
//die($this->getInnerHTML($this->dom->documentElement)); //die($this->getInnerHTML($this->dom->documentElement));
@ -191,9 +191,9 @@ class Readability
$this->success = false; $this->success = false;
$articleContent = $this->dom->createElement('div'); $articleContent = $this->dom->createElement('div');
$articleContent->setAttribute('id', 'readability-content'); $articleContent->setAttribute('id', 'readability-content');
$articleContent->innerHTML = '<p>Sorry, Readability was unable to parse this page for content.</p>'; $articleContent->innerHTML = '<p>Sorry, Readability was unable to parse this page for content.</p>';
} }
$overlay->setAttribute('id', 'readOverlay'); $overlay->setAttribute('id', 'readOverlay');
$innerDiv->setAttribute('id', 'readInner'); $innerDiv->setAttribute('id', 'readInner');
@ -201,7 +201,7 @@ class Readability
$innerDiv->appendChild($articleTitle); $innerDiv->appendChild($articleTitle);
$innerDiv->appendChild($articleContent); $innerDiv->appendChild($articleContent);
$overlay->appendChild($innerDiv); $overlay->appendChild($innerDiv);
/* Clear the old HTML, insert the new content. */ /* Clear the old HTML, insert the new content. */
$this->body->innerHTML = ''; $this->body->innerHTML = '';
$this->body->appendChild($overlay); $this->body->appendChild($overlay);
@ -209,21 +209,21 @@ class Readability
$this->body->removeAttribute('style'); $this->body->removeAttribute('style');
$this->postProcessContent($articleContent); $this->postProcessContent($articleContent);
// Set title and content instance variables // Set title and content instance variables
$this->articleTitle = $articleTitle; $this->articleTitle = $articleTitle;
$this->articleContent = $articleContent; $this->articleContent = $articleContent;
return $this->success; return $this->success;
} }
/** /**
* Debug * Debug
*/ */
protected function dbg($msg) { protected function dbg($msg) {
if ($this->debug) echo '* ',$msg, "\n"; if ($this->debug) echo '* ',$msg, "\n";
} }
/** /**
* Run any post-process modifications to article content as necessary. * Run any post-process modifications to article content as necessary.
* *
@ -231,11 +231,11 @@ class Readability
* @return void * @return void
*/ */
public function postProcessContent($articleContent) { public function postProcessContent($articleContent) {
if ($this->convertLinksToFootnotes && !preg_match('/wikipedia\.org/', @$this->url)) { if ($this->convertLinksToFootnotes && !preg_match('/wikipedia\.org/', @$this->url)) {
$this->addFootnotes($articleContent); $this->addFootnotes($articleContent);
} }
} }
/** /**
* Get the article title as an H1. * Get the article title as an H1.
* *
@ -248,11 +248,11 @@ class Readability
try { try {
$curTitle = $origTitle = $this->getInnerText($this->dom->getElementsByTagName('title')->item(0)); $curTitle = $origTitle = $this->getInnerText($this->dom->getElementsByTagName('title')->item(0));
} catch(Exception $e) {} } catch(Exception $e) {}
if (preg_match('/ [\|\-] /', $curTitle)) if (preg_match('/ [\|\-] /', $curTitle))
{ {
$curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle); $curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle);
if (count(explode(' ', $curTitle)) < 3) { if (count(explode(' ', $curTitle)) < 3) {
$curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle); $curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle);
} }
@ -279,17 +279,17 @@ class Readability
if (count(explode(' ', $curTitle)) <= 4) { if (count(explode(' ', $curTitle)) <= 4) {
$curTitle = $origTitle; $curTitle = $origTitle;
} }
$articleTitle = $this->dom->createElement('h1'); $articleTitle = $this->dom->createElement('h1');
$articleTitle->innerHTML = $curTitle; $articleTitle->innerHTML = $curTitle;
return $articleTitle; return $articleTitle;
} }
/** /**
* Prepare the HTML document for readability to scrape it. * Prepare the HTML document for readability to scrape it.
* This includes things like stripping javascript, CSS, and handling terrible markup. * This includes things like stripping javascript, CSS, and handling terrible markup.
* *
* @return void * @return void
**/ **/
protected function prepDocument() { protected function prepDocument() {
@ -328,13 +328,13 @@ class Readability
$footnotesWrapper = $this->dom->createElement('div'); $footnotesWrapper = $this->dom->createElement('div');
$footnotesWrapper->setAttribute('id', 'readability-footnotes'); $footnotesWrapper->setAttribute('id', 'readability-footnotes');
$footnotesWrapper->innerHTML = '<h3>References</h3>'; $footnotesWrapper->innerHTML = '<h3>References</h3>';
$articleFootnotes = $this->dom->createElement('ol'); $articleFootnotes = $this->dom->createElement('ol');
$articleFootnotes->setAttribute('id', 'readability-footnotes-list'); $articleFootnotes->setAttribute('id', 'readability-footnotes-list');
$footnotesWrapper->appendChild($articleFootnotes); $footnotesWrapper->appendChild($articleFootnotes);
$articleLinks = $articleContent->getElementsByTagName('a'); $articleLinks = $articleContent->getElementsByTagName('a');
$linkCount = 0; $linkCount = 0;
for ($i = 0; $i < $articleLinks->length; $i++) for ($i = 0; $i < $articleLinks->length; $i++)
{ {
@ -346,11 +346,11 @@ class Readability
if (!$linkDomain && isset($this->url)) $linkDomain = @parse_url($this->url, PHP_URL_HOST); if (!$linkDomain && isset($this->url)) $linkDomain = @parse_url($this->url, PHP_URL_HOST);
//linkDomain = footnoteLink.host ? footnoteLink.host : document.location.host, //linkDomain = footnoteLink.host ? footnoteLink.host : document.location.host,
$linkText = $this->getInnerText($articleLink); $linkText = $this->getInnerText($articleLink);
if ((strpos($articleLink->getAttribute('class'), 'readability-DoNotFootnote') !== false) || preg_match($this->regexps['skipFootnoteLink'], $linkText)) { if ((strpos($articleLink->getAttribute('class'), 'readability-DoNotFootnote') !== false) || preg_match($this->regexps['skipFootnoteLink'], $linkText)) {
continue; continue;
} }
$linkCount++; $linkCount++;
/** Add a superscript reference after the article link */ /** Add a superscript reference after the article link */
@ -358,7 +358,7 @@ class Readability
$refLink->innerHTML = '<small><sup>[' . $linkCount . ']</sup></small>'; $refLink->innerHTML = '<small><sup>[' . $linkCount . ']</sup></small>';
$refLink->setAttribute('class', 'readability-DoNotFootnote'); $refLink->setAttribute('class', 'readability-DoNotFootnote');
$refLink->setAttribute('style', 'color: inherit;'); $refLink->setAttribute('style', 'color: inherit;');
//TODO: does this work or should we use DOMNode.isSameNode()? //TODO: does this work or should we use DOMNode.isSameNode()?
if ($articleLink->parentNode->lastChild == $articleLink) { if ($articleLink->parentNode->lastChild == $articleLink) {
$articleLink->parentNode->appendChild($refLink); $articleLink->parentNode->appendChild($refLink);
@ -373,15 +373,15 @@ class Readability
$footnoteLink->innerHTML = ($footnoteLink->getAttribute('title') != '' ? $footnoteLink->getAttribute('title') : $linkText); $footnoteLink->innerHTML = ($footnoteLink->getAttribute('title') != '' ? $footnoteLink->getAttribute('title') : $linkText);
$footnoteLink->setAttribute('name', 'readabilityFootnoteLink-' . $linkCount); $footnoteLink->setAttribute('name', 'readabilityFootnoteLink-' . $linkCount);
$footnote->appendChild($footnoteLink); $footnote->appendChild($footnoteLink);
if ($linkDomain) $footnote->innerHTML = $footnote->innerHTML . '<small> (' . $linkDomain . ')</small>'; if ($linkDomain) $footnote->innerHTML = $footnote->innerHTML . '<small> (' . $linkDomain . ')</small>';
$articleFootnotes->appendChild($footnote); $articleFootnotes->appendChild($footnote);
} }
if ($linkCount > 0) { if ($linkCount > 0) {
$articleContent->appendChild($footnotesWrapper); $articleContent->appendChild($footnotesWrapper);
} }
} }
@ -404,7 +404,7 @@ class Readability
//} //}
} }
} }
/** /**
* Prepare the article node for display. Clean out any inline styles, * Prepare the article node for display. Clean out any inline styles,
* iframes, forms, strip extraneous <p> tags, etc. * iframes, forms, strip extraneous <p> tags, etc.
@ -429,7 +429,7 @@ class Readability
* as a header and not a subheader, so remove it since we already have a header. * as a header and not a subheader, so remove it since we already have a header.
***/ ***/
if (!$this->lightClean && ($articleContent->getElementsByTagName('h2')->length == 1)) { if (!$this->lightClean && ($articleContent->getElementsByTagName('h2')->length == 1)) {
$this->clean($articleContent, 'h2'); $this->clean($articleContent, 'h2');
} }
$this->clean($articleContent, 'iframe'); $this->clean($articleContent, 'iframe');
@ -448,7 +448,7 @@ class Readability
$embedCount = $articleParagraphs->item($i)->getElementsByTagName('embed')->length; $embedCount = $articleParagraphs->item($i)->getElementsByTagName('embed')->length;
$objectCount = $articleParagraphs->item($i)->getElementsByTagName('object')->length; $objectCount = $articleParagraphs->item($i)->getElementsByTagName('object')->length;
$iframeCount = $articleParagraphs->item($i)->getElementsByTagName('iframe')->length; $iframeCount = $articleParagraphs->item($i)->getElementsByTagName('iframe')->length;
if ($imgCount === 0 && $embedCount === 0 && $objectCount === 0 && $iframeCount === 0 && $this->getInnerText($articleParagraphs->item($i), false) == '') if ($imgCount === 0 && $embedCount === 0 && $objectCount === 0 && $iframeCount === 0 && $this->getInnerText($articleParagraphs->item($i), false) == '')
{ {
$articleParagraphs->item($i)->parentNode->removeChild($articleParagraphs->item($i)); $articleParagraphs->item($i)->parentNode->removeChild($articleParagraphs->item($i));
@ -457,13 +457,13 @@ class Readability
try { try {
$articleContent->innerHTML = preg_replace('/<br[^>]*>\s*<p/i', '<p', $articleContent->innerHTML); $articleContent->innerHTML = preg_replace('/<br[^>]*>\s*<p/i', '<p', $articleContent->innerHTML);
//articleContent.innerHTML = articleContent.innerHTML.replace(/<br[^>]*>\s*<p/gi, '<p'); //articleContent.innerHTML = articleContent.innerHTML.replace(/<br[^>]*>\s*<p/gi, '<p');
} }
catch (Exception $e) { catch (Exception $e) {
$this->dbg("Cleaning innerHTML of breaks failed. This is an IE strict-block-elements bug. Ignoring.: " . $e); $this->dbg("Cleaning innerHTML of breaks failed. This is an IE strict-block-elements bug. Ignoring.: " . $e);
} }
} }
/** /**
* Initialize a node with the readability object. Also checks the * Initialize a node with the readability object. Also checks the
* className/id for special names to add to its score. * className/id for special names to add to its score.
@ -474,7 +474,7 @@ class Readability
protected function initializeNode($node) { protected function initializeNode($node) {
$readability = $this->dom->createAttribute('readability'); $readability = $this->dom->createAttribute('readability');
$readability->value = 0; // this is our contentScore $readability->value = 0; // this is our contentScore
$node->setAttributeNode($readability); $node->setAttributeNode($readability);
switch (strtoupper($node->tagName)) { // unsure if strtoupper is needed, but using it just in case switch (strtoupper($node->tagName)) { // unsure if strtoupper is needed, but using it just in case
case 'DIV': case 'DIV':
@ -486,7 +486,7 @@ class Readability
case 'BLOCKQUOTE': case 'BLOCKQUOTE':
$readability->value += 3; $readability->value += 3;
break; break;
case 'ADDRESS': case 'ADDRESS':
case 'OL': case 'OL':
case 'UL': case 'UL':
@ -510,7 +510,7 @@ class Readability
} }
$readability->value += $this->getClassWeight($node); $readability->value += $this->getClassWeight($node);
} }
/*** /***
* grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is * grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is
* most likely to be the stuff a user wants to read. Then return it wrapped up in a div. * most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
@ -548,7 +548,7 @@ class Readability
$node->parentNode->removeChild($node); $node->parentNode->removeChild($node);
$nodeIndex--; $nodeIndex--;
continue; continue;
} }
} }
if ($tagName == 'P' || $tagName == 'TD' || $tagName == 'PRE') { if ($tagName == 'P' || $tagName == 'TD' || $tagName == 'PRE') {
@ -589,7 +589,7 @@ class Readability
} }
} }
} }
/** /**
* Loop through all paragraphs, and assign a score to them based on how content-y they look. * Loop through all paragraphs, and assign a score to them based on how content-y they look.
* Then add their score to their parent node. * Then add their score to their parent node.
@ -613,7 +613,7 @@ class Readability
} }
/* Initialize readability data for the parent. */ /* Initialize readability data for the parent. */
if (!$parentNode->hasAttribute('readability')) if (!$parentNode->hasAttribute('readability'))
{ {
$this->initializeNode($parentNode); $this->initializeNode($parentNode);
$candidates[] = $parentNode; $candidates[] = $parentNode;
@ -633,15 +633,15 @@ class Readability
/* Add points for any commas within this paragraph */ /* Add points for any commas within this paragraph */
$contentScore += count(explode(',', $innerText)); $contentScore += count(explode(',', $innerText));
/* For every 100 characters in this paragraph, add another point. Up to 3 points. */ /* For every 100 characters in this paragraph, add another point. Up to 3 points. */
$contentScore += min(floor(strlen($innerText) / 100), 3); $contentScore += min(floor(strlen($innerText) / 100), 3);
/* Add the score to the parent. The grandparent gets half. */ /* Add the score to the parent. The grandparent gets half. */
$parentNode->getAttributeNode('readability')->value += $contentScore; $parentNode->getAttributeNode('readability')->value += $contentScore;
if ($grandParentNode) { if ($grandParentNode) {
$grandParentNode->getAttributeNode('readability')->value += $contentScore/2; $grandParentNode->getAttributeNode('readability')->value += $contentScore/2;
} }
} }
@ -727,12 +727,12 @@ class Readability
{ {
$append = true; $append = true;
} }
if (strtoupper($siblingNode->nodeName) == 'P') { if (strtoupper($siblingNode->nodeName) == 'P') {
$linkDensity = $this->getLinkDensity($siblingNode); $linkDensity = $this->getLinkDensity($siblingNode);
$nodeContent = $this->getInnerText($siblingNode); $nodeContent = $this->getInnerText($siblingNode);
$nodeLength = strlen($nodeContent); $nodeLength = strlen($nodeContent);
if ($nodeLength > 80 && $linkDensity < 0.25) if ($nodeLength > 80 && $linkDensity < 0.25)
{ {
$append = true; $append = true;
@ -751,7 +751,7 @@ class Readability
$sibNodeName = strtoupper($siblingNode->nodeName); $sibNodeName = strtoupper($siblingNode->nodeName);
if ($sibNodeName != 'DIV' && $sibNodeName != 'P') { if ($sibNodeName != 'DIV' && $sibNodeName != 'P') {
/* We have a node that isn't a common block level element, like a form or td tag. Turn it into a div so it doesn't get filtered out later by accident. */ /* We have a node that isn't a common block level element, like a form or td tag. Turn it into a div so it doesn't get filtered out later by accident. */
$this->dbg('Altering siblingNode of ' . $sibNodeName . ' to div.'); $this->dbg('Altering siblingNode of ' . $sibNodeName . ' to div.');
$nodeToAppend = $this->dom->createElement('div'); $nodeToAppend = $this->dom->createElement('div');
try { try {
@ -770,7 +770,7 @@ class Readability
$s--; $s--;
$sl--; $sl--;
} }
/* To ensure a node does not interfere with readability styles, remove its classnames */ /* To ensure a node does not interfere with readability styles, remove its classnames */
$nodeToAppend->removeAttribute('class'); $nodeToAppend->removeAttribute('class');
@ -796,14 +796,14 @@ class Readability
// in the meantime, we check and create an empty element if it's not there. // in the meantime, we check and create an empty element if it's not there.
if (!isset($this->body->childNodes)) $this->body = $this->dom->createElement('body'); if (!isset($this->body->childNodes)) $this->body = $this->dom->createElement('body');
$this->body->innerHTML = $this->bodyCache; $this->body->innerHTML = $this->bodyCache;
if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS)) { if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS)) {
$this->removeFlag(self::FLAG_STRIP_UNLIKELYS); $this->removeFlag(self::FLAG_STRIP_UNLIKELYS);
return $this->grabArticle($this->body); return $this->grabArticle($this->body);
} }
else if ($this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) { else if ($this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) {
$this->removeFlag(self::FLAG_WEIGHT_CLASSES); $this->removeFlag(self::FLAG_WEIGHT_CLASSES);
return $this->grabArticle($this->body); return $this->grabArticle($this->body);
} }
else if ($this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) { else if ($this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
$this->removeFlag(self::FLAG_CLEAN_CONDITIONALLY); $this->removeFlag(self::FLAG_CLEAN_CONDITIONALLY);
@ -815,7 +815,7 @@ class Readability
} }
return $articleContent; return $articleContent;
} }
/** /**
* Remove script tags from document * Remove script tags from document
* *
@ -829,7 +829,7 @@ class Readability
$scripts->item($i)->parentNode->removeChild($scripts->item($i)); $scripts->item($i)->parentNode->removeChild($scripts->item($i));
} }
} }
/** /**
* Get the inner text of a node. * Get the inner text of a node.
* This also strips out any excess whitespace to be found. * This also strips out any excess whitespace to be found.
@ -878,11 +878,11 @@ class Readability
$elem->removeAttribute('style'); $elem->removeAttribute('style');
} }
} }
/** /**
* Get the density of links as a percentage of the content * Get the density of links as a percentage of the content
* This is the amount of text that is inside a link divided by the total text in the node. * This is the amount of text that is inside a link divided by the total text in the node.
* *
* @param DOMElement $e * @param DOMElement $e
* @return number (float) * @return number (float)
*/ */
@ -900,9 +900,9 @@ class Readability
return 0; return 0;
} }
} }
/** /**
* Get an elements class/id weight. Uses regular expressions to tell if this * Get an elements class/id weight. Uses regular expressions to tell if this
* element looks good or bad. * element looks good or bad.
* *
* @param DOMElement $e * @param DOMElement $e
@ -964,7 +964,7 @@ class Readability
public function clean($e, $tag) { public function clean($e, $tag) {
$targetList = $e->getElementsByTagName($tag); $targetList = $e->getElementsByTagName($tag);
$isEmbed = ($tag == 'iframe' || $tag == 'object' || $tag == 'embed'); $isEmbed = ($tag == 'iframe' || $tag == 'object' || $tag == 'embed');
for ($y=$targetList->length-1; $y >= 0; $y--) { for ($y=$targetList->length-1; $y >= 0; $y--) {
/* Allow youtube and vimeo videos through as people usually want to see those. */ /* Allow youtube and vimeo videos through as people usually want to see those. */
if ($isEmbed) { if ($isEmbed) {
@ -972,7 +972,7 @@ class Readability
for ($i=0, $il=$targetList->item($y)->attributes->length; $i < $il; $i++) { for ($i=0, $il=$targetList->item($y)->attributes->length; $i < $il; $i++) {
$attributeValues .= $targetList->item($y)->attributes->item($i)->value . '|'; // DOMAttr? (TODO: test) $attributeValues .= $targetList->item($y)->attributes->item($i)->value . '|'; // DOMAttr? (TODO: test)
} }
/* First, check the elements attributes to see if any of them contain youtube or vimeo */ /* First, check the elements attributes to see if any of them contain youtube or vimeo */
if (preg_match($this->regexps['video'], $attributeValues)) { if (preg_match($this->regexps['video'], $attributeValues)) {
continue; continue;
@ -986,10 +986,10 @@ class Readability
$targetList->item($y)->parentNode->removeChild($targetList->item($y)); $targetList->item($y)->parentNode->removeChild($targetList->item($y));
} }
} }
/** /**
* Clean an element of all tags of type "tag" if they look fishy. * Clean an element of all tags of type "tag" if they look fishy.
* "Fishy" is an algorithm based on content length, classnames, * "Fishy" is an algorithm based on content length, classnames,
* link density, number of images & embeds, etc. * link density, number of images & embeds, etc.
* *
* @param DOMElement $e * @param DOMElement $e
@ -1013,7 +1013,7 @@ class Readability
for ($i=$curTagsLength-1; $i >= 0; $i--) { for ($i=$curTagsLength-1; $i >= 0; $i--) {
$weight = $this->getClassWeight($tagsList->item($i)); $weight = $this->getClassWeight($tagsList->item($i));
$contentScore = ($tagsList->item($i)->hasAttribute('readability')) ? (int)$tagsList->item($i)->getAttribute('readability') : 0; $contentScore = ($tagsList->item($i)->hasAttribute('readability')) ? (int)$tagsList->item($i)->getAttribute('readability') : 0;
$this->dbg('Cleaning Conditionally ' . $tagsList->item($i)->tagName . ' (' . $tagsList->item($i)->getAttribute('class') . ':' . $tagsList->item($i)->getAttribute('id') . ')' . (($tagsList->item($i)->hasAttribute('readability')) ? (' with score ' . $tagsList->item($i)->getAttribute('readability')) : '')); $this->dbg('Cleaning Conditionally ' . $tagsList->item($i)->tagName . ' (' . $tagsList->item($i)->getAttribute('class') . ':' . $tagsList->item($i)->getAttribute('id') . ')' . (($tagsList->item($i)->hasAttribute('readability')) ? (' with score ' . $tagsList->item($i)->getAttribute('readability')) : ''));
if ($weight + $contentScore < 0) { if ($weight + $contentScore < 0) {
@ -1034,13 +1034,13 @@ class Readability
$embeds = $tagsList->item($i)->getElementsByTagName('embed'); $embeds = $tagsList->item($i)->getElementsByTagName('embed');
for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) { for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) { if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
$embedCount++; $embedCount++;
} }
} }
$embeds = $tagsList->item($i)->getElementsByTagName('iframe'); $embeds = $tagsList->item($i)->getElementsByTagName('iframe');
for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) { for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) { if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
$embedCount++; $embedCount++;
} }
} }
@ -1058,7 +1058,7 @@ class Readability
$toRemove = true; $toRemove = true;
} else if ( $input > floor($p/3) ) { } else if ( $input > floor($p/3) ) {
$this->dbg(' too many <input> elements'); $this->dbg(' too many <input> elements');
$toRemove = true; $toRemove = true;
} else if ($contentLength < 25 && ($embedCount === 0 && ($img === 0 || $img > 2))) { } else if ($contentLength < 25 && ($embedCount === 0 && ($img === 0 || $img > 2))) {
$this->dbg(' content length less than 25 chars, 0 embeds and either 0 images or more than 2 images'); $this->dbg(' content length less than 25 chars, 0 embeds and either 0 images or more than 2 images');
$toRemove = true; $toRemove = true;
@ -1082,7 +1082,7 @@ class Readability
$toRemove = true; $toRemove = true;
} else if ( $input > floor($p/3) ) { } else if ( $input > floor($p/3) ) {
$this->dbg(' too many <input> elements'); $this->dbg(' too many <input> elements');
$toRemove = true; $toRemove = true;
} else if ($contentLength < 25 && ($img === 0 || $img > 2) ) { } else if ($contentLength < 25 && ($img === 0 || $img > 2) ) {
$this->dbg(' content length less than 25 chars and 0 images, or more than 2 images'); $this->dbg(' content length less than 25 chars and 0 images, or more than 2 images');
$toRemove = true; $toRemove = true;
@ -1126,11 +1126,11 @@ class Readability
public function flagIsActive($flag) { public function flagIsActive($flag) {
return ($this->flags & $flag) > 0; return ($this->flags & $flag) > 0;
} }
public function addFlag($flag) { public function addFlag($flag) {
$this->flags = $this->flags | $flag; $this->flags = $this->flags | $flag;
} }
public function removeFlag($flag) { public function removeFlag($flag) {
$this->flags = $this->flags & ~$flag; $this->flags = $this->flags & ~$flag;
} }