Skip to content

Commit

Permalink
Change all arrays to use the short PHP 5.5 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
thiemowmde committed May 16, 2017
1 parent 59ae9a1 commit 5a8923a
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 151 deletions.
8 changes: 4 additions & 4 deletions PropertySuggester.alias.php
Expand Up @@ -6,9 +6,9 @@
* @ingroup Extensions
*/

$specialPageAliases = array();
$specialPageAliases = [];

/** English (English) */
$specialPageAliases['en'] = array(
'PropertySuggester' => array( 'PropertySuggester' ),
);
$specialPageAliases['en'] = [
'PropertySuggester' => [ 'PropertySuggester' ],
];
1 change: 0 additions & 1 deletion phpcs.xml
Expand Up @@ -2,7 +2,6 @@
<ruleset name="WikibaseMediaInfo">
<rule ref="vendor/wikibase/wikibase-codesniffer/Wikibase">
<!-- FIXME: The following should all be fixed. -->
<exclude name="Generic.Arrays.DisallowLongArraySyntax" />
<exclude name="MediaWiki.ControlStructures.AssignmentInControlStructures" />
<exclude name="PSR1.Classes.ClassDeclaration" />
</rule>
Expand Down
46 changes: 23 additions & 23 deletions src/PropertySuggester/GetSuggestions.php
Expand Up @@ -147,27 +147,27 @@ public function execute() {
private function querySearchApi( $resultSize, $search, $language ) {
$searchEntitiesParameters = new DerivativeRequest(
$this->getRequest(),
array(
[
'limit' => $resultSize + 1,
'continue' => 0,
'search' => $search,
'action' => 'wbsearchentities',
'language' => $language,
'uselang' => $language,
'type' => Property::ENTITY_TYPE
)
]
);

$api = new ApiMain( $searchEntitiesParameters );
$api->execute();

$apiResult = $api->getResult()->getResultData(
null,
array(
'BC' => array(),
'Types' => array(),
[
'BC' => [],
'Types' => [],
'Strip' => 'all'
)
]
);

return $apiResult['search'];
Expand All @@ -177,43 +177,43 @@ private function querySearchApi( $resultSize, $search, $language ) {
* @see ApiBase::getAllowedParams()
*/
public function getAllowedParams() {
return array(
'entity' => array(
return [
'entity' => [
ApiBase::PARAM_TYPE => 'string',
),
'properties' => array(
],
'properties' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_ISMULTI => true
),
'limit' => array(
],
'limit' => [
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_DFLT => 7,
ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2,
ApiBase::PARAM_MIN => 0,
ApiBase::PARAM_RANGE_ENFORCE => true,
),
],
'continue' => null,
'language' => array(
'language' => [
ApiBase::PARAM_TYPE => $this->languageCodes,
ApiBase::PARAM_DFLT => $this->getContext()->getLanguage()->getCode(),
),
'context' => array(
ApiBase::PARAM_TYPE => array( 'item', 'qualifier', 'reference' ),
],
'context' => [
ApiBase::PARAM_TYPE => [ 'item', 'qualifier', 'reference' ],
ApiBase::PARAM_DFLT => 'item',
),
'search' => array(
],
'search' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => '',
)
);
],
];
}

/**
* @see ApiBase::getExamplesMessages()
*/
public function getExamplesMessages() {
return array(
return [
'action=wbsgetsuggestions&entity=Q4'
=> 'apihelp-wbsgetsuggestions-example-1',
'action=wbsgetsuggestions&entity=Q4&continue=10&limit=5'
Expand All @@ -224,7 +224,7 @@ public function getExamplesMessages() {
=> 'apihelp-wbsgetsuggestions-example-4',
'action=wbsgetsuggestions&properties=P21&context=reference'
=> 'apihelp-wbsgetsuggestions-example-5'
);
];
}

}
25 changes: 13 additions & 12 deletions src/PropertySuggester/ResultBuilder.php
Expand Up @@ -61,8 +61,8 @@ public function __construct(
* @return array[]
*/
public function createResultArray( array $suggestions, $language ) {
$entries = array();
$ids = array();
$entries = [];
$ids = [];
foreach ( $suggestions as $suggestion ) {
$id = $suggestion->getPropertyId();
$ids[] = $id;
Expand All @@ -71,7 +71,7 @@ public function createResultArray( array $suggestions, $language ) {
$terms = $this->termIndex->getTermsOfEntities(
$ids,
null,
array( $language )
[ $language ]
);
$clusteredTerms = $this->clusterTerms( $terms );

Expand All @@ -89,16 +89,17 @@ public function createResultArray( array $suggestions, $language ) {
* @return array
*/
private function buildEntry( EntityId $id, array $clusteredTerms, Suggestion $suggestion ) {
$entry = array();
$entry['id'] = $id->getSerialization();
$entry['url'] = $this->entityTitleLookup->getTitleForId( $id )->getFullUrl();
$entry['rating'] = $suggestion->getProbability();
$entry = [
'id' => $id->getSerialization(),
'url' => $this->entityTitleLookup->getTitleForId( $id )->getFullUrl(),
'rating' => $suggestion->getProbability(),
];

/** @var TermIndexEntry[] $matchingTerms */
if ( isset( $clusteredTerms[$id->getSerialization()] ) ) {
$matchingTerms = $clusteredTerms[$id->getSerialization()];
} else {
$matchingTerms = array();
$matchingTerms = [];
}

foreach ( $matchingTerms as $term ) {
Expand Down Expand Up @@ -130,12 +131,12 @@ private function buildEntry( EntityId $id, array $clusteredTerms, Suggestion $su
* @return TermIndexEntry[][]
*/
private function clusterTerms( array $terms ) {
$clusteredTerms = array();
$clusteredTerms = [];

foreach ( $terms as $term ) {
$id = $term->getEntityId()->getSerialization();
if ( !isset( $clusteredTerms[$id] ) ) {
$clusteredTerms[$id] = array();
$clusteredTerms[$id] = [];
}
$clusteredTerms[$id][] = $term;
}
Expand All @@ -154,7 +155,7 @@ private function checkAndSetAlias( array &$entry, TermIndexEntry $term ) {

if ( preg_match( $this->searchPattern, $term->getText() ) ) {
if ( !isset( $entry['aliases'] ) ) {
$entry['aliases'] = array();
$entry['aliases'] = [];
ApiResult::setIndexedTagName( $entry['aliases'], 'alias' );
}
$entry['aliases'][] = $term->getText();
Expand All @@ -169,7 +170,7 @@ private function checkAndSetAlias( array &$entry, TermIndexEntry $term ) {
*/
public function mergeWithTraditionalSearchResults( array &$entries, array $searchResults, $resultSize ) {
// Avoid duplicates
$existingKeys = array();
$existingKeys = [];
foreach ( $entries as $entry ) {
$existingKeys[$entry['id']] = true;
}
Expand Down
35 changes: 20 additions & 15 deletions src/PropertySuggester/Suggesters/SimpleSuggester.php
Expand Up @@ -23,17 +23,17 @@ class SimpleSuggester implements SuggesterEngine {
/**
* @var int[]
*/
private $deprecatedPropertyIds = array();
private $deprecatedPropertyIds = [];

/**
* @var array Numeric property ids as keys, values are meaningless.
*/
private $classifyingPropertyIds = array();
private $classifyingPropertyIds = [];

/**
* @var Suggestion[]
*/
private $initialSuggestions = array();
private $initialSuggestions = [];

/**
* @var LoadBalancer
Expand Down Expand Up @@ -65,7 +65,7 @@ public function setClassifyingPropertyIds( array $classifyingPropertyIds ) {
* @param int[] $initialSuggestionIds
*/
public function setInitialSuggestions( array $initialSuggestionIds ) {
$suggestions = array();
$suggestions = [];
foreach ( $initialSuggestionIds as $id ) {
$suggestions[] = new Suggestion( PropertyId::newFromNumber( $id ), 1.0 );
}
Expand Down Expand Up @@ -110,18 +110,23 @@ private function getSuggestions( array $propertyIds, array $idTuples, $limit, $m
}
$res = $dbr->select(
'wbs_propertypairs',
array( 'pid' => 'pid2', 'prob' => "sum(probability)/$count" ),
array( $condition,
'pid2 NOT IN (' . $dbr->makeList( $excludedIds ) . ')',
'context' => $context ),
[
'pid' => 'pid2',
'prob' => "sum(probability)/$count",
],
[
$condition,
'pid2 NOT IN (' . $dbr->makeList( $excludedIds ) . ')',
'context' => $context,
],
__METHOD__,
array(
[
'GROUP BY' => 'pid2',
'ORDER BY' => 'prob DESC',
'LIMIT' => $limit,
'HAVING' => 'prob > ' . floatval( $minProbability )
)
);
]
);
$this->lb->reuseConnection( $dbr );

return $this->buildResult( $res );
Expand All @@ -141,7 +146,7 @@ public function suggestByPropertyIds( array $propertyIds, $limit, $minProbabilit
return $propertyId->getNumericId();
}, $propertyIds );

return $this->getSuggestions( $numericIds, array(), $limit, $minProbability, $context );
return $this->getSuggestions( $numericIds, [], $limit, $minProbability, $context );
}

/**
Expand All @@ -155,8 +160,8 @@ public function suggestByPropertyIds( array $propertyIds, $limit, $minProbabilit
* @return Suggestion[]
*/
public function suggestByItem( Item $item, $limit, $minProbability, $context ) {
$ids = array();
$idTuples = array();
$ids = [];
$idTuples = [];

foreach ( $item->getStatements()->toArray() as $statement ) {
$mainSnak = $statement->getMainSnak();
Expand Down Expand Up @@ -209,7 +214,7 @@ private function buildTupleCondition( $pid, $qid ) {
* @return Suggestion[]
*/
private function buildResult( ResultWrapper $res ) {
$resultArray = array();
$resultArray = [];
foreach ( $res as $row ) {
$pid = PropertyId::newFromNumber( $row->pid );
$suggestion = new Suggestion( $pid, $row->prob );
Expand Down
24 changes: 12 additions & 12 deletions src/PropertySuggester/SuggestionGenerator.php
Expand Up @@ -75,7 +75,7 @@ public function generateSuggestionsByItem( $itemIdString, $limit, $minProbabilit
* @return Suggestion[]
*/
public function generateSuggestionsByPropertyList( array $propertyIdList, $limit, $minProbability, $context ) {
$propertyIds = array();
$propertyIds = [];
foreach ( $propertyIdList as $stringId ) {
$propertyIds[] = new PropertyId( $stringId );
}
Expand All @@ -97,12 +97,12 @@ public function filterSuggestions( array $suggestions, $search, $language, $resu
}
$ids = $this->getMatchingIDs( $search, $language );

$id_set = array();
$id_set = [];
foreach ( $ids as $id ) {
$id_set[$id->getNumericId()] = true;
}

$matching_suggestions = array();
$matching_suggestions = [];
$count = 0;
foreach ( $suggestions as $suggestion ) {
if ( array_key_exists( $suggestion->getPropertyId()->getNumericId(), $id_set ) ) {
Expand All @@ -122,24 +122,24 @@ public function filterSuggestions( array $suggestions, $search, $language, $resu
*/
private function getMatchingIDs( $search, $language ) {
$termIndexEntries = $this->termIndex->getTopMatchingTerms(
array(
new TermIndexSearchCriteria( array(
[
new TermIndexSearchCriteria( [
'termLanguage' => $language,
'termText' => $search
) )
),
array(
] )
],
[
TermIndexEntry::TYPE_LABEL,
TermIndexEntry::TYPE_ALIAS,
),
],
Property::ENTITY_TYPE,
array(
[
'caseSensitive' => false,
'prefixSearch' => true,
)
]
);

$ids = array();
$ids = [];
foreach ( $termIndexEntries as $entry ) {
$ids[] = $entry->getEntityId();
}
Expand Down
16 changes: 11 additions & 5 deletions src/PropertySuggester/UpdateTable/Importer/BasicImporter.php
Expand Up @@ -42,11 +42,11 @@ public function importFromCsvFileToDb( ImportContext $importContext ) {
* @throws UnexpectedValueException
*/
private function doImport( $fileHandle, Database $db, ImportContext $importContext ) {
$accumulator = array();
$accumulator = [];
$batchSize = $importContext->getBatchSize();
$i = 0;
$header = fgetcsv( $fileHandle, 0, $importContext->getCsvDelimiter() ); //this is to get the csv-header
$expectedHeader = array( 'pid1', 'qid1', 'pid2', 'count', 'probability', 'context' );
$expectedHeader = [ 'pid1', 'qid1', 'pid2', 'count', 'probability', 'context' ];
if ( $header != $expectedHeader ) {
throw new UnexpectedValueException( "provided csv-file does not match the expected format:\n" . join( ',', $expectedHeader ) );
}
Expand All @@ -60,16 +60,22 @@ private function doImport( $fileHandle, Database $db, ImportContext $importConte
if ( ! $importContext->isQuiet() ) {
print "$i rows inserted\n";
}
$accumulator = array();
$accumulator = [];
if ( $data == false ) {
break;
}
}

$qid1 = is_numeric( $data[1] ) ? $data[1] : 0;

$accumulator[] = array( 'pid1' => $data[0], 'qid1' => $qid1, 'pid2' => $data[2], 'count' => $data[3],
'probability' => $data[4], 'context' => $data[5] );
$accumulator[] = [
'pid1' => $data[0],
'qid1' => $qid1,
'pid2' => $data[2],
'count' => $data[3],
'probability' => $data[4],
'context' => $data[5],
];
}
}

Expand Down

0 comments on commit 5a8923a

Please sign in to comment.