r97920 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97919‎ | r97920 | r97921 >
Date:14:49, 23 September 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
MoodBar: Add list=moodbarcomments API module, and add indexes to go with it
Modified paths:
  • /trunk/extensions/MoodBar/ApiQueryMoodBarComments.php (added) (history)
  • /trunk/extensions/MoodBar/MoodBar.php (modified) (history)
  • /trunk/extensions/MoodBar/sql/MoodBar.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/ApiQueryMoodBarComments.php
@@ -0,0 +1,160 @@
 2+<?php
 3+
 4+class ApiQueryMoodBarComments extends ApiQueryBase {
 5+ public function __construct( $query, $moduleName ) {
 6+ parent::__construct( $query, $moduleName, 'mbc' );
 7+ }
 8+
 9+ public function execute() {
 10+ $params = $this->extractRequestParams();
 11+
 12+ // Build the query
 13+ $this->addTables( array( 'moodbar_feedback', 'user' ) );
 14+ $this->addJoinConds( array( 'user' => array( 'LEFT JOIN', 'user_id=mbf_user_id' ) ) );
 15+ $this->addFields( array( 'user_name', 'mbf_id', 'mbf_type', 'mbf_timestamp', 'mbf_user_id', 'mbf_user_ip',
 16+ 'mbf_comment' ) );
 17+
 18+ $sortDesc = $params['dir'] == 'older';
 19+ $dir = $sortDesc ? ' DESC' : '';
 20+ $orderFields = array();
 21+ $useTypeFromContinue = false;
 22+ if ( count( $params['type'] ) ) {
 23+ $this->addWhereFld( 'mbf_type', $params['type'] );
 24+ if ( count( $params['type'] ) > 1 ) {
 25+ $orderFields[] = 'mbf_type' . $dir;
 26+ $useTypeFromContinue = true;
 27+ }
 28+ }
 29+ if ( $params['user'] !== null ) {
 30+ $user = User::newFromName( $params['user'] );
 31+ if ( !$user || $user->isAnon() ) {
 32+ $this->addWhereFld( 'mbf_user_id', 0 );
 33+ $this->addWhereFld( 'mbf_user_ip', $params['user'] );
 34+ } else {
 35+ $this->addWhereFld( 'mbf_user_id', $user->getID() );
 36+ $this->addWhere( 'mbf_user_ip IS NULL' );
 37+ }
 38+ }
 39+
 40+ if ( $params['continue'] !== null ) {
 41+ $this->applyContinue( $params['continue'], $sortDesc, $useTypeFromContinue );
 42+ }
 43+
 44+ $orderFields[] = 'mbf_timestamp' . $dir;
 45+ $this->addOption( 'ORDER BY', $orderFields );
 46+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 47+
 48+ $res = $this->select( __METHOD__ );
 49+ $result = $this->getResult();
 50+ $count = 0;
 51+ foreach ( $res as $row ) {
 52+ if ( ++$count > $params['limit'] ) {
 53+ // We've reached the one extra which shows that there are additional rows. Stop here
 54+ $this->setContinueEnumParameter( 'continue', $this->getContinue( $row ) );
 55+ break;
 56+ }
 57+
 58+ $vals = $this->extractRowInfo( $row );
 59+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
 60+ if ( !$fit ) {
 61+ $this->setContinueEnumParameter( 'continue', $this->getContinue( $row ) );
 62+ break;
 63+ }
 64+ }
 65+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'comment' );
 66+ }
 67+
 68+ protected function getContinue( $row ) {
 69+ $ts = wfTimestamp( TS_MW, $row->mbf_timestamp );
 70+ return "$ts|{$row->mbf_type}|{$row->mbf_id}";
 71+ }
 72+
 73+ protected function applyContinue( $continue, $sortDesc, $useType ) {
 74+ $vals = explode( '|', $continue, 4 );
 75+ if ( count( $vals ) !== 3 ) {
 76+ // TODO this should be a standard message in ApiBase
 77+ $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
 78+ }
 79+
 80+ $db = $this->getDB();
 81+ $ts = $db->addQuotes( $db->timestamp( $vals[0] ) );
 82+ $type = $db->addQuotes( $vals[1] );
 83+ $id = intval( $vals[2] );
 84+ $op = $sortDesc ? '<' : '>';
 85+ // TODO there should be a standard way to do this in DatabaseBase or ApiQueryBase something
 86+ if ( $useType ) {
 87+ $this->addWhere( "mbf_type $op $type OR " .
 88+ "(mbf_type = $type AND " .
 89+ "(mbf_timestamp $op $ts OR " .
 90+ "(mbf_timestamp = $ts AND " .
 91+ "mbf_id $op= $id)))"
 92+ );
 93+ } else {
 94+ $this->addWhere( "mbf_timestamp $op $ts OR " .
 95+ "(mbf_timestamp = $ts AND " .
 96+ "mbf_id $op= $id)"
 97+ );
 98+ }
 99+ }
 100+
 101+ protected function extractRowInfo( $row ) {
 102+ $r = array(
 103+ 'id' => intval( $row->mbf_id ),
 104+ 'type' => $row->mbf_type,
 105+ 'timestamp' => wfTimestamp( TS_ISO_8601, $row->mbf_timestamp ),
 106+ 'userid' => intval( $row->mbf_user_id ),
 107+ 'username' => $row->mbf_user_ip === null ? $row->user_name : $row->mbf_user_ip,
 108+ );
 109+ ApiResult::setContent( $r, $row->mbf_comment );
 110+ return $r;
 111+ }
 112+
 113+ public function getAllowedParams() {
 114+ return array(
 115+ 'limit' => array(
 116+ ApiBase::PARAM_DFLT => 10,
 117+ ApiBase::PARAM_TYPE => 'limit',
 118+ ApiBase::PARAM_MIN => 1,
 119+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 120+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 121+ ),
 122+ 'dir' => array(
 123+ ApiBase::PARAM_DFLT => 'older',
 124+ ApiBase::PARAM_TYPE => array(
 125+ 'newer',
 126+ 'older'
 127+ )
 128+ ),
 129+ 'continue' => null,
 130+ 'type' => array(
 131+ ApiBase::PARAM_TYPE => MBFeedbackItem::getValidTypes(),
 132+ ApiBase::PARAM_ISMULTI => true,
 133+ ApiBase::PARAM_DFLT => '', // all
 134+ ),
 135+ 'user' => array(
 136+ ApiBase::PARAM_TYPE => 'user',
 137+ ),
 138+ );
 139+ }
 140+
 141+ public function getVersion() {
 142+ return __CLASS__ . ': $Id$';
 143+ }
 144+
 145+ public function getParamDescription() {
 146+ return array(
 147+ 'limit' => 'How many comments to return',
 148+ 'continue' => 'When more results are available, use this to continue',
 149+ 'type' => 'Only return comments of the given type(s). If not set or empty, return all comments',
 150+ 'user' =>' Only return comments submitted by the given user',
 151+ );
 152+ }
 153+
 154+ public function getDescription() {
 155+ return 'List all feedback comments submitted through the MoodBar extension.';
 156+ }
 157+
 158+ public function getCacheMode( $params ) {
 159+ return 'public';
 160+ }
 161+}
