r96364 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96363‎ | r96364 | r96365 >
Date:19:53, 6 September 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on datastructures and api
Modified paths:
  • /trunk/extensions/Survey/api/ApiDeleteSurvey.php (modified) (history)
  • /trunk/extensions/Survey/includes/Survey.class.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyAnswer.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyDBClass.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveyQuestion.php (modified) (history)
  • /trunk/extensions/Survey/includes/SurveySubmission.php (modified) (history)
  • /trunk/extensions/Survey/specials/SpecialSurvey.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Survey/specials/SpecialSurvey.php
@@ -75,7 +75,10 @@
7676 $survey = Survey::newFromId( $wgRequest->getInt( 'survey-id' ), null, false );
7777 }
7878
79 - $survey->setField( 'name', $wgRequest->getText( 'survey-name' ) );
 79+ foreach ( array( 'name', 'header', 'footer', 'thanks' ) as $field ) {
 80+ $survey->setField( $field, $wgRequest->getText( 'survey-' . $field ) );
 81+ }
 82+
8083 $survey->setField( 'enabled', $wgRequest->getCheck( 'survey-enabled' ) );
8184
8285 $survey->setQuestions( $this->getSubmittedQuestions() );
Index: trunk/extensions/Survey/includes/SurveyDBClass.php
@@ -20,7 +20,7 @@
2121 * @since 0.1
2222 * @var array
2323 */
24 - protected $fields;
 24+ protected $fields = array();
2525
2626 /**
2727 * Constructor.
@@ -33,6 +33,10 @@
3434 public function __construct( $fields, $loadDefaults = false ) {
3535 $this->setField( static::getIDField(), null );
3636
 37+ if ( !is_array( $fields ) ) {
 38+ $fields = array();
 39+ }
 40+
3741 if ( $loadDefaults ) {
3842 $fields = array_merge( static::getDefaults(), $fields );
3943 }
@@ -47,9 +51,11 @@
4852 * field name => type
4953 *
5054 * Allowed types:
 55+ * * id
5156 * * str
5257 * * int
5358 * * bool
 59+ * * array
5460 *
5561 * @since 0.1
5662 *
@@ -270,7 +276,7 @@
271277 );
272278
273279 if ( $sucecss ) {
274 - $this->removeField( static::getIDField() );
 280+ $this->setField( static::getIDField(), null );
275281 }
276282
277283 return $sucecss;
@@ -339,6 +345,10 @@
340346 if ( is_string( $value ) ) {
341347 $value = unserialize( $value );
342348 }
 349+ case 'id':
 350+ if ( is_string( $value ) ) {
 351+ $value = (int)$value;
 352+ }
343353 }
344354
345355 $this->fields[$name] = $value;
@@ -363,7 +373,6 @@
364374 return $this->fields[$name];
365375 }
366376 else {
367 - var_dump($this->fields);
368377 throw new MWException( 'Attempted to get not-set field ' . $name );
369378 }
370379 }
Index: trunk/extensions/Survey/includes/SurveySubmission.php
@@ -14,84 +14,50 @@
1515 class SurveySubmission extends SurveyDBClass {
1616
1717 /**
18 - * The ID of the survey this submission is for.
19 - *
20 - * @since 0.1
21 - * @var integer
 18+ * @see SurveyDBClass::getDBTable()
2219 */
23 - protected $surveyId;
 20+ protected static function getDBTable() {
 21+ return 'survey_submissions';
 22+ }
2423
2524 /**
26 - * The ID of the page this submission was made on.
 25+ * Gets the db field prefix.
2726 *
2827 * @since 0.1
29 - * @var integer
30 - */
31 - protected $pageId;
32 -
33 - /**
34 - * The name of the user that made the submission (username or ip).
3528 *
36 - * @since 0.1
37 - * @var string
 29+ * @return string
3830 */
39 - protected $userName;
 31+ protected static function getFieldPrefix() {
 32+ return 'submission_';
 33+ }
