r96259 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96258‎ | r96259 | r96260 >
Date:21:36, 4 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
some work on the object classes
Modified paths:
  • /trunk/extensions/Survey/Survey.sql (modified) (history)
  • /trunk/extensions/Survey/api/ApiQuerySurveys.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.sql
@@ -6,7 +6,10 @@
77 CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/surveys (
88 survey_id SMALLINT unsigned NOT NULL auto_increment PRIMARY KEY,
99 survey_name VARCHAR(255) NOT NULL,
10 - survey_enabled TINYINT NOT NULL default '0'
 10+ survey_enabled TINYINT NOT NULL default '0',
 11+ survey_header TEXT NOT NULL,
 12+ survey_footer TEXT NOT NULL,
 13+ survey_thanks TEXT NOT NULL
1114 ) /*$wgDBTableOptions*/;
1215
1316 -- Questions
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -185,6 +185,30 @@
186186 'name' => 'survey-enabled',
187187 );
188188
 189+ $fields[] = array(
 190+ 'type' => 'text',
 191+ 'default' => $survey->getHeader(),
 192+ 'label-message' => 'survey-special-label-header',
 193+ 'id' => 'survey-header',
 194+ 'name' => 'survey-header',
 195+ );
 196+
 197+ $fields[] = array(
 198+ 'type' => 'text',
 199+ 'default' => $survey->getFooter(),
 200+ 'label-message' => 'survey-special-label-footer',
 201+ 'id' => 'survey-footer',
 202+ 'name' => 'survey-footer',
 203+ );
 204+
 205+ $fields[] = array(
 206+ 'type' => 'text',
 207+ 'default' => $survey->getThanks(),
 208+ 'label-message' => 'survey-special-label-thanks',
 209+ 'id' => 'survey-thanks',
 210+ 'name' => 'survey-thanks',
 211+ );
 212+
