4 * Base query module for querying results from ORMTables.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 abstract class ApiQueryORM
extends ApiQueryBase
{
32 * Returns an instance of the IORMTable table being queried.
38 abstract protected function getTable();
41 * Returns the name of the individual rows.
42 * For example: page, user, contest, campaign, etc.
43 * This is used to appropriately name elements in XML.
44 * Deriving classes typically override this method.
50 protected function getRowName() {
55 * Returns the name of the list of rows.
56 * For example: pages, users, contests, campaigns, etc.
57 * This is used to appropriately name nodes in the output.
58 * Deriving classes typically override this method.
64 protected function getListName() {
69 * Returns the path to where the items results should be added in the result.
73 * @return null|string|array
75 protected function getResultPath() {
80 * Get the parameters, find out what the conditions for the query are,
81 * run it, and add the results.
85 public function execute() {
86 $params = $this->getParams();
88 if ( !in_array( 'id', $params['props'] ) ) {
89 $params['props'][] = 'id';
92 $results = $this->getResults( $params, $this->getConditions( $params ) );
93 $this->addResults( $params, $results );
97 * Get the request parameters and remove all params set
98 * to null (ie those that are not actually provided).
104 protected function getParams() {
106 $this->extractRequestParams(),
108 return isset( $prop );
114 * Get the conditions for the query. These will be provided as
115 * regular parameters, together with limit, props, continue,
116 * and possibly others which we need to get rid off.
120 * @param array $params
124 protected function getConditions( array $params ) {
125 $conditions = array();
126 $fields = $this->getTable()->getFields();
128 foreach ( $params as $name => $value ) {
129 if ( array_key_exists( $name, $fields ) ) {
130 $conditions[$name] = $value;
138 * Get the actual results.
142 * @param array $params
143 * @param array $conditions
147 protected function getResults( array $params, array $conditions ) {
148 return $this->getTable()->select(
152 'LIMIT' => $params['limit'] +
1,
153 'ORDER BY' => $this->getTable()->getPrefixedField( 'id' ) . ' ASC',
160 * Serialize the results and add them to the result object.
164 * @param array $params
165 * @param ORMResult $results
167 protected function addResults( array $params, ORMResult
$results ) {
168 $serializedResults = array();
171 foreach ( $results as /* IORMRow */ $result ) {
172 if ( ++
$count > $params['limit'] ) {
173 // We've reached the one extra which shows that
174 // there are additional pages to be had. Stop here...
175 $this->setContinueEnumParameter( 'continue', $result->getId() );
179 $serializedResults[] = $this->formatRow( $result, $params );
182 $this->setIndexedTagNames( $serializedResults );
183 $this->addSerializedResults( $serializedResults );
187 * Formats a row to it's desired output format.
191 * @param IORMRow $result
192 * @param array $params
196 protected function formatRow( IORMRow
$result, array $params ) {
197 return $result->toArray( $params['props'] );
201 * Set the tag names for formats such as XML.
205 * @param array $serializedResults
207 protected function setIndexedTagNames( array &$serializedResults ) {
208 $this->getResult()->setIndexedTagName( $serializedResults, $this->getRowName() );
212 * Add the serialized results to the result object.
216 * @param array $serializedResults
218 protected function addSerializedResults( array $serializedResults ) {
219 $this->getResult()->addValue(
220 $this->getResultPath(),
221 $this->getListName(),
227 * @see ApiBase::getAllowedParams()
230 public function getAllowedParams() {
233 ApiBase
::PARAM_TYPE
=> $this->getTable()->getFieldNames(),
234 ApiBase
::PARAM_ISMULTI
=> true,
235 ApiBase
::PARAM_REQUIRED
=> true,
238 ApiBase
::PARAM_DFLT
=> 20,
239 ApiBase
::PARAM_TYPE
=> 'limit',
240 ApiBase
::PARAM_MIN
=> 1,
241 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
242 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
247 return array_merge( $this->getTable()->getAPIParams(), $params );
251 * @see ApiBase::getParamDescription()
254 public function getParamDescription() {
255 $descriptions = array(
256 'props' => 'Fields to query',
257 'continue' => 'Offset number from where to continue the query',
258 'limit' => 'Max amount of rows to return',
261 return array_merge( $this->getTable()->getFieldDescriptions(), $descriptions );