r92555 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92554‎ | r92555 | r92556 >
Date:19:27, 19 July 2011
Author:jeroendedauw
Status:ok (Comments)
Tags:
Comment:
work on upload campaign configuration
Modified paths:
  • /trunk/extensions/UploadWizard/SpecialUploadCampaign.php (added) (history)
  • /trunk/extensions/UploadWizard/SpecialUploadCampaigns.php (modified) (history)
  • /trunk/extensions/UploadWizard/UWCampaign.php (added) (history)
  • /trunk/extensions/UploadWizard/UploadWizard.alias.php (modified) (history)
  • /trunk/extensions/UploadWizard/UploadWizard.i18n.php (modified) (history)
  • /trunk/extensions/UploadWizard/UploadWizard.php (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/UWCampaign.php
@@ -0,0 +1,421 @@
 2+<?php
 3+
 4+/**
 5+ * Class that represents a single upload campaign.
 6+ *
 7+ * @file
 8+ * @ingroup Upload
 9+ *
 10+ * @since 1.2
 11+ *
 12+ * @licence GNU GPL v3+
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class UWCampaign {
 16+
 17+ /**
 18+ * If the ID of the campaign.
 19+ * Either this matched a record in the uw_campaigns table or is null.
 20+ *
 21+ * @since 1.2
 22+ * @var integer or null
 23+ */
 24+ protected $id;
 25+
 26+ /**
 27+ * If the name of the campaign.
 28+ * This name is the string used to invoke the campaign via campaign=name.
 29+ *
 30+ * @since 1.2
 31+ * @var string
 32+ */
 33+ protected $name;
 34+
 35+ /**
 36+ * If the campaign is enabled or not.
 37+ *
 38+ * @since 1.2
 39+ * @var boolean
 40+ */
 41+ protected $isEnabled;
 42+
 43+ /**
 44+ * The campaign configuration.
 45+ *
 46+ * @since 1.2
 47+ * @var array
 48+ */
 49+ protected $config;
 50+
 51+ /**
 52+ * If the campaign config has been loaded or not.
 53+ *
 54+ * @since 1.2
 55+ * @var boolean
 56+ */
 57+ protected $loadedConfig = false;
 58+
 59+ /**
 60+ * Create a new instance of $campaignName.
 61+ *
 62+ * @since 1.2
 63+ *
 64+ * @param integer $id
 65+ * @param string $name
 66+ * @param boolean $isEnabled
 67+ * @param array $config
 68+ */
 69+ public function __construct( $id, $name, $isEnabled, array $config ) {
 70+ $this->id = $id;
 71+ $this->name = $name;
 72+ $this->isEnabled = $isEnabled;
 73+ $this->config = $config;
 74+
 75+ $this->loadedConfig = count( $this->config ) > 0;
 76+ }
 77+
 78+ /**
 79+ * Returns the UWCampaign with specified name, or false if there is no such campaign.
 80+ *
 81+ * @since 1.2
 82+ *
 83+ * @param string $campaignName
 84+ * @param boolean $loadConfig
 85+ *
 86+ * @return UWCampaign or false
 87+ */
 88+ public static function newFromName( $campaignName, $loadConfig = true ) {
 89+ return self::newFromDB( array( 'campaign_name' => $campaignName ), $loadConfig );
 90+ }
 91+
 92+ /**
 93+ * Returns the UWCampaign with specified ID, or false if there is no such campaign.
 94+ *
 95+ * @since 1.2
 96+ *
 97+ * @param integer $campaignId
 98+ * @param boolean $loadConfig
 99+ *
 100+ * @return UWCampaign or false
 101+ */
 102+ public static function newFromId( $campaignId, $loadConfig = true ) {
 103+ return self::newFromDB( array( 'campaign_id' => $campaignId ), $loadConfig );
 104+ }
 105+
 106+ /**
 107+ * Returns a new instance of UWCampaign build from a database result
 108+ * obtained by doing a select with the porvided conditions on the uw_campaigns table.
 109+ * If no campaign matches the conditions, false will be returned.
 110+ *
 111+ * @since 1.2
 112+ *
 113+ * @param array $conditions
 114+ * @param boolean $loadConfig
 115+ *
 116+ * @return UWCampaign or false
 117+ */
 118+ protected static function newFromDB( array $conditions, $loadConfig = true ) {
 119+ $dbr = wfGetDB( DB_SLAVE );
 120+
 121+ $campaign = $dbr->selectRow(
 122+ 'uw_campaigns',
 123+ array(
 124+ 'campaign_id',
 125+ 'campaign_name',
 126+ 'campaign_enabled',
 127+ ),
 128+ $conditions
 129+ );
 130+
 131+ if ( !$campaign ) {
 132+ return false;
 133+ }
 134+
 135+ $config = $loadConfig ? self::getPropsFromDB( $dbr, $campaign->campaign_id ) : array();
 136+
 137+ return new self(
 138+ $campaign->campaign_id,
 139+ $campaign->campaign_name,
 140+ $campaign->campaign_enabled,
 141+ $config
 142+ );
 143+ }
 144+
 145+ /**
 146+ * Returns the id of the campaign.
 147+ *
 148+ * @since 1.2
 149+ *
 150+ * @return intgere
 151+ */
 152+ public function getId() {
 153+ return $this->id;
 154+ }
 155+
 156+ /**
 157+ * Returns the name of the campaign.
 158+ *
 159+ * @since 1.2
 160+ *
 161+ * @return string
 162+ */
 163+ public function getName() {
 164+ return $this->name;
 165+ }
 166+
 167+ /**
 168+ * Returns if the campaign is enabled.
 169+ *
 170+ * @since 1.2
 171+ *
 172+ * @return boolean
 173+ */
 174+ public function getIsEnabled() {
 175+ return $this->isEnabled;
 176+ }
 177+
 178+ /**
 179+ * Sets all config properties.
 180+ *
 181+ * @since 1.2
 182+ *
 183+ * @param array $config
 184+ */
 185+ public function setConfig( array $config ) {
 186+ $this->config = $config;
 187+ $this->loadedConfig = true;
 188+ }
 189+
 190+ /**
 191+ * Returns all set config properties.
 192+ * Property name => property value
 193+ *
 194+ * @since 1.2
 195+ *
 196+ * @return array
 197+ */
 198+ public function getConfig() {
 199+ if ( !$this->loadedConfig ) {
 200+ if ( !is_null( $this->id ) ) {
 201+ $this->config = self::getPropsFromDB( wfGetDB( DB_SLAVE ), $this->id );
 202+ }
 203+
 204+ $this->loadedConfig = true;
 205+ }
 206+
 207+ return $this->config;
 208+ }
 209+
 210+ /**
 211+ * Returns all config properties by merging the set ones with a list of default ones.
 212+ * Property name => array( 'default' => $value, 'type' => HTMLForm input type )
 213+ *
 214+ * @since 1.2
 215+ *
 216+ * @return array
 217+ */
 218+ public function getAllConfig() {
 219+ $config = $this->getConfig();
 220+
 221+ foreach ( self::getDefaultConfig() as $name => $data ) {
 222+ if ( array_key_exists( $name, $config ) ) {
 223+ $data['default'] = $config[$name];
 224+ }
 225+
 226+ $config[$name] = $data;
 227+ }
 228+
 229+ return $config;
 230+ }
 231+
 232+ /**
 233+ * Returns the default configuration values.
 234+ *
 235+ * @since 1.2
 236+ *
 237+ * @return array
 238+ */
 239+ public static function getDefaultConfig() {
 240+ return array (
 241+ 'skiptutorial' => array ( 'type' => 'check', 'default' => true )
 242+ );
 243+ }
 244+
 245+ /**
 246+ * Returns the value of the specified config property.
 247+ *
 248+ * @since 1.2
 249+ *
 250+ * @param string $property
 251+ *
 252+ * @return mixed
 253+ */
 254+ public function getProperty( $property ) {
 255+ global $wgUploadWizardConfig;
 256+
 257+ if ( array_key_exists( $property, $this->config ) ) {
 258+ return $this->config[$property];
 259+ }
 260+ elseif ( array_key_exists( $property, $wgUploadWizardConfig['campaignDefaults'] ) ) {
 261+ return $wgUploadWizardConfig['campaignDefaults'][$property];
 262+ }
 263+ else {
 264+ return null;
 265+ }
 266+ }
 267+
 268+ /**
 269+ * Write the campaign to the DB.
 270+ * If it's already there, it'll be updated, else it'll be inserted.
 271+ *
 272+ * @since 1.2
 273+ *
 274+ * @return boolean Success indicator
 275+ */
 276+ public function writeToDB() {
 277+ if ( is_null( $this->id ) ) {
 278+ return $this->insertIntoDB();
 279+ }
 280+ else {
 281+ return $this->updateInDB();
 282+ }
 283+ }
 284+
 285+ /**
 286+ * Insert the campaign into the DB.
 287+ *
 288+ * @since 1.2
 289+ *
 290+ * @return boolean Success indicator
 291+ */
 292+ protected function insertIntoDB() {
 293+ $dbw = wfGetDB( DB_MASTER );
 294+
 295+ $success = $dbw->insert(
 296+ 'uw_campaigns',
 297+ array(
 298+ 'campaign_name' => $this->name,
 299+ 'campaign_enabled' => $this->isEnabled,
 300+ ),
 301+ array( 'campaign_id' => $this->id )
 302+ );
 303+
 304+ if ( $success ) {
 305+ $this->id = $dbw->insertId();
 306+ $success &= $this->writePropsToDB( $dbw );
 307+ }
 308+
 309+ return $success;
 310+ }
 311+
 312+ /**
 313+ * Update the campaign in the DB.
 314+ *
 315+ * @since 1.2
 316+ *
 317+ * @return boolean Success indicator
 318+ */
 319+ protected function updateInDB() {
 320+ $dbw = wfGetDB( DB_MASTER );
 321+
 322+ $success = $dbw->update(
 323+ 'uw_campaigns',
 324+ array(
 325+ 'campaign_name' => $this->name,
 326+ 'campaign_enabled' => $this->isEnabled,
 327+ ),
 328+ array( 'campaign_id' => $this->id )
 329+ );
 330+
 331+ // Delete and insert instead of update.
 332+ // This will not result into dead-data when config vars are removed.
 333+ $success &= $dbw->delete(
 334+ 'uw_campaign_conf',
 335+ array( 'cc_campaign_id' => $this->id )
 336+ );
 337+
 338+ $success &= $this->writePropsToDB( $dbw );
 339+
 340+ return $success;
 341+ }
 342+
 343+ /**
 344+ * Write (insert) the properties into the DB.
 345+ *
 346+ * @since 1.2
 347+ *
 348+ * @param Database $dbw
 349+ *
 350+ * @return boolean Success indicator
 351+ */
 352+ protected function writePropsToDB( DatabaseBase $dbw ) {
 353+ $success = true;
 354+
 355+ foreach ( $this->config as $prop => $value ) {
 356+ $success &= $dbw->insert(
 357+ 'uw_campaign_conf',
 358+ array(
 359+ 'cc_campaign_id' => $this->id,
 360+ 'cc_property' => $prop,
 361+ 'cc_value' => $value
 362+ )
 363+ );
 364+ }
 365+
 366+ return $success;
 367+ }
 368+
 369+ /**
 370+ * Get the configuration properties from the DB.
 371+ *
 372+ * @since 1.2
 373+ *
 374+ * @param Database $dbr
 375+ * @param integer $campaignId
 376+ *
 377+ * @return array
 378+ */
 379+ protected static function getPropsFromDB( DatabaseBase $dbr, $campaignId ) {
 380+ $config = array();
 381+
 382+ $confProps = $dbr->select(
 383+ 'uw_campaign_conf',
 384+ array( 'cc_property', 'cc_value' ),
 385+ array( 'cc_campaign_id' => $campaignId )
 386+ );
 387+
 388+ foreach ( $confProps as $confProp ) {
 389+ $config[$confProp->cc_property] = $confProp->cc_value;
 390+ }
 391+
 392+ return $config;
 393+ }
 394+
 395+ /**
 396+ * Delete the campaign from the DB (when present).
 397+ *
 398+ * @since 1.2
 399+ *
 400+ * @return boolean Success indicator
 401+ */
 402+ public function deleteFromDB() {
 403+ if ( is_null( $this->id ) ) {
 404+ return true;
 405+ }
 406+
 407+ $dbw = wfGetDB( DB_MASTER );
 408+
 409+ $d1 = $dbw->delete(
 410+ 'uw_campaigns',
 411+ array( 'campaign_id' => $this->id )
 412+ );
 413+
 414+ $d2 = $dbw->delete(
 415+ 'uw_campaign_conf',
 416+ array( 'cc_campaign_id' => $this->id )
 417+ );
 418+
 419+ return $d1 && $d2;
 420+ }
 421+
 422+}
