Index: trunk/extensions/Reviews/includes/ReviewsTag.php |
— | — | @@ -48,7 +48,7 @@ |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** |
52 | | - * Renrder the survey div. |
| 52 | + * Renrder the reviews div. |
53 | 53 | * |
54 | 54 | * @since 0.1 |
55 | 55 | * |
— | — | @@ -109,18 +109,18 @@ |
110 | 110 | 'state' => array( Review::STATUS_NEW, Review::STATUS_REVIEWED ) |
111 | 111 | ); |
112 | 112 | |
113 | | - if ( $this->contents['id'] ) { |
114 | | - $conditions['id'] = $this->contents['id']; |
| 113 | + if ( $this->parameters['id'] ) { |
| 114 | + $conditions['id'] = $this->parameters['id']; |
115 | 115 | } |
116 | 116 | |
117 | | - if ( $this->contents['page'] ) { |
| 117 | + if ( $this->parameters['page'] ) { |
118 | 118 | $ids = array(); |
119 | 119 | |
120 | 120 | if ( $this->titleCondition !== false ) { |
121 | 121 | $ids[] = $this->titleCondition->getArticleID(); |
122 | 122 | } |
123 | 123 | |
124 | | - $title = Title::newFromText( $this->contents['page'] ); |
| 124 | + $title = Title::newFromText( $this->parameters['page'] ); |
125 | 125 | |
126 | 126 | if ( !is_null( $title ) ) { |
127 | 127 | $ids[] = $title->getArticleID(); |
— | — | @@ -134,8 +134,8 @@ |
135 | 135 | $conditions['page_id'] = $title->getArticleID(); |
136 | 136 | } |
137 | 137 | |
138 | | - if ( $this->contents['user'] ) { |
139 | | - $user = User::newFromName( $this->contents['user'] ); |
| 138 | + if ( $this->parameters['user'] ) { |
| 139 | + $user = User::newFromName( $this->parameters['user'] ); |
140 | 140 | |
141 | 141 | if ( $user !== false ) { |
142 | 142 | $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 @@ |
124 | 124 | * @since 0.1 |
125 | 125 | * |
126 | 126 | * @param ReviewRating $rating |
| 127 | + * @param integer|false $count |
127 | 128 | * |
128 | 129 | * @return string |
129 | 130 | */ |
130 | | - public static function getDisplayHTMLFor( ReviewRating $rating ) { |
| 131 | + public static function getDisplayHTMLFor( ReviewRating $rating, $count = false ) { |
131 | 132 | $attribs = array( |
132 | 133 | 'class' => 'review-rating-display', |
133 | 134 | 'data-value' => $rating->getField( 'value' ), |
— | — | @@ -136,6 +137,10 @@ |
137 | 138 | $attribs['data-type'] = $rating->getField( 'type' ); |
138 | 139 | } |
139 | 140 | |
| 141 | + if ( $count !== false ) { |
| 142 | + $attribs['data-count'] = $count; |
| 143 | + } |
| 144 | + |
140 | 145 | return Html::element( 'div', $attribs ); |
141 | 146 | } |
142 | 147 | |
Index: trunk/extensions/Reviews/Reviews.php |
— | — | @@ -60,6 +60,7 @@ |
61 | 61 | $wgAutoloadClasses['ReviewControl'] = dirname( __FILE__ ) . '/includes/ReviewControl.php'; |
62 | 62 | $wgAutoloadClasses['ReviewPager'] = dirname( __FILE__ ) . '/includes/ReviewPager.php'; |
63 | 63 | $wgAutoloadClasses['ReviewRating'] = dirname( __FILE__ ) . '/includes/ReviewRating.php'; |
| 64 | +$wgAutoloadClasses['ReviewRatingsTag'] = dirname( __FILE__ ) . '/includes/ReviewRatingsTag.php'; |
64 | 65 | $wgAutoloadClasses['ReviewsDBObject'] = dirname( __FILE__ ) . '/includes/ReviewsDBObject.php'; |
65 | 66 | $wgAutoloadClasses['ReviewsTag'] = dirname( __FILE__ ) . '/includes/ReviewsTag.php'; |
66 | 67 | |
Index: trunk/extensions/Reviews/Reviews.hooks.php |
— | — | @@ -207,6 +207,21 @@ |
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
| 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 | + /** |
211 | 226 | * Register the reviews tag extension when the parser initializes. |
212 | 227 | * |
213 | 228 | * @since 0.1 |
— | — | @@ -217,6 +232,7 @@ |
218 | 233 | */ |
219 | 234 | public static function onParserFirstCallInit( Parser &$parser ) { |
220 | 235 | $parser->setHook( 'reviews', __CLASS__ . '::onReviewsRender' ); |
| 236 | + $parser->setHook( 'review_ratings', __CLASS__ . '::onRatingsRender' ); |
221 | 237 | return true; |
222 | 238 | } |
223 | 239 | |