r103681 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103680‎ | r103681 | r103682 >
Date:15:11, 19 November 2011
Author:jeroendedauw
Status:reverted
Tags:
Comment:
adding more needed stuffz
Modified paths:
  • /trunk/extensions/Reviews/Reviews.php (modified) (history)
  • /trunk/extensions/Reviews/Reviews.settings.php (modified) (history)
  • /trunk/extensions/Reviews/api/ApiDeleteReviews.php (added) (history)
  • /trunk/extensions/Reviews/api/ApiQueryReviews.php (added) (history)
  • /trunk/extensions/Reviews/api/ApiReviewQuery.php (added) (history)
  • /trunk/extensions/Reviews/api/ApiSubmitReview.php (added) (history)
  • /trunk/extensions/Reviews/includes/Review.php (added) (history)
  • /trunk/extensions/Reviews/includes/ReviewPager.php (added) (history)
  • /trunk/extensions/Reviews/includes/ReviewRating.php (added) (history)
  • /trunk/extensions/Reviews/includes/ReviewsDBObject.php (modified) (history)
  • /trunk/extensions/Reviews/specials (added) (history)
  • /trunk/extensions/Reviews/specials/SpecialMyReviews.php (added) (history)
  • /trunk/extensions/Reviews/specials/SpecialReviews.php (added) (history)

Diff [purge]

Index: trunk/extensions/Reviews/specials/SpecialMyReviews.php
@@ -0,0 +1,62 @@
 2+<?php
 3+
 4+/**
 5+ * List of reviews for a user.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SpecialMyReviews.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class SpecialMyReviews extends SpecialPage {
 16+
 17+ public $subPage;
 18+
 19+ /**
 20+ * Constructor.
 21+ *
 22+ * @since 0.1
 23+ */
 24+ public function __construct() {
 25+ parent::__construct( 'MyReviews', 'reviewer' );
 26+ }
 27+
 28+ /**
 29+ * Main method.
 30+ *
 31+ * @since 0.1
 32+ *
 33+ * @param string $arg
 34+ */
 35+ public function execute( $subPage ) {
 36+ $this->subPage = str_replace( '_', ' ', $subPage );
 37+
 38+ $this->setHeaders();
 39+ $this->outputHeader();
 40+
 41+ // If the user is authorized, display the page, if not, show an error.
 42+ if ( !$this->userCanExecute( $this->getUser() ) ) {
 43+ $this->displayRestrictionError();
 44+ return false;
 45+ }
 46+
 47+ if ( $this->getRequest()->wasPosted() ) {
 48+
 49+ }
 50+ else {
 51+ if ( $subPage === '' ) {
 52+ $this->displayReviewList();
 53+ }
 54+ else {
 55+
 56+ }
 57+ }
 58+ }
 59+
 60+ protected function displayReviewList() {
 61+ }
 62+
 63+}
Property changes on: trunk/extensions/Reviews/specials/SpecialMyReviews.php
___________________________________________________________________
Added: svn:eol-style
164 + native
Index: trunk/extensions/Reviews/specials/SpecialReviews.php
@@ -0,0 +1,62 @@
 2+<?php
 3+
 4+/**
 5+ * List of reviews.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file SpecialReviews.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class SpecialReviews extends SpecialPage {
 16+
 17+ public $subPage;
 18+
 19+ /**
 20+ * Constructor.
 21+ *
 22+ * @since 0.1
 23+ */
 24+ public function __construct() {
 25+ parent::__construct( 'Reviews', 'reviewer' );
 26+ }
 27+
 28+ /**
 29+ * Main method.
 30+ *
 31+ * @since 0.1
 32+ *
 33+ * @param string $arg
 34+ */
 35+ public function execute( $subPage ) {
 36+ $this->subPage = str_replace( '_', ' ', $subPage );
 37+
 38+ $this->setHeaders();
 39+ $this->outputHeader();
 40+
 41+ // If the user is authorized, display the page, if not, show an error.
 42+ if ( !$this->userCanExecute( $this->getUser() ) ) {
 43+ $this->displayRestrictionError();
 44+ return false;
 45+ }
 46+
 47+ if ( $this->getRequest()->wasPosted() ) {
 48+
 49+ }
 50+ else {
 51+ if ( $subPage === '' ) {
 52+ $this->displayReviewList();
 53+ }
 54+ else {
 55+
 56+ }
 57+ }
 58+ }
 59+
 60+ protected function displayReviewList() {
 61+ }
 62+
 63+}
