r98967 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98966‎ | r98967 | r98968 >
Date:01:28, 5 October 2011
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
added API module to query contests
Modified paths:
  • /trunk/extensions/Contest/Contest.php (modified) (history)
  • /trunk/extensions/Contest/api/ApiQueryContests.php (added) (history)
  • /trunk/extensions/Contest/includes/ContestDBObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Contest/Contest.php
@@ -50,6 +50,7 @@
5151 $wgAutoloadClasses['ContestSettings'] = dirname( __FILE__ ) . '/Contest.settings.php';
5252
5353 $wgAutoloadClasses['ApiDeleteContest'] = dirname( __FILE__ ) . '/api/ApiDeleteContest.php';
 54+$wgAutoloadClasses['ApiQueryContests'] = dirname( __FILE__ ) . '/api/ApiQueryContests.php';
5455
5556 $wgAutoloadClasses['Contest'] = dirname( __FILE__ ) . '/includes/Contest.class.php';
5657 $wgAutoloadClasses['ContestantPager'] = dirname( __FILE__ ) . '/includes/ContestantPager.php';
@@ -87,6 +88,7 @@
8889
8990 // API
9091 $wgAPIModules['deletecontest'] = 'ApiDeleteContest';
 92+$wgAPIListModules['contests'] = 'ApiQueryContests';
9193
9294 // Hooks
9395 $wgHooks['LoadExtensionSchemaUpdates'][] = 'ContestHooks::onSchemaUpdate';
Index: trunk/extensions/Contest/includes/ContestDBObject.php
@@ -666,4 +666,65 @@
667667 return array_keys( $this->getFieldTypes() );
668668 }
669669
670 -}
\ No newline at end of file
 670+ /**
 671+ * Returns an array with the fields and their descriptions.
 672+ *
 673+ * field name => field description
 674+ *
 675+ * @since 0.1
 676+ *
 677+ * @return array
 678+ */
 679+ public function getFieldDescriptions() {
 680+ return array();
 681+ }
 682+
 683+ /**
 684+ * Get API parameters for the fields supported by this object.
 685+ *
 686+ * @since 0.1
 687+ *
 688+ * @param boolean $requireParams
 689+ * @param boolean $setDefaults
 690+ *
 691+ * @return array
 692+ */
 693+ public function getAPIParams( $requireParams = false, $setDefaults = false ) {
 694+ $typeMap = array(
 695+ 'id' => 'integer',
 696+ 'int' => 'integer',
 697+ 'float' => 'NULL',
 698+ 'str' => 'string',
 699+ 'bool' => 'integer',
 700+ 'array' => 'string'
 701+ );
 702+
 703+ $params = array();
 704+ $defaults = $this->getDefaults();
 705+
 706+ foreach ( $this->getFieldTypes() as $field => $type ) {
 707+ if ( $field == 'id' ) {
 708+ continue;
 709+ }
 710+
 711+ $hasDefault = array_key_exists( $field, $defaults );
 712+
 713+ $params[$field] = array(
 714+ ApiBase::PARAM_TYPE => $typeMap[$type],
 715+ ApiBase::PARAM_REQUIRED => $requireParams && !$hasDefault
 716+ );
 717+
 718+ if ( $type == 'array' ) {
 719+ $params[$field][ApiBase::PARAM_ISMULTI] = true;
 720+ }
 721+
 722+ if ( $setDefaults && $hasDefault ) {
 723+ $default = is_array( $defaults[$field] ) ? implode( '|', $defaults[$field] ) : $defaults[$field];
 724+ $params[$field][ApiBase::PARAM_DFLT] = $default;
 725+ }
 726+ }
 727+
 728+ return $params;
 729+ }
 730+
 731+}
