r77652 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77651‎ | r77652 | r77653 >
Date:13:51, 3 December 2010
Author:catrope
Status:ok
Tags:
Comment:
Part 2 of r77651 copy the API files from the old ArticleAssessmentPilot directory, preserving history this time
Modified paths:
  • /trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php (added) (history)
  • /trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php (added) (history)

Diff [purge]

Index: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
@@ -0,0 +1,238 @@
 2+<?php
 3+class ApiArticleAssessment extends ApiBase {
 4+ public function __construct( $query, $moduleName ) {
 5+ parent::__construct( $query, $moduleName, '' );
 6+ }
 7+
 8+ public function execute() {
 9+ global $wgUser, $wgArticleAssessmentRatings;
 10+ $params = $this->extractRequestParams();
 11+
 12+ $token = array();
 13+ if ( $wgUser->isAnon() ) {
 14+ if ( !isset( $params['anontoken'] ) ) {
 15+ $this->dieUsageMsg( array( 'missingparam', 'anontoken' ) );
 16+ } elseif ( strlen( $params['anontoken'] ) != 32 ) {
 17+ $this->dieUsage( 'The anontoken is not 32 characters', 'invalidtoken' );
 18+ }
 19+
 20+ $token['aa_user_anon_token'] = $params['anontoken'];
 21+ }
 22+
 23+ $dbr = wfGetDB( DB_SLAVE );
 24+
 25+ // Query the latest ratings by this user for this page,
 26+ // possibly for an older revision
 27+ $res = $dbr->select(
 28+ 'article_assessment',
 29+ array( 'aa_rating_id', 'aa_rating_value', 'aa_revision' ),
 30+ array_merge(
 31+ array(
 32+ 'aa_user_id' => $wgUser->getId(),
 33+ 'aa_user_text' => $wgUser->getName(),
 34+ 'aa_page_id' => $params['pageid'],
 35+ 'aa_rating_id' => $wgArticleAssessmentRatings,
 36+ ),
 37+ $token
 38+ ),
 39+ __METHOD__,
 40+ array(
 41+ 'ORDER BY' => 'aa_revision DESC',
 42+ 'LIMIT' => count( $wgArticleAssessmentRatings ),
 43+ )
 44+ );
 45+
 46+ $lastRatings = array();
 47+
 48+ foreach ( $res as $row ) {
 49+ $lastRatings[$row->aa_rating_id] = $row->aa_rating_value;
 50+ }
 51+
 52+ $pageId = $params['pageid'];
 53+ $revisionId = $params['revid'];
 54+
 55+ foreach( $wgArticleAssessmentRatings as $rating ) {
 56+ $lastRating = false;
 57+ if ( isset( $lastRatings[$rating] ) ) {
 58+ $lastRating = $lastRatings[$rating];
 59+ }
 60+
 61+ $thisRating = false;
 62+ if ( isset( $params["r{$rating}"] ) ) {
 63+ $thisRating = $params["r{$rating}"];
 64+ }
 65+
 66+ $this->insertPageRating( $pageId, $rating, ( $thisRating - $lastRating ),
 67+ ( $lastRating === false && $thisRating !== false )
 68+ );
 69+
 70+ $this->insertUserRatings( $pageId, $revisionId, $wgUser, $token, $rating, $thisRating );
 71+ }
 72+
 73+ $r = array( 'result' => 'Success' );
 74+ $this->getResult()->addValue( null, $this->getModuleName(), $r );
 75+ }
 76+
 77+ /**
 78+ * Inserts (or Updates, where appropriate) the aggregate page rating
 79+ *
 80+ * @param $pageId Integer: Page Id
 81+ * @param $ratingId Integer: Rating Id
 82+ * @param $updateAddition Integer: Difference between user's last rating (if applicable)
 83+ * @param $newRating Boolean: Whether this is a new rating (for update, whether this increases the count)
 84+ */
 85+ private function insertPageRating( $pageId, $ratingId, $updateAddition, $newRating ) {
 86+ $dbw = wfGetDB( DB_MASTER );
 87+
 88+ $dbw->insert(
 89+ 'article_assessment_pages',
 90+ array(
 91+ 'aap_page_id' => $pageId,
 92+ 'aap_total' => 0,
 93+ 'aap_count' => 0,
 94+ 'aap_rating_id' => $ratingId,
 95+ ),
 96+ __METHOD__,
 97+ array( 'IGNORE' )
 98+ );
 99+
 100+ $dbw->update(
 101+ 'article_assessment_pages',
 102+ array(
 103+ 'aap_total = aap_total + ' . $updateAddition,
 104+ 'aap_count = aap_count + ' . ( $newRating ? 1 : 0 ),
 105+ ),
 106+ array(
 107+ 'aap_page_id' => $pageId,
 108+ 'aap_rating_id' => $ratingId,
 109+ ),
 110+ __METHOD__
 111+ );
 112+ }
 113+
 114+ /**
 115+ * Inserts (or Updates, where appropriate) the users ratings for a specific revision
 116+ *
 117+ * @param $pageId Integer: Page Id
 118+ * @param $revisionId Integer: Revision Id
 119+ * @param $user User: Current User object
 120+ * @param $token Array: Token if necessary
 121+ * @param $ratingId Integer: Rating Id
 122+ * @param $ratingValue Integer: Value of the Rating
 123+ */
 124+ private function insertUserRatings( $pageId, $revisionId, $user, $token, $ratingId, $ratingValue ) {
 125+ $dbw = wfGetDB( DB_MASTER );
 126+
 127+ $timestamp = $dbw->timestamp();
 128+
 129+ $dbw->insert(
 130+ 'article_assessment',
 131+ array_merge(
 132+ array(
 133+ 'aa_page_id' => $pageId,
 134+ 'aa_user_id' => $user->getId(),
 135+ 'aa_user_text' => $user->getName(),
 136+ 'aa_revision' => $revisionId,
 137+ 'aa_timestamp' => $timestamp,
 138+ 'aa_rating_id' => $ratingId,
 139+ 'aa_rating_value' => $ratingValue,
 140+ ),
 141+ $token
 142+ ),
 143+ __METHOD__,
 144+ array( 'IGNORE' )
 145+ );
 146+
 147+ if ( !$dbw->affectedRows() ) {
 148+ $dbw->update(
 149+ 'article_assessment',
 150+ array(
 151+ 'aa_timestamp' => $timestamp,
 152+ 'aa_rating_value' => $ratingValue,
 153+ ),
 154+ array_merge(
 155+ array(
 156+ 'aa_page_id' => $pageId,
 157+ 'aa_user_text' => $user->getName(),
 158+ 'aa_revision' => $revisionId,
 159+ 'aa_rating_id' => $ratingId,
 160+ ),
 161+ $token
 162+ ),
 163+ __METHOD__
 164+ );
 165+ }
 166+ }
 167+
 168+ public function getAllowedParams() {
 169+ global $wgArticleAssessmentRatings;
 170+ $ret = array(
 171+ 'pageid' => array(
 172+ ApiBase::PARAM_TYPE => 'integer',
 173+ ApiBase::PARAM_REQUIRED => true,
 174+ ApiBase::PARAM_ISMULTI => false,
 175+ ),
 176+ 'revid' => array(
 177+ ApiBase::PARAM_TYPE => 'integer',
 178+ ApiBase::PARAM_REQUIRED => true,
 179+ ApiBase::PARAM_ISMULTI => false,
 180+ ),
 181+ 'anontoken' => null,
 182+ );
 183+
 184+ foreach( $wgArticleAssessmentRatings as $rating ) {
 185+ $ret["r{$rating}"] = array(
 186+ ApiBase::PARAM_TYPE => 'limit',
 187+ ApiBase::PARAM_DFLT => 0,
 188+ ApiBase::PARAM_MIN => 0,
 189+ ApiBase::PARAM_MAX => 5,
 190+ ApiBase::PARAM_MAX2 => 5,
 191+ );
 192+ }
 193+ return $ret;
 194+ }
 195+
 196+ public function getParamDescription() {
 197+ global $wgArticleAssessmentRatings;
 198+ $ret = array(
 199+ 'pageid' => 'Page ID to submit assessment for',
 200+ 'revid' => 'Revision ID to submit assessment for',
 201+ 'anontoken' => 'Token for anonymous users',
 202+ );
 203+ foreach( $wgArticleAssessmentRatings as $rating ) {
 204+ $ret["r{$rating}"] = "Rating {$rating}";
 205+ }
 206+ return $ret;
 207+ }
 208+
 209+ public function getDescription() {
 210+ return array(
 211+ 'Submit article assessments'
 212+ );
 213+ }
 214+
 215+ public function mustBePosted() {
 216+ return true;
 217+ }
 218+
 219+ public function isWriteMode() {
 220+ return true;
 221+ }
 222+
 223+ public function getPossibleErrors() {
 224+ return array_merge( parent::getPossibleErrors(), array(
 225+ array( 'missingparam', 'anontoken' ),
 226+ array( 'code' => 'invalidtoken', 'info' => 'The anontoken is not 32 characters' ),
 227+ ) );
 228+ }
 229+
 230+ protected function getExamples() {
 231+ return array(
 232+ 'api.php?action=articleassessment'
 233+ );
 234+ }
 235+
 236+ public function getVersion() {
 237+ return __CLASS__ . ': $Id$';
 238+ }
 239+}
