r96354 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96353‎ | r96354 | r96355 >
Date:17:58, 6 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on special page and api
Modified paths:
  • /trunk/extensions/Survey/api/ApiAddSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyDBClass.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -39,14 +39,15 @@
4040 }
4141 else {
4242 if ( is_null( $subPage ) ) {
43 - $survey = new Survey( null );
 43+ $survey = new Survey( null, true );
 44+ $survey->loadDefaults();
4445 }
4546 else {
4647 $survey = Survey::newFromName( $subPage, null, true );
4748 }
4849
4950 if ( $survey === false ) {
50 - $survey = new Survey( array( 'name' => $subPage ) );
 51+ $survey = new Survey( array( 'name' => $subPage ), true );
5152 }
5253
5354 $this->showSurvey( $survey );
@@ -92,10 +93,6 @@
9394 protected function getSubmittedQuestions() {
9495 $questions = array();
9596
96 -// foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) {
97 -//
98 -// }
99 -
10097 foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) {
10198 $matches = array();
10299
Index: trunk/extensions/Survey/includes/SurveyDBClass.php
@@ -28,14 +28,16 @@
2929 * @since 0.1
3030 *
3131 * @param array|null $fields
 32+ * @param boolean $loadDefaults
3233 */
33 - public function __construct( $fields ) {
34 - if ( is_array( $fields ) ) {
35 - $this->setFields( $fields );
 34+ public function __construct( $fields, $loadDefaults = false ) {
 35+ $this->setField( static::getIDField(), null );
 36+
 37+ if ( $loadDefaults ) {
 38+ $fields = array_merge( static::getDefaults(), $fields );
3639 }
37 - else {
38 - throw new Exception('');
39 - }
 40+
 41+ $this->setFields( $fields );
4042 }
4143
4244 /**
@@ -75,7 +77,7 @@
7678 * @since 0.1
7779 *
7880 * @throws MWException
79 - * @return array
 81+ * @return string
8082 */
8183 protected static function getFieldPrefix() {
8284 throw new MWException( 'Class did not implement getFieldPrefix' );
@@ -119,11 +121,12 @@
120122 * @since 0.1
121123 *
122124 * @param array $data
 125+ * @param boolean $loadDefaults
123126 *
124127 * @return SurveyDBClass
125128 */
126 - public static function newFromArray( array $data ) {
127 - return new static( $data );
 129+ public static function newFromArray( array $data, $loadDefaults = false ) {
 130+ return new static( $data, $loadDefaults );
128131 }
129132
130133 /**
@@ -353,11 +356,12 @@
354357 * @return mixed
355358 */
356359 public function getField( $name ) {
357 - if ( array_key_exists( $name, static::getFieldTypes() ) ) {
 360+ if ( $this->hasField( $name ) ) {
358361 return $this->fields[$name];
359362 }
360363 else {
361 - throw new MWException( 'Attempted to get unknonw field ' . $name );
 364+ var_dump($this->fields);
 365+ throw new MWException( 'Attempted to get not-set field ' . $name );
362366 }
363367 }
364368
@@ -437,11 +441,14 @@
438442 *
439443 * @since 0.1
440444 *
441 - * @param array $fields
 445+ * @param array $fields The fields to set
 446+ * @param boolean $override Override already set fields with the provided values?
442447 */
443 - public function setFields( array $fields ) {
 448+ public function setFields( array $fields, $override = true ) {
444449 foreach ( $fields as $name => $value ) {
445 - $this->setField( $name, $value );
 450+ if ( $override || !$this->hasField( $name ) ) {
 451+ $this->setField( $name, $value );
 452+ }
446453 }
447454 }
448455
@@ -544,25 +551,50 @@
545552 return $data;
546553 }
547554
 555+ public function loadDefaults( $override = true ) {
 556+ $this->setFields( static::getDefaults(), $override );
 557+ }
 558+
548559 /**
549 - * Creates and returns a new instance from an array.
 560+ * Returns a list of default field values.
 561+ * field name => field value
550562 *
551563 * @since 0.1
552564 *
553 - * @param array $data
554 - *
555 - * @return SurveyDBClass
 565+ * @return array
556566 */
557 - public static function fromArray( array $data ) {
558 - $validData = array();
 567+ public static function getDefaults() {
 568+ return array();
 569+ }
 570+
 571+ public static function getAPIParams() {
 572+ $typeMap = array(
 573+ 'int' => 'integer',
 574+ 'str' => 'string',
 575+ 'bool' => 'integer'
 576+ );
559577
560 - foreach ( $data as $name => $value ) {
561 - if ( static::canHasField( $name ) ) {
562 - $validData[$name] = $value;
 578+ $params = array();
 579+ $defaults = static::getDefaults();
 580+
 581+ foreach ( static::getFieldTypes() as $field => $type ) {
 582+ if ( $field == static::getIDField() ) {
 583+ continue;
563584 }
 585+
 586+ $hasDefault = array_key_exists( $field, $defaults );
 587+
 588+ $params[$field] = array(
 589+ ApiBase::PARAM_TYPE => $typeMap[$type],
 590+ ApiBase::PARAM_REQUIRED => !$hasDefault
 591+ );
 592+
 593+ if ( $hasDefault ) {
 594+ $params[$field][ApiBase::PARAM_DFLT] = $defaults[$field];
 595+ }
564596 }
565597
566 - return new static( $validData );
 598+ return $params;
567599 }
568600
569601 }
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -39,7 +39,32 @@
4040 );
4141 }
4242
43 - protected static function getFieldPrefix() {
 43+ /**
 44+ * Returns a list of default field values.
 45+ * field name => field value
 46+ *
 47+ * @since 0.1
 48+ *
 49+ * @return array
 50+ */
 51+ public static function getDefaults() {
 52+ return array(
 53+ 'name' => '',
 54+ 'enabled' => '0',
 55+ 'header' => '',
 56+ 'footer' => '',
 57+ 'thanks' => '',
 58+ );
 59+ }
 60+
 61+ /**
 62+ * Gets the db field prefix.
 63+ *
 64+ * @since 0.1
 65+ *
 66+ * @return string
 67+ */
 68+ protected static function getFieldPrefix() {
4469 return 'survey_';
4570 }
4671
@@ -89,7 +114,7 @@
90115 public static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) {
91116 $survey = self::selectRow( $fields, $conditions );
92117
93 - if ( $loadQuestions ) {
 118+ if ( $survey !== false && $loadQuestions ) {
94119 $survey->loadQuestionsFromDB();
95120 }
96121
@@ -102,10 +127,11 @@
103128 * @since 0.1
104129 *
105130 * @param array|null $fields
 131+ * @param boolean $loadDefaults
106132 * @param array $questions
107133 */
108 - public function __construct( $fields, array $questions = array() ) {
109 - parent::__construct( $fields );
 134+ public function __construct( $fields, $loadDefaults = false, array $questions = array() ) {
 135+ parent::__construct( $fields, $loadDefaults );
110136 $this->setQuestions( $questions );
111137 }
112138
Index: trunk/extensions/Survey/api/ApiAddSurvey.php
@@ -23,7 +23,7 @@
2424
2525 if ( !$wgUser->isAllowed( 'surveyadmin' ) || $wgUser->isBlocked() ) {
2626 $this->dieUsageMsg( array( 'badaccess-groups' ) );
27 - }
 27+ }
2828
2929 $params = $this->extractRequestParams();
3030
@@ -69,24 +69,16 @@
7070 );
7171 }
7272
73 - public function needsToken() {
74 - return true;
75 - }
76 -
77 - public function getTokenSalt() {
78 - return 'addsurvey';
79 - }
 73+// public function needsToken() {
 74+// return true;
 75+// }
 76+//
 77+// public function getTokenSalt() {
 78+// return 'addsurvey';
 79+// }
8080
8181 public function getAllowedParams() {
82 - return array(
83 - 'name' => array(
84 - ApiBase::PARAM_TYPE => 'string',
85 - ApiBase::PARAM_REQUIRED => true,
86 - ),
87 - 'enabled' => array(
88 - ApiBase::PARAM_TYPE => 'integer',
89 - ApiBase::PARAM_REQUIRED => true,
90 - ),
 82+ $params = array(
9183 'questions' => array(
9284 ApiBase::PARAM_TYPE => 'string',
9385 ApiBase::PARAM_ISMULTI => true,
@@ -94,6 +86,8 @@
9587 ),
9688 'token' => null,
9789 );
 90+
 91+ return array_merge( Survey::getAPIParams(), $params );
9892 }
9993
10094 public function getParamDescription() {

Status & tagging log