MediaWiki  REL1_21
ApiQueryORM.php
Go to the documentation of this file.
00001 <?php
00002 
00029 abstract class ApiQueryORM extends ApiQueryBase {
00030 
00038         abstract protected function getTable();
00039 
00050         protected function getRowName() {
00051                 return 'item';
00052         }
00053 
00064         protected function getListName() {
00065                 return 'items';
00066         }
00067 
00075         protected function getResultPath() {
00076                 return null;
00077         }
00078 
00085         public function execute() {
00086                 $params = $this->getParams();
00087 
00088                 if ( !in_array( 'id', $params['props'] ) ) {
00089                         $params['props'][] = 'id';
00090                 }
00091 
00092                 $results = $this->getResults( $params, $this->getConditions( $params ) );
00093                 $this->addResults( $params, $results );
00094         }
00095 
00104         protected function getParams() {
00105                 return array_filter(
00106                         $this->extractRequestParams(),
00107                         function( $prop ) {
00108                                 return isset( $prop );
00109                         }
00110                 );
00111         }
00112 
00124         protected function getConditions( array $params ) {
00125                 $conditions = array();
00126                 $fields = $this->getTable()->getFields();
00127 
00128                 foreach ( $params as $name => $value ) {
00129                         if ( array_key_exists( $name, $fields ) ) {
00130                                 $conditions[$name] = $value;
00131                         }
00132                 }
00133 
00134                 return $conditions;
00135         }
00136 
00147         protected function getResults( array $params, array $conditions ) {
00148                 return $this->getTable()->select(
00149                         $params['props'],
00150                         $conditions,
00151                         array(
00152                                 'LIMIT' => $params['limit'] + 1,
00153                                 'ORDER BY' => $this->getTable()->getPrefixedField( 'id' ) . ' ASC',
00154                         ),
00155                         __METHOD__
00156                 );
00157         }
00158 
00167         protected function addResults( array $params, ORMResult $results ) {
00168                 $serializedResults = array();
00169                 $count = 0;
00170 
00171                 foreach ( $results as /* IORMRow */ $result ) {
00172                         if ( ++$count > $params['limit'] ) {
00173                                 // We've reached the one extra which shows that
00174                                 // there are additional pages to be had. Stop here...
00175                                 $this->setContinueEnumParameter( 'continue', $result->getId() );
00176                                 break;
00177                         }
00178 
00179                         $serializedResults[] = $this->formatRow( $result, $params );
00180                 }
00181 
00182                 $this->setIndexedTagNames( $serializedResults );
00183                 $this->addSerializedResults( $serializedResults );
00184         }
00185 
00196         protected function formatRow( IORMRow $result, array $params ) {
00197                 return $result->toArray( $params['props'] );
00198         }
00199 
00207         protected function setIndexedTagNames( array &$serializedResults ) {
00208                 $this->getResult()->setIndexedTagName( $serializedResults, $this->getRowName() );
00209         }
00210 
00218         protected function addSerializedResults( array $serializedResults ) {
00219                 $this->getResult()->addValue(
00220                         $this->getResultPath(),
00221                         $this->getListName(),
00222                         $serializedResults
00223                 );
00224         }
00225 
00230         public function getAllowedParams() {
00231                 $params = array (
00232                         'props' => array(
00233                                 ApiBase::PARAM_TYPE => $this->getTable()->getFieldNames(),
00234                                 ApiBase::PARAM_ISMULTI => true,
00235                                 ApiBase::PARAM_REQUIRED => true,
00236                         ),
00237                         'limit' => array(
00238                                 ApiBase::PARAM_DFLT => 20,
00239                                 ApiBase::PARAM_TYPE => 'limit',
00240                                 ApiBase::PARAM_MIN => 1,
00241                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00242                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00243                         ),
00244                         'continue' => null,
00245                 );
00246 
00247                 return array_merge( $this->getTable()->getAPIParams(), $params );
00248         }
00249 
00254         public function getParamDescription() {
00255                 $descriptions = array (
00256                         'props' => 'Fields to query',
00257                         'continue' => 'Offset number from where to continue the query',
00258                         'limit' => 'Max amount of rows to return',
00259                 );
00260 
00261                 return array_merge( $this->getTable()->getFieldDescriptions(), $descriptions );
00262         }
00263 
00264 }