Property changes on: trunk/extensions/UploadWizard/UWCampaign.php
___________________________________________________________________
Added: svn:eol-style
1423 + native
Index: trunk/extensions/UploadWizard/UploadWizard.alias.php
@@ -10,6 +10,7 @@
1111 $specialPageAliases['en'] = array(
1212 'UploadWizard' => array( 'UploadWizard' ),
1313 'UploadCampaigns' => array( 'UploadCampaigns' ),
 14+ 'UploadCampaign' => array( 'UploadCampaign' ),
1415 );
1516
1617 /** Arabic (العربية) */
Index: trunk/extensions/UploadWizard/UploadWizard.php
@@ -39,9 +39,11 @@
4040 foreach ( array(
4141 'SpecialUploadWizard',
4242 'SpecialUploadCampaigns',
 43+ 'SpecialUploadCampaign',
4344 'UploadWizardMessages',
4445 'UploadWizardHooks',
45 - 'UploadWizardTutorial'
 46+ 'UploadWizardTutorial',
 47+ 'UWCampaign'
4648 ) as $module ) {
4749 $wgAutoloadLocalClasses[$module] = $wgUpwizDir . '/' . $module . '.php';
4850 }
@@ -56,6 +58,9 @@
5759 $wgSpecialPages['UploadCampaigns'] = 'SpecialUploadCampaigns';
5860 $wgSpecialPageGroups['UploadCampaigns'] = 'media';
5961
 62+$wgSpecialPages['UploadCampaign'] = 'SpecialUploadCampaign';
 63+$wgSpecialPageGroups['UploadCampaign'] = 'media';
 64+
