2013-12-17 21:20:13 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2014-12-31 23:02:05 +00:00
|
|
|
|
namespace TextExtracts;
|
2016-04-13 04:23:01 +00:00
|
|
|
|
|
2014-12-31 23:02:05 +00:00
|
|
|
|
use Config;
|
2016-09-23 01:51:12 +00:00
|
|
|
|
use DOMElement;
|
2016-04-13 04:23:01 +00:00
|
|
|
|
use HtmlFormatter\HtmlFormatter;
|
2014-12-31 23:02:05 +00:00
|
|
|
|
|
2013-12-17 21:20:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* Provides text-only or limited-HTML extracts of page HTML
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
class ExtractFormatter extends HtmlFormatter {
|
|
|
|
|
const SECTION_MARKER_START = "\1\2";
|
|
|
|
|
const SECTION_MARKER_END = "\2\1";
|
|
|
|
|
|
|
|
|
|
private $plainText;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-09-01 04:58:00 +00:00
|
|
|
|
* @param string $text Text to convert
|
|
|
|
|
* @param bool $plainText Whether extract should be plaintext
|
2017-11-30 02:56:29 +00:00
|
|
|
|
* @param Config $config Configuration object
|
2013-12-17 21:20:13 +00:00
|
|
|
|
*/
|
2014-08-13 06:18:34 +00:00
|
|
|
|
public function __construct( $text, $plainText, Config $config ) {
|
2013-12-17 21:20:13 +00:00
|
|
|
|
parent::__construct( HtmlFormatter::wrapHTML( $text ) );
|
|
|
|
|
$this->plainText = $plainText;
|
|
|
|
|
|
|
|
|
|
$this->setRemoveMedia( true );
|
2014-08-13 06:18:34 +00:00
|
|
|
|
$this->remove( $config->get( 'ExtractsRemoveClasses' ) );
|
2013-12-17 21:20:13 +00:00
|
|
|
|
|
|
|
|
|
if ( $plainText ) {
|
|
|
|
|
$this->flattenAllTags();
|
|
|
|
|
} else {
|
2016-09-23 01:38:27 +00:00
|
|
|
|
$this->flatten( [ 'a' ] );
|
2013-12-17 21:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 03:52:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* Performs final transformations (such as newline replacement for plaintext
|
|
|
|
|
* option) and returns resulting HTML.
|
|
|
|
|
*
|
|
|
|
|
* @param DOMElement|string|null $element ID of element to get HTML from.
|
|
|
|
|
* Ignored
|
|
|
|
|
* @return string Processed HTML
|
|
|
|
|
*/
|
|
|
|
|
public function getText( $element = null ) {
|
2013-12-17 21:20:13 +00:00
|
|
|
|
$this->filterContent();
|
|
|
|
|
$text = parent::getText();
|
|
|
|
|
if ( $this->plainText ) {
|
|
|
|
|
$text = html_entity_decode( $text );
|
2017-11-30 00:47:01 +00:00
|
|
|
|
// replace nbsp with space
|
2019-02-02 20:53:08 +00:00
|
|
|
|
$text = str_replace( "\u{00A0}", ' ', $text );
|
2017-11-30 00:47:01 +00:00
|
|
|
|
// for Windows
|
|
|
|
|
$text = str_replace( "\r", "\n", $text );
|
|
|
|
|
// normalise newlines
|
|
|
|
|
$text = preg_replace( "/\n{3,}/", "\n\n", $text );
|
2013-12-17 21:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 03:52:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param string $html HTML string to process
|
|
|
|
|
* @return string Processed HTML
|
|
|
|
|
*/
|
2013-12-17 21:20:13 +00:00
|
|
|
|
public function onHtmlReady( $html ) {
|
|
|
|
|
if ( $this->plainText ) {
|
|
|
|
|
$html = preg_replace( '/\s*(<h([1-6])\b)/i',
|
2016-09-23 01:38:27 +00:00
|
|
|
|
"\n\n" . self::SECTION_MARKER_START . '$2' . self::SECTION_MARKER_END . '$1',
|
2013-12-17 21:20:13 +00:00
|
|
|
|
$html
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2014-04-23 01:12:44 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns no more than the given number of sentences
|
|
|
|
|
*
|
2017-11-30 02:56:29 +00:00
|
|
|
|
* @param string $text Source text to extract from
|
|
|
|
|
* @param int $requestedSentenceCount Maximum number of sentences to extract
|
2014-04-23 01:12:44 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFirstSentences( $text, $requestedSentenceCount ) {
|
2017-01-11 22:13:45 +00:00
|
|
|
|
if ( $requestedSentenceCount <= 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 01:12:44 +00:00
|
|
|
|
// Based on code from OpenSearchXml by Brion Vibber
|
2016-09-23 01:38:27 +00:00
|
|
|
|
$endchars = [
|
2017-11-30 00:47:01 +00:00
|
|
|
|
// regular ASCII
|
|
|
|
|
'[^\p{Lu}]\.(?:[ \n]|$)', '[\!\?](?:[ \n]|$)',
|
|
|
|
|
// full-width ideographic full-stop
|
|
|
|
|
'。',
|
|
|
|
|
// double-width roman forms
|
|
|
|
|
'.', '!', '?',
|
|
|
|
|
// half-width ideographic full stop
|
|
|
|
|
'。',
|
2016-09-23 01:38:27 +00:00
|
|
|
|
];
|
2014-04-23 01:12:44 +00:00
|
|
|
|
|
|
|
|
|
$endgroup = implode( '|', $endchars );
|
2017-01-11 22:13:45 +00:00
|
|
|
|
$regexp = "/($endgroup)+/u";
|
|
|
|
|
|
2016-09-23 01:38:27 +00:00
|
|
|
|
$matches = [];
|
2017-01-11 22:13:45 +00:00
|
|
|
|
$res = preg_match_all( $regexp, $text, $matches, PREG_OFFSET_CAPTURE );
|
|
|
|
|
|
2016-09-23 01:38:27 +00:00
|
|
|
|
if ( $res ) {
|
2017-01-11 22:13:45 +00:00
|
|
|
|
$index = min( $requestedSentenceCount, $res ) - 1;
|
|
|
|
|
list( $tail, $length ) = $matches[0][ $index ];
|
|
|
|
|
// PCRE returns raw offsets, so using substr() instead of mb_substr()
|
|
|
|
|
$text = substr( $text, 0, $length ) . trim( $tail );
|
2014-04-23 01:12:44 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Just return the first line
|
2017-01-11 22:13:45 +00:00
|
|
|
|
$lines = explode( "\n", $text, 2 );
|
2014-04-23 01:12:44 +00:00
|
|
|
|
$text = trim( $lines[0] );
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns no more than a requested number of characters, preserving words
|
|
|
|
|
*
|
2017-11-30 02:56:29 +00:00
|
|
|
|
* @param string $text Source text to extract from
|
|
|
|
|
* @param int $requestedLength Maximum number of characters to return
|
2014-04-23 01:12:44 +00:00
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFirstChars( $text, $requestedLength ) {
|
2017-01-11 01:51:04 +00:00
|
|
|
|
if ( $requestedLength <= 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2014-04-23 01:12:44 +00:00
|
|
|
|
$length = mb_strlen( $text );
|
|
|
|
|
if ( $length <= $requestedLength ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2017-01-11 01:36:05 +00:00
|
|
|
|
$pattern = "#^[\\w/]*>?#su";
|
|
|
|
|
preg_match( $pattern, mb_substr( $text, $requestedLength ), $m );
|
|
|
|
|
return mb_substr( $text, 0, $requestedLength ) . $m[0];
|
2014-04-23 01:12:44 +00:00
|
|
|
|
}
|
2015-01-08 12:50:23 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes content we've chosen to remove then removes class and style
|
|
|
|
|
* attributes from the remaining span elements.
|
|
|
|
|
*
|
|
|
|
|
* @return array Array of removed DOMElements
|
|
|
|
|
*/
|
|
|
|
|
public function filterContent() {
|
|
|
|
|
$removed = parent::filterContent();
|
|
|
|
|
|
|
|
|
|
$doc = $this->getDoc();
|
|
|
|
|
$spans = $doc->getElementsByTagName( 'span' );
|
|
|
|
|
|
2016-09-23 01:51:12 +00:00
|
|
|
|
/** @var DOMElement $span */
|
2015-01-08 12:50:23 +00:00
|
|
|
|
foreach ( $spans as $span ) {
|
|
|
|
|
$span->removeAttribute( 'class' );
|
|
|
|
|
$span->removeAttribute( 'style' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $removed;
|
|
|
|
|
}
|
2013-12-17 21:20:13 +00:00
|
|
|
|
}
|