Property changes on: trunk/extensions/MoodBar/ApiQueryMoodBarComments.php
___________________________________________________________________
Added: svn:eol-style
1162 + native
Added: svn:keywords
2163 + Id
Index: trunk/extensions/MoodBar/sql/MoodBar.sql
@@ -23,8 +23,12 @@
2424 mbf_locale varchar(32) binary null, -- The locale of the user's browser
2525 mbf_editing tinyint unsigned not null, -- Whether or not the user was editing
2626 mbf_bucket varchar(128) binary null -- Bucket, for A/B testing
27 -) /*$wgDBtableOptions*/;
 27+) /*$wgDBTableOptions*/;
2828
2929 -- A little overboard with the indexes perhaps, but we want to be able to dice this data a lot!
3030 CREATE INDEX /*i*/type_timestamp ON /*_*/moodbar_feedback (mbf_type,mbf_timestamp);
3131 CREATE INDEX /*i*/title_type ON /*_*/moodbar_feedback (mbf_namespace,mbf_title,mbf_type,mbf_timestamp);
 32+-- CREATE INDEX /*i*/mbf_namespace_title_timestamp ON /*_*/moodbar_feedback (mbf_namespace, mbf_title, mbf_timestamp); --maybe in the future if we actually do per-page filtering
 33+CREATE INDEX /*i*/mbf_userid_ip_timestamp ON /*_*/moodbar_feedback (mbf_user_id, mbf_user_ip, mbf_timestamp);
 34+CREATE INDEX /*i*/mbf_type_userid_ip_timestamp ON /*_*/moodbar_feedback (mbf_type, mbf_user_id, mbf_user_ip, mbf_timestamp);
 35+CREATE INDEX /*i*/mbf_timestamp ON /*_*/moodbar_feedback (mbf_timestamp);
Index: trunk/extensions/MoodBar/MoodBar.php
@@ -20,6 +20,8 @@
2121 // API
2222 $wgAutoloadClasses['ApiMoodBar'] = dirname(__FILE__).'/ApiMoodBar.php';
2323 $wgAPIModules['moodbar'] = 'ApiMoodBar';
 24+$wgAutoloadClasses['ApiQueryMoodBarComments'] = dirname( __FILE__ ). '/ApiQueryMoodBarComments.php';
 25+$wgAPIListModules['moodbarcomments'] = 'ApiQueryMoodBarComments';
2426
2527 // Hooks
2628 $wgAutoloadClasses['MoodBarHooks'] = dirname(__FILE__).'/MoodBar.hooks.php';

Sign-offs

UserFlagDate
Krinkleinspected20:56, 24 September 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r97921Add updater for r97920catrope14:52, 23 September 2011

Comments

#Comment by Krinkle (talk | contribs)   20:24, 24 September 2011

This module should check user right " moodbar-view" (like the SpecialPage does), right ?

#Comment by Catrope (talk | contribs)   20:25, 24 September 2011

No. moodbar-view controls viewing all of the data, this only exposes some of the fields.

#Comment by Krinkle (talk | contribs)   20:56, 24 September 2011

OK. Didn't see that, just checking :)

Status & tagging log