Property changes on: trunk/extensions/Reviews/specials/SpecialReviews.php
___________________________________________________________________
Added: svn:eol-style
164 + native
Index: trunk/extensions/Reviews/includes/ReviewPager.php
@@ -0,0 +1,147 @@
 2+<?php
 3+
 4+/**
 5+ * Review pager, for on Special:Reviews and Special:MyReviews
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ReviewPager.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class ReviewPager extends TablePager {
 16+
 17+ /**
 18+ * Query conditions, full field names (ie inc prefix).
 19+ * @var array
 20+ */
 21+ protected $conds;
 22+
 23+ /**
 24+ * Constructor.
 25+ *
 26+ * @param array $conds
 27+ */
 28+ public function __construct( array $conds ) {
 29+ $this->conds = $conds;
 30+ $this->mDefaultDirection = true;
 31+
 32+ // when MW 1.19 becomes min, we want to pass an IContextSource $context here.
 33+ parent::__construct();
 34+ }
 35+
 36+ /**
 37+ * Gets the title of a challenge given it's id.
 38+ *
 39+ * @since 0.1
 40+ *
 41+ * @param integer $challengeId
 42+ * @throws MWException
 43+ */
 44+ protected function getChallengeTitle( $challengeId ) {
 45+ if ( array_key_exists( $challengeId, $this->challengeTitles ) ) {
 46+ return $this->challengeTitles[$challengeId];
 47+ }
 48+ else {
 49+ throw new MWException( 'Attempt to get non-set challenge title' );
 50+ }
 51+ }
 52+
 53+ /**
 54+ * Get the OutputPage being used for this instance.
 55+ * IndexPager extends ContextSource as of 1.19.
 56+ *
 57+ * @since 0.1
 58+ *
 59+ * @return OutputPage
 60+ */
 61+ public function getOutput() {
 62+ return version_compare( $GLOBALS['wgVersion'], '1.18', '>' ) ? parent::getOutput() : $GLOBALS['wgOut'];
 63+ }
 64+
 65+ /**
 66+ * Get the Language being used for this instance.
 67+ * IndexPager extends ContextSource as of 1.19.
 68+ *
 69+ * @since 0.1
 70+ *
 71+ * @return Language
 72+ */
 73+ public function getLang() {
 74+ return version_compare( $GLOBALS['wgVersion'], '1.18', '>' ) ? parent::getLang() : $GLOBALS['wgLang'];
 75+ }
 76+
 77+ /**
 78+ * @return array
 79+ */
 80+ public function getFieldNames() {
 81+ static $headers = null;
 82+
 83+ if ( is_null( $headers ) ) {
 84+ $headers = array(
 85+ // TODO
 86+ );
 87+
 88+ $headers = array_map( 'wfMsg', $headers );
 89+ }
 90+
 91+ return $headers;
 92+ }
 93+
 94+ /**
 95+ * @param $row
 96+ * @return string
 97+ */
 98+ function getRowClass( $row ) {
 99+ return 'review-row';
 100+ }
 101+
 102+ /**
 103+ * @param $name
 104+ * @param $value
 105+ * @return string
 106+ */
 107+ public function formatValue( $name, $value ) {
 108+ switch ( $name ) {
 109+ case '':
 110+
 111+ break;
 112+ }
 113+
 114+ return $value;
 115+ }
 116+
 117+ function getQueryInfo() {
 118+ return array(
 119+ 'tables' => array( 'reviews' ),
 120+ 'fields' => array(
 121+ // TODO
 122+ ),
 123+ 'conds' => $this->conds,
 124+ );
 125+ }
 126+
 127+ public function getTableClass(){
 128+ return 'TablePager review-contestants';
 129+ }
 130+
 131+ function getIndexField() {
 132+ return 'review_id';
 133+ }
 134+
 135+ function getDefaultSort() {
 136+ return 'review_time';
 137+ }
 138+
 139+ function isFieldSortable( $name ) {
 140+ return in_array(
 141+ $name,
 142+ array(
 143+ // TODO
 144+ )
 145+ );
 146+ }
 147+
 148+}