\ No newline at end of file
Property changes on: trunk/extensions/ArticleFeedback/api/ApiArticleFeedback.php
___________________________________________________________________
Added: svn:eol-style
1240 + native
Added: svn:keywords
2241 + Id
Index: trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php
@@ -0,0 +1,171 @@
 2+<?php
 3+class ApiQueryArticleAssessment extends ApiQueryBase {
 4+ public function __construct( $query, $moduleName ) {
 5+ parent::__construct( $query, $moduleName, 'aa' );
 6+ }
 7+
 8+ public function execute() {
 9+ global $wgArticleAssessmentRatings;
 10+ $params = $this->extractRequestParams();
 11+
 12+ $result = $this->getResult();
 13+
 14+ $this->addTables( array( 'article_assessment_pages', 'article_assessment_ratings' ) );
 15+
 16+ $this->addFields( array( 'aap_page_id', 'aap_total', 'aap_count', 'aap_rating_id', 'aar_rating' ) );
 17+
 18+ $this->addJoinConds( array(
 19+ 'article_assessment_ratings' => array( 'LEFT JOIN', array(
 20+ 'aar_id=aap_rating_id',
 21+ 'aap_rating_id' => $wgArticleAssessmentRatings,
 22+ )
 23+ ),
 24+ ) );
 25+
 26+ $this->addWhereFld( 'aap_page_id', $params['pageid'] );
 27+
 28+ if ( $params['userrating'] ) {
 29+ global $wgUser;
 30+
 31+ $leftJoinConds = array(
 32+ 'aa_page_id=aap_page_id',
 33+ 'aa_rating_id=aap_rating_id',
 34+ 'aa_user_id=' . $wgUser->getId() );
 35+
 36+ if ( $wgUser->isAnon() ) {
 37+ if ( !isset( $params['anontoken'] ) ) {
 38+ $this->dieUsageMsg( array( 'missingparam', 'anontoken' ) );
 39+ } elseif ( strlen( $params['anontoken'] ) != 32 ) {
 40+ $this->dieUsage( 'The anontoken is not 32 characters', 'invalidtoken' );
 41+ }
 42+
 43+ $leftJoinConds['aa_user_anon_token'] = $params['anontoken'];
 44+ }
 45+
 46+ $this->addTables( 'article_assessment' );
 47+ $this->addJoinConds( array(
 48+ 'article_assessment' => array( 'LEFT JOIN', $leftJoinConds ),
 49+ )
 50+ );
 51+
 52+ $this->addFields( array( 'aa_rating_value', 'aa_revision' ) );
 53+
 54+ $this->addOption( 'ORDER BY', 'aa_revision DESC' );
 55+ }
 56+
 57+ $this->addOption( 'LIMIT', count( $wgArticleAssessmentRatings ) );
 58+
 59+ $res = $this->select( __METHOD__ );
 60+
 61+ $ratings = array();
 62+
 63+ $userRatedArticle = false;
 64+
 65+ foreach ( $res as $row ) {
 66+ $pageId = $row->aap_page_id;
 67+
 68+ if ( !isset( $ratings[$pageId] ) ) {
 69+ $page = array(
 70+ 'pageid' => $pageId,
 71+ );
 72+
 73+ if ( $params['userrating'] ) {
 74+ $page['revid'] = $row->aa_revision;
 75+ }
 76+
 77+ $ratings[$pageId] = $page;
 78+ }
 79+
 80+ $thisRow = array(
 81+ 'ratingid' => $row->aap_rating_id,
 82+ 'ratingdesc' => $row->aar_rating,
 83+ 'total' => $row->aap_total,
 84+ 'count' => $row->aap_count,
 85+ );
 86+
 87+ if ( $params['userrating'] && !is_null( $row->aa_rating_value ) ) {
 88+ $thisRow['userrating'] = $row->aa_rating_value;
 89+
 90+ $userRatedArticle = true;
 91+ }
 92+
 93+ $ratings[$pageId]['ratings'][] = $thisRow;
 94+ }
 95+
 96+ //Only can actually be "stale" if the user has rated the article before
 97+ if ( $params['userrating'] && $userRatedArticle ) {
 98+ $dbr = wfGetDb( DB_SLAVE );
 99+
 100+ global $wgArticleAssessmentStaleCount;
 101+
 102+ $res = $dbr->select(
 103+ 'revision',
 104+ 'rev_id',
 105+ array(
 106+ 'rev_page' => $params['pageid'],
 107+ 'rev_id > ' . $ratings[$pageId]['revid']
 108+ ),
 109+ __METHOD__,
 110+ array ( 'LIMIT', $wgArticleAssessmentStaleCount + 1 )
 111+ );
 112+
 113+ if ( $res && $dbr->numRows( $res ) > $wgArticleAssessmentStaleCount ) {
 114+ //it's stale!
 115+ $ratings[$params['pageid']]['stale'] = '';
 116+ }
 117+ }
 118+
 119+ foreach ( $ratings as $rat ) {
 120+ $result->setIndexedTagName( $rat['ratings'], 'r' );
 121+ $result->addValue( array( 'query', $this->getModuleName() ), null, $rat );
 122+ }
 123+
 124+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'aa' );
 125+ }
 126+
 127+ public function getAllowedParams() {
 128+ return array(
 129+ 'pageid' => array(
 130+ ApiBase::PARAM_REQUIRED => true,
 131+ ApiBase::PARAM_ISMULTI => false,
 132+ ApiBase::PARAM_TYPE => 'integer',
 133+ ),
 134+ 'userrating' => false,
 135+ 'anontoken' => null,
 136+ );
 137+ }
 138+
 139+ public function getParamDescription() {
 140+ return array(
 141+ 'pageid' => 'Page ID to get assessments for',
 142+ 'userrating' => "Whether to get the current user's ratings for the specific rev/article",
 143+ 'anontoken' => 'Token for anonymous users',
 144+ );
 145+ }
 146+
 147+ public function getDescription() {
 148+ return array(
 149+ 'List all article assessments'
 150+ );
 151+ }
 152+
 153+ public function getPossibleErrors() {
 154+ return array_merge( parent::getPossibleErrors(), array(
 155+ array( 'missingparam', 'anontoken' ),
 156+ array( 'code' => 'invalidtoken', 'info' => 'The anontoken is not 32 characters' ),
 157+ )
 158+ );
 159+ }
 160+
 161+ protected function getExamples() {
 162+ return array(
 163+ 'api.php?action=query&list=articleassessment',
 164+ 'api.php?action=query&list=articleassessment&aapageid=1',
 165+ 'api.php?action=query&list=articleassessment&aapageid=1&aauserrating',
 166+ );
 167+ }
 168+
 169+ public function getVersion() {
 170+ return __CLASS__ . ': $Id$';
 171+ }
 172+}
\ No newline at end of file
Property changes on: trunk/extensions/ArticleFeedback/api/ApiQueryArticleFeedback.php
___________________________________________________________________
Added: svn:eol-style
1173 + native
Added: svn:keywords
2174 + Id

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r77651First step towards cleaning up r76961, which moved files without preserving h...catrope13:49, 3 December 2010

Status & tagging log