r96266 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96265‎ | r96266 | r96267 >
Date:01:45, 5 September 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
work on db object classes
Modified paths:
  • /trunk/extensions/Survey/INSTALL (modified) (history)
  • /trunk/extensions/Survey/Survey.i18n.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiQuerySurveys.php (modified) (history)
  • /trunk/extensions/Survey/api/ApiSubmitSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyDBClass.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/Survey.i18n.php
@@ -71,4 +71,7 @@
7272 'survey-special-label-text' => 'Question text',
7373 'survey-special-remove' => 'Remove question',
7474 'survey-special-remove-confirm' => 'Are you sure you want to remove this question?',
 75+ 'survey-special-label-header' => 'Text to display above the survey',
 76+ 'survey-special-label-footer' => 'Text to display below the survey',
 77+ 'survey-special-label-thanks' => 'Thanks message to display after submission of the survey',
7578 );
Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -42,7 +42,7 @@
4343 $survey = new Survey( null );
4444 }
4545 else {
46 - $survey = Survey::newFromName( $subPage, true );
 46+ $survey = Survey::newFromName( $subPage, null, true );
4747 }
4848
4949 if ( $survey === false ) {
@@ -71,7 +71,7 @@
7272 $survey = new Survey( null );
7373 }
7474 else {
75 - $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), false );
 75+ $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), null, false );