6065 $wgResourceLoaderNamedPaths[ 'UploadWizardPage' ] = 'extensions/UploadWizard/UploadWizardPage.js';
6166
6267 // Set up the javascript path for the loader and localization file.
@@ -69,6 +74,15 @@
7075 $wgHooks['CanonicalNamespaces'][] = 'UploadWizardHooks::canonicalNamespaces';
7176 $wgHooks['LoadExtensionSchemaUpdates'][] = 'UploadWizardHooks::onSchemaUpdate';
7277
 78+$wgAvailableRights[] = 'upwizcampaigns';
 79+
 80+# Users that can modify the upload campaigns (ie via Special:UploadCampaigns)
 81+$wgGroupPermissions['*' ]['upwizcampaigns'] = false;
 82+$wgGroupPermissions['user' ]['upwizcampaigns'] = false;
 83+$wgGroupPermissions['autoconfirmed']['upwizcampaigns'] = false;
 84+$wgGroupPermissions['bot' ]['upwizcampaigns'] = false;
 85+$wgGroupPermissions['sysop' ]['upwizcampaigns'] = true;
 86+
7387 // Init the upload wizard config array
7488 // UploadWizard.config.php includes default configuration
7589 // any value can be modified in localSettings.php by setting $wgUploadWizardConfig['name'] = 'value;
Index: trunk/extensions/UploadWizard/SpecialUploadCampaigns.php
@@ -3,7 +3,7 @@
44 /**
55 * Special:UploadCampaigns
66 *
7 - * Configuration interface for upload wizard campaigns.
 7+ * Lists the upload campaigns.
88 *
99 * @file
1010 * @ingroup SpecialPage
@@ -23,9 +23,7 @@
2424 * @param $par is everything in the URL after Special:UploadCampaigns. Not sure what we can use it for
2525 */
2626 public function __construct( $request = null, $par = null ) {
27 - global $wgRequest;
28 -
29 - parent::__construct( 'UploadCampaigns', 'delete' );
 27+ parent::__construct( 'UploadCampaigns', 'upwizcampaigns' );
3028 }
3129
3230 /**
@@ -36,48 +34,36 @@
3735 public function execute( $subPage ) {
3836 $this->setHeaders();
3937 $this->outputHeader();
 38+ $subPage = explode( '/', $subPage, 2 );
4039
4140 // If the user is authorized, display the page, if not, show an error.
4241 if ( $this->userCanExecute( $GLOBALS['wgUser'] ) ) {
43 - if ( $GLOBALS['wgRequest']->wasPosted() && $GLOBALS['wgUser']->matchEditToken( $GLOBALS['wgRequest']->getVal( 'wpEditToken' ) ) ) {
44 - $this->handleSubmission();
45 - }
 42+ global $wgRequest;
4643
47 - if ( !is_null( $subPage ) && trim( $subPage ) != '' ) {
48 - if ( $subPage == 'new' ) {
49 - $this->displayCampaign( new UWCampaign( null, '', true, array() ), true );
50 - }
51 - else {
52 - $campaign = UWCampaign::newFromName( $subPage );
53 -
54 - if ( $campaign === false ) {
55 - $this->displayCampaignList();
56 - }
57 - else {
58 - $this->displayCampaign( $campaign );
59 - }
60 - }
 44+ if ( $wgRequest->wasPosted()
 45+ && $GLOBALS['wgUser']->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) )
 46+ && $wgRequest->getCheck( 'newcampaign' ) ) {
 47+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'UploadCampaign', $wgRequest->getVal( 'newcampaign' ) )->getLocalURL() );
6148 }
 49+ elseif ( count( $subPage ) == 2 && $subPage[0] == 'del' ) {
 50+ $campaign = UWCampaign::newFromName( $subPage[1], false );
 51+ $campaign->deleteFromDB();
 52+ $this->getOutput()->redirect( $this->getTitle()->getLocalURL() );
 53+ }
6254 else {
63 - $this->displayCampaignList();
 55+ $this->displayUploadCamaigns();
6456 }
6557 } else {
6658 $this->displayRestrictionError();
6759 }
6860 }
6961
70 - protected function handleSubmission() {
71 - global $wgOut;
72 -
73 - // TODO
74 - }
75 -
7662 /**
77 - * Displays a list of all campaigns.
 63+ * Displays the pages regular output.
7864 *
7965 * @since 1.2
8066 */
81 - protected function displayCampaignList() {
 67+ protected function displayUploadCamaigns() {
8268 $this->displayAddNewControl();
8369
8470 $dbr = wfGetDB( DB_SLAVE );
@@ -96,10 +82,15 @@
9783 }
9884 }
9985
 86+ /**
 87+ * Displays a small form to add a new campaign.
 88+ *
 89+ * @since 1.2
 90+ */
