r105391 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105390‎ | r105391 | r105392 >
Date:00:29, 7 December 2011
Author:jeroendedauw
Status:reverted
Tags:
Comment:
review_ratings tag
Modified paths:
  • /trunk/extensions/Reviews/Reviews.hooks.php (modified) (history)
  • /trunk/extensions/Reviews/Reviews.php (modified) (history)
  • /trunk/extensions/Reviews/includes/ReviewRating.php (modified) (history)
  • /trunk/extensions/Reviews/includes/ReviewRatingsTag.php (added) (history)
  • /trunk/extensions/Reviews/includes/ReviewsTag.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Reviews/includes/ReviewsTag.php
@@ -48,7 +48,7 @@
4949 }
5050
5151 /**
52 - * Renrder the survey div.
 52+ * Renrder the reviews div.
5353 *
5454 * @since 0.1
5555 *
@@ -109,18 +109,18 @@
110110 'state' => array( Review::STATUS_NEW, Review::STATUS_REVIEWED )
111111 );
112112
113 - if ( $this->contents['id'] ) {
114 - $conditions['id'] = $this->contents['id'];
 113+ if ( $this->parameters['id'] ) {
 114+ $conditions['id'] = $this->parameters['id'];
115115 }
116116
117 - if ( $this->contents['page'] ) {
 117+ if ( $this->parameters['page'] ) {
118118 $ids = array();
119119
120120 if ( $this->titleCondition !== false ) {
121121 $ids[] = $this->titleCondition->getArticleID();
122122 }
123123
124 - $title = Title::newFromText( $this->contents['page'] );
 124+ $title = Title::newFromText( $this->parameters['page'] );
125125
126126 if ( !is_null( $title ) ) {
127127 $ids[] = $title->getArticleID();
@@ -134,8 +134,8 @@
135135 $conditions['page_id'] = $title->getArticleID();
136136 }
137137
138 - if ( $this->contents['user'] ) {
139 - $user = User::newFromName( $this->contents['user'] );
 138+ if ( $this->parameters['user'] ) {
 139+ $user = User::newFromName( $this->parameters['user'] );
140140
141141 if ( $user !== false ) {
142142 $conditions['user_id'] = $user->getId();
Index: trunk/extensions/Reviews/includes/ReviewRatingsTag.php
@@ -0,0 +1,153 @@
 2+<?php
 3+
 4+/**
 5+ * Class to render ratings tags.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file ReviewRatingsTag.php
 10+ * @ingroup Reviews
 11+ *
 12+ * @licence GNU GPL v3+
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class ReviewRatingsTag {
 16+
 17+ /**
 18+ * List of review parameters.
 19+ *
 20+ * @since 0.1
 21+ *
 22+ * @var array
 23+ */
 24+ protected $parameters;
 25+
 26+ protected $contents;
 27+
 28+ /**
 29+ * Constructor.
 30+ *
 31+ * @since 0.1
 32+ *
 33+ * @param array $args
 34+ * @param string|null $contents
 35+ */
 36+ public function __construct( array $args = array(), $contents = null ) {
 37+ $this->contents = $contents;
 38+
 39+ $args = filter_var_array( $args, $this->getTagParameters() );
 40+
 41+ if ( is_array( $args ) ) {
 42+ $this->parameters = $args;
 43+ } else {
 44+ // TODO: nicer handling
 45+ throw new MWException( 'Invalid parameters for reviews tag.' );
 46+ }
 47+ }
 48+
 49+ /**
 50+ * Renrder the ratings div.
 51+ *
 52+ * @since 0.1
 53+ *
 54+ * @param Parser $parser
 55+ *
 56+ * @return string
 57+ */
 58+ public function render( Parser $parser ) {
 59+ $ratings = $this->getRatings( $parser->getTitle() );
 60+
 61+ $count = count( $ratings );
 62+ $sum = array_sum( $ratings );
 63+
 64+ $rating = new ReviewRating( array(
 65+ 'value' => $sum / $count
 66+ ) );
 67+
 68+ return ReviewRating::getDisplayHTMLFor( $rating, $count );
 69+ }
 70+
 71+ /**
 72+ *
 73+ *
 74+ * @since 0.1
 75+ *
 76+ * @param Title $title
 77+ *
 78+ * @return array of floats
 79+ */
 80+ protected function getRatings( Title $title ) {
 81+ $conditions = array();
 82+
 83+ $titleArg = null;
 84+
 85+ if ( $this->parameters['page'] ) {
 86+ $titleArg = Title::newFromText( $this->parameters['page'] );
 87+ }
 88+
 89+ if ( is_null( $titleArg ) ) {
 90+ $titleArg = $title;
 91+ }
 92+
 93+ $conditions[Review::getPrefixedField( 'page_id' )] = $title->getArticleID();
 94+
 95+ if ( $this->parameters['user'] ) {
 96+ $user = User::newFromName( $this->parameters['user'] );
 97+
 98+ if ( $user !== false ) {
 99+ $conditions[Review::getPrefixedField( 'user_id' )] = $user->getId();
 100+ }
 101+ }
 102+
 103+ $hasType = (bool)$this->parameters['type'];
 104+ $dbr = wfGetDB( DB_SLAVE );
 105+
 106+ if ( $hasType ) {
 107+ $conditions[ReviewRating::getPrefixedField( 'type' )] = $this->parameters['type'];
 108+
 109+ $result = $dbr->select(
 110+ array( 'review_ratings', 'reviews' ),
 111+ array( 'rating_value' ),
 112+ $conditions,
 113+ __METHOD__,
 114+ array(),
 115+ array(
 116+ 'reviews' => array( 'INNER JOIN', array( 'review_id=rating_review_id' ) ),
 117+ )
 118+ );
 119+ }
 120+ else {
 121+ $result = $dbr->select(
 122+ 'reviews',
 123+ array( 'review_rating' ),
 124+ $conditions
 125+ );
 126+ }
 127+
 128+ $results = array();
 129+
 130+ foreach ( $result as $item ) {
 131+ $results[] = $hasType ? (float)$item->rating_value : (float)$item->review_rating;
 132+ }
 133+
 134+ return $results;
 135+ }
 136+
 137+ /**
 138+ * Gets the parameters accepted by this tag extension.
 139+ *
 140+ * @since 0.1
 141+ *
 142+ * @param array $args
 143+ *
 144+ * @return array
 145+ */
 146+ protected function getTagParameters() {
 147+ return array(
 148+ 'page' => array(),
 149+ 'user' => array(),
 150+ 'type' => array(),
 151+ );
 152+ }
 153+
 154+}
