Index: trunk/extensions/Contest/Contest.php |
— | — | @@ -49,6 +49,7 @@ |
50 | 50 | $wgAutoloadClasses['ContestHooks'] = dirname( __FILE__ ) . '/Contest.hooks.php'; |
51 | 51 | $wgAutoloadClasses['ContestSettings'] = dirname( __FILE__ ) . '/Contest.settings.php'; |
52 | 52 | |
| 53 | +$wgAutoloadClasses['ApiContestQuery'] = dirname( __FILE__ ) . '/api/ApiContestQuery.php'; |
53 | 54 | $wgAutoloadClasses['ApiDeleteContest'] = dirname( __FILE__ ) . '/api/ApiDeleteContest.php'; |
54 | 55 | $wgAutoloadClasses['ApiQueryContests'] = dirname( __FILE__ ) . '/api/ApiQueryContests.php'; |
55 | 56 | |
Index: trunk/extensions/Contest/api/ApiContestQuery.php |
— | — | @@ -0,0 +1,142 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Base class for API query modules that return results using a |
| 6 | + * ContestDBClass deriving class. |
| 7 | + * |
| 8 | + * @since 0.1 |
| 9 | + * |
| 10 | + * @file ApiContestQuery.php |
| 11 | + * @ingroup Contest |
| 12 | + * @ingroup API |
| 13 | + * |
| 14 | + * @licence GNU GPL v3+ |
| 15 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 16 | + */ |
| 17 | +abstract class ApiContestQuery extends ApiQueryBase { |
| 18 | + |
| 19 | + protected abstract function getClassName(); |
| 20 | + |
| 21 | + protected function getClass() { |
| 22 | + $className = $this->getClassName(); |
| 23 | + return $className::s(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Retrieve the special words from the database. |
| 28 | + */ |
| 29 | + public function execute() { |
| 30 | + $params = $this->getParams(); |
| 31 | + $results = $this->getResults( $params, $this->getConditions( $params ) ); |
| 32 | + $this->addResults( $params, $results ); |
| 33 | + } |
| 34 | + |
| 35 | + protected function getParams() { |
| 36 | + // Get the requests parameters. |
| 37 | + $params = $this->extractRequestParams(); |
| 38 | + |
| 39 | + $starPropPosition = array_search( '*', $params['props'] ); |
| 40 | + |
| 41 | + if ( $starPropPosition !== false ) { |
| 42 | + unset( $params['props'][$starPropPosition] ); |
| 43 | + $params['props'] = array_merge( $params['props'], $this->getClass()->getFieldNames() ); |
| 44 | + } |
| 45 | + |
| 46 | + return array_filter( $params, create_function( '$p', 'return isset( $p );' ) ); |
| 47 | + } |
| 48 | + |
| 49 | + protected function getConditions( array $params ) { |
| 50 | + $conditions = array(); |
| 51 | + |
| 52 | + foreach ( $params as $name => $value ) { |
| 53 | + if ( $this->getClass()->canHasField( $name ) ) { |
| 54 | + $conditions[$name] = $value; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return $conditions; |
| 59 | + } |
| 60 | + |
| 61 | + protected function getResults( array $params, array $conditions ) { |
| 62 | + return $this->getClass()->select( |
| 63 | + $params['props'], |
| 64 | + $conditions, |
| 65 | + array( |
| 66 | + 'LIMIT' => $params['limit'] + 1, |
| 67 | + 'ORDER BY' => $this->getClass()->getPrefixedField( 'id' ) . ' ASC' |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + protected function addResults( array $params, array $results ) { |
| 73 | + $serializedResults = array(); |
| 74 | + $count = 0; |
| 75 | + |
| 76 | + foreach ( $results as $result ) { |
| 77 | + if ( ++$count > $params['limit'] ) { |
| 78 | + // We've reached the one extra which shows that |
| 79 | + // there are additional pages to be had. Stop here... |
| 80 | + $this->setContinueEnumParameter( 'continue', $result->getId() ); |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + $serializedResults[] = $result->toArray(); |
| 85 | + } |
| 86 | + |
| 87 | + $this->getResult()->setIndexedTagName( $serializedResults, 'contest' ); |
| 88 | + |
| 89 | + $this->getResult()->addValue( |
| 90 | + null, |
| 91 | + 'contests', |
| 92 | + $serializedResults |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * (non-PHPdoc) |
| 98 | + * @see includes/api/ApiBase#getAllowedParams() |
| 99 | + */ |
| 100 | + public function getAllowedParams() { |
| 101 | + $params = array ( |
| 102 | + 'props' => array( |
| 103 | + ApiBase::PARAM_TYPE => array_merge( $this->getClass()->getFieldNames(), array( '*' ) ), |
| 104 | + ApiBase::PARAM_ISMULTI => true, |
| 105 | + ApiBase::PARAM_DFLT => '*' |
| 106 | + ), |
| 107 | + 'limit' => array( |
| 108 | + ApiBase::PARAM_DFLT => 20, |
| 109 | + ApiBase::PARAM_TYPE => 'limit', |
| 110 | + ApiBase::PARAM_MIN => 1, |
| 111 | + ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 112 | + ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 113 | + ), |
| 114 | + 'continue' => null, |
| 115 | + ); |
| 116 | + |
| 117 | + return array_merge( $this->getClass()->getAPIParams(), $params ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * (non-PHPdoc) |
| 122 | + * @see includes/api/ApiBase#getParamDescription() |
| 123 | + */ |
| 124 | + public function getParamDescription() { |
| 125 | + $descs = array ( |
| 126 | + 'props' => 'Fields to query', |
| 127 | + 'continue' => 'Offset number from where to continue the query', |
| 128 | + 'limit' => 'Max amount of rows to return', |
| 129 | + ); |
| 130 | + |
| 131 | + return array_merge( $this->getClass()->getFieldDescriptions(), $descs ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * (non-PHPdoc) |
| 136 | + * @see includes/api/ApiBase#getPossibleErrors() |
| 137 | + */ |
| 138 | + public function getPossibleErrors() { |
| 139 | + return array_merge( parent::getPossibleErrors(), array( |
| 140 | + ) ); |
| 141 | + } |
| 142 | + |
| 143 | +} |
Property changes on: trunk/extensions/Contest/api/ApiContestQuery.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 144 | + native |
Index: trunk/extensions/Contest/api/ApiQueryContests.php |
— | — | @@ -12,8 +12,12 @@ |
13 | 13 | * @licence GNU GPL v3+ |
14 | 14 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
15 | 15 | */ |
16 | | -class ApiQueryContests extends ApiQueryBase { |
| 16 | +class ApiQueryContests extends ApiContestQuery { |
17 | 17 | |
| 18 | + protected function getClassName() { |
| 19 | + return 'Contest'; |
| 20 | + } |
| 21 | + |
18 | 22 | public function __construct( $main, $action ) { |
19 | 23 | parent::__construct( $main, $action, 'co' ); |
20 | 24 | } |
— | — | @@ -28,98 +32,11 @@ |
29 | 33 | $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
30 | 34 | } |
31 | 35 | |
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 | | - ); |
| 36 | + parent::execute(); |
82 | 37 | } |
83 | 38 | |
84 | 39 | /** |
85 | 40 | * (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 | 41 | * @see includes/api/ApiBase#getDescription() |
125 | 42 | */ |
126 | 43 | public function getDescription() { |
— | — | @@ -128,15 +45,6 @@ |
129 | 46 | |
130 | 47 | /** |
131 | 48 | * (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 | 49 | * @see includes/api/ApiBase#getExamples() |
142 | 50 | */ |
143 | 51 | protected function getExamples() { |