10091 protected function displayAddNewControl() {
101 - global $wgOut;
 92+ $out = $this->getOutput();
10293
103 - $wgOut->addHTML( Html::openElement(
 94+ $out->addHTML( Html::openElement(
10495 'form',
10596 array(
10697 'method' => 'post',
@@ -107,414 +98,82 @@
10899 )
109100 ) );
110101
111 - $wgOut->addHTML( '<fieldset>' );
 102+ $out->addHTML( '<fieldset>' );
112103
113 - $wgOut->addHTML( '<legend>' . htmlspecialchars( wfMsg( 'mwe-upwiz-campaigns-addnew' ) ) . '</legend>' );
 104+ $out->addHTML( '<legend>' . htmlspecialchars( wfMsg( 'mwe-upwiz-campaigns-addnew' ) ) . '</legend>' );
114105
115 - $wgOut->addHTML( Html::element( 'p', array(), wfMsg( 'mwe-upwiz-campaigns-namedoc' ) ) );
 106+ $out->addHTML( Html::element( 'p', array(), wfMsg( 'mwe-upwiz-campaigns-namedoc' ) ) );
116107
117 - $wgOut->addHTML( Html::element( 'label', array( 'for' => 'newcampaign' ), wfMsg( 'mwe-upwiz-campaigns-newname' ) ) );
 108+ $out->addHTML( Html::element( 'label', array( 'for' => 'newcampaign' ), wfMsg( 'mwe-upwiz-campaigns-newname' ) ) );
118109
119 - $wgOut->addHTML( Html::input( 'newcampaign' ) . '&#160;' );
 110+ $out->addHTML( Html::input( 'newcampaign' ) . '&#160;' );
120111
121 - $wgOut->addHTML( Html::input(
 112+ $out->addHTML( Html::input(
122113 'addnewcampaign',
123114 wfMsg( 'mwe-upwiz-campaigns-add' ),
124115 'submit'
125116 ) );
126117
127 - $wgOut->addHTML( Html::hidden( 'wpEditToken', $GLOBALS['wgUser']->editToken() ) );
 118+ $out->addHTML( Html::hidden( 'wpEditToken', $GLOBALS['wgUser']->editToken() ) );
128119
129 - $wgOut->addHTML( '</fieldset></form>' );
 120+ $out->addHTML( '</fieldset></form>' );
130121 }
131122
 123+ /**
 124+ * Displays a list of all campaigns.
 125+ *
 126+ * @since 1.2
 127+ *
 128+ * @param ResultWrapper $campaigns
 129+ */
132130 protected function displayCampaignTable( ResultWrapper $campaigns ) {
133 - global $wgOut;
 131+ $out = $this->getOutput();
134132
135 - $wgOut->addHTML( Html::element( 'h2', array(), wfMsg( 'mwe-upwiz-campaigns-existing' ) ) );
 133+ $out->addHTML( Html::element( 'h2', array(), wfMsg( 'mwe-upwiz-campaigns-existing' ) ) );
136134
137 - $wgOut->addHTML( '<table>' );
 135+ $out->addHTML( Xml::openElement(
 136+ 'table',
 137+ array( 'class' => 'wikitable', 'style' => 'width:400px' )
 138+ ) );
138139
139 - $wgOut->addHTML(
 140+ $out->addHTML(
140141 '<tr>' .
141142 Html::element( 'th', array(), wfMsg( 'mwe-upwiz-campaigns-name' ) ) .
142143 Html::element( 'th', array(), wfMsg( 'mwe-upwiz-campaigns-status' ) ) .
143144 Html::element( 'th', array(), wfMsg( 'mwe-upwiz-campaigns-edit' ) ) .
 145+ Html::element( 'th', array(), wfMsg( 'mwe-upwiz-campaigns-delete' ) ) .
144146 '</tr>'
145147 );
146148
147149 foreach ( $campaigns as $campaign ) {
148 - $wgOut->addHTML(
 150+ $out->addHTML(
149151 '<tr>' .
150152 Html::element( 'td', array(), $campaign->campaign_name ) .
151153 Html::element( 'td', array(), wfMsg( 'mwe-upwiz-campaigns-' . ( $campaign->campaign_enabled ? 'enabled' : 'disabled' ) ) ) .
152 - Html::element( 'td', array(), wfMsg( 'mwe-upwiz-campaigns-edit' ) ) .
 154+ '<td>' .
 155+ Html::element(
 156+ 'a',
 157+ array(
 158+ 'href' => SpecialPage::getTitleFor( 'UploadCampaign', $campaign->campaign_name )->getLocalURL()
 159+ ),
 160+ wfMsg( 'mwe-upwiz-campaigns-edit' )
 161+ ) .
 162+ '</td>' .
 163+ '<td>' .
 164+ Html::element(
 165+ 'a',
 166+ array(
 167+ 'href' => SpecialPage::getTitleFor( 'UploadCampaigns', 'del/' . $campaign->campaign_name )->getLocalURL(),
 168+ 'onclick' => 'return confirm( "' . wfMsg( 'mwe-upwiz-campaigns-confdel' ) . '" )'
 169+ ),
 170+ wfMsg( 'mwe-upwiz-campaigns-delete' )
 171+ ) .
 172+ '</td>' .
153173 '</tr>'
154174 );
155175 }
156176
157 - $wgOut->addHTML( '</table>' );
 177+ $out->addHTML( '</table>' );
158178 }
159 -
160 - /**
161 - * Displays a form to modify a single campaign.
162 - *
163 - * @since 1.2
164 - *
165 - * @param UWCampaign $campaign
166 - * @param boolean $isInsert
167 - */
168 - protected function displayCampaign( UWCampaign $campaign, $isInsert = false ) {
169 - global $wgOut;
170 -
171 - // TODO
172 - }
173179
174180 }
175 -
176 -class UWCampaign {
177 -
178 - /**
179 - * If the ID of the campaign.
180 - * Either this matched a record in the uw_campaigns table or is null.
181 - *
182 - * @since 1.2
183 - * @var integer or null
184 - */
185 - protected $id;
186 -
187 - /**
188 - * If the name of the campaign.
189 - * This name is the string used to invoke the campaign via campaign=name.
190 - *
191 - * @since 1.2
192 - * @var string
193 - */
194 - protected $name;
195 -
196 - /**
197 - * If the campaign is enabled or not.
198 - *
199 - * @since 1.2
200 - * @var boolean
201 - */
202 - protected $isEnabled;
203 -
204 - /**
205 - * The campaign configuration.
206 - *
207 - * @since 1.2
208 - * @var array
209 - */
210 - protected $config;
211 -
212 - /**
213 - * Create a new instance of $campaignName.
214 - *
215 - * @since 1.2
216 - *
217 - * @param integer $id
218 - * @param string $name
219 - * @param boolean $isEnabled
220 - * @param array $config
221 - */
222 - public function __construct( $id, $name, $isEnabled, array $config ) {
223 - $this->id = $id;
224 - $this->name = $name;
225 - $this->isEnabled = $isEnabled;
226 - $this->config = $config;
227 - }
228 -
229 - /**
230 - * Returns the UWCampaign with specified name, or false if there is no such campaign.
231 - *
232 - * @since 1.2
233 - *
234 - * @param string $campaignName
235 - *
236 - * @return UWCampaign or false
237 - */
238 - public static function newFromName( $campaignName ) {
239 - return self::newFromDB( array( 'campaign_name' => $campaignName ) );
240 - }
241 -
242 - /**
243 - * Returns the UWCampaign with specified ID, or false if there is no such campaign.
244 - *
245 - * @since 1.2
246 - *
247 - * @param integer $campaignId
248 - *
249 - * @return UWCampaign or false
250 - */
251 - public static function newFromId( $campaignId ) {
252 - return self::newFromDB( array( 'campaign_id' => $campaignId ) );
253 - }
254 -
255 - /**
256 - * Returns a new instance of UWCampaign build from a database result
257 - * obtained by doing a select with the porvided conditions on the uw_campaigns table.
258 - * If no campaign matches the conditions, false will be returned.
259 - *
260 - * @since 1.2
261 - *
262 - * @param array $conditions
263 - *
264 - * @return UWCampaign or false
265 - */
266 - protected static function newFromDB( array $conditions ) {
267 - $dbr = wfGetDB( DB_SLAVE );
268 -
269 - $campaign = $dbr->selectRow(
270 - 'uw_campaigns',
271 - array(
272 - 'campaign_id',
273 - 'campaign_name',
274 - 'campaign_enabled',
275 - ),
276 - $conditions
277 - );
278 -
279 - if ( !$campaign ) {
280 - return false;
281 - }
282 -
283 - $confProps = $dbr->select(
284 - 'uw_campaign_conf',
285 - array( 'cc_property', 'cc_value' ),
286 - array( 'cc_campaign_id' => $campaign->campaign_id )
287 - );
288 -
289 - $config = array();
290 -
291 - foreach ( $confProps as $confProp ) {
292 - $config[$confProp->cc_property] = $confProp->cc_value;
293 - }
294 -
295 - return new self(
296 - $campaign->campaign_id,
297 - $campaign->campaign_name,
298 - $campaign->campaign_enabled,
299 - $config
300 - );
301 - }
302 -
303 - /**
304 - * Returns the id of the campaign.
305 - *
306 - * @since 1.2
307 - *
308 - * @return intgere
309 - */
310 - public function getId() {
311 - return $this->id;
312 - }
313 -
314 - /**
315 - * Returns the name of the campaign.
316 - *
317 - * @since 1.2
318 - *
319 - * @return string
320 - */
321 - public function getName() {
322 - return $this->name;
323 - }
324 -
325 - /**
326 - * Returns if the campaign is enabled.
327 - *
328 - * @since 1.2
329 - *
330 - * @return boolean
331 - */
332 - public function getIsEnabled() {
333 - return $this->isEnabled;
334 - }
335 -
336 - /**
337 - * Sets all config properties.
338 - *
339 - * @since 1.2
340 - *
341 - * @param array $config
342 - */
343 - public function setConfig( array $config ) {
344 - $this->config = $config;
345 - }
346 -
347 - /**
348 - * Returns all config properties.
349 - *
350 - * @since 1.2
351 - *
352 - * @return array
353 - */
354 - public function getConfig() {
355 - return $this->config;
356 - }
357 -
358 - /**
359 - * Returns the value of the specified config property.
360 - *
361 - * @since 1.2
362 - *
363 - * @param string $property
364 - *
365 - * @return mixed
366 - */
367 - public function getProperty( $property ) {
368 - global $wgUploadWizardConfig;
369 -
370 - if ( array_key_exists( $property, $this->config ) ) {
371 - return $this->config[$property];
372 - }
373 - elseif ( array_key_exists( $property, $wgUploadWizardConfig['campaignDefaults'] ) ) {
374 - return $wgUploadWizardConfig['campaignDefaults'][$property];
375 - }
376 - else {
377 - return null;
378 - }
379 - }
380 -
381 - /**
382 - * Set the value of a config property.
383 - *
384 - * @since 1.2
385 - *
386 - * @param string $property
387 - * @param mixed $value
388 - */
389 - public function setProperty( $property, $value ) {
390 - $this->config[$property] = $value;
391 - }
392 -
393 - /**
394 - * Write the campaign to the DB.
395 - * If it's already there, it'll be updated, else it'll be inserted.
396 - *
397 - * @since 1.2
398 - *
399 - * @return boolean Success indicator
400 - */
401 - public function writeToDB() {
402 - if ( is_null( $this->id ) ) {
403 - return $this->insertIntoDB();
404 - }
405 - else {
406 - return $this->updateInDB();
407 - }
408 - }
409 -
410 - /**
411 - * Insert the campaign into the DB.
412 - *
413 - * @since 1.2
414 - *
415 - * @return boolean Success indicator
416 - */
417 - protected function insertIntoDB() {
418 - $dbw = wfGetDB( DB_MASTER );
419 -
420 - $success = $dbw->insert(
421 - 'uw_campaigns',
422 - array(
423 - 'campaign_name' => $this->name,
424 - 'campaign_enabled' => $this->isEnabled,
425 - ),
426 - array( 'campaign_id' => $this->id )
427 - );
428 -
429 - if ( $success ) {
430 - $this->id = $dbr->insertId();
431 - $success &= $this->writePropsToDB( $dbw );
432 - }
433 -
434 - return $success;
435 - }
436 -
437 - /**
438 - * Update the campaign in the DB.
439 - *
440 - * @since 1.2
441 - *
442 - * @return boolean Success indicator
443 - */
444 - protected function updateInDB() {
445 - $dbw = wfGetDB( DB_MASTER );
446 -
447 - $success = $dbw->update(
448 - 'uw_campaigns',
449 - array(
450 - 'campaign_name' => $this->name,
451 - 'campaign_enabled' => $this->isEnabled,
452 - ),
453 - array( 'campaign_id' => $this->id )
454 - );
455 -
456 - // Delete and insert instead of update.
457 - // This will not result into dead-data when config vars are removed.
458 - $success &= $dbw->delete(
459 - 'uw_campaign_conf',
460 - array( 'cc_campaign_id' => $this->id )
461 - );
462 -
463 - $success &= $this->writePropsToDB( $dbw );
464 -
465 - return $success;
466 - }
467 -
468 - /**
469 - * Write (insert) the properties into the DB.
470 - *
471 - * @since 1.2
472 - *
473 - * @param Database $dbw
474 - *
475 - * @return boolean Success indicator
476 - */
477 - protected function writePropsToDB( Database $dbw ) {
478 - $success = true;
479 -
480 - foreach ( $this->config as $prop => $value ) {
481 - $success &= $dbw->insert(
482 - 'uw_campaign_conf',
483 - array(
484 - 'cc_campaign_id' => $this->id,
485 - 'cc_property' => $prop,
486 - 'cc_value' => $value
487 - )
488 - );
489 - }
490 -
491 - return $success;
492 - }
493 -
494 - /**
495 - * Delete the campaign from the DB (when present).
496 - *
497 - * @since 1.2
498 - *
499 - * @return boolean Success indicator
500 - */
501 - public function deleteFromDB() {
502 - if ( is_null( $this->id ) ) {
503 - return true;
504 - }
505 -
506 - $dbw = wfGetDB( DB_MASTER );
507 -
508 - $d1 = $dbw->delete(
509 - 'uw_campaigns',
510 - array( 'campaign_id' => $this->id )
511 - );
512 -
513 - $d2 = $dbw->delete(
514 - 'uw_campaign_conf',
515 - array( 'cc_campaign_id' => $this->id )
516 - );
517 -
518 - return $d1 && $d2;
519 - }
520 -
521 -}
Index: trunk/extensions/UploadWizard/UploadWizard.i18n.php
@@ -15,6 +15,8 @@
1616 'uploadwizard' => 'Upload wizard',
1717 'uploadwizard-desc' => 'Upload wizard, developed for the Multimedia Usability grant',
1818 'uploadcampaigns' => 'Upload campaigns',
 19+ 'uploadcampaign' => 'Upload campaign configuration',
 20+ 'right-upwizcampaigns' => 'Configure Upload Wizard campaigns',
1921 'mwe-upwiz-js-off' => 'UploadWizard uses JavaScript for an improved interface. Your browser either does not support JavaScript or has JavaScript turned off, so we are showing you a simple upload form.',
2022 'mwe-upwiz-extension-disabled' => 'This page has been disabled due to temporary technical problems. In the meantime try the standard upload form.',
2123 'mwe-upwiz-code-unknown' => 'Unknown language',
@@ -284,6 +286,16 @@
285287 'mwe-upwiz-campaigns-newname' => 'Campaign name: ',
286288 'mwe-upwiz-campaigns-namedoc' => 'The name of the campaign is the identifier used in URLs. ie "name" in ?campaign=name',
287289 'mwe-upwiz-campaigns-existing' => 'Existing campaigns',
 290+ 'mwe-upwiz-campaigns-editing' => 'Upload campaign configuration',
 291+ 'mwe-upwiz-campaigns-delete' => 'Delete',
 292+ 'mwe-upwiz-campaigns-confdel' => 'Are you sure you want to delete this campaign?',
 293+
 294+ // Special:UploadCampaign
 295+ 'uploadcampaign-legend' => 'Upload campaign configuration',
 296+ 'uploadcampaign-text' => 'You are modifying an Upload Wizard campaign.',
 297+ 'mwe-upwiz-campaign-name' => 'Campaign name',
 298+ 'mwe-upwiz-campaign-enabled' => 'Campaign enabled',
 299+ 'mwe-upwiz-campaign-conf-skiptutorial' => 'Skipp the licencing tutorial',
288300 );
289301
290302 /** Message documentation (Message documentation)
Index: trunk/extensions/UploadWizard/SpecialUploadCampaign.php
@@ -0,0 +1,95 @@
 2+<?php
 3+
 4+/**
 5+ * Special:UploadCampaign
 6+ *
 7+ * Configuration interface for an upload wizard campaign.
 8+ *
 9+ * @file
 10+ * @ingroup SpecialPage
 11+ * @ingroup Upload
 12+ *
 13+ * @since 1.2
 14+ *
 15+ * @licence GNU GPL v3+
 16+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 17+ */
 18+class SpecialUploadCampaign extends FormSpecialPage {
 19+
 20+ protected $subPage;
 21+
 22+ /**
 23+ * Constructor.
 24+ *
 25+ * @param $request is the request (usually wgRequest)
 26+ * @param $par is everything in the URL after Special:UploadCampaign. Not sure what we can use it for
 27+ */
 28+ public function __construct($request = null, $par = null) {
 29+ parent::__construct ( 'UploadCampaign', 'upwizcampaigns', false );
 30+ }
 31+
 32+ protected function setParameter( $par ){
 33+ $this->subPage = $par;
 34+ }
 35+
 36+ /**
 37+ * (non-PHPdoc)
 38+ * @see FormSpecialPage::getFormFields()
 39+ */
 40+ protected function getFormFields() {
 41+ $dbr = wfGetDB( DB_SLAVE );
 42+
 43+ $campaign = UWCampaign::newFromName( $this->subPage );
 44+
 45+ $id = $campaign ? $campaign->getId() : null;
 46+ $enabled = $campaign ? $campaign->getIsEnabled() : false;
 47+ $configFields = $campaign ? $campaign->getAllConfig() : UWCampaign::getDefaultConfig();
 48+
 49+ $fields = array ();
 50+
 51+ $fields['Campaignid'] = array ( 'type' => 'hidden', 'default' => $id );
 52+ $fields['Campaignname'] = array ( 'type' => 'text', 'default' => $this->subPage, 'label-message' => 'mwe-upwiz-campaign-name' );
 53+ $fields['Campaignenabled'] = array ( 'type' => 'check', 'default' => $enabled, 'label-message' => 'mwe-upwiz-campaign-enabled' );
 54+
 55+ foreach ( $configFields as $name => $data ) {
 56+ $data['label-message'] = 'mwe-upwiz-campaign-conf-' . $name;
 57+ $fields[$name] = $data;
 58+ }
 59+
 60+ return $fields;
 61+ }
 62+
 63+ /**
 64+ * Process the form. At this point we know that the user passes all the criteria in
 65+ * userCanExecute(), and if the data array contains 'Username', etc, then Username
 66+ * resets are allowed.
 67+ * @param $data array
 68+ * @return Bool|Array
 69+ */
 70+ public function onSubmit( array $data ) {
 71+ $id = $data['Campaignid'] == '' ? null : $data['Campaignid'];
 72+ unset( $data['Campaignid'] );
 73+
 74+ $name = $data['Campaignname'];
 75+ unset( $data['Campaignname'] );
 76+
 77+ $enabled = $data['Campaignenabled'];
 78+ unset( $data['Campaignenabled'] );
 79+
 80+ $campaign = new UWCampaign( $id, $name, $enabled, $data );
 81+
 82+ $success = $campaign->writeToDB();
 83+
 84+ if ( $success ) {
 85+ return true;
 86+ }
 87+ else {
 88+ return array(); // TODO
 89+ }
 90+ }
 91+
 92+ public function onSuccess() {
 93+ $this->getOutput()->redirect( SpecialPage::getTitleFor( 'UploadCampaigns' )->getLocalURL() );
 94+ }
 95+
 96+}
\ No newline at end of file
Property changes on: trunk/extensions/UploadWizard/SpecialUploadCampaign.php
___________________________________________________________________
Added: svn:eol-style
197 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r92603fu r92555jeroendedauw23:38, 19 July 2011
r92604fu r92555jeroendedauw23:50, 19 July 2011

Comments

#Comment by Siebrand (talk | contribs)   20:05, 19 July 2011

Typo: skipp -> skip

#Comment by Jeroen De Dauw (talk | contribs)   12:56, 20 July 2011

Question to reviewers: using $this->getOutput() instead of $wgOut is the correct way to do this now, right?

Status & tagging log