2013-12-17 21:20:13 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-12-31 23:02:05 +00:00
|
|
|
namespace TextExtracts;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiMain;
|
|
|
|
use ApiQueryBase;
|
|
|
|
use Config;
|
|
|
|
use FauxRequest;
|
2016-09-23 01:51:12 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2014-12-31 23:02:05 +00:00
|
|
|
use MWTidy;
|
|
|
|
use ParserCache;
|
|
|
|
use ParserOptions;
|
|
|
|
use Title;
|
2016-11-03 19:16:57 +00:00
|
|
|
use ApiUsageException;
|
2014-12-31 23:02:05 +00:00
|
|
|
use UsageException;
|
|
|
|
use User;
|
|
|
|
use WikiPage;
|
|
|
|
|
2013-12-17 21:20:13 +00:00
|
|
|
class ApiQueryExtracts extends ApiQueryBase {
|
|
|
|
/**
|
|
|
|
* @var ParserOptions
|
|
|
|
*/
|
|
|
|
private $parserOptions;
|
|
|
|
private $params;
|
2014-08-13 06:18:34 +00:00
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
2013-12-17 21:20:13 +00:00
|
|
|
|
2015-07-30 04:28:41 +00:00
|
|
|
// TODO: Allow extensions to hook into this to opt-in.
|
|
|
|
// This is partly for security reasons; see T107170.
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-23 01:38:27 +00:00
|
|
|
private $supportedContentModels = [ 'wikitext' ];
|
2015-07-30 04:28:41 +00:00
|
|
|
|
2014-08-13 06:18:34 +00:00
|
|
|
public function __construct( $query, $moduleName, Config $conf ) {
|
2013-12-17 21:20:13 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'ex' );
|
2014-08-13 06:18:34 +00:00
|
|
|
$this->config = $conf;
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$titles = $this->getPageSet()->getGoodTitles();
|
|
|
|
if ( count( $titles ) == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$isXml = $this->getMain()->isInternalMode() || $this->getMain()->getPrinter()->getFormat() == 'XML';
|
|
|
|
$result = $this->getResult();
|
|
|
|
$params = $this->params = $this->extractRequestParams();
|
|
|
|
$this->requireMaxOneParameter( $params, 'chars', 'sentences' );
|
|
|
|
$continue = 0;
|
|
|
|
$limit = intval( $params['limit'] );
|
|
|
|
if ( $limit > 1 && !$params['intro'] ) {
|
|
|
|
$limit = 1;
|
2016-11-03 19:16:57 +00:00
|
|
|
if ( is_callable( [ $this, 'addWarning' ] ) ) {
|
|
|
|
$this->addWarning( [ 'apiwarn-textextracts-limit', $limit ] );
|
|
|
|
} else {
|
|
|
|
$this->setWarning( "exlimit was too large for a whole article extracts request, lowered to $limit" );
|
|
|
|
}
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
if ( isset( $params['continue'] ) ) {
|
|
|
|
$continue = intval( $params['continue'] );
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieContinueUsageIf( $continue < 0 || $continue > count( $titles ) );
|
2013-12-17 21:20:13 +00:00
|
|
|
$titles = array_slice( $titles, $continue, null, true );
|
|
|
|
}
|
|
|
|
$count = 0;
|
|
|
|
/** @var Title $t */
|
|
|
|
foreach ( $titles as $id => $t ) {
|
|
|
|
if ( ++$count > $limit ) {
|
|
|
|
$this->setContinueEnumParameter( 'continue', $continue + $count - 1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$text = $this->getExtract( $t );
|
2014-04-23 01:12:44 +00:00
|
|
|
$text = $this->truncate( $text );
|
2013-12-17 21:20:13 +00:00
|
|
|
if ( $this->params['plaintext'] ) {
|
|
|
|
$text = $this->doSections( $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $isXml ) {
|
2016-09-23 01:38:27 +00:00
|
|
|
$fit = $result->addValue( [ 'query', 'pages', $id ], 'extract', [ '*' => $text ] );
|
2013-12-17 21:20:13 +00:00
|
|
|
} else {
|
2016-09-23 01:38:27 +00:00
|
|
|
$fit = $result->addValue( [ 'query', 'pages', $id ], 'extract', $text );
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
if ( !$fit ) {
|
|
|
|
$this->setContinueEnumParameter( 'continue', $continue + $count - 1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 11:53:59 +00:00
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
return 'public';
|
|
|
|
}
|
|
|
|
|
2013-12-17 21:20:13 +00:00
|
|
|
/**
|
|
|
|
* Returns a processed, but not trimmed extract
|
|
|
|
* @param Title $title
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getExtract( Title $title ) {
|
2015-07-30 04:28:41 +00:00
|
|
|
$contentModel = $title->getContentModel();
|
|
|
|
if ( !in_array( $contentModel, $this->supportedContentModels, true ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
if ( is_callable( [ $this, 'addWarning' ] ) ) {
|
|
|
|
$this->addWarning(
|
|
|
|
[ 'apiwarn-textextracts-unsupportedmodel', wfEscapeWikiText( $title->getPrefixedText() ), $contentModel ]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->setWarning( "{$title->getPrefixedDBkey()} has content model '$contentModel', which is not supported; returning an empty extract." );
|
|
|
|
}
|
2015-07-30 04:28:41 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-12-17 21:20:13 +00:00
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
|
|
|
$introOnly = $this->params['intro'];
|
|
|
|
$text = $this->getFromCache( $page, $introOnly );
|
|
|
|
// if we need just first section, try retrieving full page and getting first section out of it
|
|
|
|
if ( $text === false && $introOnly ) {
|
|
|
|
$text = $this->getFromCache( $page, false );
|
|
|
|
if ( $text !== false ) {
|
|
|
|
$text = $this->getFirstSection( $text, $this->params['plaintext'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $text === false ) {
|
|
|
|
$text = $this->parse( $page );
|
2016-09-23 01:51:12 +00:00
|
|
|
$text = $this->convertText( $text );
|
2013-12-17 21:20:13 +00:00
|
|
|
$this->setCache( $page, $text );
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cacheKey( WikiPage $page, $introOnly ) {
|
2015-11-01 02:56:19 +00:00
|
|
|
return wfMemcKey( 'textextracts', $page->getId(), $page->getTouched(),
|
2014-06-19 00:57:11 +00:00
|
|
|
$page->getTitle()->getPageLanguage()->getPreferredVariant(),
|
|
|
|
$this->params['plaintext'], $introOnly
|
|
|
|
);
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getFromCache( WikiPage $page, $introOnly ) {
|
|
|
|
global $wgMemc;
|
|
|
|
|
|
|
|
$key = $this->cacheKey( $page, $introOnly );
|
|
|
|
return $wgMemc->get( $key );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setCache( WikiPage $page, $text ) {
|
|
|
|
global $wgMemc;
|
|
|
|
|
|
|
|
$key = $this->cacheKey( $page, $this->params['intro'] );
|
|
|
|
$wgMemc->set( $key, $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFirstSection( $text, $plainText ) {
|
|
|
|
if ( $plainText ) {
|
|
|
|
$regexp = '/^(.*?)(?=' . ExtractFormatter::SECTION_MARKER_START . ')/s';
|
|
|
|
} else {
|
|
|
|
$regexp = '/^(.*?)(?=<h[1-6]\b)/s';
|
|
|
|
}
|
|
|
|
if ( preg_match( $regexp, $text, $matches ) ) {
|
|
|
|
$text = $matches[0];
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns page HTML
|
|
|
|
* @param WikiPage $page
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function parse( WikiPage $page ) {
|
|
|
|
if ( !$this->parserOptions ) {
|
|
|
|
$this->parserOptions = new ParserOptions( new User( '127.0.0.1' ) );
|
|
|
|
}
|
|
|
|
// first try finding full page in parser cache
|
2016-09-23 01:58:55 +00:00
|
|
|
if ( $page->shouldCheckParserCache( $this->parserOptions, 0 ) ) {
|
2013-12-17 21:20:13 +00:00
|
|
|
$pout = ParserCache::singleton()->get( $page, $this->parserOptions );
|
|
|
|
if ( $pout ) {
|
2014-04-18 05:14:13 +00:00
|
|
|
$pout->setTOCEnabled( false );
|
2013-12-17 21:20:13 +00:00
|
|
|
$text = $pout->getText();
|
|
|
|
if ( $this->params['intro'] ) {
|
|
|
|
$text = $this->getFirstSection( $text, false );
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 01:38:27 +00:00
|
|
|
$request = [
|
2013-12-17 21:20:13 +00:00
|
|
|
'action' => 'parse',
|
|
|
|
'page' => $page->getTitle()->getPrefixedText(),
|
|
|
|
'prop' => 'text'
|
2016-09-23 01:38:27 +00:00
|
|
|
];
|
2013-12-17 21:20:13 +00:00
|
|
|
if ( $this->params['intro'] ) {
|
|
|
|
$request['section'] = 0;
|
|
|
|
}
|
|
|
|
// in case of cache miss, render just the needed section
|
2016-09-23 01:38:27 +00:00
|
|
|
$api = new ApiMain( new FauxRequest( $request ) );
|
2014-12-17 15:01:06 +00:00
|
|
|
try {
|
|
|
|
$api->execute();
|
2016-09-23 01:38:27 +00:00
|
|
|
$data = $api->getResult()->getResultData( null, [
|
|
|
|
'BC' => [],
|
|
|
|
'Types' => [],
|
|
|
|
] );
|
2016-11-03 19:16:57 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
|
|
|
if ( $e->getStatusValue()->hasMessage( 'apierror-nosuchsection' ) ) {
|
|
|
|
// Looks like we tried to get the intro to a page without
|
|
|
|
// sections! Lets just grab what we can get.
|
|
|
|
unset( $request['section'] );
|
|
|
|
$api = new ApiMain( new FauxRequest( $request ) );
|
|
|
|
$api->execute();
|
|
|
|
$data = $api->getResult()->getResultData( null, [
|
|
|
|
'BC' => [],
|
|
|
|
'Types' => [],
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
// Some other unexpected error - lets just report it to the user
|
|
|
|
// on the off chance that is the right thing.
|
|
|
|
throw $e;
|
|
|
|
}
|
2014-12-17 15:01:06 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
if ( $e->getCodeString() === 'nosuchsection' ) {
|
|
|
|
// Looks like we tried to get the intro to a page without
|
|
|
|
// sections! Lets just grab what we can get.
|
|
|
|
unset( $request['section'] );
|
2016-09-23 01:38:27 +00:00
|
|
|
$api = new ApiMain( new FauxRequest( $request ) );
|
2014-12-17 15:01:06 +00:00
|
|
|
$api->execute();
|
2016-09-23 01:38:27 +00:00
|
|
|
$data = $api->getResult()->getResultData( null, [
|
|
|
|
'BC' => [],
|
|
|
|
'Types' => [],
|
|
|
|
] );
|
2014-12-17 15:01:06 +00:00
|
|
|
} else {
|
|
|
|
// Some other unexpected error - lets just report it to the user
|
|
|
|
// on the off chance that is the right thing.
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 21:20:13 +00:00
|
|
|
return $data['parse']['text']['*'];
|
|
|
|
}
|
|
|
|
|
2015-12-09 23:33:24 +00:00
|
|
|
/**
|
2015-12-11 23:11:43 +00:00
|
|
|
* @param \ApiQuery $query
|
2015-12-09 23:33:24 +00:00
|
|
|
* @param string $action
|
2015-12-11 23:11:43 +00:00
|
|
|
* @return ApiQueryExtracts
|
2015-12-09 23:33:24 +00:00
|
|
|
*/
|
|
|
|
public static function factory( $query, $action ) {
|
2016-09-23 01:51:12 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'textextracts' );
|
2015-12-11 23:11:43 +00:00
|
|
|
return new self( $query, $action, $config );
|
2015-12-09 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
2013-12-17 21:20:13 +00:00
|
|
|
/**
|
|
|
|
* Converts page HTML into an extract
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function convertText( $text ) {
|
2014-08-13 06:18:34 +00:00
|
|
|
$fmt = new ExtractFormatter(
|
|
|
|
$text,
|
|
|
|
$this->params['plaintext'],
|
|
|
|
$this->config
|
|
|
|
);
|
2013-12-17 21:20:13 +00:00
|
|
|
$text = $fmt->getText();
|
|
|
|
|
|
|
|
return trim( $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Truncate the given text to a certain number of characters or sentences
|
|
|
|
* @param string $text The text to truncate
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-23 01:12:44 +00:00
|
|
|
private function truncate( $text ) {
|
2013-12-17 21:20:13 +00:00
|
|
|
if ( $this->params['chars'] ) {
|
|
|
|
return $this->getFirstChars( $text, $this->params['chars'] );
|
|
|
|
} elseif ( $this->params['sentences'] ) {
|
2014-04-23 01:12:44 +00:00
|
|
|
return $this->getFirstSentences( $text, $this->params['sentences'] );
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns no more than a requested number of characters
|
|
|
|
* @param string $text
|
|
|
|
* @param int $requestedLength
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getFirstChars( $text, $requestedLength ) {
|
2014-04-23 01:12:44 +00:00
|
|
|
$text = ExtractFormatter::getFirstChars( $text, $requestedLength );
|
2013-12-17 21:20:13 +00:00
|
|
|
// Fix possibly unclosed tags
|
|
|
|
$text = $this->tidy( $text );
|
|
|
|
$text .= wfMessage( 'ellipsis' )->inContentLanguage()->text();
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $text
|
|
|
|
* @param int $requestedSentenceCount
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-23 01:12:44 +00:00
|
|
|
private function getFirstSentences( $text, $requestedSentenceCount ) {
|
|
|
|
$text = ExtractFormatter::getFirstSentences( $text, $requestedSentenceCount );
|
2013-12-17 21:20:13 +00:00
|
|
|
$text = $this->tidy( $text );
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple wrapper around tidy
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function tidy( $text ) {
|
2014-08-13 06:18:34 +00:00
|
|
|
if ( $this->getConfig()->get( 'UseTidy' ) && !$this->params['plaintext'] ) {
|
2016-09-23 01:38:27 +00:00
|
|
|
$text = trim( MWTidy::tidy( $text ) );
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doSections( $text ) {
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
"/" . ExtractFormatter::SECTION_MARKER_START . '(\d)'. ExtractFormatter::SECTION_MARKER_END . "(.*?)$/m",
|
2016-09-23 01:38:27 +00:00
|
|
|
[ $this, 'sectionCallback' ],
|
2013-12-17 21:20:13 +00:00
|
|
|
$text
|
|
|
|
);
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sectionCallback( $matches ) {
|
|
|
|
if ( $this->params['sectionformat'] == 'raw' ) {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
$func = __CLASS__ . "::doSection_{$this->params['sectionformat']}";
|
|
|
|
return call_user_func( $func, $matches[1], trim( $matches[2] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function doSection_wiki( $level, $text ) {
|
|
|
|
$bars = str_repeat( '=', $level );
|
|
|
|
return "\n$bars $text $bars";
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function doSection_plain( $level, $text ) {
|
|
|
|
return "\n$text";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-09-23 01:38:27 +00:00
|
|
|
return [
|
|
|
|
'chars' => [
|
2013-12-17 21:20:13 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
2016-09-23 01:38:27 +00:00
|
|
|
],
|
|
|
|
'sentences' => [
|
2013-12-17 21:20:13 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
ApiBase::PARAM_MAX => 10,
|
2016-09-23 01:38:27 +00:00
|
|
|
],
|
|
|
|
'limit' => [
|
2013-12-17 21:20:13 +00:00
|
|
|
ApiBase::PARAM_DFLT => 1,
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
ApiBase::PARAM_MAX => 20,
|
|
|
|
ApiBase::PARAM_MAX2 => 20,
|
2016-09-23 01:38:27 +00:00
|
|
|
],
|
2013-12-17 21:20:13 +00:00
|
|
|
'intro' => false,
|
|
|
|
'plaintext' => false,
|
2016-09-23 01:38:27 +00:00
|
|
|
'sectionformat' => [
|
|
|
|
ApiBase::PARAM_TYPE => [ 'plain', 'wiki', 'raw' ],
|
2013-12-17 21:20:13 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'wiki',
|
2016-09-23 01:38:27 +00:00
|
|
|
],
|
|
|
|
'continue' => [
|
2013-12-17 21:20:13 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
2016-09-20 19:23:03 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
2016-09-23 01:38:27 +00:00
|
|
|
],
|
|
|
|
];
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 18:50:23 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2016-09-23 01:38:27 +00:00
|
|
|
return [
|
2014-10-29 18:50:23 +00:00
|
|
|
'action=query&prop=extracts&exchars=175&titles=Therion'
|
|
|
|
=> 'apihelp-query+extracts-example-1',
|
2016-09-23 01:38:27 +00:00
|
|
|
];
|
2014-10-29 18:50:23 +00:00
|
|
|
}
|
2013-12-17 21:20:13 +00:00
|
|
|
|
|
|
|
public function getHelpUrls() {
|
2014-01-14 22:51:08 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Extension:TextExtracts#API';
|
2013-12-17 21:20:13 +00:00
|
|
|
}
|
|
|
|
}
|