jenkins-bot has submitted this change and it was merged.

Change subject: Query API modules for campaigns and participants
......................................................................


Query API modules for campaigns and participants

Change-Id: Ie969bbc4be8ccd9d188fdf03f599806f6fa8a612
---
M Campaigns.php
A includes/api/ApiAllCampaigns.php
A includes/api/ApiCampaignParticipants.php
3 files changed, 273 insertions(+), 0 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Campaigns.php b/Campaigns.php
index 33ccd1a..aff8145 100644
--- a/Campaigns.php
+++ b/Campaigns.php
@@ -70,6 +70,14 @@
 $wgAutoloadClasses['Campaigns\Persistence\Internal\Db\DBPersistenceManager']  
= $dir . '/includes/persistence/internal/db/DBPersistenceManager.php';
 $wgAutoloadClasses['Campaigns\Persistence\Internal\Db\DBMapper']  = $dir . 
'/includes/persistence/internal/db/DBMapper.php';
 $wgAutoloadClasses['Campaigns\Persistence\Internal\Db\IDBMapper']  = $dir . 
'/includes/persistence/internal/db/IDBMapper.php';
+$wgAutoloadClasses['Campaigns\Api\ApiAllCampaigns'] = $dir . 
'/includes/api/ApiAllCampaigns.php';
+$wgAutoloadClasses['Campaigns\Api\ApiCampaignParticipants'] = $dir . 
'/includes/api/ApiCampaignParticipants.php';
+
+
+// API modules
+
+$wgAPIListModules['allcampaigns'] = 'Campaigns\Api\ApiAllCampaigns';
+$wgAPIListModules['campaignparticipants'] = 
'Campaigns\Api\ApiCampaignParticipants';
 
 $wgAutoloadClasses['Campaigns\Services\ICampaignFromUrlKeyProvider']  = $dir . 
'/includes/services/ICampaignFromUrlKeyProvider.php';
 $wgAutoloadClasses['Campaigns\Services\CampaignFromUrlKeyProvider']  = $dir . 