4034
4135 /**
42 - * Timestamp idnicating when the submission was made.
 36+ * Returns an array with the fields and their types this object contains.
 37+ * This corresponds directly to the fields in the database, without prefix.
4338 *
44 - * @since 0.1
45 - * @var string
46 - */
47 - protected $timeStamp;
48 -
49 -
50 - /**
51 - * Constructor.
 39+ * survey_id:
 40+ * The ID of the survey this submission is for.
5241 *
53 - * @since 0.1
 42+ * page_id:
 43+ * The ID of the page this submission was made on.
5444 *
55 - * @param integer|null $id
56 - * @param integer $surveyId
57 - * @param string $userName
58 - * @param integer $pageId
59 - * @param string $timeStamp
60 - */
61 - public function __construct( $id, $surveyId, $userName, $pageId, $timeStamp ) {
62 - $this->id = is_null( $id ) ? $id : (int)$id;
63 - $this->surveyId = (int)$surveyId;
64 - $this->userName = $userName;
65 - $this->pageId = (int)$pageId;
66 - $this->timeStamp = $timeStamp;
67 - }
68 -
69 - /**
70 - * @see SurveyDBClass::getDBTable()
71 - */
72 - protected function getDBTable() {
73 - return 'survey_submissions';
74 - }
75 -
76 - /**
77 - * @see SurveyDBClass::getIDField()
78 - */
79 - protected function getIDField() {
80 - return 'submission_id';
81 - }
82 -
83 - /**
84 - * Gets the fields => values to write to the survey_sumissions table.
 45+ * user_name:
 46+ * The name of the user that made the submission (username or ip).
8547 *
 48+ * time:
 49+ * Timestamp idnicating when the submission was made.
 50+ *
8651 * @since 0.1
8752 *
8853 * @return array
8954 */
90 - protected function getWriteValues() {
 55+ protected static function getFieldTypes() {
9156 return array(
92 - 'submission_survey_id' => $this->surveyId,
93 - 'submission_user_name' => $this->userName,
94 - 'submission_page_id' => $this->pageId,
95 - 'submission_time' => $this->timeStamp,
 57+ 'id' => 'id',
 58+ 'survey_id' => 'id',
 59+ 'page_id' => 'id',
 60+ 'user_name' => 'str',
 61+ 'time' => 'str',
9662 );
9763 }
9864
Index: trunk/extensions/Survey/includes/SurveyAnswer.php
@@ -14,72 +14,46 @@
1515 class SurveyAnswer extends SurveyDBClass {
1616
1717 /**
18 - * The answer text.
19 - *
20 - * @since 0.1
21 - * @var string
 18+ * @see SurveyDBClass::getDBTable()
2219 */
23 - protected $text;
 20+ protected static function getDBTable() {
 21+ return 'survey_answers';
 22+ }
2423
2524 /**
26 - * The ID of the submission this answer is part of.
 25+ * Gets the db field prefix.
2726 *
2827 * @since 0.1
29 - * @var integer
30 - */
31 - protected $submissionId;
32 -
33 - /**
34 - * The ID of the question this answer corresponds to.
3528 *
36 - * @since 0.1
37 - * @var integer
 29+ * @return string
3830 */
39 - protected $questionId;
 31+ protected static function getFieldPrefix() {
 32+ return 'answer_';
 33+ }
4034
4135 /**
42 - * Constructor.
 36+ * Returns an array with the fields and their types this object contains.
 37+ * This corresponds directly to the fields in the database, without prefix.
4338 *
44 - * @since 0.1
 39+ * text:
 40+ * The answer text.
4541 *
46 - * @param integer|null $id
47 - * @param integer $submissionId
48 - * @param integer $questionId
49 - * @param string $text
50 - */
51 - public function __construct( $id, $submissionId, $questionId, $text ) {
52 - $this->id = is_null( $id ) ? $id : (int)$id;
53 - $this->submissionId = (int)$submissionId;
54 - $this->questionId = (int)$questionId;
55 - $this->text = $text;
56 - }
57 -
58 - /**
59 - * @see SurveyDBClass::getDBTable()
60 - */
61 - protected function getDBTable() {
62 - return 'survey_answers';
63 - }
64 -
65 - /**
66 - * @see SurveyDBClass::getIDField()
67 - */
68 - protected function getIDField() {
69 - return 'answer_id';
70 - }
71 -
72 - /**
73 - * Gets the fields => values to write to the survey_answers table.
 42+ * submission_id:
 43+ * The ID of the submission this answer is part of.
7444 *
 45+ * question_id:
 46+ * The ID of the question this answer corresponds to.
 47+ *
7548 * @since 0.1
7649 *
7750 * @return array
7851 */
79 - protected function getWriteValues() {
 52+ protected static function getFieldTypes() {
8053 return array(
81 - 'answer_submission_id' => $this->submissionId,
82 - 'answer_question_id' => $this->questionId,
83 - 'answer_text' => $this->text,
 54+ 'id' => 'id',
 55+ 'text' => 'str',
 56+ 'submission_id' => 'id',
 57+ 'question_id' => 'id',
8458 );
8559 }
8660
Index: trunk/extensions/Survey/includes/Survey.class.php
@@ -226,7 +226,7 @@
227227 $submissionsForSurvey = $dbr->select(
228228 'survey_submissions',
229229 array( 'submission_id' ),
230 - array( 'submission_survey_id' => $this->id )
 230+ array( 'submission_survey_id' => $this->getId() )
231231 );
232232
233233 $dbw = wfgetDB( DB_MASTER );
@@ -237,18 +237,18 @@
238238
239239 $sucecss = $dbw->delete(
240240 'survey_questions',
241 - array( 'question_survey_id' => $this->id )
 241+ array( 'question_survey_id' => $this->getId() )
242242 ) && $sucecss;
243243
244244 $sucecss = $dbw->delete(
245245 'survey_submissions',
246 - array( 'submission_survey_id' => $this->id )
 246+ array( 'submission_survey_id' => $this->getId() )
247247 ) && $sucecss;
248248
249249 foreach ( $submissionsForSurvey as $nr => $submission ) {
250250 $sucecss = $dbw->delete(
251251 'survey_answers',
252 - array( 'answer_submission_id' => $submission->submission_id )
 252+ array( 'answer_submission_id' => $submission->getId() )
253253 ) && $sucecss;
254254
255255 if ( $nr % 500 == 0 ) {
Index: trunk/extensions/Survey/includes/SurveyQuestion.php
@@ -52,7 +52,7 @@
5353 */
5454 protected static function getFieldTypes() {
5555 return array(
56 - 'id' => 'int',
 56+ 'id' => 'id',
5757 'survey_id' => 'int',
5858 'text' => 'str',
5959 'type' => 'int',
Index: trunk/extensions/Survey/api/ApiDeleteSurvey.php
@@ -30,7 +30,7 @@
3131 $everythingOk = true;
3232
3333 foreach ( $params['ids'] as $id ) {
34 - $surey = new Survey( $id );
 34+ $surey = new Survey( array( 'id' => $id ) );
3535 $everythingOk = $surey->removeFromDB() && $everythingOk;
3636 }
3737

Status & tagging log