7676 }
7777
7878 $survey->setName( $wgRequest->getText( 'survey-name' ) );
@@ -171,7 +171,7 @@
172172
173173 $fields[] = array(
174174 'type' => 'text',
175 - 'default' => $survey->getName(),
 175+ 'default' => $survey->getField( 'name' ),
176176 'label-message' => 'survey-special-label-name',
177177 'id' => 'survey-name',
178178 'name' => 'survey-name',
@@ -179,7 +179,7 @@
180180
181181 $fields[] = array(
182182 'type' => 'check',
183 - 'default' => $survey->isEnabled() ? '1' : '0',
 183+ 'default' => $survey->getField( 'enabled' ) ? '1' : '0',
184184 'label-message' => 'survey-special-label-enabled',
185185 'id' => 'survey-enabled',
186186 'name' => 'survey-enabled',
@@ -187,7 +187,7 @@
188188
189189 $fields[] = array(
190190 'type' => 'text',
191 - 'default' => $survey->getHeader(),
 191+ 'default' => $survey->getField( 'header' ),
192192 'label-message' => 'survey-special-label-header',
193193 'id' => 'survey-header',
194194 'name' => 'survey-header',
@@ -195,7 +195,7 @@
196196
197197 $fields[] = array(
198198 'type' => 'text',
199 - 'default' => $survey->getFooter(),
 199+ 'default' => $survey->getField( 'footer' ),
200200 'label-message' => 'survey-special-label-footer',
201201 'id' => 'survey-footer',
202202 'name' => 'survey-footer',
@@ -203,7 +203,7 @@
204204
205205 $fields[] = array(
206206 'type' => 'text',
207 - 'default' => $survey->getThanks(),
 207+ 'default' => $survey->getField( 'thanks' ),
208208 'label-message' => 'survey-special-label-thanks',
209209 'id' => 'survey-thanks',
210210 'name' => 'survey-thanks',
@@ -213,9 +213,9 @@
214214 $fields[] = array(
215215 'class' => 'SurveyQuestionField',
216216 'options' => array(
217 - 'required' => $question->isRequired(),
218 - 'text' => $question->getText(),
219 - 'type' => $question->getType(),
 217+ 'required' => $question->getField( 'required' ),
 218+ 'text' => $question->getField( 'text' ),
 219+ 'type' => $question->getField( 'type' ),
220220 'id' => $question->getId(),
221221 )
222222 );
Index: trunk/extensions/Survey/INSTALL
@@ -9,7 +9,7 @@
1010 Survey requires:
1111
1212 * MediaWiki 1.17 or above
13 -* PHP 5.2 or above
 13+* PHP 5.3 or above
1414
1515 == Download ==
1616
Index: trunk/extensions/Survey/includes/SurveyDBClass.php
@@ -14,41 +14,187 @@
1515 abstract class SurveyDBClass {
1616
1717 /**
 18+ * The fields of the object.
 19+ * field name (w/o prefix) => value
 20+ *
 21+ * @since 0.1
 22+ * @var array
 23+ */
 24+ protected $fields;
 25+
 26+ /**
 27+ * Constructor.
 28+ *
 29+ * @since 0.1
 30+ *
 31+ * @param array|null $fields
 32+ */
 33+ public function __construct( $fields ) {
 34+ if ( !is_null( $fields ) ) {
 35+ $this->setFields( $fields );
 36+ }
 37+ }
 38+
 39+ /**
 40+ * Returns an array with the fields and their types this object contains.
 41+ * This corresponds directly to the fields in the database, without prefix.
 42+ *
 43+ * field name => type
 44+ *
 45+ * Allowed types:
 46+ * * str
 47+ * * int
 48+ * * bool
 49+ *
 50+ * @since 0.1
 51+ *
 52+ * @return array
 53+ */
 54+ protected static function getFieldTypes() {
 55+ return array();
 56+ }
 57+
 58+ /**
1859 * Returns the name of the database table objects of this type are stored in.
1960 *
2061 * @since 0.1
2162 *
 63+ * @throws MWException
2264 * @return string
2365 */
24 - protected abstract function getDBTable();
 66+ protected static function getDBTable() {
 67+ throw new MWException( 'Class did not implement getDBTable' );
 68+ }
 69+
 70+ /**
 71+ * Gets the db field prefix.
 72+ *
 73+ * @since 0.1
 74+ *
 75+ * @throws MWException
 76+ * @return array
 77+ */
 78+ protected static function getFieldPrefix() {
 79+ throw new MWException( 'Class did not implement getFieldPrefix' );
 80+ }
2581
2682 /**
27 - * Returns the name of the id field.
 83+ * Returns the name of the id db field, without prefix.
2884 *
2985 * @since 0.1
3086 *
3187 * @return string
3288 */
33 - protected abstract function getIDField();
 89+ protected static function getIDField() {
 90+ return 'id';
 91+ }
3492
3593 /**
36 - * Gets the fields => values to write to the database table.
 94+ * Get a new instance of the class from a database result.
3795 *
3896 * @since 0.1
3997 *
40 - * @return array
 98+ * @param object $result
 99+ *
 100+ * @return SurveyDBClass
41101 */
42 - protected abstract function getWriteValues();
 102+ public static function newFromDBResult( $result ) {
 103+ $result = (array)$result;
 104+ $data = array();
 105+ $idFieldLength = strlen( static::getFieldPrefix() );
 106+
 107+ foreach ( $result as $name => $value ) {
 108+ $data[substr( $name, $idFieldLength )] = $value;
 109+ }
 110+
 111+ return static::newFromArray( $data );
 112+ }
43113
44114 /**
45 - * The ID of the question DB record, or null if not inserted yet.
 115+ * Get a new instance of the class from an array.
46116 *
47117 * @since 0.1
48 - * @var integer|null
 118+ *
 119+ * @param array $data
 120+ *
 121+ * @return SurveyDBClass
 122+ */
 123+ public static function newFromArray( array $data ) {
 124+ return new static( $data );
 125+ }
 126+
 127+ /**
 128+ * Selects the the specified fields of the records matching the provided
 129+ * conditions.
 130+ *
 131+ * @since 0.1
 132+ *
 133+ * @param array|null $fields
 134+ * @param array $conditions
 135+ * @param array $options
 136+ *
 137+ * @return array of self
49138 */
50 - protected $id;
 139+ public static function select( $fields = null, array $conditions = array(), array $options = array() ) {
 140+ $result = static::rawSelect( $fields, $conditions, $options );
 141+
 142+ $objects = array();
 143+
 144+ foreach ( $result as $record ) {
 145+ $objects[] = static::newFromDBResult( $record );
 146+ }
 147+
 148+ return $objects;
 149+ }
51150
52151 /**
 152+ * Selects the the specified fields of the first matching record.
 153+ *
 154+ * @since 0.1
 155+ *
 156+ * @param array|null $fields
 157+ * @param array $conditions
 158+ * @param array $options
 159+ *
 160+ * @return self|false
 161+ */
 162+ public static function selectRow( $fields = null, array $conditions = array(), array $options = array() ) {
 163+ $options['LIMIT'] = 1;
 164+
 165+ $objects = static::select( $fields, $conditions, $options );
 166+
 167+ return count( $objects ) > 0 ? $objects[0] : false;
 168+ }
 169+
 170+ /**
 171+ * Selects the the specified fields of the records matching the provided
 172+ * conditions.
 173+ *
 174+ * @since 0.1
 175+ *
 176+ * @param array|null $fields
 177+ * @param array $conditions
 178+ * @param array $options
 179+ *
 180+ * @return ResultWrapper
 181+ */
 182+ public static function rawSelect( $fields = null, array $conditions = array(), array $options = array() ) {
 183+ if ( is_null( $fields ) ) {
 184+ $fields = array_keys( static::getFieldTypes() );
 185+ }
 186+
 187+ $dbr = wfgetDB( DB_SLAVE );
 188+
 189+ return $dbr->select(
 190+ static::getDBTable(),
 191+ static::getPrefixedFields( $fields ),
 192+ count( $conditions ) == 0 ? '' : static::getPrefixedValues( $conditions ),
 193+ '',
 194+ $options
 195+ );
 196+ }
 197+
 198+ /**
53199 * Writes the answer to the database, either updating it
54200 * when it already exists, or inserting it when it doesn't.
55201 *
@@ -57,11 +203,11 @@
58204 * @return boolean Success indicator
59205 */
60206 public function writeToDB() {
61 - if ( is_null( $this->id ) ) {
 207+ if ( $this->hadIdField() ) {
62208 return $this->insertIntoDB();
63209 }
64210 else {
65 - return $this->updateInDB();
 211+ return $this->updateInDB();
66212 }
67213 }
68214
@@ -78,7 +224,7 @@
79225 return $dbw->update(
80226 $this->getDBTable(),
81227 $this->getWriteValues(),
82 - array( $this->getIDField() => $this->id )
 228+ array( static::getFieldPrefix() . static::getIDField() => $this->getId() )
83229 );
84230 }
85231
@@ -93,11 +239,11 @@
94240 $dbw = wfGetDB( DB_MASTER );
95241
96242 $result = $dbw->insert(
97 - $this->getDBTable(),
98 - $this->getWriteValues()
 243+ static::getDBTable(),
 244+ static::getWriteValues()
99245 );
100246
101 - $this->id = $dbw->insertId();
 247+ $this->setField( static::getIDField(), $dbw->insertId() );
102248
103249 return $result;
104250 }
@@ -113,18 +259,117 @@
114260 $dbw = wfGetDB( DB_MASTER );
115261
116262 $sucecss = $dbw->delete(
117 - $this->getDBTable(),
118 - array( $this->getIDField() => $this->id )
 263+ static::getDBTable(),
 264+ array( static::getFieldPrefix() . static::getIDField() => $this->getId() )
119265 );
120266
121267 if ( $sucecss ) {
122 - $this->id = null;
 268+ $this->removeField( static::getIDField() );
123269 }
124270
125271 return $sucecss;
126272 }
127273
128274 /**
 275+ * Return the names of the fields.
 276+ *
 277+ * @since 0.1
 278+ *
 279+ * @return array
 280+ */
 281+ public function getFields() {
 282+ return $this->fields;
 283+ }
 284+
 285+ /**
 286+ * Return the names of the fields.
 287+ *
 288+ * @since 0.1
 289+ *
 290+ * @return array
 291+ */
 292+ public static function getFieldNames() {
 293+ return array_keys( static::getFieldTypes() );
 294+ }
 295+
 296+ /**
 297+ * Return the names of the fields.
 298+ *
 299+ * @since 0.1
 300+ *
 301+ * @return array
 302+ */
 303+ public function getSetFieldNames() {
 304+ return array_keys( $this->fields );
 305+ }
 306+
 307+ /**
 308+ * Sets the value of a field.
 309+ * Strings can be provided for other types,
 310+ * so this method can be called from unserialization handlers.
 311+ *
 312+ * @since 0.1
 313+ *
 314+ * @param string $name
 315+ * @param mixed $value
 316+ *
 317+ * @throws MWException
 318+ */
 319+ public function setField( $name, $value ) {
 320+ $fields = static::getFieldTypes();
 321+
 322+ if ( array_key_exists( $name, $fields ) ) {
 323+ switch ( $fields[$name] ) {
 324+ case 'int':
 325+ $value = (int)$value;
 326+ case 'bool':
 327+ if ( is_string( $value ) ) {
 328+ $value = $value != '0';
 329+ }
 330+ case 'array':
 331+ if ( is_string( $value ) ) {
 332+ $value = unserialize( $value );
 333+ }
 334+ }
 335+
 336+ $this->fields[$name] = $value;
 337+ }
 338+ else {
 339+ throw new MWException( 'Attempted to set unknonw field ' . $name );
 340+ }
 341+ }
 342+
 343+ /**
 344+ * Gets the value of a field.
 345+ *
 346+ * @since 0.1
 347+ *
 348+ * @param string $name
 349+ *
 350+ * @throws MWException
 351+ * @return mixed
 352+ */
 353+ public function getField( $name ) {
 354+ if ( array_key_exists( $name, static::getFieldTypes() ) ) {
 355+ return $this->fields[$name];
 356+ }
 357+ else {
 358+ throw new MWException( 'Attempted to get unknonw field ' . $name );
 359+ }
 360+ }
 361+
 362+ /**
 363+ * Remove a field.
 364+ *
 365+ * @since 0.1
 366+ *
 367+ * @param string $name
 368+ */
 369+ public function removeField( $name ) {
 370+ unset( $this->fields[$name] );
 371+ }
 372+
 373+ /**
129374 * Returns the objects database id.
130375 *
131376 * @since 0.1
@@ -132,7 +377,176 @@
133378 * @return integer|null
134379 */
135380 public function getId() {
136 - return $this->id;
 381+ return $this->getField( static::getIDField() );
137382 }
138383
 384+ /**
 385+ * Gets if a certain field is set.
 386+ *
 387+ * @since 0.1
 388+ *
 389+ * @param string $name
 390+ *
 391+ * @return boolean
 392+ */
 393+ public function hasField( $name ) {
 394+ return array_key_exists( $name, $this->fields );
 395+ }
 396+
 397+ /**
 398+ * Gets if the object can take a certain field.
 399+ *
 400+ * @since 0.1
 401+ *
 402+ * @param string $name
 403+ *
 404+ * @return boolean
 405+ */
 406+ public static function canHasField( $name ) {
 407+ return array_key_exists( $name, static::getFieldTypes() );
 408+ }
 409+
 410+ /**
 411+ * Gets if the id field is set.
 412+ *
 413+ * @since 0.1
 414+ *
 415+ * @return boolean
 416+ */
 417+ public function hasIdField() {
 418+ return $this->hasField( static::getIDField() )
 419+ && !is_null( $this->getField( static::getIDField() ) );
 420+ }
 421+
 422+ /**
 423+ * Sets multiple fields.
 424+ *
 425+ * @since 0.1
 426+ *
 427+ * @param array $fields
 428+ */
 429+ public function setFields( array $fields ) {
 430+ foreach ( $fields as $name => $value ) {
 431+ $this->setField( $name, $value );
 432+ }
 433+ }
 434+
 435+ /**
 436+ * Gets the fields => values to write to the surveys table.
 437+ *
 438+ * @since 0.1
 439+ *
 440+ * @return array
 441+ */
 442+ protected function getWriteValues() {
 443+ $values = array();
 444+
 445+ foreach ( static::getFieldTypes() as $name => $type ) {
 446+ $value = $this->fields[$name];
 447+
 448+ switch ( $type ) {
 449+ case 'array':
 450+ $value = serialize( (array)$value );
 451+ }
 452+
 453+ $values[static::getFieldPrefix() . $name] = $value;
 454+ }
 455+
 456+ return $values;
 457+ }
 458+
 459+ /**
 460+ * Takes in a field or array of fields and returns an
 461+ * array with their prefixed versions, ready for db usage.
 462+ *
 463+ * @since 0.1
 464+ *
 465+ * @param array|string $fields
 466+ *
 467+ * @return array
 468+ */
 469+ public static function getPrefixedFields( $fields ) {
 470+ $fields = (array)$fields;
 471+
 472+ foreach ( $fields as &$field ) {
 473+ $field = static::getFieldPrefix() . $field;
 474+ }
 475+
 476+ return $fields;
 477+ }
 478+
 479+ /**
 480+ * Takes in an associative array with field names as keys and
 481+ * their values as value. The field names are prefixed with the
 482+ * db field prefix.
 483+ *
 484+ * @since 0.1
 485+ *
 486+ * @param array $values
 487+ *
 488+ * @return array
 489+ */
 490+ public static function getPrefixedValues( array $values ) {
 491+ $prefixedValues = array();
 492+
 493+ foreach ( $values as $field => $value ) {
 494+ $prefixedValues[static::getFieldPrefix() . $field] = $value;
 495+ }
 496+
 497+ return $prefixedValues;
 498+ }
 499+
 500+ /**
 501+ * Serializes the survey to an associative array which
 502+ * can then easily be converted into JSON or similar.
 503+ *
 504+ * @since 0.1
 505+ *
 506+ * @param null|array $props
 507+ *
 508+ * @return array
 509+ */
 510+ public function toArray( $fields = null ) {
 511+ $data = array();
 512+ $setFields = array();
 513+
 514+ if ( !is_array( $fields ) ) {
 515+ $setFields = $this->getSetFieldNames();
 516+ }
 517+ else {
 518+ foreach ( $fields as $field ) {
 519+ if ( $this->hasField( $field ) ) {
 520+ $setFields[] = $field;
 521+ }
 522+ }
 523+ }
 524+
 525+ foreach ( $setFields as $field ) {
 526+ $data[$field] = $this->getField( $field );
 527+ }
 528+
 529+ return $data;
 530+ }
 531+
 532+ /**
 533+ * Creates and returns a new instance from an array.
 534+ *
 535+ * @since 0.1
 536+ *
 537+ * @param array $data
 538+ *
 539+ * @return SurveyDBClass
 540+ */
 541+ public static function fromArray( array $data ) {
 542+ $validData = array();
 543+
 544+ foreach ( $data as $name => $value ) {
 545+ if ( static::canHasField( $name ) ) {
 546+ $validData[$name] = $value;
 547+ }
 548+ }
 549+
 550+ return new static( $validData );
 551+ }
 552+
139553 }
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -13,99 +13,36 @@
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 -
2617 /**
27 - *
28 - *
29 - * @since 0.1
30 - * @var string
31 - */
32 - protected static $fieldPrefix = 'survey_';
33 -
34 - /**
35 - * The name of the survey.
36 - *
37 - * @since 0.1
38 - * @var string
39 - */
40 - protected $name;
41 -
42 - /**
43 - * If the survey is enabled or not.
44 - *
45 - * @since 0.1
46 - * @var boolean
47 - */
48 - protected $enabled;
49 -
50 - /**
51 - * The questions that go with this survey.
52 - *
53 - * @since 0.1
54 - * @var array of SurveyQuestion
55 - */
56 - protected $questions;
57 -
58 - /**
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 - /**
8318 * @see SurveyDBClass::getDBTable()
8419 */
85 - protected function getDBTable() {
 20+ protected static function getDBTable() {
8621 return 'surveys';
8722 }
8823
8924 /**
90 - * @see SurveyDBClass::getIDField()
91 - */
92 - protected function getIDField() {
93 - return 'survey_id';
94 - }
95 -
96 - /**
97 - * Gets the fields => values to write to the surveys table.
 25+ * Returns an array with the fields and their types this object contains.
 26+ * This corresponds directly to the fields in the database, without prefix.
9827 *
9928 * @since 0.1
10029 *
10130 * @return array
10231 */
103 - protected function getWriteValues() {
 32+ protected static function getFieldTypes() {
10433 return array(
105 - 'survey_enabled' => $this->enabled,
106 - 'survey_name' => $this->name,
 34+ 'id' => 'int',
 35+ 'name' => 'str',
 36+ 'enabled' => 'bool',
 37+ 'header' => 'str',
 38+ 'footer' => 'str',
 39+ 'thanks' => 'str',
10740 );
10841 }
10942
 43+ protected static function getFieldPrefix() {
 44+ return 'survey_';
 45+ }
 46+
11047 /**
11148 * Returns the Survey with specified name, or false if there is no such survey.
11249 *
@@ -118,7 +55,7 @@
11956 * @return Survey or false
12057 */
12158 public static function newFromName( $surveyName, $fields = null, $loadQuestions = true ) {
122 - return self::newFromDB( array( 'survey_name' => $surveyName ), $fields, $loadQuestions );
 59+ return self::newFromDB( array( 'name' => $surveyName ), $fields, $loadQuestions );
12360 }
12461
12562 /**
@@ -133,7 +70,7 @@
13471 * @return Survey or false
13572 */
13673 public static function newFromId( $surveyId, $fields = null, $loadQuestions = true ) {
137 - return self::newFromDB( array( 'survey_id' => $surveyId ), $fields, $loadQuestions );
 74+ return self::newFromDB( array( 'id' => $surveyId ), $fields, $loadQuestions );
13875 }
13976
14077 /**
@@ -149,29 +86,9 @@
15087 *
15188 * @return Survey or false
15289 */
153 - protected static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) {
154 - $dbr = wfGetDB( DB_SLAVE );
 90+ public static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) {
 91+ $survey = self::selectRow( $fields, $conditions );
15592
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 -
164 - $survey = $dbr->selectRow(
165 - 'surveys',
166 - $fields,
167 - $conditions
168 - );
169 -
170 - if ( !$survey ) {
171 - return false;
172 - }
173 -
174 - $survey = self::newFromDBResult( $survey );
175 -
17693 if ( $loadQuestions ) {
17794 $survey->loadQuestionsFromDB();
17895 }
@@ -179,52 +96,16 @@
18097 return $survey;
18198 }
18299
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 -
202100 /**
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 - /**
215101 * Constructor.
216102 *
217103 * @since 0.1
218104 *
219 - * @param integer|null $id
220 - * @param string $name
221 - * @param boolean $enabled
 105+ * @param array|null $fields
222106 * @param array $questions
223107 */
224 - public function __construct( $id, $name = '', $enabled = false, array $questions = array() ) {
225 - $this->id = $id;
226 - $this->name = $name;
227 - $this->enabled = $enabled;
228 -
 108+ public function __construct( $fields, array $questions = array() ) {
 109+ parent::__construct( $fields );
229110 $this->setQuestions( $questions );
230111 }
231112
@@ -234,7 +115,7 @@
235116 * @since 0.1
236117 */
237118 public function loadQuestionsFromDB() {
238 - $this->questions = SurveyQuestion::getQuestionsForSurvey( $this->id );
 119+ $this->questions = SurveyQuestion::getQuestionsForSurvey( $this->getId() );
239120 }
240121
241122 /**
@@ -262,52 +143,7 @@
263144 return $success;
264145 }
265146
266 -
267147 /**
268 - * Returns the survey name.
269 - *
270 - * @since 0.1
271 - *
272 - * @return string
273 - */
274 - public function getName() {
275 - return $this->name;
276 - }
277 -
278 - /**
279 - * Sets the survey name.
280 - *
281 - * @since 0.1
282 - *
283 - * @param string $name
284 - */
285 - public function setName( $name ) {
286 - $this->name = $name;
287 - }
288 -
289 - /**
290 - * Returns if the survey is enabled.
291 - *
292 - * @since 0.1
293 - *
294 - * @return boolean
295 - */
296 - public function isEnabled() {
297 - return $this->enabled;
298 - }
299 -
300 - /**
301 - * Sets if the survey is enabled or not.
302 - *
303 - * @since 0.1
304 - *
305 - * @param boolean $enabled
306 - */
307 - public function setEnabled( $enabled ) {
308 - $this->enabled = $enabled;
309 - }
310 -
311 - /**
312148 * Returns the surveys questions.
313149 *
314150 * @since 0.1
@@ -319,39 +155,6 @@
320156 }
321157
322158 /**
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 - /**
356159 * Sets the surveys questions.
357160 *
358161 * @since 0.1
@@ -368,44 +171,22 @@
369172 *
370173 * @since 0.1
371174 *
372 - * @param null|array $props
 175+ * @param null|array $fields
373176 *
374177 * @return array
375178 */
376 - public function toArray( $props = null ) {
377 - $data = array(
378 - 'questions' => array(),
379 - );
 179+ public function toArray( $fields = null ) {
 180+ $data = parent::toArray( $fields );
380181
 182+ $data['questions'] = array();
 183+
381184 foreach ( $this->questions as /* SurveyQuestion */ $question ) {
382185 $data['questions'][] = $question->toArray();
383186 }
384187
385 - if ( !is_array( $props ) ) {
386 - $props = array_keys( self::$fields );
387 - }
388 -
389 - foreach ( $props as $prop ) {
390 - if ( !( $prop == 'id' && is_null( $this->{ $prop } ) ) ) {
391 - $data[$prop] = $this->getProp( $prop );
392 - }
393 - }
394 -
395188 return $data;
396189 }
397190
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 -
410191 /**
411192 * Removes the object from the database.
412193 *
@@ -455,38 +236,4 @@
456237 return $sucecss;
457238 }
458239
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 -
493240 }
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -19,80 +19,47 @@
2020 public static $TYPE_RADIO = 3;
2121
2222 /**
 23+ * Returns an array with the fields and their types this object contains.
 24+ * This corresponds directly to the fields in the database, without prefix.
 25+ *
 26+ * survey_id:
2327 * The ID of the survey this question belongs to.
2428 * This can be null. When written to the db via Survey::writeToDB of
2529 * a Survey holding this question, the survey ID will first be set.
2630 *
27 - * @since 0.1
28 - * @var integer|null
29 - */
30 - protected $surveyId;
31 -
32 - /**
 31+ * text:
3332 * The question text.
3433 *
35 - * @since 0.1
36 - * @var string
37 - */
38 - protected $text;
39 -
40 - /**
 34+ * type:
4135 * The question type.
4236 *
43 - * @since 0.1
44 - * @var integer
45 - */
46 - protected $type;
47 -
48 - /**
 37+ * required:
4938 * Indicated if the question is required,
5039 * ie if the user can not submit the survey without answering it.
5140 *
52 - * @since 0.1
53 - * @var boolean
54 - */
55 - protected $required;
56 -
57 - /**
 41+ * answers:
5842 * List of allowed values for the question.
5943 * Empty list for no restrictions.
6044 *
61 - * @since 0.1
62 - * @var array of string|number
63 - */
64 - protected $answers;
65 -
66 - /**
 45+ * removed:
6746 * Indicated if the question was removed.
6847 * Removed questions are kept in the db so their answers can
6948 * still be used untill the survey itself gets removed.
7049 *
7150 * @since 0.1
72 - * @var boolean
73 - */
74 - protected $removed;
75 -
76 - /**
77 - * Constructor.
7851 *
79 - * @since 0.1
80 - *
81 - * @param integer|null $id
82 - * @param integer $surveyId
83 - * @param string $text
84 - * @param integer $type
85 - * @param boolean $required
86 - * @param array $answers
87 - * @param boolean $removed
 52+ * @return array
8853 */
89 - public function __construct( $id, $surveyId, $text, $type, $required, array $answers = array(), $removed = false ) {
90 - $this->id = is_null( $id ) ? $id : (int)$id;
91 - $this->surveyId = (int)$surveyId;
92 - $this->text = $text;
93 - $this->type = $type;
94 - $this->required = (boolean)$required;
95 - $this->answers = $answers;
96 - $this->removed = $removed;
 54+ protected static function getFieldTypes() {
 55+ return array(
 56+ 'id' => 'int',
 57+ 'survey_id' => 'int',
 58+ 'text' => 'str',
 59+ 'type' => 'int',
 60+ 'required' => 'bool',
 61+ 'answers' => 'array',
 62+ 'removed' => 'bool',
 63+ );
9764 }
9865
9966 /**
@@ -111,27 +78,6 @@
11279 }
11380
11481 /**
115 - * Unserializes a survey question in the form of an associative array.
116 - *
117 - * @since 0.1
118 - *
119 - * @param array $args
120 - *
121 - * @return SurveyQuestion
122 - */
123 - public static function newFromArray( array $args ) {
124 - return new self(
125 - array_key_exists( 'id', $args ) ? $args['id'] : null,
126 - array_key_exists( 'surveyId', $args ) ? $args['surveyId'] : null,
127 - $args['text'],
128 - $args['type'],
129 - $args['required'],
130 - $args['answers'],
131 - $args['removed']
132 - );
133 - }
134 -
135 - /**
13682 * Serialization method for survey questions that need to be passed via multi-value API parameter.
13783 * Uses base64 and replaces padding = by !, so the values does not contain any = or |.
13884 *
@@ -144,31 +90,6 @@
14591 }
14692
14793 /**
148 - * Serializes the survey question to an associative array which
149 - * can then easily be converted into JSON or similar.
150 - *
151 - * @since 0.1
152 - *
153 - * @return array
154 - */
155 - public function toArray() {
156 - $args = array(
157 - 'surveyId' => $this->surveyId,
158 - 'text' => $this->text,
159 - 'type' => $this->type,
160 - 'required' => $this->required,
161 - 'answers' => $this->answers,
162 - 'removed' => $this->removed,
163 - );
164 -
165 - if ( !is_null( $this->id ) ) {
166 - $args['id'] = $this->id;
167 - }
168 -
169 - return $args;
170 - }
171 -
172 - /**
17394 * Returns the questions for the specified survey.
17495 *
17596 * @since 0.1
@@ -179,148 +100,24 @@
180101 * @return array of SurveyQuestion
181102 */
182103 public static function getQuestionsForSurvey( $surveyId, $incRemoved = false ) {
183 - $conditions = array( 'question_survey_id' => $surveyId );
 104+ $conditions = array( 'survey_id' => $surveyId );
184105
185106 if ( $incRemoved === false ) {
186 - $conditions['question_removed'] = 0;
 107+ $conditions['removed'] = 0;
187108 }
188109
189 - return self::getQuestionsFromDB( $conditions );
 110+ return self::select( null, $conditions );
190111 }
191112
192113 /**
193 - * Returns the questions matching the specified conditions.
194 - *
195 - * @since 0.1
196 - *
197 - * @param array $conditions
198 - *
199 - * @return array of SurveyQuestion
200 - */
201 - public static function getQuestionsFromDB( array $conditions ) {
202 - $dbr = wfgetDB( DB_SLAVE );
203 -
204 - $questions = $dbr->select(
205 - 'survey_questions',
206 - array(
207 - 'question_id',
208 - 'question_survey_id',
209 - 'question_text',
210 - 'question_type',
211 - 'question_required',
212 - 'question_answers',
213 - ),
214 - $conditions
215 - );
216 -
217 - if ( !$questions ) {
218 - return array();
219 - }
220 -
221 - $sQuestions = array();
222 -
223 - foreach ( $questions as $question ) {
224 - $sQuestions[] = new SurveyQuestion(
225 - $question->question_id,
226 - $question->question_survey_id,
227 - $question->question_text,
228 - $question->question_type,
229 - $question->question_required,
230 - unserialize( $question->question_answers )
231 - );
232 - }
233 -
234 - return $sQuestions;
235 - }
236 -
237 - /**
238114 * @see SurveyDBClass::getDBTable()
239115 */
240 - protected function getDBTable() {
 116+ protected static function getDBTable() {
241117 return 'survey_questions';
242118 }
243119
244 - /**
245 - * @see SurveyDBClass::getIDField()
246 - */
247 - protected function getIDField() {
248 - return 'question_id';
 120+ protected static function getFieldPrefix() {
 121+ return 'question_';
249122 }
250123
251 - /**
252 - * Gets the fields => values to write to the survey_questions table.
253 - *
254 - * @since 0.1
255 - *
256 - * @param integer $surveyId
257 - *
258 - * @return array
259 - */
260 - protected function getWriteValues() {
261 - return array(
262 - 'question_survey_id' => $this->surveyId,
263 - 'question_text' => $this->text,
264 - 'question_type' => $this->type,
265 - 'question_required' => $this->required,
266 - 'question_answers' => serialize( $this->answers ),
267 - );
268 - }
269 -
270 - /**
271 - * Gets the question text.
272 - *
273 - * @since 0.1
274 - *
275 - * @return string
276 - */
277 - public function getText() {
278 - return $this->text;
279 - }
280 -
281 - /**
282 - * Gets the questions type.
283 - *
284 - * @since 0.1
285 - *
286 - * @return integer
287 - */
288 - public function getType() {
289 - return $this->type;
290 - }
291 -
292 - /**
293 - * Gets if the question is required.
294 - *
295 - * @since 0.1
296 - *
297 - * @return boolean
298 - */
299 - public function isRequired() {
300 - return $this->required;
301 - }
302 -
303 - /**
304 - *
305 - *
306 - * @since 0.1
307 - *
308 - * @param integer|null $id
309 - */
310 - public function setSurveyId( $id ) {
311 - $this->surveyId = $id;
312 - }
313 -
314 - /**
315 - * Gets if the question was removed.
316 - * This means it should not be shown in the UI,
317 - * and is only kept to make sense of old answers liked to it.
318 - *
319 - * @since 0.1
320 - *
321 - * @return boolean
322 - */
323 - public function wasRemoved() {
324 - return $this->removed;
325 - }
326 -
327124 }
Index: trunk/extensions/Survey/api/ApiSubmitSurvey.php
@@ -32,7 +32,7 @@
3333 }
3434
3535 if ( isset( $params['name'] ) ) {
36 - $survey = Survey::newFromName( $params['name'], false );
 36+ $survey = Survey::newFromName( $params['name'], null, false );
3737
3838 if ( $survey === false ) {
3939 $this->dieUsage( wfMsgExt( 'survey-err-survey-name-unknown', 'parsemag', $params['name'] ), 'survey-name-unknown' );
Index: trunk/extensions/Survey/api/ApiQuerySurveys.php
@@ -41,7 +41,7 @@
4242
4343 $fields = array_merge( array( 'id' ), $params['props'] );
4444
45 - $this->addFields( $this->getSurveyFields( $fields ) );
 45+ $this->addFields( Survey::getPrefixedFields( $fields ) );
4646
4747 if ( isset( $params['ids'] ) ) {
4848 $this->addWhere( array( 'survey_id' => $params['ids'] ) );
@@ -73,12 +73,8 @@
7474 break;
7575 }
7676
77 - $surveyObject = new Survey( $survey->survey_id );
 77+ $surveyObject = Survey::newFromDBResult( $survey );
7878
79 - foreach ( $fields as $prop ) {
80 - $surveyObject->setProp( $prop, $survey->{ 'survey_' . $prop } );
81 - }
82 -
8379 if ( $params['incquestions'] ) {
8480 $surveyObject->loadQuestionsFromDB();
8581 }
@@ -95,19 +91,15 @@
9692 );
9793 }
9894
99 - protected function getSurveyFields( array $props ) {
100 - $fields = array();
101 - $allowedFields = Survey::getSurveyProps();
102 -
103 - foreach ( $props as $prop ) {
104 - if ( in_array( $prop, $allowedFields ) ) {
105 - $fields[] = "survey_$prop";
106 - }
107 - }
108 -
109 - return $fields;
110 - }
111 -
 95+ /**
 96+ *
 97+ *
 98+ * @since 0.1
 99+ *
 100+ * @param array $survey
 101+ *
 102+ * @return $survey
 103+ */
112104 protected function getSurveyData( array $survey ) {
113105 foreach ( $survey['questions'] as $nr => $question ) {
114106 $this->getResult()->setIndexedTagName( $survey['questions'][$nr], 'answer' );
@@ -133,7 +125,7 @@
134126 ApiBase::PARAM_ISMULTI => true,
135127 ),
136128 'props' => array(
137 - ApiBase::PARAM_TYPE => Survey::getSurveyProps(),
 129+ ApiBase::PARAM_TYPE => Survey::getFieldNames(),
138130 ApiBase::PARAM_ISMULTI => true,
139131 ApiBase::PARAM_DFLT => 'id|name|enabled'
140132 ),

Follow-up revisions

RevisionCommit summaryAuthorDate
r96268follow up to r96266jeroendedauw01:50, 5 September 2011
r96284follow up to r96266jeroendedauw15:59, 5 September 2011

Comments

#Comment by Nikerabbit (talk | contribs)   06:16, 5 September 2011

Uuh PHP 5.3 already?

#Comment by Alphos (talk | contribs)   06:25, 5 September 2011

PHP 5.2 is not supported anymore for quite some time now, I'd say it's about time "we" too stop supporting it ;-)

#Comment by Jeroen De Dauw (talk | contribs)   14:48, 5 September 2011

Yeah. This class was just screaming for LSB, and my client has PHP 5.3, so I'm very happy that for once I can drop compat w/ 5.2. To bad it'll take quite a while before we can do this in core :(

#Comment by 😂 (talk | contribs)   04:38, 6 September 2011

Extension authors are free to support whichever versions of PHP they want (as long as it's at least the minimum version supported by MW). If you want to write an extension using 5.4beta features, go for it ;-)

Status & tagging log