Index: trunk/extensions/Reviews/includes/ReviewRating.php
@@ -123,10 +123,11 @@
124124 * @since 0.1
125125 *
126126 * @param ReviewRating $rating
 127+ * @param integer|false $count
127128 *
128129 * @return string
129130 */
130 - public static function getDisplayHTMLFor( ReviewRating $rating ) {
 131+ public static function getDisplayHTMLFor( ReviewRating $rating, $count = false ) {
131132 $attribs = array(
132133 'class' => 'review-rating-display',
133134 'data-value' => $rating->getField( 'value' ),
@@ -136,6 +137,10 @@
137138 $attribs['data-type'] = $rating->getField( 'type' );
138139 }
139140
 141+ if ( $count !== false ) {
 142+ $attribs['data-count'] = $count;
 143+ }
 144+
140145 return Html::element( 'div', $attribs );
141146 }
142147
Index: trunk/extensions/Reviews/Reviews.php
@@ -60,6 +60,7 @@
6161 $wgAutoloadClasses['ReviewControl'] = dirname( __FILE__ ) . '/includes/ReviewControl.php';
6262 $wgAutoloadClasses['ReviewPager'] = dirname( __FILE__ ) . '/includes/ReviewPager.php';
6363 $wgAutoloadClasses['ReviewRating'] = dirname( __FILE__ ) . '/includes/ReviewRating.php';
 64+$wgAutoloadClasses['ReviewRatingsTag'] = dirname( __FILE__ ) . '/includes/ReviewRatingsTag.php';
6465 $wgAutoloadClasses['ReviewsDBObject'] = dirname( __FILE__ ) . '/includes/ReviewsDBObject.php';
6566 $wgAutoloadClasses['ReviewsTag'] = dirname( __FILE__ ) . '/includes/ReviewsTag.php';
6667
Index: trunk/extensions/Reviews/Reviews.hooks.php
@@ -207,6 +207,21 @@
208208 }
209209
210210 /**
 211+ * Render the reviews tag.
 212+ *
 213+ * @since 0.1
 214+ *
 215+ * @param mixed $input
 216+ * @param array $args
 217+ * @param Parser $parser
 218+ * @param PPFrame $frame
 219+ */
 220+ public static function onRatingsRender( $input, array $args, Parser $parser, PPFrame $frame ) {
 221+ $tag = new ReviewRatingsTag( $args, $input );
 222+ return $tag->render( $parser );
 223+ }
 224+
 225+ /**
211226 * Register the reviews tag extension when the parser initializes.
212227 *
213228 * @since 0.1
@@ -217,6 +232,7 @@
218233 */
219234 public static function onParserFirstCallInit( Parser &$parser ) {
220235 $parser->setHook( 'reviews', __CLASS__ . '::onReviewsRender' );
 236+ $parser->setHook( 'review_ratings', __CLASS__ . '::onRatingsRender' );
221237 return true;
222238 }
223239

Status & tagging log