mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-15 03:35:20 +00:00
Merge "CodeSniffer fixes"
This commit is contained in:
commit
27c935f66c
|
@ -48,7 +48,7 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $supportedContentModels = array( 'wikitext' );
|
||||
private $supportedContentModels = [ 'wikitext' ];
|
||||
|
||||
public function __construct( $query, $moduleName, Config $conf ) {
|
||||
parent::__construct( $query, $moduleName, 'ex' );
|
||||
|
@ -91,9 +91,9 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
}
|
||||
|
||||
if ( $isXml ) {
|
||||
$fit = $result->addValue( array( 'query', 'pages', $id ), 'extract', array( '*' => $text ) );
|
||||
$fit = $result->addValue( [ 'query', 'pages', $id ], 'extract', [ '*' => $text ] );
|
||||
} else {
|
||||
$fit = $result->addValue( array( 'query', 'pages', $id ), 'extract', $text );
|
||||
$fit = $result->addValue( [ 'query', 'pages', $id ], 'extract', $text );
|
||||
}
|
||||
if ( !$fit ) {
|
||||
$this->setContinueEnumParameter( 'continue', $continue + $count - 1 );
|
||||
|
@ -196,33 +196,33 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
return $text;
|
||||
}
|
||||
}
|
||||
$request = array(
|
||||
$request = [
|
||||
'action' => 'parse',
|
||||
'page' => $page->getTitle()->getPrefixedText(),
|
||||
'prop' => 'text'
|
||||
);
|
||||
];
|
||||
if ( $this->params['intro'] ) {
|
||||
$request['section'] = 0;
|
||||
}
|
||||
// in case of cache miss, render just the needed section
|
||||
$api = new ApiMain( new FauxRequest( $request ) );
|
||||
$api = new ApiMain( new FauxRequest( $request ) );
|
||||
try {
|
||||
$api->execute();
|
||||
$data = $api->getResult()->getResultData( null, array(
|
||||
'BC' => array(),
|
||||
'Types' => array(),
|
||||
) );
|
||||
$data = $api->getResult()->getResultData( null, [
|
||||
'BC' => [],
|
||||
'Types' => [],
|
||||
] );
|
||||
} 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'] );
|
||||
$api = new ApiMain( new FauxRequest( $request ) );
|
||||
$api = new ApiMain( new FauxRequest( $request ) );
|
||||
$api->execute();
|
||||
$data = $api->getResult()->getResultData( null, array(
|
||||
'BC' => array(),
|
||||
'Types' => array(),
|
||||
) );
|
||||
$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.
|
||||
|
@ -304,7 +304,7 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
*/
|
||||
private function tidy( $text ) {
|
||||
if ( $this->getConfig()->get( 'UseTidy' ) && !$this->params['plaintext'] ) {
|
||||
$text = trim ( MWTidy::tidy( $text ) );
|
||||
$text = trim( MWTidy::tidy( $text ) );
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
|
@ -312,7 +312,7 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
private function doSections( $text ) {
|
||||
$text = preg_replace_callback(
|
||||
"/" . ExtractFormatter::SECTION_MARKER_START . '(\d)'. ExtractFormatter::SECTION_MARKER_END . "(.*?)$/m",
|
||||
array( $this, 'sectionCallback' ),
|
||||
[ $this, 'sectionCallback' ],
|
||||
$text
|
||||
);
|
||||
return $text;
|
||||
|
@ -336,44 +336,44 @@ class ApiQueryExtracts extends ApiQueryBase {
|
|||
}
|
||||
|
||||
public function getAllowedParams() {
|
||||
return array(
|
||||
'chars' => array(
|
||||
return [
|
||||
'chars' => [
|
||||
ApiBase::PARAM_TYPE => 'integer',
|
||||
ApiBase::PARAM_MIN => 1,
|
||||
),
|
||||
'sentences' => array(
|
||||
],
|
||||
'sentences' => [
|
||||
ApiBase::PARAM_TYPE => 'integer',
|
||||
ApiBase::PARAM_MIN => 1,
|
||||
ApiBase::PARAM_MAX => 10,
|
||||
),
|
||||
'limit' => array(
|
||||
],
|
||||
'limit' => [
|
||||
ApiBase::PARAM_DFLT => 1,
|
||||
ApiBase::PARAM_TYPE => 'limit',
|
||||
ApiBase::PARAM_MIN => 1,
|
||||
ApiBase::PARAM_MAX => 20,
|
||||
ApiBase::PARAM_MAX2 => 20,
|
||||
),
|
||||
],
|
||||
'intro' => false,
|
||||
'plaintext' => false,
|
||||
'sectionformat' => array(
|
||||
ApiBase::PARAM_TYPE => array( 'plain', 'wiki', 'raw' ),
|
||||
'sectionformat' => [
|
||||
ApiBase::PARAM_TYPE => [ 'plain', 'wiki', 'raw' ],
|
||||
ApiBase::PARAM_DFLT => 'wiki',
|
||||
),
|
||||
'continue' => array(
|
||||
],
|
||||
'continue' => [
|
||||
ApiBase::PARAM_TYPE => 'integer',
|
||||
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ApiBase::getExamplesMessages()
|
||||
*/
|
||||
protected function getExamplesMessages() {
|
||||
return array(
|
||||
return [
|
||||
'action=query&prop=extracts&exchars=175&titles=Therion'
|
||||
=> 'apihelp-query+extracts-example-1',
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
public function getHelpUrls() {
|
||||
|
|
|
@ -45,7 +45,7 @@ class ExtractFormatter extends HtmlFormatter {
|
|||
if ( $plainText ) {
|
||||
$this->flattenAllTags();
|
||||
} else {
|
||||
$this->flatten( array( 'a' ) );
|
||||
$this->flatten( [ 'a' ] );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ class ExtractFormatter extends HtmlFormatter {
|
|||
public function onHtmlReady( $html ) {
|
||||
if ( $this->plainText ) {
|
||||
$html = preg_replace( '/\s*(<h([1-6])\b)/i',
|
||||
"\n\n" . self::SECTION_MARKER_START . '$2' . self::SECTION_MARKER_END . '$1' ,
|
||||
"\n\n" . self::SECTION_MARKER_START . '$2' . self::SECTION_MARKER_END . '$1',
|
||||
$html
|
||||
);
|
||||
}
|
||||
|
@ -80,21 +80,21 @@ class ExtractFormatter extends HtmlFormatter {
|
|||
*/
|
||||
public static function getFirstSentences( $text, $requestedSentenceCount ) {
|
||||
// Based on code from OpenSearchXml by Brion Vibber
|
||||
$endchars = array(
|
||||
$endchars = [
|
||||
'[^\p{Lu}]\.(?:[ \n]|$)', '[\!\?](?:[ \n]|$)', // regular ASCII
|
||||
'。', // full-width ideographic full-stop
|
||||
'.', '!', '?', // double-width roman forms
|
||||
'。', // half-width ideographic full stop
|
||||
);
|
||||
];
|
||||
|
||||
$endgroup = implode( '|', $endchars );
|
||||
$end = "(?:$endgroup)";
|
||||
$sentence = ".+?$end+";
|
||||
$requestedSentenceCount = intval( $requestedSentenceCount );
|
||||
$regexp = "/^($sentence){1,{$requestedSentenceCount}}/u";
|
||||
$matches = array();
|
||||
$matches = [];
|
||||
$res = preg_match( $regexp, $text, $matches );
|
||||
if( $res ) {
|
||||
if ( $res ) {
|
||||
$text = trim( $matches[0] );
|
||||
} else {
|
||||
if ( $res === false ) {
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace TextExtracts;
|
||||
|
||||
|
||||
use ApiMain;
|
||||
use ApiResult;
|
||||
use ConfigFactory;
|
||||
|
@ -22,17 +21,17 @@ class Hooks {
|
|||
}
|
||||
$pageIds = array_keys( $results );
|
||||
$api = new ApiMain( new FauxRequest(
|
||||
array(
|
||||
[
|
||||
'action' => 'query',
|
||||
'prop' => 'extracts',
|
||||
'explaintext' => true,
|
||||
'exintro' => true,
|
||||
'exlimit' => count( $results ),
|
||||
'pageids' => implode( '|', $pageIds ),
|
||||
) )
|
||||
] )
|
||||
);
|
||||
$api->execute();
|
||||
$data = $api->getResult()->getResultData( array( 'query', 'pages' ) );
|
||||
$data = $api->getResult()->getResultData( [ 'query', 'pages' ] );
|
||||
foreach ( $pageIds as $id ) {
|
||||
$contentKey = isset( $data[$id]['extract'][ApiResult::META_CONTENT] )
|
||||
? $data[$id]['extract'][ApiResult::META_CONTENT]
|
||||
|
|
|
@ -22,29 +22,29 @@ class ExtractFormatterTest extends MediaWikiTestCase {
|
|||
public function provideExtracts() {
|
||||
$dutch = '<b>Dutch</b> (<span class="unicode haudio" style="white-space:nowrap;"><span class="fn"><a href="/wiki/File:Nl-Nederlands.ogg" title="About this sound"><img alt="About this sound" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/11px-Loudspeaker.svg.png" width="11" height="11" srcset="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/17px-Loudspeaker.svg.png 1.5x, https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Loudspeaker.svg/22px-Loudspeaker.svg.png 2x" /></a> <a href="https://upload.wikimedia.org/wikipedia/commons/d/db/Nl-Nederlands.ogg" class="internal" title="Nl-Nederlands.ogg"><i>Nederlands</i></a></span> <small class="metadata audiolinkinfo" style="cursor:help;">(<a href="/w/index.php?title=Wikipedia:Media_help&action=edit&redlink=1" class="new" title="Wikipedia:Media help (page does not exist)"><span style="cursor:help;">help</span></a>·<a href="/wiki/File:Nl-Nederlands.ogg" title="File:Nl-Nederlands.ogg"><span style="cursor:help;">info</span></a>)</small></span>) is a <a href="/w/index.php?title=West_Germanic_languages&action=edit&redlink=1" class="new" title="West Germanic languages (page does not exist)">West Germanic language</a> and the native language of most of the population of the <a href="/w/index.php?title=Netherlands&action=edit&redlink=1" class="new" title="Netherlands (page does not exist)">Netherlands</a>';
|
||||
|
||||
return array(
|
||||
array(
|
||||
return [
|
||||
[
|
||||
"Dutch ( Nederlands ) is a West Germanic language and the native language of most of the population of the Netherlands",
|
||||
$dutch,
|
||||
true,
|
||||
),
|
||||
],
|
||||
|
||||
array(
|
||||
[
|
||||
"<span><span lang=\"baz\">qux</span></span>",
|
||||
'<span class="foo"><span lang="baz">qux</span></span>',
|
||||
false,
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
"<span><span lang=\"baz\">qux</span></span>",
|
||||
'<span style="foo: bar;"><span lang="baz">qux</span></span>',
|
||||
false,
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
"<span><span lang=\"qux\">quux</span></span>",
|
||||
'<span class="foo"><span style="bar: baz;" lang="qux">quux</span></span>',
|
||||
false,
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,68 +58,68 @@ class ExtractFormatterTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function provideGetFirstSentences() {
|
||||
return array(
|
||||
array(
|
||||
return [
|
||||
[
|
||||
'Foo is a bar. Such a smart boy. But completely useless.',
|
||||
2,
|
||||
'Foo is a bar. Such a smart boy.',
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
'Foo is a bar. Such a smart boy. But completely useless.',
|
||||
1,
|
||||
'Foo is a bar.',
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
'Foo is a bar. Such a smart boy.',
|
||||
2,
|
||||
'Foo is a bar. Such a smart boy.',
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
'Foo is a bar.',
|
||||
1,
|
||||
'Foo is a bar.',
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
'Foo is a bar.',
|
||||
2,
|
||||
'Foo is a bar.',
|
||||
),
|
||||
array(
|
||||
],
|
||||
[
|
||||
'',
|
||||
1,
|
||||
'',
|
||||
),
|
||||
],
|
||||
// Exclamation points too!!!
|
||||
array(
|
||||
[
|
||||
'Foo is a bar! Such a smart boy! But completely useless!',
|
||||
1,
|
||||
'Foo is a bar!',
|
||||
),
|
||||
],
|
||||
// A tricky one
|
||||
array(
|
||||
[
|
||||
"Acid phosphatase (EC 3.1.3.2) is a chemical you don't want to mess with. Polyvinyl acetate, however, is another story.",
|
||||
1,
|
||||
"Acid phosphatase (EC 3.1.3.2) is a chemical you don't want to mess with.",
|
||||
),
|
||||
],
|
||||
// Bug T118621
|
||||
array(
|
||||
[
|
||||
'Foo was born in 1977. He enjoys listening to Siouxsie and the Banshees.',
|
||||
1,
|
||||
'Foo was born in 1977.',
|
||||
),
|
||||
],
|
||||
// Bug T115795 - Test no cropping after initials
|
||||
array(
|
||||
[
|
||||
'P.J. Harvey is a singer. She is awesome!',
|
||||
1,
|
||||
'P.J. Harvey is a singer.',
|
||||
),
|
||||
],
|
||||
// Bug T115817 - Non-breaking space is not a delimiter
|
||||
array(
|
||||
[
|
||||
html_entity_decode( 'Pigeons (lat. Columbidae) are birds. They primarily feed on seeds.' ),
|
||||
1,
|
||||
html_entity_decode( 'Pigeons (lat. Columbidae) are birds.' ),
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -134,13 +134,13 @@ class ExtractFormatterTest extends MediaWikiTestCase {
|
|||
|
||||
public function provideGetFirstChars() {
|
||||
$text = 'Lullzy lulz are lullzy!';
|
||||
return array(
|
||||
//array( $text, 0, '' ),
|
||||
array( $text, 100, $text ),
|
||||
array( $text, 1, 'Lullzy' ),
|
||||
array( $text, 6, 'Lullzy' ),
|
||||
//array( $text, 7, 'Lullzy' ),
|
||||
array( $text, 8, 'Lullzy lulz' ),
|
||||
);
|
||||
return [
|
||||
// [ $text, 0, '' ],
|
||||
[ $text, 100, $text ],
|
||||
[ $text, 1, 'Lullzy' ],
|
||||
[ $text, 6, 'Lullzy' ],
|
||||
// [ $text, 7, 'Lullzy' ],
|
||||
[ $text, 8, 'Lullzy lulz' ],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue