r99045 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99044‎ | r99045 | r99046 >
Date:20:20, 5 October 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
refactored all magic ContestDBClass API behvaiour into a base class, so we need no logic at all in most implementations :)
Modified paths:
  • /trunk/extensions/Contest/Contest.php (modified) (history)
  • /trunk/extensions/Contest/api/ApiContestQuery.php (added) (history)
  • /trunk/extensions/Contest/api/ApiQueryContests.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Contest/Contest.php
@@ -49,6 +49,7 @@
5050 $wgAutoloadClasses['ContestHooks'] = dirname( __FILE__ ) . '/Contest.hooks.php';
5151 $wgAutoloadClasses['ContestSettings'] = dirname( __FILE__ ) . '/Contest.settings.php';
5252
 53+$wgAutoloadClasses['ApiContestQuery'] = dirname( __FILE__ ) . '/api/ApiContestQuery.php';
5354 $wgAutoloadClasses['ApiDeleteContest'] = dirname( __FILE__ ) . '/api/ApiDeleteContest.php';
5455 $wgAutoloadClasses['ApiQueryContests'] = dirname( __FILE__ ) . '/api/ApiQueryContests.php';
5556
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
1144 + native
Index: trunk/extensions/Contest/api/ApiQueryContests.php
@@ -12,8 +12,12 @@
1313 * @licence GNU GPL v3+
1414 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1515 */
16 -class ApiQueryContests extends ApiQueryBase {
 16+class ApiQueryContests extends ApiContestQuery {
1717
 18+ protected function getClassName() {
 19+ return 'Contest';
 20+ }
 21+
1822 public function __construct( $main, $action ) {
1923 parent::__construct( $main, $action, 'co' );
2024 }
@@ -28,98 +32,11 @@
2933 $this->dieUsageMsg( array( 'badaccess-groups' ) );
3034 }
3135
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();
8237 }
8338
8439 /**
8540 * (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)
12441 * @see includes/api/ApiBase#getDescription()
12542 */
12643 public function getDescription() {
@@ -128,15 +45,6 @@
12946
13047 /**
13148 * (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)
14149 * @see includes/api/ApiBase#getExamples()
14250 */
14351 protected function getExamples() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r99048follow up to r99045 - docs++jeroendedauw20:30, 5 October 2011

Status & tagging log