Property changes on: trunk/extensions/Reviews/includes/ReviewPager.php
___________________________________________________________________
Added: svn:eol-style
1149 + native
Index: trunk/extensions/Reviews/includes/Review.php
@@ -0,0 +1,82 @@
 2+<?php
 3+
 4+/**
 5+ * Class representing a single review.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file Review.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class Review extends ReviewsDBObject {
 16+
 17+ // Constants representing the states a review can have.
 18+ const STATUS_NEW = 0;
 19+ const STATUS_FLAGGED = 1;
 20+ const STATUS_REVIEWED = 2;
 21+
 22+ /**
 23+ *
 24+ *
 25+ * @since 0.1
 26+ * @var array of ReviewRating
 27+ */
 28+ protected $ratings = false;
 29+
 30+ /**
 31+ * @see parent::getFieldTypes
 32+ *
 33+ * @since 0.1
 34+ *
 35+ * @return string
 36+ */
 37+ public static function getDBTable() {
 38+ return 'reviews';
 39+ }
 40+
 41+ /**
 42+ * @see parent::getFieldTypes
 43+ *
 44+ * @since 0.1
 45+ *
 46+ * @return string
 47+ */
 48+ protected static function getFieldPrefix() {
 49+ return 'review_';
 50+ }
 51+
 52+ /**
 53+ * @see parent::getFieldTypes
 54+ *
 55+ * @since 0.1
 56+ *
 57+ * @return array
 58+ */
 59+ protected static function getFieldTypes() {
 60+ return array(
 61+ 'id' => 'id',
 62+ 'time' => 'str', // TS_MW
 63+
 64+ );
 65+ }
 66+
 67+ /**
 68+ * @see parent::getDefaults
 69+ *
 70+ * @since 0.1
 71+ *
 72+ * @return array
 73+ */
 74+ public static function getDefaults() {
 75+ return array(
 76+ );
 77+ }
 78+
 79+ public function removeAllFromDB() {
 80+
 81+ }
 82+
 83+}