Index: trunk/extensions/Contest/api/ApiQueryContests.php
@@ -0,0 +1,152 @@
 2+<?php
 3+
 4+/**
 5+ * API module to get a list of contests.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ApiQueryContests.php
 10+ * @ingroup Contest
 11+ * @ingroup API
 12+ *
 13+ * @licence GNU GPL v3+
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ */
 16+class ApiQueryContests extends ApiQueryBase {
 17+
 18+ public function __construct( $main, $action ) {
 19+ parent::__construct( $main, $action, 'co' );
 20+ }
 21+
 22+ /**
 23+ * Retrieve the special words from the database.
 24+ */
 25+ public function execute() {
 26+ global $wgUser;
 27+
 28+ if ( !$wgUser->isAllowed( 'contestadmin' ) || $wgUser->isBlocked() ) {
 29+ $this->dieUsageMsg( array( 'badaccess-groups' ) );
 30+ }
 31+
 32+ // Get the requests parameters.
 33+ $params = $this->extractRequestParams();
 34+
 35+ $starPropPosition = array_search( '*', $params['props'] );
 36+
 37+ if ( $starPropPosition !== false ) {
 38+ unset( $params['props'][$starPropPosition] );
 39+ $params['props'] = array_merge( $params['props'], Contest::s()->getFieldNames() );
 40+ }
 41+
 42+ $params = array_filter( $params, create_function( '$p', 'return isset( $p );' ) );
 43+
 44+ $conditions = array();
 45+
 46+ foreach ( $params as $name => $value ) {
 47+ if ( Contest::s()->canHasField( $name ) ) {
 48+ $conditions[$name] = $value;
 49+ }
 50+ }
 51+
 52+ $results = Contest::s()->select(
 53+ $params['props'],
 54+ $conditions,
 55+ array(
 56+ 'LIMIT' => $params['limit'] + 1,
 57+ 'ORDER BY' => Contest::s()->getPrefixedField( 'id' ) . ' ASC'
 58+ )
 59+ );
 60+
 61+ $serializedResults = array();
 62+ $count = 0;
 63+
 64+ foreach ( $results as $result ) {
 65+ if ( ++$count > $params['limit'] ) {
 66+ // We've reached the one extra which shows that
 67+ // there are additional pages to be had. Stop here...
 68+ $this->setContinueEnumParameter( 'continue', $result->getId() );
 69+ break;
 70+ }
 71+
 72+ $serializedResults[] = $result->toArray();
 73+ }
 74+
 75+ $this->getResult()->setIndexedTagName( $serializedResults, 'contest' );
 76+
 77+ $this->getResult()->addValue(
 78+ null,
 79+ 'contests',
 80+ $serializedResults
 81+ );
 82+ }
 83+
 84+ /**
 85+ * (non-PHPdoc)
 86+ * @see includes/api/ApiBase#getAllowedParams()
 87+ */
 88+ public function getAllowedParams() {
 89+ $params = array (
 90+ 'props' => array(
 91+ ApiBase::PARAM_TYPE => array_merge( Contest::s()->getFieldNames(), array( '*' ) ),
 92+ ApiBase::PARAM_ISMULTI => true,
 93+ ApiBase::PARAM_DFLT => '*'
 94+ ),
 95+ 'limit' => array(
 96+ ApiBase::PARAM_DFLT => 20,
 97+ ApiBase::PARAM_TYPE => 'limit',
 98+ ApiBase::PARAM_MIN => 1,
 99+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 100+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 101+ ),
 102+ 'continue' => null,
 103+ );
 104+
 105+ return array_merge( Contest::s()->getAPIParams(), $params );
 106+ }
 107+
 108+ /**
 109+ * (non-PHPdoc)
 110+ * @see includes/api/ApiBase#getParamDescription()
 111+ */
 112+ public function getParamDescription() {
 113+ $descs = array (
 114+ 'props' => 'Contest data to query',
 115+ 'continue' => 'Offset number from where to continue the query',
 116+ 'limit' => 'Max amount of words to return',
 117+ );
 118+
 119+ return array_merge( Contest::s()->getFieldDescriptions(), $descs );
 120+ }
 121+
 122+ /**
 123+ * (non-PHPdoc)
 124+ * @see includes/api/ApiBase#getDescription()
 125+ */
 126+ public function getDescription() {
 127+ return 'API module for obatining survey answers';
 128+ }
 129+
 130+ /**
 131+ * (non-PHPdoc)
 132+ * @see includes/api/ApiBase#getPossibleErrors()
 133+ */
 134+ public function getPossibleErrors() {
 135+ return array_merge( parent::getPossibleErrors(), array(
 136+ ) );
 137+ }
 138+
 139+ /**
 140+ * (non-PHPdoc)
 141+ * @see includes/api/ApiBase#getExamples()
 142+ */
 143+ protected function getExamples() {
 144+ return array (
 145+ 'api.php?action=query&list=contests&',
 146+ );
 147+ }
 148+
 149+ public function getVersion() {
 150+ return __CLASS__ . ': $Id$';
 151+ }
 152+
 153+}
Property changes on: trunk/extensions/Contest/api/ApiQueryContests.php
___________________________________________________________________
Added: svn:eol-style
1154 + native
Added: svn:keywords
2155 + Id

Follow-up revisions

RevisionCommit summaryAuthorDate
r98968follow up to r98967jeroendedauw01:31, 5 October 2011

Comments

#Comment by Johnduhart (talk | contribs)   10:33, 5 October 2011

Please make all API methods public, thank you.

#Comment by Jeroen De Dauw (talk | contribs)   12:54, 5 October 2011

Your comment made me wonder why it's protected here (I just copied it from somewhere), so I had a look at ApiBase. It's protected there as well, and so are getDescription and getParamDescription. I'm more inclined to make the later two protected so it matches the base class, then making getExamples public. Any reasons to not do this?

#Comment by Jeroen De Dauw (talk | contribs)   20:07, 9 October 2011
  • poke*
#Comment by Johnduhart (talk | contribs)   00:20, 10 October 2011

Roan says all API functions should be public, but informed me that even if they're not there's a loophole which allows paraminfo and the like to function. Why ApiBase has those functions protected is a mystery, but changing them would break modules who still have protected functions.

Status & tagging log