Addshore has uploaded a new change for review.
https://gerrit.wikimedia.org/r/222770
Change subject: Stop using ApiWikibase in ParseValue
......................................................................
Stop using ApiWikibase in ParseValue
This also adds the setServices method
This also fixes PHPCS errors spotted
This also moves a method from ApiWikibase to
Parsevalue that was only used here.
Change-Id: I492d7cfe576111b55ccf8ea609308da3982133cd
---
M repo/includes/api/ApiWikibase.php
M repo/includes/api/ParseValue.php
2 files changed, 69 insertions(+), 35 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/70/222770/1
diff --git a/repo/includes/api/ApiWikibase.php
b/repo/includes/api/ApiWikibase.php
index dac5bcc..ebd6b63 100644
--- a/repo/includes/api/ApiWikibase.php
+++ b/repo/includes/api/ApiWikibase.php
@@ -436,25 +436,6 @@
}
/**
- * Returns a Status object representing the given exception using a
localized message.
- *
- * @note: The returned Status will always be fatal, that is,
$status->isOk() will return false.
- *
- * @see getExceptionMessage().
- *
- * @param Exception $error
- *
- * @return Status
- */
- protected function getExceptionStatus( Exception $error ) {
- $msg = $this->exceptionLocalizer->getExceptionMessage( $error );
- $status = Status::newFatal( $msg );
- $status->setResult( false, $error->getMessage() );
-
- return $status;
- }
-
- /**
* @deprecated since 0.5, use dieError(), dieException() or the
* methods in $this->apiErrorReporter instead.
*
diff --git a/repo/includes/api/ParseValue.php b/repo/includes/api/ParseValue.php
index 2cdfa24..369f843 100644
--- a/repo/includes/api/ParseValue.php
+++ b/repo/includes/api/ParseValue.php
@@ -3,14 +3,19 @@
namespace Wikibase\Api;
use ApiBase;
+use ApiMain;
use ApiResult;
use DataValues\DataValue;
+use Exception;
use LogicException;
use OutOfBoundsException;
+use Status;
use ValueParsers\ParseException;
use ValueParsers\ParserOptions;
use ValueParsers\ValueParser;
+use Wikibase\Lib\Localizer\ExceptionLocalizer;
use Wikibase\Repo\ValueParserFactory;
+use Wikibase\Repo\WikibaseRepo;
/**
* API module for using value parsers.
@@ -20,23 +25,50 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
* @author Daniel Kinzler
+ * @author Adam Shorland
*/
-class ParseValue extends ApiWikibase {
+class ParseValue extends ApiBase {
/**
- * @var null|ValueParserFactory
+ * @var ValueParserFactory
*/
- private $factory = null;
+ private $valueParserFactory;
/**
- * @return ValueParserFactory
+ * @var ExceptionLocalizer
*/
- private function getFactory() {
- if ( $this->factory === null ) {
- $this->factory = new ValueParserFactory(
$GLOBALS['wgValueParsers'] );
- }
+ private $exceptionLocalizer;
- return $this->factory;
+ /**
+ * @var ApiErrorReporter
+ */
+ private $errorReporter;
+
+ /**
+ * @param ApiMain $mainModule
+ * @param string $moduleName
+ * @param string $modulePrefix
+ *
+ * @see ApiBase::__construct
+ */
+ public function __construct( ApiMain $mainModule, $moduleName,
$modulePrefix = '' ) {
+ parent::__construct( $mainModule, $moduleName, $modulePrefix );
+
+ $this->setServices(
+ new ValueParserFactory( $GLOBALS['wgValueParsers'] ),
+
WikibaseRepo::getDefaultInstance()->getExceptionLocalizer(),
+
WikibaseRepo::getDefaultInstance()->getApiHelperFactory()->getErrorReporter(
$this )
+ );
+ }
+
+ public function setServices(
+ ValueParserFactory $valueParserFactory,
+ ExceptionLocalizer $exceptionLocalizer,
+ ApiErrorReporter $errorReporter
+ ) {
+ $this->valueParserFactory = $valueParserFactory;
+ $this->exceptionLocalizer = $exceptionLocalizer;
+ $this->errorReporter = $errorReporter;
}
/**
@@ -68,7 +100,7 @@
$options = $this->getOptionsObject( $params['options'] );
try {
- $parser = $this->getFactory()->newParser(
$params['parser'], $options );
+ $parser = $this->valueParserFactory->newParser(
$params['parser'], $options );
} catch ( OutOfBoundsException $ex ) {
throw new LogicException( 'Could not obtain a
ValueParser instance' );
}
@@ -113,7 +145,7 @@
$result['expected-format'] = $parseError->getExpectedFormat();
$status = $this->getExceptionStatus( $parseError );
- $this->getErrorReporter()->addStatusToResult( $status, $result
);
+ $this->errorReporter->addStatusToResult( $status, $result );
}
private function outputResults( array $results ) {
@@ -139,7 +171,7 @@
$options = json_decode( $optionsParam, true );
if ( !is_array( $options ) ) {
- $this->dieError( 'Malformed options parameter',
'malformed-options' );
+ $this->errorReporter->dieError( 'Malformed
options parameter', 'malformed-options' );
}
foreach ( $options as $name => $value ) {
@@ -151,12 +183,31 @@
}
/**
+ * Returns a Status object representing the given exception using a
localized message.
+ *
+ * @note: The returned Status will always be fatal, that is,
$status->isOk() will return false.
+ *
+ * @see getExceptionMessage().
+ *
+ * @param Exception $error
+ *
+ * @return Status
+ */
+ protected function getExceptionStatus( Exception $error ) {
+ $msg = $this->exceptionLocalizer->getExceptionMessage( $error );
+ $status = Status::newFatal( $msg );
+ $status->setResult( false, $error->getMessage() );
+
+ return $status;
+ }
+
+ /**
* @see ApiBase::getAllowedParams
*/
- protected function getAllowedParams() {
+ public function getAllowedParams() {
return array(
'parser' => array(
- ApiBase::PARAM_TYPE =>
$this->getFactory()->getParserIds(),
+ ApiBase::PARAM_TYPE =>
$this->valueParserFactory->getParserIds(),
ApiBase::PARAM_REQUIRED => true,
),
'values' => array(
@@ -176,8 +227,10 @@
*/
protected function getExamplesMessages() {
return array(
- 'action=wbparsevalue&parser=null&values=foo|bar' =>
'apihelp-wbparsevalue-example-1',
-
'action=wbparsevalue&parser=time&values=1994-02-08&options={"precision":9}' =>
'apihelp-wbparsevalue-example-2',
+ 'action=wbparsevalue&parser=null&values=foo|bar' =>
+ 'apihelp-wbparsevalue-example-1',
+
'action=wbparsevalue&parser=time&values=1994-02-08&options={"precision":9}' =>
+ 'apihelp-wbparsevalue-example-2',
);
}
--
To view, visit https://gerrit.wikimedia.org/r/222770
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I492d7cfe576111b55ccf8ea609308da3982133cd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits