r98182 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98181‎ | r98182 | r98183 >
Date:22:03, 26 September 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
work on contest editing special page
Modified paths:
  • /trunk/extensions/Contest/Contest.i18n.php (modified) (history)
  • /trunk/extensions/Contest/Contest.sql (modified) (history)
  • /trunk/extensions/Contest/INSTALL (modified) (history)
  • /trunk/extensions/Contest/includes/Contest.class.php (modified) (history)
  • /trunk/extensions/Contest/specials/SpecialContests.php (modified) (history)
  • /trunk/extensions/Contest/specials/SpecialEditContest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Contest/Contest.i18n.php
@@ -25,6 +25,11 @@
2626 'right-contestparticipant' => 'Participate in contests',
2727 'right-contestjudge' => 'Judge contest submissions',
2828
 29+ // Contest statuses
 30+ 'contest-status-draft' => 'Draft (disabled)',
 31+ 'contest-status-active' => 'Active (enabled)',
 32+ 'contest-status-finished' => 'Finished (disabled)',
 33+
2934 // Special page names
3035 'special-contest' => 'Contest',
3136 'special-contests' => 'Contests',
@@ -37,6 +42,12 @@
3843 'contest-special-namedoc' => 'The name of the contest is the identifier used in URLs. ie "name" in Special:Contest/name',
3944 'contest-special-newname' => 'Contest name',
4045 'contest-special-add' => 'Add contest',
 46+
 47+ // Special:EditContest
 48+ 'editcontest-text' => 'You are editing a contest.',
 49+ 'editcontest-legend' => 'Contest',
 50+ 'contest-edit-name' => 'Contest name',
 51+ 'contest-edit-status' => 'Contest status',
4152 );
4253
4354 /** Message documentation (Message documentation)
Index: trunk/extensions/Contest/specials/SpecialContests.php
@@ -95,7 +95,7 @@
9696 'submit'
9797 ) );
9898
99 - $out->addHTML( Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) );
 99+ $out->addHTML( Html::hidden( 'newEditToken', $this->getUser()->editToken() ) );
100100
101101 $out->addHTML( '</fieldset></form>' );
102102 }
Index: trunk/extensions/Contest/specials/SpecialEditContest.php
@@ -11,8 +11,11 @@
1212 * @licence GNU GPL v3 or later
1313 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1414 */
15 -class SpecialEditContest extends SpecialContestPage {
 15+class SpecialEditContest extends FormSpecialPage {
1616
 17+ protected $contest = false;
 18+ protected $isNewPost = false;
 19+
1720 /**
1821 * Constructor.
1922 *
@@ -23,6 +26,27 @@
2427 }
2528
2629 /**
 30+ * @see SpecialPage::getDescription
 31+ *
 32+ * @since 0.1
 33+ */
 34+ public function getDescription() {
 35+ return wfMsg( 'special-' . strtolower( $this->getName() ) );
 36+ }
 37+
 38+ /**
 39+ * Sets headers - this should be called from the execute() method of all derived classes!
 40+ *
 41+ * @since 0.1
 42+ */
 43+ public function setHeaders() {
 44+ $out = $this->getOutput();
 45+ $out->setArticleRelated( false );
 46+ $out->setRobotPolicy( 'noindex,nofollow' );
 47+ $out->setPageTitle( $this->getDescription() );
 48+ }
 49+
 50+ /**
2751 * Main method.
2852 *
2953 * @since 0.1
@@ -30,36 +54,141 @@
3155 * @param string $arg
3256 */
3357 public function execute( $subPage ) {
34 - if ( !parent::execute( $subPage ) ) {
35 - return;
36 - }
 58+ $this->setParameter( $subPage );
 59+ $this->setHeaders();
 60+ $this->outputHeader();
 61+
 62+ // This will throw exceptions if there's a problem
 63+ $this->userCanExecute( $this->getUser() );
3764
38 - if ( $this->getRequest()->wasPosted() && $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'wpEditToken' ) ) ) {
 65+ if ( $this->getRequest()->wasPosted() && $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'newEditToken' ) ) ) {
3966 $data = array( 'name' => $this->getRequest()->getVal( 'newcontest' ) );
4067
4168 $contest = Contest::s()->selectRow( null, $data );
4269
4370 if ( $contest === false ) {
44 - $contest = new Contest( $data );
 71+ $contest = new Contest( $data, true );
4572 }
4673 else {
4774 // TODO: warn not new
4875 }
4976 }
5077 else {
51 - $contest = Contest::s()->selectRow( array( 'name' => $subPage ) );
 78+ $contest = Contest::s()->selectRow( null, array( 'name' => $subPage ) );
5279 }
5380
5481 if ( $contest === false ) {
5582 $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Contests' )->getLocalURL() );
5683 }
5784 else {
58 - $this->displayForm( $contest );
 85+ $this->contest = $contest;
 86+ $form = $this->getForm();
 87+
 88+ if ( $form->show() ) {
 89+ $this->onSuccess();
 90+ }
5991 }
6092 }
6193
62 - protected function displayForm( Contest $contest ) {
 94+ /**
 95+ * (non-PHPdoc)
 96+ * @see FormSpecialPage::getForm()
 97+ */
 98+ protected function getForm() {
 99+ $form = parent::getForm();
63100
 101+ $form->addButton(
 102+ 'cancelEdit',
 103+ wfMsg( 'cancel' ),
 104+ 'cancelEdit'
 105+ );
 106+
 107+// $form->addButton(
 108+// 'deleteEdit',
 109+// wfMsg( 'delete' ),
 110+// 'deleteEdit'
 111+// );
 112+
 113+ return $form;
64114 }
65115
 116+ /**
 117+ * (non-PHPdoc)
 118+ * @see FormSpecialPage::getFormFields()
 119+ */
 120+ protected function getFormFields() {
 121+ $contest = $this->contest;
 122+
 123+ if ( $contest === false ) {
 124+ return array();
 125+ }
 126+
 127+ $fields = array();
 128+
 129+ $fields['id'] = array ( 'type' => 'hidden', 'default' => $contest->getId() );
 130+ $fields['name'] = array ( 'type' => 'text', 'default' => $contest->getField( 'name' ), 'label-message' => 'contest-edit-name' );
 131+ $fields['status'] = array (
 132+ 'type' => 'radio',
 133+ 'default' => $contest->getField( 'status' ),
 134+ 'label-message' => 'contest-edit-status',
 135+ 'options' => Contest::getStatusMessages()
 136+ );
 137+
 138+ // TODO
 139+
 140+ $mappedFields = array();
 141+
 142+ foreach ( $fields as $name => $field ) {
 143+ $mappedFields['contest-' . $name] = $field;
 144+ }
 145+
 146+ return $mappedFields;
 147+ }
 148+
 149+ /**
 150+ * Process the form. At this point we know that the user passes all the criteria in
 151+ * userCanExecute(), and if the data array contains 'Username', etc, then Username
 152+ * resets are allowed.
 153+ * @param $data array
 154+ * @return Bool|Array
 155+ */
 156+ public function onSubmit( array $data ) {
 157+ $fields = array();
 158+
 159+ foreach ( $data as $name => $value ) {
 160+ $matches = array();
 161+
 162+ if ( preg_match( '/contest-(\.+)/', $name, $matches ) ) {
 163+ $fields[$matches[1]] = $value;
 164+ }
 165+ }
 166+
 167+ if ( !array_key_exists( 'id', $fields ) || $fields['id'] === '' ) {
 168+ $fields['id'] = null;
 169+ }
 170+
 171+ $sessionField = 'contestid-' . $fields['name'];
 172+
 173+ if ( is_null( $fields['id'] ) && !is_null( $wgRequest->getSessionData( $sessionField ) ) ) {
 174+ $contest->setId( $wgRequest->getSessionData( $sessionField ) );
 175+ }
 176+
 177+ $contest = new Contest( $fields );
 178+
 179+ $success = $contest->writeToDB();
 180+
 181+ $this->getRequest()->setSessionData( $sessionField, $contest->getId() );
 182+
 183+ if ( $success ) {
 184+ return true;
 185+ }
 186+ else {
 187+ return array(); // TODO
 188+ }
 189+ }
 190+
 191+ public function onSuccess() {
 192+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Contests' )->getLocalURL() );
 193+ }
 194+
66195 }
\ No newline at end of file
Index: trunk/extensions/Contest/INSTALL
@@ -8,7 +8,7 @@
99
1010 Contest requires:
1111
12 -* MediaWiki 1.17 or above
 12+* MediaWiki 1.18 or above
1313 * PHP 5.2 or above
1414 * MySQL
1515
Index: trunk/extensions/Contest/includes/Contest.class.php
@@ -13,6 +13,10 @@
1414 */
1515 class Contest extends ContestDBObject {
1616
 17+ const STATUS_DRAFT = 0;
 18+ const STATUS_ACTIVE = 1;
 19+ const STATUS_FINISHED = 2;
 20+
1721 /**
1822 * Method to get an instance so methods that ought to be static,
1923 * but can't be due to PHP 5.2 not having LSB, can be called on
@@ -66,7 +70,7 @@
6771 return array(
6872 'id' => 'id',
6973 'name' => 'str',
70 - 'enabled' => 'bool',
 74+ 'status' => 'int',
7175 'submission_count' => 'int'
7276 );
7377 }
@@ -81,9 +85,33 @@
8286 public function getDefaults() {
8387 return array(
8488 'name' => '',
85 - 'enabled' => false,
 89+ 'status' => self::STATUS_DRAFT,
8690 'submission_count' => 0,
8791 );
8892 }
8993
 94+ public static function getStatusMessage( $status ) {
 95+ static $map = false;
 96+
 97+ if ( $map === false ) {
 98+ $map = array_flip( self::getStatusMessages() );
 99+ }
 100+
 101+ return $map[$status];
 102+ }
 103+
 104+ public static function getStatusMessages() {
 105+ static $map = false;
 106+
 107+ if ( $map === false ) {
 108+ $map = array(
 109+ wfMsg( 'contest-status-draft' ) => self::STATUS_DRAFT,
 110+ wfMsg( 'contest-status-active' ) => self::STATUS_ACTIVE,
 111+ wfMsg( 'contest-status-finished' ) => self::STATUS_FINISHED,
 112+ );
 113+ }
 114+
 115+ return $map;
 116+ }
 117+
90118 }
\ No newline at end of file
Index: trunk/extensions/Contest/Contest.sql
@@ -6,7 +6,7 @@
77 CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/contests (
88 contest_id SMALLINT unsigned NOT NULL auto_increment PRIMARY KEY,
99 contest_name VARCHAR(255) NOT NULL, -- String indentifier for the contest
10 - contest_enabled TINYINT NOT NULL default '0', -- If the contest can be taken by users
 10+ contest_status TINYINT unsigned NOT NULL default '0', -- Status of the contest
1111 contest_submission_count SMALLINT unsigned NOT NULL --
1212 ) /*$wgDBTableOptions*/;
1313

Comments

#Comment by Siebrand (talk | contribs)   05:03, 27 September 2011

Please add message documentation for the newly added messages. Thanks.

Status & tagging log