189213 foreach ( $survey->getQuestions() as /* SurveyQuestion */ $question ) {
190214 $fields[] = array(
191215 'class' => 'SurveyQuestionField',
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -13,7 +13,24 @@
1414 */
1515 class Survey extends SurveyDBClass {
1616
 17+ protected static $fields = array(
 18+ 'id' => 'int',
 19+ 'name' => 'str',
 20+ 'enabled' => 'bool',
 21+ 'header' => 'str',
 22+ 'footer' => 'str',
 23+ 'thanks' => 'str',
 24+ );
 25+
1726 /**
 27+ *
 28+ *
 29+ * @since 0.1
 30+ * @var string
 31+ */
 32+ protected static $fieldPrefix = 'survey_';
 33+
 34+ /**
1835 * The name of the survey.
1936 *
2037 * @since 0.1
@@ -38,6 +55,30 @@
3956 protected $questions;
4057
4158 /**
 59+ * Header text to display above the questions.
 60+ *
 61+ * @since 0.1
 62+ * @var string
 63+ */
 64+ protected $header;
 65+
 66+ /**
 67+ * Footer text to display below the questions.
 68+ *
 69+ * @since 0.1
 70+ * @var string
 71+ */
 72+ protected $footer;
 73+
 74+ /**
 75+ * Thanks message to display after submission of the survey.
 76+ *
 77+ * @since 0.1
 78+ * @var string
 79+ */
 80+ protected $thanksMessage;
 81+
 82+ /**
4283 * @see SurveyDBClass::getDBTable()
4384 */
4485 protected function getDBTable() {
@@ -71,12 +112,13 @@
72113 * @since 0.1
73114 *
74115 * @param string $surveyName
 116+ * @param array|null $fields
75117 * @param boolean $loadQuestions
76118 *
77119 * @return Survey or false
78120 */
79 - public static function newFromName( $surveyName, $loadQuestions = true ) {
80 - return self::newFromDB( array( 'survey_name' => $surveyName ), $loadQuestions );
 121+ public static function newFromName( $surveyName, $fields = null, $loadQuestions = true ) {
 122+ return self::newFromDB( array( 'survey_name' => $surveyName ), $fields, $loadQuestions );
81123 }
82124
83125 /**
@@ -85,12 +127,13 @@
86128 * @since 0.1
87129 *
88130 * @param integer surveyId
 131+ * @param array|null $fields
89132 * @param boolean $loadQuestions
90133 *
91134 * @return Survey or false
92135 */
93 - public static function newFromId( $surveyId, $loadQuestions = true ) {
94 - return self::newFromDB( array( 'survey_id' => $surveyId ), $loadQuestions );
 136+ public static function newFromId( $surveyId, $fields = null, $loadQuestions = true ) {
 137+ return self::newFromDB( array( 'survey_id' => $surveyId ), $fields, $loadQuestions );
95138 }
96139
97140 /**
@@ -101,20 +144,25 @@
102145 * @since 0.1
103146 *
104147 * @param array $conditions
 148+ * @param array|null $fields
105149 * @param boolean $loadQuestions
106150 *
107151 * @return Survey or false
108152 */
109 - protected static function newFromDB( array $conditions, $loadQuestions = true ) {
 153+ protected static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) {
110154 $dbr = wfGetDB( DB_SLAVE );
111155
 156+ if ( is_null( $fields ) ) {
 157+ $fields = array_keys( self::$fields );
 158+ }
 159+
 160+ foreach ( $fields as &$field ) {
 161+ $field = self::$fieldPrefix . $field;
 162+ }
 163+
112164 $survey = $dbr->selectRow(
113165 'surveys',
114 - array(
115 - 'survey_id',
116 - 'survey_name',
117 - 'survey_enabled',
118 - ),
 166+ $fields,
119167 $conditions
120168 );
121169
@@ -122,11 +170,7 @@
123171 return false;
124172 }
125173
126 - $survey = new self(
127 - (int)$survey->survey_id,
128 - $survey->survey_name,
129 - (int)$survey->survey_enabled == 1
130 - );
 174+ $survey = self::newFromDBResult( $survey );
131175
132176 if ( $loadQuestions ) {
133177 $survey->loadQuestionsFromDB();
@@ -135,7 +179,38 @@
136180 return $survey;
137181 }
138182
 183+ protected static function newFromDBResult( $result ) {
 184+ $result = (array)$result;
 185+ $data = array();
 186+
 187+ foreach ( $result as $name => $value ) {
 188+ $data[substr( $name, strlen( self::$fieldPrefix ) )] = $value;
 189+ }
 190+
 191+ return self::newFromArray( $data );
 192+ }
 193+
 194+ protected static function newFromArray( array $data ) {
 195+ $survey = new Survey( array_key_exists( 'id', $data ) ? $data['id'] : null );
 196+
 197+ $survey->setProps( $data );
 198+
 199+ return $survey;
 200+ }
 201+
139202 /**
 203+ * Returns the survey fields.
 204+ * Field name => db field without prefix
 205+ *
 206+ * @since 0.1
 207+ *
 208+ * @return array
 209+ */
 210+ public static function getSurveyProps() {
 211+ return array_keys( self::$fields );
 212+ }
 213+
 214+ /**
140215 * Constructor.
141216 *
142217 * @since 0.1
@@ -244,6 +319,39 @@
245320 }
246321
247322 /**
 323+ * Returns the surveys header.
 324+ *
 325+ * @since 0.1
 326+ *
 327+ * @return string
 328+ */
 329+ public function getHeader() {
 330+ return $this->header;
 331+ }
 332+
 333+ /**
 334+ * Returns the surveys footer.
 335+ *
 336+ * @since 0.1
 337+ *
 338+ * @return string
 339+ */
 340+ public function getFooter() {
 341+ return $this->footer;
 342+ }
 343+
 344+ /**
 345+ * Returns the surveys thanks message.
 346+ *
 347+ * @since 0.1
 348+ *
 349+ * @return string
 350+ */
 351+ public function getThanks() {
 352+ return $this->thanksMessage;
 353+ }
 354+
 355+ /**
248356 * Sets the surveys questions.
249357 *
250358 * @since 0.1
@@ -260,12 +368,12 @@
261369 *
262370 * @since 0.1
263371 *
 372+ * @param null|array $props
 373+ *
264374 * @return array
265375 */
266 - public function toArray() {
 376+ public function toArray( $props = null ) {
267377 $data = array(
268 - 'enabled' => $this->enabled,
269 - 'name' => $this->name,
270378 'questions' => array(),
271379 );
272380
@@ -273,13 +381,31 @@
274382 $data['questions'][] = $question->toArray();
275383 }
276384
277 - if ( !is_null( $this->id ) ) {
278 - $data['id'] = $this->id;
 385+ if ( !is_array( $props ) ) {
 386+ $props = array_keys( self::$fields );
279387 }
280388
 389+ foreach ( $props as $prop ) {
 390+ if ( !( $prop == 'id' && is_null( $this->{ $prop } ) ) ) {
 391+ $data[$prop] = $this->getProp( $prop );
 392+ }
 393+ }
 394+
281395 return $data;
282396 }
283397
 398+ public static function fromArray( array $data ) {
 399+ $survey = new Survey( array_key_exists( 'id', $data ) ? $data['id'] : null );
 400+
 401+ foreach ( $data as $name => $value ) {
 402+ if ( $name != 'id' && array_key_exists( $name, self::$fields ) ) {
 403+ $survey->setProp( $name, $value );
 404+ }
 405+ }
 406+
 407+ return $survey;
 408+ }
 409+
284410 /**
285411 * Removes the object from the database.
286412 *
@@ -329,4 +455,38 @@
330456 return $sucecss;
331457 }
332458
 459+ public function setProp( $name, $value ) {
 460+ if ( array_key_exists( $name, self::$fields ) ) {
 461+ switch ( self::$fields[$name] ) {
 462+ case 'int':
 463+ $value = (int)$value;
 464+ case 'bool':
 465+ if ( is_string( $value ) ) {
 466+ $value = $value != '0';
 467+ }
 468+ }
 469+
 470+ $this->{ $name } = $value;
 471+ }
 472+ }
 473+
 474+ public function getProp( $name ) {
 475+ if ( array_key_exists( $name, self::$fields ) ) {
 476+ return $this->{ $name };
 477+ }
 478+ else {
 479+ // TODO: exception
 480+ }
 481+ }
 482+
 483+ public function setProps( array $props ) {
 484+ if ( array_key_exists( 'id', $props ) ) {
 485+ unset( $props['id'] );
 486+ }
 487+
 488+ foreach ( $props as $name => $value ) {
 489+ $this->setProp( $name, $value );
 490+ }
 491+ }
 492+
333493 }
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php
@@ -31,15 +31,18 @@
3232 // Get the requests parameters.
3333 $params = $this->extractRequestParams();
3434
35 - if ( !( isset( $params['ids'] ) XOR isset( $params['names'] ) ) ) {
 35+ if ( !( ( isset( $params['ids'] ) && count( $params['ids'] ) > 0 )
 36+ XOR ( isset( $params['names'] ) && count( $params['names'] ) > 0 )
 37+ ) ) {
3638 $this->dieUsage( wfMsgExt( 'survey-err-ids-xor-names' ), 'ids-xor-names' );
3739 }
3840
39 -
4041 $this->addTables( 'surveys' );
4142
42 - $this->addFields( 'survey_id' );
 43+ $fields = array_merge( array( 'id' ), $params['props'] );
4344
 45+ $this->addFields( $this->getSurveyFields( $fields ) );
 46+
4447 if ( isset( $params['ids'] ) ) {
4548 $this->addWhere( array( 'survey_id' => $params['ids'] ) );
4649 }
@@ -70,10 +73,17 @@
7174 break;
7275 }
7376
74 - $resultSurveys[] = $this->getSurveyData( Survey::newFromId(
75 - $survey->survey_id,
76 - $params['incquestions'] == 1
77 - ) );
 77+ $surveyObject = new Survey( $survey->survey_id );
 78+
 79+ foreach ( $fields as $prop ) {
 80+ $surveyObject->setProp( $prop, $survey->{ 'survey_' . $prop } );
 81+ }
 82+
 83+ if ( $params['incquestions'] ) {
 84+ $surveyObject->loadQuestionsFromDB();
 85+ }
 86+
 87+ $resultSurveys[] = $this->getSurveyData( $surveyObject->toArray( $fields ) );
7888 }
7989
8090 $this->getResult()->setIndexedTagName( $resultSurveys, 'survey' );
@@ -85,9 +95,20 @@
8696 );
8797 }
8898
89 - function getSurveyData( Survey $survey ) {
90 - $survey = $survey->toArray();
 99+ protected function getSurveyFields( array $props ) {
 100+ $fields = array();
 101+ $allowedFields = Survey::getSurveyProps();
91102
 103+ foreach ( $props as $prop ) {
 104+ if ( in_array( $prop, $allowedFields ) ) {
 105+ $fields[] = "survey_$prop";
 106+ }
 107+ }
 108+
 109+ return $fields;
 110+ }
 111+
 112+ protected function getSurveyData( array $survey ) {
92113 foreach ( $survey['questions'] as $nr => $question ) {
93114 $this->getResult()->setIndexedTagName( $survey['questions'][$nr], 'answer' );
94115 }
@@ -111,6 +132,11 @@
112133 ApiBase::PARAM_TYPE => 'string',
113134 ApiBase::PARAM_ISMULTI => true,
114135 ),
 136+ 'props' => array(
 137+ ApiBase::PARAM_TYPE => Survey::getSurveyProps(),
 138+ ApiBase::PARAM_ISMULTI => true,
 139+ ApiBase::PARAM_DFLT => 'id|name|enabled'
 140+ ),
115141 'incquestions' => array(
116142 ApiBase::PARAM_TYPE => 'integer',
117143 ApiBase::PARAM_DFLT => '0',
@@ -141,6 +167,7 @@
142168 'names' => 'The names of the surveys to return',
143169 'incquestions' => 'Include the questions of the surveys or not',
144170 'enabled' => 'Enabled state to filter on',
 171+ 'props' => 'Survey data to query',
145172 'continue' => 'Offset number from where to continue the query',
146173 'limit' => 'Max amount of words to return',
147174 'token' => 'Edit token. You can get one of these through prop=info.',

Status & tagging log