Index: trunk/extensions/Contest/Contest.php |
— | — | @@ -50,6 +50,7 @@ |
51 | 51 | $wgAutoloadClasses['ContestSettings'] = dirname( __FILE__ ) . '/Contest.settings.php'; |
52 | 52 | |
53 | 53 | $wgAutoloadClasses['ApiDeleteContest'] = dirname( __FILE__ ) . '/api/ApiDeleteContest.php'; |
| 54 | +$wgAutoloadClasses['ApiQueryContests'] = dirname( __FILE__ ) . '/api/ApiQueryContests.php'; |
54 | 55 | |
55 | 56 | $wgAutoloadClasses['Contest'] = dirname( __FILE__ ) . '/includes/Contest.class.php'; |
56 | 57 | $wgAutoloadClasses['ContestantPager'] = dirname( __FILE__ ) . '/includes/ContestantPager.php'; |
— | — | @@ -87,6 +88,7 @@ |
88 | 89 | |
89 | 90 | // API |
90 | 91 | $wgAPIModules['deletecontest'] = 'ApiDeleteContest'; |
| 92 | +$wgAPIListModules['contests'] = 'ApiQueryContests'; |
91 | 93 | |
92 | 94 | // Hooks |
93 | 95 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'ContestHooks::onSchemaUpdate'; |
Index: trunk/extensions/Contest/includes/ContestDBObject.php |
— | — | @@ -666,4 +666,65 @@ |
667 | 667 | return array_keys( $this->getFieldTypes() ); |
668 | 668 | } |
669 | 669 | |
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 |
1 | 154 | + native |
Added: svn:keywords |
2 | 155 | + Id |