Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge "Removed url args for usekeys/nousekeys, and also setting throu…
Browse files Browse the repository at this point in the history
…gh config"
  • Loading branch information
tobijat authored and Gerrit Code Review committed Aug 10, 2012
2 parents 18b862a + 399d443 commit aed7add
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 57 deletions.
7 changes: 1 addition & 6 deletions repo/Wikibase.hooks.php
Expand Up @@ -396,10 +396,6 @@ public static function onWikibaseDefaultSettings( array &$settings ) {
// set to true will always delete empty items
'apiDeleteEmpty' => false,

// Defaults to turn off use of keys
// set to true will always return the key form
'apiUseKeys' => true,

// Set API in debug mode
// do not turn on in production!
'apiInDebug' => false,
Expand All @@ -411,7 +407,7 @@ public static function onWikibaseDefaultSettings( array &$settings ) {
'apiDebugWithRights' => false,
'apiDebugWithTokens' => false,

// Which formats to use with keys when there are a "usekeys" in the URL
// Which formats to inject keys into
// undefined entries are interpreted as false
'formatsWithKeys' => array(
'json' => true,
Expand All @@ -424,7 +420,6 @@ public static function onWikibaseDefaultSettings( array &$settings ) {
'xmlfm' => false,
'yaml' => true,
'yamlfm' => true,
'raw' => true,
'rawfm' => true,
'txtfm' => true,
'dbg' => true,
Expand Down
66 changes: 20 additions & 46 deletions repo/includes/api/Api.php
Expand Up @@ -16,27 +16,19 @@
* @author John Erling Blad < jeblad@gmail.com >
*/
abstract class Api extends \ApiBase {

/**
* Var to keep the set status for later use
* @var bool how to handle the keys
*/
protected $usekeys = false;

/**
* Sets the usekeys-state for later use (and misuse)
* Figure out the usekeys-state
*
* @param $params array parameters requested in subclass
* @return bool true if the keys should be present
*/
protected function setUsekeys( array $params ) {
$usekeys = Settings::get( 'apiUseKeys' ) || ( isset( $params['usekeys'] ) ? $params['usekeys'] : false );

if ( $usekeys ) {
$format = $this->getMain()->getRequest()->getVal( 'format' );
protected function getUsekeys() {
static $withkeys = false;
if ( $withkeys === false ) {
$withkeys = Settings::get( 'formatsWithKeys' );
$usekeys = isset( $format ) && isset( $withkeys[$format] ) ? $withkeys[$format] : false;
}

$this->usekeys = $usekeys;
$format = $this->getMain()->getRequest()->getVal( 'format' );
return ( isset( $format ) && isset( $withkeys[$format] ) ) ? $withkeys[$format] : false ;
}

/**
Expand All @@ -52,41 +44,23 @@ public function getPossibleErrors() {
* @see ApiBase::getParamDescription()
*/
public function getParamDescription() {
$descriptions = array(
return array(
'gettoken' => array( 'If set, a new "modifyitem" token will be returned if the request completes.',
'The remaining of the call must be valid, otherwise an error can be returned without the token included.'
),
);
if ( Settings::get( 'apiUseKeys' ) ) {
$descriptions['nousekeys'] = array( 'Turn off use the keys. The use of keys are only used in formats that supports them,',
'otherwise fall back to the ordinary style which is to use keys.'
);
}
else {
$descriptions['usekeys'] = array( 'Turn on use the keys. The use of keys are only used in formats that supports them,',
'otherwise fall back to the ordinary style which is to use keys.'
);
}
return $descriptions;
}

/**
* @see ApiBase::getAllowedParams()
*/
public function getAllowedParams() {
$allowedParams = array(
return array(
'gettoken' => array(
ApiBase::PARAM_TYPE => 'boolean',
ApiBase::PARAM_DFLT => false
),
);
if ( Settings::get( 'apiUseKeys' ) ) {
$allowedParams['nousekeys'] = array( \ApiBase::PARAM_TYPE => 'boolean' );
}
else {
$allowedParams['usekeys'] = array( \ApiBase::PARAM_TYPE => 'boolean' );
}
return $allowedParams;
}

/**
Expand Down Expand Up @@ -131,7 +105,7 @@ protected function addTokenToResult( $token, $path=null, $name = 'itemtoken' ) {
protected function addAliasesToResult( array $aliases, $path, $name = 'aliases', $tag = 'alias' ) {
$value = array();

if ( $this->usekeys ) {
if ( $this->getUsekeys() ) {
foreach ( $aliases as $languageCode => $alarr ) {
$arr = array();
foreach ( $alarr as $alias ) {
Expand All @@ -155,7 +129,7 @@ protected function addAliasesToResult( array $aliases, $path, $name = 'aliases',
}

if ( $value !== array() ) {
if (!$this->usekeys) {
if ( !$this->getUsekeys() ) {
$this->getResult()->setIndexedTagName( $value, $tag );
}
$this->getResult()->addValue( $path, $name, $value );
Expand Down Expand Up @@ -202,12 +176,12 @@ protected function addSiteLinksToResult( array $siteLinks, $path, $name = 'sitel
}
}

$key = $this->usekeys ? $link->getSiteID() : $idx++;
$key = $this->getUsekeys() ? $link->getSiteID() : $idx++;
$value[$key] = $response;
}

if ( $value !== array() ) {
if ( !$this->usekeys ) {
if ( !$this->getUsekeys() ) {
$this->getResult()->setIndexedTagName( $value, $tag );
}

Expand All @@ -233,21 +207,21 @@ protected function addDescriptionsToResult( array $descriptions, $path, $name =

foreach ( $descriptions as $languageCode => $description ) {
if ( $description === '' ) {
$value[$this->usekeys ? $languageCode : $idx++] = array(
$value[$this->getUsekeys() ? $languageCode : $idx++] = array(
'language' => $languageCode,
'removed' => '',
);
}
else {
$value[$this->usekeys ? $languageCode : $idx++] = array(
$value[$this->getUsekeys() ? $languageCode : $idx++] = array(
'language' => $languageCode,
'value' => $description,
);
}
}

if ( $value !== array() ) {
if (!$this->usekeys) {
if ( !$this->getUsekeys() ) {
$this->getResult()->setIndexedTagName( $value, $tag );
}
$this->getResult()->addValue( $path, $name, $value );
Expand All @@ -272,21 +246,21 @@ protected function addLabelsToResult( array $labels, $path, $name = 'labels', $t

foreach ( $labels as $languageCode => $label ) {
if ( $label === '' ) {
$value[$this->usekeys ? $languageCode : $idx++] = array(
$value[$this->getUsekeys() ? $languageCode : $idx++] = array(
'language' => $languageCode,
'removed' => '',
);
}
else {
$value[$this->usekeys ? $languageCode : $idx++] = array(
$value[$this->getUsekeys() ? $languageCode : $idx++] = array(
'language' => $languageCode,
'value' => $label,
);
}
}

if ( $value !== array() ) {
if (!$this->usekeys) {
if ( !$this->getUsekeys() ) {
$this->getResult()->setIndexedTagName( $value, $tag );
}
$this->getResult()->addValue( $path, $name, $value );
Expand Down
2 changes: 0 additions & 2 deletions repo/includes/api/ApiGetItems.php
Expand Up @@ -77,8 +77,6 @@ public function execute() {

$languages = $params['languages'];

$this->setUsekeys( $params );

// This really needs a more generic solution as similar tricks will be
// done to other props as well, for example variants for the language
// attributes. It would also be nice to write something like */urls for
Expand Down
1 change: 0 additions & 1 deletion repo/includes/api/ApiModifyItem.php
Expand Up @@ -175,7 +175,6 @@ public function execute() {
}

// FIXME: we can (?) do this before we do permission checks as long as we don't save
$this->setUsekeys( $params );
$success = $this->modifyItem( $itemContent, $params );

if ( !$success ) {
Expand Down
1 change: 0 additions & 1 deletion repo/includes/api/ApiSetItem.php
Expand Up @@ -194,7 +194,6 @@ protected function modifyItem( ItemContent &$itemContent, array $params ) {

$res = $this->getResult();

$this->setUsekeys( $params );
$this->addLabelsToResult( $item->getLabels(), 'item' );
$this->addDescriptionsToResult( $item->getDescriptions(), 'item' );
$this->addAliasesToResult( $item->getAllAliases(), 'item' );
Expand Down
55 changes: 55 additions & 0 deletions repo/tests/phpunit/includes/api/ApiGetItemsTest.php
Expand Up @@ -340,5 +340,60 @@ function testSitelinkUrls( $handle ) {
$this->assertArrayHasKey( 'url', $link );
}
}

/**
* @dataProvider providerGetItemFormat
*/
function testGetItemFormat( $format, $usekeys ) {
$this->createItems();

$item = $this->getItemOutput( 'Berlin' );
$id = $item['id'];

list($res,,) = $this->doApiRequest(
array(
'action' => 'wbgetitems',
'format' => $format,
'ids' => $id )
);

$this->assertSuccess( $res, 'items', $id );
if ( $usekeys ) {
foreach ( array( 'sitelinks' => 'site', 'alias' => false, 'labels' => 'language', 'descriptions' => 'language' ) as $prop => $field) {
if ( array_key_exists( $prop, $res['items'][$id] ) ) {
foreach ( $res['items'][$id][$prop] as $key => $value ) {
$this->assertTrue( is_string( $key ) );
if ( $field !== false ) {
$this->assertArrayHasKey( $field, $value );
$this->assertTrue( is_string( $value[$field] ) );
$this->assertEquals( $key, $value[$field] );
}
}
}
}
}
else {
foreach ( array( 'sitelinks' => 'site', 'alias' => false, 'labels' => 'language', 'descriptions' => 'language' ) as $prop => $field) {
if ( array_key_exists( $prop, $res['items'][$id] ) ) {
foreach ( $res['items'][$id][$prop] as $key => $value ) {
$this->assertTrue( is_integer( $key ) );
if ( $field !== false ) {
$this->assertArrayHasKey( $field, $value );
$this->assertTrue( is_string( $value[$field] ) );
}
}
}
}
}
}

function providerGetItemFormat() {
$calls = array();
foreach ( Settings::get( 'formatsWithKeys' ) as $format => $usekeys) {
$calls[] = array( $format, $usekeys );
}
return $calls;
}

}

1 change: 0 additions & 1 deletion repo/tests/phpunit/includes/api/ApiSetAliasesTest.php
Expand Up @@ -80,7 +80,6 @@ public function testSetAliases( $handle, $langCode, $op, $value, $expected ) {
// update the item ----------------------------------------------------------------
$req = array(
'token' => $this->getItemToken(),
'usekeys' => true,
'id' => $id,
'action' => 'wbsetaliases',
'language' => $langCode,
Expand Down

0 comments on commit aed7add

Please sign in to comment.