r96359 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96358‎ | r96359 | r96360 >
Date:19:04, 6 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on api
Modified paths:
  • /trunk/extensions/Survey/api/ApiAddSurvey.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiDeleteSurvey.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiEditSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyDBClass.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -30,7 +30,7 @@
3131 */
3232 protected static function getFieldTypes() {
3333 return array(
34 - 'id' => 'int',
 34+ 'id' => 'id',
3535 'name' => 'str',
3636 'enabled' => 'bool',
3737 'header' => 'str',
Index: trunk/extensions/Survey/includes/SurveyDBClass.php
@@ -330,8 +330,11 @@
331331 $value = (int)$value;
332332 case 'bool':
333333 if ( is_string( $value ) ) {
334 - $value = $value != '0';
 334+ $value = $value !== '0';
335335 }
 336+ else if ( is_int( $value ) ) {
 337+ $value = $value !== 0;
 338+ }
336339 case 'array':
337340 if ( is_string( $value ) ) {
338341 $value = unserialize( $value );
@@ -597,4 +600,31 @@
598601 return $params;
599602 }
600603
 604+ /**
 605+ * Create a new instance from API parameters.
 606+ *
 607+ * @since 0.1
 608+ *
 609+ * @param array $params
 610+ *
 611+ * @return SurveyDBClass
 612+ */
 613+ public static function newFromAPIParams( array $params, $id = false ) {
 614+ $validParams = array();
 615+
 616+ $fields = static::getFieldTypes();
 617+
 618+ foreach ( $params as $name => $value ) {
 619+ if ( array_key_exists( $name, $fields ) ) {
 620+ $validParams[$name] = $value;
 621+ }
 622+ }
 623+
 624+ if ( $id !== false ) {
 625+ $validParams[static::getIDField()] = $id;
 626+ }
 627+
 628+ return new static( $validParams );
 629+ }
 630+
601631 }
Index: trunk/extensions/Survey/api/ApiDeleteSurvey.php
@@ -49,6 +49,10 @@
5050 return 'deletesurvey';
5151 }
5252
 53+ public function mustBePosted() {
 54+ return true;
 55+ }
 56+
5357 public function getAllowedParams() {
5458 return array(
5559 'ids' => array(
Index: trunk/extensions/Survey/api/ApiEditSurvey.php
@@ -27,20 +27,11 @@
2828
2929 $params = $this->extractRequestParams();
3030
31 -// $q = new SurveyQuestion(null, 'new q', 1, false);
32 -// var_dump($q->toUrlData());exit;
33 - // eyJ0ZXh0IjoibmV3IHEiLCJ0eXBlIjoxLCJyZXF1aXJlZCI6ZmFsc2UsImFuc3dlcnMiOltdfQ!!
34 -
3531 foreach ( $params['questions'] as &$question ) {
3632 $question = SurveyQuestion::newFromUrlData( $question );
3733 }
3834
39 - $survey = new Survey(
40 - $params['id'],
41 - $params['name'],
42 - $params['enabled'] == 1,
43 - $params['questions']
44 - );
 35+ $survey = Survey::newFromAPIParams( $params, $params['id'] );
4536
4637 $this->getResult()->addValue(
4738 null,
@@ -57,7 +48,7 @@
5849 $this->getResult()->addValue(
5950 'survey',
6051 'name',
61 - $survey->getName()
 52+ $survey->getField( 'name' )
6253 );
6354 }
6455
@@ -68,21 +59,17 @@
6960 public function getTokenSalt() {
7061 return 'editsurvey';
7162 }
 63+
 64+ public function mustBePosted() {
 65+ return true;
 66+ }
7267
7368 public function getAllowedParams() {
74 - return array(
 69+ $params = array(
7570 'id' => array(
7671 ApiBase::PARAM_TYPE => 'integer',
7772 ApiBase::PARAM_REQUIRED => true,
7873 ),
79 - 'name' => array(
80 - ApiBase::PARAM_TYPE => 'string',
81 - ApiBase::PARAM_REQUIRED => true,
82 - ),
83 - 'enabled' => array(
84 - ApiBase::PARAM_TYPE => 'integer',
85 - ApiBase::PARAM_REQUIRED => true,
86 - ),
8774 'questions' => array(
8875 ApiBase::PARAM_TYPE => 'string',
8976 ApiBase::PARAM_ISMULTI => true,
@@ -90,6 +77,8 @@
9178 ),
9279 'token' => null,
9380 );
 81+
 82+ return array_merge( Survey::getAPIParams(), $params );
9483 }
9584
9685 public function getParamDescription() {
Index: trunk/extensions/Survey/api/ApiAddSurvey.php
@@ -32,13 +32,7 @@
3333 }
3434
3535 try {
36 - $survey = new Survey(
37 - null,
38 - $params['name'],
39 - $params['enabled'] == 1,
40 - $params['questions']
41 - );
42 -
 36+ $survey = Survey::newFromAPIParams( $params );
4337 $success = $survey->writeToDB();
4438 }
4539 catch ( DBQueryError $ex ) {
@@ -65,17 +59,21 @@
6660 $this->getResult()->addValue(
6761 'survey',
6862 'name',
69 - $survey->getName()
 63+ $survey->getField( 'name' )
7064 );
7165 }
7266
73 -// public function needsToken() {
74 -// return true;
75 -// }
76 -//
77 -// public function getTokenSalt() {
78 -// return 'addsurvey';
79 -// }
 67+ public function needsToken() {
 68+ return true;
 69+ }
 70+
 71+ public function getTokenSalt() {
 72+ return 'addsurvey';
 73+ }
 74+
 75+ public function mustBePosted() {
 76+ return true;
 77+ }
8078
8179 public function getAllowedParams() {
8280 $params = array(

Status & tagging log