'/includes/services/CampaignFromUrlKeyProvider.php';
diff --git a/includes/api/ApiAllCampaigns.php b/includes/api/ApiAllCampaigns.php
new file mode 100644
index 0000000..730ce19
--- /dev/null
+++ b/includes/api/ApiAllCampaigns.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Campaigns\Api;
+
+use ApiQueryBase;
+use ApiBase;
+use Campaigns\Setup\Setup;
+use Campaigns\Persistence\CampaignFields;
+
+/**
+ * Query module to enumerate all campagins.
+ *
+ * @ingroup API
+ */
+class ApiAllCampaigns extends ApiQueryBase {
+
+       public function __construct( $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'allc' );
+       }
+
+       public function execute() {
+
+               $params = $this->extractRequestParams();
+
+               $cRepo = Setup::getInstance()->get(
+                       'Campaigns\Domain\ICampaignRepository' );
+
+               // prefix
+               $prefix = $params['prefix'];
+               $prefix = $prefix ? $prefix : null;
+
+               // limit
+               // superclass will ensure there's at least something in this 
parameter
+               $limit = $params['limit'];
+               $repoLimit = $cRepo->getMaxFetchLimit();
+               $limit = $limit > $repoLimit ? $repoLimit : $limit;
+
+               // continue
+               if ( !is_null( $params['continue'] ) ) {
+                       $cont = explode( '|', $params['continue'] );
+                       $this->dieContinueUsageIf( count( $cont ) != 1 );
+                       $continue = $cont[0];
+               } else {
+                       $continue = null;
+               }
+
+               // fetch the campaigns
+               // this will modify $continue as needed
+               $campaigns = $cRepo->getCampaigns( $prefix, $limit, $continue );
+
+               // if there are more campaigns, set continue result property
+               if ( !is_null( $continue ) ) {
+                       $this->setContinueEnumParameter( 'continue', $continue 
);
+               }
+
+               // fill up the results
+               $r = $this->getResult();
+               $pathForCampaigns = array( 'query', $this->getModuleName() );
+               foreach ( $campaigns as $c ) {
+
+                       $r->addValue(
+                               $pathForCampaigns,
+                               null,
+                               array(
+                                       'id' => $c->getId(),
+                                       'name' => $c->getName(),
+                                       'timecreated' => $c->getTimeCreated()
+                               )
+                       );
+               }
+
+               // set the tag name of the elements in the list
+               $r->setIndexedTagName_internal( $pathForCampaigns, 'c' );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'prefix' => array(
+                               ApiBase::PARAM_TYPE => 'string'
+                       ),
+                       'limit' => array(
+                               ApiBase::PARAM_TYPE => 'limit', // see ApiBase
+                               ApiBase::PARAM_DFLT => 10,
+                               ApiBase::PARAM_MIN => 1,
+                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, // 
max for users
+                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 // 
max for bots
+                       ),
+                       'continue' => null
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'prefix' => 'Return only campaigns whose name begins 
with this value',
+                       'limit' => 'How many campaigns to return'
+               );
+       }
+
+       public function getResultProperties() {
+               return array(
+                       '' => array(
+                               'id' => 'integer',
+                               'name' => 'string',
+                               'timecreated' => 'timestamp'
+                       )
+               );
+       }
+
+       public function getDescription() {
+               return 'Enumerate all campaigns.';
+       }
+
+       public function getExamples() {
+               return 'api.php?action=query&list=allcampaigns&allclimit=10';
+       }
+}
\ No newline at end of file
diff --git a/includes/api/ApiCampaignParticipants.php 
b/includes/api/ApiCampaignParticipants.php
new file mode 100644
index 0000000..0b38691
--- /dev/null
+++ b/includes/api/ApiCampaignParticipants.php
@@ -0,0 +1,149 @@
+<?php
+
+namespace Campaigns\Api;
+
+use ApiQueryBase;
+use ApiBase;
+use Campaigns\Setup\Setup;
+use Campaigns\Persistence\ParticipationFields;
+
+/**
+ * Query module to fetch participants in a campaign.
+ *
+ * @ingroup API
+ */
+class ApiCampaignParticipants extends ApiQueryBase {
+
+       public function __construct( $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'campp' );
+       }
+
+       public function execute() {
+
+               $params = $this->extractRequestParams();
+
+               $setup = Setup::getInstance();
+               $cRepo = $setup->get( 'Campaigns\Domain\ICampaignRepository' );
+               $pRepo = $setup->get( 
'Campaigns\Domain\IParticipationRepository' );
+
+               // fetch the campaign
+               if ( $params['id'] ) {
+                       $campaign = $cRepo->getCampaignById( $params['id'] );
+               } elseif( $params['name'] ) {
+                       $campaign = $cRepo->getCampaignByName( $params['name'] 
);
+               } else {
+                       $this->dieUsage( 'Either the id or the name parameter 
must be set.',
+                               'no_id_or_name' );
+               }
+
+               // we get null if there's no such campaign
+               if ( is_null( $campaign ) ) {
+                       $this->dieUsage( 'No such campaign found.', 
'no_such_campaign' );
+               }
+
+               // limit
+               $limit = $params['limit'];
+               $climit = $pRepo->getMaxFetchLimit();
+               $limit = $limit > $climit ? $climit : $limit;
+
+               // continue
+               if ( !is_null( $params['continue'] ) ) {
+                       $cont = explode( '|', $params['continue'] );
+                       $this->dieContinueUsageIf( count( $cont ) != 1 );
+                       $continue = $cont[0];
+               } else {
+                       $continue = null;
+               }
+
+               // fetch the participations
+               // this will modify $continue as needed
+               // for now just include organizers
+               $participations = $pRepo->getParticipations( $campaign,
+                       true, $limit, $continue );
+
+               // if there are more campaigns, set continue result property
+               if ( !is_null( $continue ) ) {
+                       $this->setContinueEnumParameter( 'continue', $continue 
);
+               }
+
+               // fill up the results
+               $r = $this->getResult();
+               $pathForParticipants = array( 'query', $this->getModuleName() );
+               foreach ( $participations as $p ) {
+
+                       $r->addValue(
+                               $pathForParticipants,
+                               null,
+                               array(
+                                       'id' => $p->getUserId(),
+                               )
+                       );
+               }
+
+               // set the tag name of the elements in the list
+               $r->setIndexedTagName_internal( $pathForParticipants, 'p' );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'id' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_REQUIRED => false
+                       ),
+                       'name' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => false
+                       ),
+                       'limit' => array(
+                               ApiBase::PARAM_TYPE => 'limit', // see ApiBase
+                               ApiBase::PARAM_DFLT => 10,
+                               ApiBase::PARAM_MIN => 1,
+                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, // 
max for users
+                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 // 
max for bots
+                       ),
+                       'continue' => null
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'id' => 'The ID of the campaign to get a list of 
participants for;' .
+                               'either this parameter or camppname must be 
provided.',
+                       'name' => 'The name of the campaign to get a list of 
participants' .
+                               'for; either this parameter or camppid must be 
provided.',
+                       'limit' => 'How many participants to return'
+               );
+       }
+
+       public function getResultProperties() {
+               return array(
+                       '' => array(
+                               'id' => 'integer'
+                       )
+               );
+       }
+
+       public function getDescription() {
+               return 'Enumerate the current participants in a campaign.';
+       }
+
+       public function getPossibleErrors() {
+               return array_merge(
+                       parent::getPossibleErrors(),
+                       array(
+                               array(
+                                       'code' => 'no_such_campaign',
+                                       'info' => 'No such campaign found.'
+                               ),
+                               array(
+                                       'code' => 'no_id_or_name',
+                                       'info' => 'Either the id or the name 
parameter must be set.'
+                               )
+                       )
+               );
+       }
+
+       public function getExamples() {
+               return 
'api.php?action=query&list=campaignparticipants&camppid=1&campplimit=100';
+       }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/117380
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie969bbc4be8ccd9d188fdf03f599806f6fa8a612
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/Campaigns
Gerrit-Branch: wip/editorcampaigns
Gerrit-Owner: AndyRussG <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ragesoss <[email protected]>
Gerrit-Reviewer: Swalling <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to