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

Change subject: Add API to query campaigns
......................................................................


Add API to query campaigns

Change-Id: I308df2ca47ff4f98c88ccab5af8aee2584cb5a29
---
M UploadWizard.php
A includes/ApiQueryAllCampaigns.php
2 files changed, 129 insertions(+), 0 deletions(-)

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



diff --git a/UploadWizard.php b/UploadWizard.php
index e2d182a..610f14f 100644
--- a/UploadWizard.php
+++ b/UploadWizard.php
@@ -65,8 +65,12 @@
 
        // Special Pages
        'SpecialUploadWizard' => $wgUpwizDir . 
'/includes/specials/SpecialUploadWizard.php',
+
+       // API
+       'ApiQueryAllCampaigns' => $wgUpwizDir . 
'/includes/ApiQueryAllCampaigns.php'
 );
 
+$wgAPIListModules['allcampaigns'] = 'ApiQueryAllCampaigns';
 // $wgAPIModules['titlecheck'] = 'ApiTitleCheck';
 // $wgAPIListModules['titlecheck'] = 'ApiTitleCheck';
 
diff --git a/includes/ApiQueryAllCampaigns.php 
b/includes/ApiQueryAllCampaigns.php
new file mode 100644
index 0000000..35a8027
--- /dev/null
+++ b/includes/ApiQueryAllCampaigns.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ *
+ *
+ * Copyright © 2013 Yuvi Panda <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Query module to enumerate all registered campaigns
+ *
+ * @ingroup API
+ */
+class ApiQueryAllCampaigns extends ApiQueryBase {
+       public function __construct( $query, $moduleName ) {
+               parent::__construct( $query, $moduleName, 'uc' );
+       }
+
+       public function execute() {
+               $params = $this->extractRequestParams();
+
+               $limit = $params['limit'];
+
+               $this->addTables( 'uw_campaigns' );
+
+               $this->addWhereIf( array( 'campaign_enabled' => 1 ), 
$params['enabledonly'] );
+               $this->addOption( 'LIMIT', $limit + 1 );
+               $this->addOption( 'ORDER BY', 'campaign_id' ); // Not sure if 
required?
+
+               $this->addFields( array(
+                       'campaign_id',
+                       'campaign_name',
+                       'campaign_enabled'
+               ) );
+
+               if ( !is_null( $params['continue'] ) ) {
+                       $from_id = (int)$params['continue'];
+                       $this->addWhere( "campaign_id >= $from_id" ); // Not 
SQL Injection, since we already force this to be an integer
+               }
+
+               $res = $this->select( __METHOD__ );
+
+               $result = $this->getResult();
+
+               $count = 0;
+
+               foreach ( $res as $row ) {
+                       if ( ++$count > $limit ) {
+                               // We have more results than $limit. Set 
continue
+                               $this->setContinueEnumParameter( 'continue', 
$row->campaign_id );
+                               break;
+                       }
+                       $campaign = UploadWizardCampaign::newFromName( 
$row->campaign_name );
+                       $result->addValue(
+                               array( 'query', $this->getModuleName(), 
$row->campaign_id ),
+                               '*',
+                               json_encode( $campaign->getConfig() )
+                       );
+                       $result->addValue(
+                               array( 'query', $this->getModuleName(), 
$row->campaign_id ),
+                               'name',
+                               $campaign->getName()
+                       );
+               }
+               $result->setIndexedTagName_internal( array( 'query', 
$this->getModuleName() ), 'campaign' );
+       }
+
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'enabledonly' => false,
+                       'limit' => array(
+                               ApiBase::PARAM_DFLT => 50,
+                               ApiBase::PARAM_TYPE => 'limit',
+                               ApiBase::PARAM_MIN => 1,
+                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
+                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
+                       ),
+                       'continue' => null
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'enabledonly' => 'Only list campaigns that are enabled',
+                       'limit' => 'Number of campaigns to return',
+                       'continue' => 'When more results are available, use 
this paramter to continue'
+               );
+       }
+
+       public function getDescription() {
+               return 'Enumerate all Campaigns';
+       }
+
+
+       public function getExamples() {
+               return array(
+                       'api.php?action=query&list=allcampaigns&ucenabledonly=' 
=> 'Enumerate enabled campaigns'
+               );
+       }
+
+       public function getHelpUrls() {
+               return 
'https://www.mediawiki.org/wiki/Extension:UploadWizard/API';
+       }
+}
+

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I308df2ca47ff4f98c88ccab5af8aee2584cb5a29
Gerrit-PatchSet: 16
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>
Gerrit-Reviewer: Brian Wolff <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: MarkTraceur <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to