Property changes on: trunk/extensions/Reviews/includes/Review.php
___________________________________________________________________
Added: svn:eol-style
184 + native
Index: trunk/extensions/Reviews/includes/ReviewsDBObject.php
@@ -2,16 +2,21 @@
33
44 /**
55 * Abstract base class for representing objects that are stored in some DB table.
 6+ * These methods must be implemented in deriving classes:
 7+ * * getDBTable
 8+ * * getFieldPrefix
 9+ * * getFieldTypes
 10+ * * getDefaults
611 *
712 * @since 0.1
813 *
9 - * @file ReviewDBObject.php
 14+ * @file ReviewsDBObject.php
1015 * @ingroup Review
1116 *
1217 * @licence GNU GPL v3 or later
1318 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1419 */
15 -abstract class ReviewDBObject {
 20+abstract class ReviewsDBObject {
1621
1722 /**
1823 * The fields of the object.
@@ -31,6 +36,66 @@
3237 protected static $readDb = DB_SLAVE;
3338
3439 /**
 40+ * Returns the name of the database table objects of this type are stored in.
 41+ *
 42+ * @since 0.1
 43+ *
 44+ * @throws MWException
 45+ * @return string
 46+ */
 47+ public static function getDBTable() {
 48+ throw new MWException( 'Class did not implement getDBTable' );
 49+ }
 50+
 51+ /**
 52+ * Gets the db field prefix.
 53+ *
 54+ * @since 0.1
 55+ *
 56+ * @throws MWException
 57+ * @return string
 58+ */
 59+ protected static function getFieldPrefix() {
 60+ throw new MWException( 'Class did not implement getFieldPrefix' );
 61+ }
 62+
 63+ /**
 64+ * Returns an array with the fields and their types this object contains.
 65+ * This corresponds directly to the fields in the database, without prefix.
 66+ *
 67+ * field name => type
 68+ *
 69+ * Allowed types:
 70+ * * id
 71+ * * str
 72+ * * int
 73+ * * float
 74+ * * bool
 75+ * * array
 76+ *
 77+ * @since 0.1
 78+ *
 79+ * @throws MWException
 80+ * @return array
 81+ */
 82+ protected static function getFieldTypes() {
 83+ throw new MWException( 'Class did not implement getFieldTypes' );
 84+ }
 85+
 86+ /**
 87+ * Returns a list of default field values.
 88+ * field name => field value
 89+ *
 90+ * @since 0.1
 91+ *
 92+ * @throws MWException
 93+ * @return array
 94+ */
 95+ public static function getDefaults() {
 96+ throw new MWException( 'Class did not implement getDefaults' );
 97+ }
 98+
 99+ /**
35100 * Constructor.
36101 *
37102 * @since 0.1
@@ -84,24 +149,6 @@
85150 }
86151
87152 /**
88 - * Returns the name of the database table objects of this type are stored in.
89 - *
90 - * @since 0.1
91 - *
92 - * @return string
93 - */
94 - public abstract function getDBTable();
95 -
96 - /**
97 - * Gets the db field prefix.
98 - *
99 - * @since 0.1
100 - *
101 - * @return string
102 - */
103 - protected abstract function getFieldPrefix();
104 -
105 - /**
106153 * Gets the value of a field.
107154 *
108155 * @since 0.1
@@ -408,36 +455,6 @@
409456 }
410457
411458 /**
412 - * Returns an array with the fields and their types this object contains.
413 - * This corresponds directly to the fields in the database, without prefix.
414 - *
415 - * field name => type
416 - *
417 - * Allowed types:
418 - * * id
419 - * * str
420 - * * int
421 - * * float
422 - * * bool
423 - * * array
424 - *
425 - * @since 0.1
426 - *
427 - * @return array
428 - */
429 - protected abstract function getFieldTypes();
430 -
431 - /**
432 - * Returns a list of default field values.
433 - * field name => field value
434 - *
435 - * @since 0.1
436 - *
437 - * @return array
438 - */
439 - public abstract function getDefaults();
440 -
441 - /**
442459 * Get a new instance of the class from an array.
443460 *
444461 * @since 0.1
Index: trunk/extensions/Reviews/includes/ReviewRating.php
@@ -0,0 +1,64 @@
 2+<?php
 3+
 4+/**
 5+ * Class representing a single review rating.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ReviewRating.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class ReviewRating extends ReviewsDBObject {
 16+
 17+ /**
 18+ * @see parent::getFieldTypes
 19+ *
 20+ * @since 0.1
 21+ *
 22+ * @return string
 23+ */
 24+ public static function getDBTable() {
 25+ return 'review_ratings';
 26+ }
 27+
 28+ /**
 29+ * @see parent::getFieldTypes
 30+ *
 31+ * @since 0.1
 32+ *
 33+ * @return string
 34+ */
 35+ protected static function getFieldPrefix() {
 36+ return 'rating_';
 37+ }
 38+
 39+ /**
 40+ * @see parent::getFieldTypes
 41+ *
 42+ * @since 0.1
 43+ *
 44+ * @return array
 45+ */
 46+ protected static function getFieldTypes() {
 47+ return array(
 48+ 'id' => 'id',
 49+
 50+ );
 51+ }
 52+
 53+ /**
 54+ * @see parent::getDefaults
 55+ *
 56+ * @since 0.1
 57+ *
 58+ * @return array
 59+ */
 60+ public static function getDefaults() {
 61+ return array(
 62+ );
 63+ }
 64+
 65+}
Property changes on: trunk/extensions/Reviews/includes/ReviewRating.php
___________________________________________________________________
Added: svn:eol-style
166 + native
Index: trunk/extensions/Reviews/Reviews.settings.php
@@ -28,6 +28,7 @@
2929 */
3030 protected static function getDefaultSettings() {
3131 return array(
 32+ 'reviewDeletionEnabled' => true,
3233 'enableTopLink' => true,
3334 );
3435 }
Index: trunk/extensions/Reviews/api/ApiSubmitReview.php
@@ -0,0 +1,99 @@
 2+<?php
 3+
 4+/**
 5+ * API module to submit reviews.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ApiSubmitReview.php
 10+ * @ingroup Reviews
 11+ * @ingroup API
 12+ *
 13+ * @licence GNU GPL v3+
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ */
 16+class ApiSubmitReview extends ApiBase {
 17+
 18+ public function execute() {
 19+ if ( !$this->getUser()->isAllowed( 'review' ) || $this->getUser()->isBlocked() ) {
 20+ $this->dieUsageMsg( array( 'badaccess-groups' ) );
 21+ }
 22+
 23+ $params = $this->extractRequestParams();
 24+
 25+ if ( !( isset( $params['id'] ) XOR isset( $params['name'] ) ) ) {
 26+ $this->dieUsage( wfMsgExt( 'review-err-id-xor-name' ), 'id-xor-name' );
 27+ }
 28+
 29+ if ( isset( $params['name'] ) ) {
 30+ $review = Review::selectRow( null, array( 'name' => $params['name'] ) );
 31+
 32+ if ( $review === false ) {
 33+ $this->dieUsage( wfMsgExt( 'review-err-review-name-unknown', 'parsemag', $params['name'] ), 'review-name-unknown' );
 34+ }
 35+ } else {
 36+ $review = Review::selectRow( null, array( 'id' => $params['id'] ) );
 37+
 38+ if ( $review === false ) {
 39+ $this->dieUsage( wfMsgExt( 'review-err-review-id-unknown', 'parsemag', $params['id'] ), 'review-id-unknown' );
 40+ }
 41+ }
 42+
 43+ // TODO
 44+ }
 45+
 46+ public function needsToken() {
 47+ return true;
 48+ }
 49+
 50+ public function getTokenSalt() {
 51+ return serialize( array( 'submitreview', $this->getUser()->getName() ) );
 52+ }
 53+
 54+ public function mustBePosted() {
 55+ return true;
 56+ }
 57+
 58+ public function getAllowedParams() {
 59+ return array(
 60+ 'id' => array(
 61+ ApiBase::PARAM_TYPE => 'integer',
 62+ ),
 63+ 'name' => array(
 64+ ApiBase::PARAM_TYPE => 'string',
 65+ ),
 66+ // TODO
 67+ 'token' => null,
 68+ );
 69+ }
 70+
 71+ public function getParamDescription() {
 72+ return array(
 73+ 'id' => 'The ID of the review being submitted.',
 74+ 'name' => 'The name of the review being submitted.',
 75+ 'token' => 'Edit token. You can get one of these through prop=info.',
 76+ );
 77+ }
 78+
 79+ public function getDescription() {
 80+ return array(
 81+ ''
 82+ );
 83+ }
 84+
 85+ public function getPossibleErrors() {
 86+ return array_merge( parent::getPossibleErrors(), array(
 87+ ) );
 88+ }
 89+
 90+ protected function getExamples() {
 91+ return array(
 92+ 'api.php?action=submitreview&',
 93+ );
 94+ }
 95+
 96+ public function getVersion() {
 97+ return __CLASS__ . ': $Id$';
 98+ }
 99+
 100+}
Property changes on: trunk/extensions/Reviews/api/ApiSubmitReview.php
___________________________________________________________________
Added: svn:eol-style
1101 + native
Added: svn:keywords
2102 + Id
Index: trunk/extensions/Reviews/api/ApiDeleteReviews.php
@@ -0,0 +1,96 @@
 2+<?php
 3+
 4+/**
 5+ * API module to delete reviews.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ApiDeleteReviews.php
 10+ * @ingroup Reviews
 11+ * @ingroup API
 12+ *
 13+ * @licence GNU GPL v3+
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ */
 16+class ApiDeleteReviews extends ApiBase {
 17+
 18+ public function execute() {
 19+ if ( !ReviewsSettings::get( 'reviewDeletionEnabled' ) ) {
 20+ $this->dieUsage( 'Review deletion is disabled', 'reviewdeletiondisabled' );
 21+ }
 22+
 23+ if ( !$this->getUser()->isAllowed( 'reviewadmin' ) || $this->getUser()->isBlocked() ) {
 24+ $this->dieUsageMsg( array( 'badaccess-groups' ) );
 25+ }
 26+
 27+ $params = $this->extractRequestParams();
 28+
 29+ $everythingOk = true;
 30+
 31+ foreach ( $params['ids'] as $id ) {
 32+ $review = new Review( array( 'id' => $id ) );
 33+ $everythingOk = $review->removeAllFromDB() && $everythingOk;
 34+ }
 35+
 36+ $this->getResult()->addValue(
 37+ null,
 38+ 'success',
 39+ $everythingOk
 40+ );
 41+ }
 42+
 43+ public function needsToken() {
 44+ return true;
 45+ }
 46+
 47+ public function getTokenSalt() {
 48+ $params = $this->extractRequestParams();
 49+ return 'deletereview' . implode( '|', $params['ids'] );
 50+ }
 51+
 52+ public function mustBePosted() {
 53+ return true;
 54+ }
 55+
 56+ public function getAllowedParams() {
 57+ return array(
 58+ 'ids' => array(
 59+ ApiBase::PARAM_TYPE => 'integer',
 60+ ApiBase::PARAM_REQUIRED => true,
 61+ ApiBase::PARAM_ISMULTI => true,
 62+ ),
 63+ 'token' => null,
 64+ );
 65+ }
 66+
 67+ public function getParamDescription() {
 68+ return array(
 69+ 'ids' => 'The IDs of the reviews to delete',
 70+ 'token' => 'Edit token, salted with the review ids',
 71+ );
 72+ }
 73+
 74+ public function getDescription() {
 75+ return array(
 76+ 'API module for deleting reviews.'
 77+ );
 78+ }
 79+
 80+ public function getPossibleErrors() {
 81+ return array_merge( parent::getPossibleErrors(), array(
 82+ array( 'code' => 'reviewdeletiondisabled', 'info' => 'Review deletion is disabled' ),
 83+ ) );
 84+ }
 85+
 86+ protected function getExamples() {
 87+ return array(
 88+ 'api.php?action=deletereviews&ids=42',
 89+ 'api.php?action=deletereviews&ids=4|2',
 90+ );
 91+ }
 92+
 93+ public function getVersion() {
 94+ return __CLASS__ . ': $Id$';
 95+ }
 96+
 97+}
Property changes on: trunk/extensions/Reviews/api/ApiDeleteReviews.php
___________________________________________________________________
Added: svn:eol-style
198 + native
Added: svn:keywords
299 + Id
Index: trunk/extensions/Reviews/api/ApiReviewQuery.php
@@ -0,0 +1,233 @@
 2+<?php
 3+
 4+/**
 5+ * Base class for API query modules that return results using a
 6+ * ReviewsDBObject deriving class.
 7+ *
 8+ * @since 0.1
 9+ *
 10+ * @file ApiReviewQuery.php
 11+ * @ingroup Reviews
 12+ * @ingroup API
 13+ *
 14+ * @licence GNU GPL v3+
 15+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 16+ */
 17+abstract class ApiReviewQuery extends ApiQueryBase {
 18+
 19+ /**
 20+ * Returns the class specific info.
 21+ * * class: name of the ReviewsDBObject deriving class (ie Contest)
 22+ * * item: item name (ie contest)
 23+ * * set: item set name (ie contests)
 24+ *
 25+ * @since 0.1
 26+ *
 27+ * @return array of string
 28+ */
 29+ protected abstract function getClassInfo();
 30+
 31+ /**
 32+ * Returns the name of the ReviewsDBObject deriving class.
 33+ *
 34+ * @since 0.1
 35+ *
 36+ * @return string
 37+ */
 38+ protected function getClass() {
 39+ $className = $this->getClassInfo();
 40+ return $className['class'];
 41+ }
 42+
 43+ /**
 44+ * Get the parameters, find out what the conditions for the query are,
 45+ * run it, and add the results.
 46+ *
 47+ * @since 0.1
 48+ */
 49+ public function execute() {
 50+ $params = $this->getParams();
 51+
 52+ if ( count( $params['props'] ) > 0 ) {
 53+ $results = $this->getResults( $params, $this->getConditions( $params ) );
 54+ $this->addResults( $params, $results );
 55+ }
 56+ }
 57+
 58+ /**
 59+ * Get the request parameters, handle the * value for the props param
 60+ * and remove all params set to null (ie those that are not actually provided).
 61+ *
 62+ * @since 0.1
 63+ *
 64+ * @return array
 65+ */
 66+ protected function getParams() {
 67+ // Get the requests parameters.
 68+ $params = $this->extractRequestParams();
 69+
 70+ $starPropPosition = array_search( '*', $params['props'] );
 71+
 72+ if ( $starPropPosition !== false ) {
 73+ unset( $params['props'][$starPropPosition] );
 74+ $class = $this->getClass();
 75+ $params['props'] = array_merge( $params['props'], $class::getFieldNames() );
 76+ }
 77+
 78+ return array_filter( $params, create_function( '$p', 'return isset( $p );' ) );
 79+ }
 80+
 81+ /**
 82+ * Get the conditions for the query. These will be provided as
 83+ * regular parameters, together with limit, props, continue,
 84+ * and possibly others which we need to get rid off.
 85+ *
 86+ * @since 0.1
 87+ *
 88+ * @param array $params
 89+ *
 90+ * @return array
 91+ */
 92+ protected function getConditions( array $params ) {
 93+ $conditions = array();
 94+
 95+ foreach ( $params as $name => $value ) {
 96+ $class = $this->getClass();
 97+ if ( $class::canHasField( $name ) ) {
 98+ $conditions[$name] = $value;
 99+ }
 100+ }
 101+
 102+ return $conditions;
 103+ }
 104+
 105+ /**
 106+ * Get the actual results.
 107+ *
 108+ * @since 0.1
 109+ *
 110+ * @param array $params
 111+ * @param array $conditions
 112+ *
 113+ * @return array of ReviewsDBObject
 114+ */
 115+ protected function getResults( array $params, array $conditions ) {
 116+ $class = $this->getClass();
 117+ return $this->getClass()->select(
 118+ $params['props'],
 119+ $conditions,
 120+ array(
 121+ 'LIMIT' => $params['limit'] + 1,
 122+ 'ORDER BY' => $class::getPrefixedField( 'id' ) . ' ASC'
 123+ )
 124+ );
 125+ }
 126+
 127+ /**
 128+ * Serialize the results and add them to the result object.
 129+ *
 130+ * @since 0.1
 131+ *
 132+ * @param array $params
 133+ * @param array $results
 134+ */
 135+ protected function addResults( array $params, array /* of ReviewsDBObject */ $results ) {
 136+ $serializedResults = array();
 137+ $count = 0;
 138+
 139+ foreach ( $results as $result ) {
 140+ if ( ++$count > $params['limit'] ) {
 141+ // We've reached the one extra which shows that
 142+ // there are additional pages to be had. Stop here...
 143+ $this->setContinueEnumParameter( 'continue', $result->getId() );
 144+ break;
 145+ }
 146+
 147+ $serializedResults[] = $result->toArray( $params['props'] );
 148+ }
 149+
 150+ $this->setIndexedTagNames( $serializedResults );
 151+ $this->addSerializedResults( $serializedResults );
 152+ }
 153+
 154+ /**
 155+ * Set the tag names for formats such as XML.
 156+ *
 157+ * @since 0.1
 158+ *
 159+ * @param array $serializedResults
 160+ */
 161+ protected function setIndexedTagNames( array &$serializedResults ) {
 162+ $classInfo = $this->getClassInfo();
 163+ $this->getResult()->setIndexedTagName( $serializedResults, $classInfo['item'] );
 164+ }
 165+
 166+ /**
 167+ * Add the serialized results to the result object.
 168+ *
 169+ * @since 0.1
 170+ *
 171+ * @param array $serializedResults
 172+ */
 173+ protected function addSerializedResults( array $serializedResults ) {
 174+ $classInfo = $this->getClassInfo();
 175+
 176+ $this->getResult()->addValue(
 177+ null,
 178+ $classInfo['set'],
 179+ $serializedResults
 180+ );
 181+ }
 182+
 183+ /**
 184+ * (non-PHPdoc)
 185+ * @see includes/api/ApiBase#getAllowedParams()
 186+ * @return array
 187+ */
 188+ public function getAllowedParams() {
 189+ $class = $this->getClass();
 190+ $params = array (
 191+ 'props' => array(
 192+ ApiBase::PARAM_TYPE => array_merge( $class::getFieldNames(), array( '*' ) ),
 193+ ApiBase::PARAM_ISMULTI => true,
 194+ ApiBase::PARAM_DFLT => '*'
 195+ ),
 196+ 'limit' => array(
 197+ ApiBase::PARAM_DFLT => 20,
 198+ ApiBase::PARAM_TYPE => 'limit',
 199+ ApiBase::PARAM_MIN => 1,
 200+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 201+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 202+ ),
 203+ 'continue' => null,
 204+ );
 205+
 206+ return array_merge( $class::getAPIParams(), $params );
 207+ }
 208+
 209+ /**
 210+ * (non-PHPdoc)
 211+ * @see includes/api/ApiBase#getParamDescription()
 212+ */
 213+ public function getParamDescription() {
 214+ $descs = array (
 215+ 'props' => 'Fields to query',
 216+ 'continue' => 'Offset number from where to continue the query',
 217+ 'limit' => 'Max amount of rows to return',
 218+ );
 219+
 220+ $class = $this->getClass();
 221+ return array_merge( $class::getFieldDescriptions(), $descs );
 222+ }
 223+
 224+ /**
 225+ * (non-PHPdoc)
 226+ * @see includes/api/ApiBase#getPossibleErrors()
 227+ */
 228+ public function getPossibleErrors() {
 229+ return array_merge( parent::getPossibleErrors(), array(
 230+ array( 'badaccess-groups' ),
 231+ ) );
 232+ }
 233+
 234+}
Property changes on: trunk/extensions/Reviews/api/ApiReviewQuery.php
___________________________________________________________________
Added: svn:eol-style
1235 + native
Added: svn:keywords
2236 + Id
Index: trunk/extensions/Reviews/api/ApiQueryReviews.php
@@ -0,0 +1,75 @@
 2+<?php
 3+
 4+/**
 5+ * API module to get a list of reviews.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ApiQueryReviews.php
 10+ * @ingroup Reviews
 11+ * @ingroup API
 12+ *
 13+ * @licence GNU GPL v3+
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ */
 16+class ApiQueryReviews extends ApiReviewQuery {
 17+
 18+ /**
 19+ * (non-PHPdoc)
 20+ * @see ApiContestQuery::getClassInfo()
 21+ * @return array
 22+ */
 23+ protected function getClassInfo() {
 24+ return array(
 25+ 'class' => 'Review',
 26+ 'item' => 'review',
 27+ 'set' => 'reviews',
 28+ );
 29+ }
 30+
 31+ public function __construct( $main, $action ) {
 32+ parent::__construct( $main, $action, 're' );
 33+ }
 34+
 35+ /**
 36+ * Handle the API request.
 37+ * Checks for access rights and then let's the parent method do the actual work.
 38+ */
 39+ public function execute() {
 40+ global $wgUser;
 41+
 42+ if ( !$wgUser->isAllowed( 'contestadmin' ) || $wgUser->isBlocked() ) {
 43+ $this->dieUsageMsg( array( 'badaccess-groups' ) );
 44+ }
 45+
 46+ parent::execute();
 47+ }
 48+
 49+ /**
 50+ * (non-PHPdoc)
 51+ * @see includes/api/ApiBase#getDescription()
 52+ */
 53+ public function getDescription() {
 54+ return 'API module for querying reviews';
 55+ }
 56+
 57+ /**
 58+ * (non-PHPdoc)
 59+ * @see includes/api/ApiBase#getExamples()
 60+ */
 61+ protected function getExamples() {
 62+ return array (
 63+ 'api.php?action=query&list=reviews&reprops=id|name',
 64+ 'api.php?action=query&list=reviews&restatus=1',
 65+ );
 66+ }
 67+
 68+ /**
 69+ * (non-PHPdoc)
 70+ * @see includes/api/ApiBase#getVersion()
 71+ */
 72+ public function getVersion() {
 73+ return __CLASS__ . ': $Id$';
 74+ }
 75+
 76+}
Property changes on: trunk/extensions/Reviews/api/ApiQueryReviews.php
___________________________________________________________________
Added: svn:eol-style
177 + native
Added: svn:keywords
278 + Id
Index: trunk/extensions/Reviews/Reviews.php
@@ -49,7 +49,19 @@
5050 $wgAutoloadClasses['ReviewsHooks'] = dirname( __FILE__ ) . '/Reviews.hooks.php';
5151 $wgAutoloadClasses['ReviewsSettings'] = dirname( __FILE__ ) . '/Reviews.settings.php';
5252
 53+$wgAutoloadClasses['ApiDeleteReviews'] = dirname( __FILE__ ) . '/api/ApiDeleteReviews.php';
 54+$wgAutoloadClasses['ApiQueryReviews'] = dirname( __FILE__ ) . '/api/ApiQueryReviews.php';
 55+$wgAutoloadClasses['ApiReviewQuery'] = dirname( __FILE__ ) . '/api/ApiReviewQuery.php';
 56+$wgAutoloadClasses['ApiSubmitReview'] = dirname( __FILE__ ) . '/api/ApiSubmitReview.php';
5357
 58+$wgAutoloadClasses['Review'] = dirname( __FILE__ ) . '/includes/Review.php';
 59+$wgAutoloadClasses['ReviewPager'] = dirname( __FILE__ ) . '/includes/ReviewPager.php';
 60+$wgAutoloadClasses['ReviewRating'] = dirname( __FILE__ ) . '/includes/ReviewRating.php';
 61+$wgAutoloadClasses['ReviewsDBObject'] = dirname( __FILE__ ) . '/includes/ReviewsDBObject.php';
 62+
 63+$wgAutoloadClasses['SpecialMyReviews'] = dirname( __FILE__ ) . '/specials/SpecialMyReviews.php';
 64+$wgAutoloadClasses['SpecialReviews'] = dirname( __FILE__ ) . '/specials/SpecialReviews.php';
 65+
5466 // Special pages
5567 $wgSpecialPages['Reviews'] = 'SpecialReviews';
5668 $wgSpecialPages['MyReviews'] = 'SpecialMyReviews';
@@ -58,6 +70,9 @@
5971 $wgSpecialPageGroups['MyReviews'] = 'reviews';
6072
6173 // API
 74+$wgAPIModules['deletereviews'] = 'ApiDeleteReviews';
 75+$wgAPIModules['submitreview'] = 'ApiSubmitReview';
 76+$wgAPIListModules['reviews'] = 'ApiQueryReviews';
6277
6378 // Hooks
6479 $wgHooks['LoadExtensionSchemaUpdates'][] = 'ReviewsHooks::onSchemaUpdate';

Status & tagging log