r100243 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100242‎ | r100243 | r100244 >
Date:17:35, 19 October 2011
Author:nikerabbit
Status:resolved (Comments)
Tags:
Comment:
Api module for marking translations as reviewed.
Internationalization/#128
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/api/ApiTranslationReview.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -85,6 +85,8 @@
8686 $wgAPIListModules['messagecollection'] = 'ApiQueryMessageCollection';
8787 $wgAPIMetaModules['messagegroups'] = 'ApiQueryMessageGroups';
8888 $wgAPIMetaModules['messagetranslations'] = 'ApiQueryMessageTranslations';
 89+$wgAPIModules['translationreview'] = 'ApiTranslationReview';
 90+$wgHooks['APIQueryInfoTokens'][] = 'ApiTranslationReview::injectTokenFunction';
8991
9092 // Register hooks.
9193 $wgHooks['EditPage::showEditForm:initial'][] = 'TranslateEditAddons::addTools';
Index: trunk/extensions/Translate/_autoload.php
@@ -193,4 +193,5 @@
194194 $wgAutoloadClasses['ApiQueryMessageCollection'] = $dir . 'api/ApiQueryMessageCollection.php';
195195 $wgAutoloadClasses['ApiQueryMessageGroups'] = $dir . 'api/ApiQueryMessageGroups.php';
196196 $wgAutoloadClasses['ApiQueryMessageTranslations'] = $dir . 'api/ApiQueryMessageTranslations.php';
 197+$wgAutoloadClasses['ApiTranslationReview'] = $dir . 'api/ApiTranslationReview.php';
197198 /**@}*/
Index: trunk/extensions/Translate/api/ApiTranslationReview.php
@@ -0,0 +1,132 @@
 2+<?php
 3+/**
 4+ * API module for marking translations as reviewed
 5+ * @file
 6+ * @author Niklas Laxström
 7+ * @copyright Copyright © 2011, Niklas Laxström
 8+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 9+ */
 10+
 11+/**
 12+ * API module for marking translations as reviewed
 13+ *
 14+ * @ingroup API TranslateAPI
 15+ */
 16+class ApiTranslationReview extends ApiBase {
 17+
 18+ public function execute() {
 19+ global $wgUser;
 20+ if ( !$wgUser->isallowed( 'translate-messagereview' ) ) {
 21+ $this->dieUsageMsg( 'permissiondenied' );
 22+ }
 23+
 24+ $params = $this->extractRequestParams();
 25+
 26+ $revision = Revision::newFromId( $params['revision'] );
 27+ if ( !$revision ) {
 28+ $this->dieUsage( 'You made me sad :(', 'invalidrevision' );
 29+ }
 30+
 31+ $title = $revision->getTitle();
 32+ $handle = new MessageHandle( $title );
 33+ if ( !$handle->isValid() ) {
 34+ $this->dieUsage( 'You made me confused :X', 'unknownmessage' );
 35+ }
 36+
 37+ $dbw = wfGetDB( DB_MASTER );
 38+ $table = 'translate_reviews';
 39+ $row = array(
 40+ 'trr_user' => $wgUser->getId(),
 41+ 'trr_page' => $revision->getPage(),
 42+ 'trr_revision' => $revision->getId(),
 43+ );
 44+ $options = array( 'IGNORE' );
 45+ $res = $dbw->insert( $table, $row, __METHOD__, $options );
 46+ if ( !$dbw->affectedRows() ) {
 47+ $this->setWarning( 'Already marked as reviewed by you' );
 48+ }
 49+
 50+ $output = array( 'review' => array(
 51+ 'title' => $title->getPrefixedText(),
 52+ 'pageid' => $revision->getPage(),
 53+ 'revision' => $revision->getId()
 54+ ) );
 55+
 56+ $this->getResult()->addValue( null, $this->getModuleName(), $output );
 57+ }
 58+
 59+ public function isWriteMode() {
 60+ return true;
 61+ }
 62+
 63+ public function needsToken() {
 64+ return true;
 65+ }
 66+
 67+ public function getTokenSalt() {
 68+ return 'translate-messagereview';
 69+ }
 70+
 71+ public function getAllowedParams() {
 72+ return array(
 73+ 'revision' => array(
 74+ ApiBase::PARAM_TYPE => 'integer',
 75+ ApiBase::PARAM_REQUIRED => true,
 76+ ),
 77+ 'token' => array(
 78+ ApiBase::PARAM_TYPE => 'string',
 79+ ApiBase::PARAM_REQUIRED => true,
 80+ ),
 81+ );
 82+ }
 83+
 84+ public function getParamDescription() {
 85+ return array(
 86+ 'revision' => 'The revision number to review',
 87+ 'token' => 'A token previously acquired with prop=token&intoken=translationreview',
 88+ );
 89+ }
 90+
 91+ public function getDescription() {
 92+ return 'Mark translations reviewed';
 93+ }
 94+
 95+ public function getPossibleErrors() {
 96+ return array_merge( parent::getPossibleErrors(), array(
 97+ array( 'code' => 'permissiondenied', 'info' => 'You must have translate-messagereview right' ),
 98+ array( 'code' => 'unknownmessage', 'info' => 'Title $1 does not belong to a message group' ),
 99+ array( 'code' => 'invalidrevision', 'info' => 'Revision $1 is invalid' ),
 100+ ) );
 101+ }
 102+
 103+ public function getExamples() {
 104+ return array(
 105+ 'api.php?action=translationreview&revision=1&token=foo',
 106+ );
 107+ }
 108+
 109+ public function getVersion() {
 110+ return __CLASS__ . ': $Id$';
 111+ }
 112+
 113+ public static function getToken( $pageid, $title ) {
 114+ global $wgUser;
 115+ if ( !$wgUser->isAllowed( 'translate-messagereview' ) ) {
 116+ return false;
 117+ }
 118+
 119+ static $cachedToken = null;
 120+ if ( !is_null( $cachedToken ) ) {
 121+ return $cachedToken;
 122+ }
 123+
 124+ $cachedToken = $wgUser->editToken( 'translate-messagereview' );
 125+ return $cachedToken;
 126+ }
 127+
 128+ public static function injectTokenFunction( &$list ) {
 129+ $list['translationreview'] = array( __CLASS__, 'getToken' );
 130+ return true; // Hooks must return bool
 131+ }
 132+
 133+}
Property changes on: trunk/extensions/Translate/api/ApiTranslationReview.php
___________________________________________________________________
Added: svn:eol-style
1134 + native
Added: svn:keywords
2135 + Id

Follow-up revisions

RevisionCommit summaryAuthorDate
r100346Followup r100243: prevent reviewing fuzzy messages...nikerabbit14:05, 20 October 2011
r100585Better error messages and corrected param descriptionnikerabbit08:51, 24 October 2011

Comments

#Comment by Siebrand (talk | contribs)   17:51, 19 October 2011

Will this API module return English text to clients ("you have already reviewed this message")? If so, how will that be i18n-ed?

#Comment by Nikerabbit (talk | contribs)   18:43, 19 October 2011

Api isn't i18ned. It will give error codes that clients can handle.

#Comment by Siebrand (talk | contribs)   18:56, 19 October 2011

Just so it get it... When there is a JavaScript component that uses this API in the Translate UI, then messages in JavaScript have to be mapped against the error codes permissiondenied/unknownmessage/invalidrevision?

#Comment by Siebrand (talk | contribs)   18:57, 19 October 2011

Just so it get it... -> Just so I get it...

#Comment by Krinkle (talk | contribs)   19:02, 19 October 2011

It's not an ideal situation but that's how it's done so far. We could create a convention that every error code should have an entry in i18n that modules can use (at least for the plain English one).

For example, now several gadgets, extensions as well as core features duplicate messages for generic errors that the API might throw (the kind of errors that aren't module-specific). Having generic messages for "ajax request failed", "api error foo bar", ... would be nice.

Back to the present, yeah. Create whatever messages needed. Usually this means not informing the user about what exactly went wrong (in most cases it's not of interest wether the request failed because of a missing edittoken or because the request required POST)

#Comment by Amire80 (talk | contribs)   10:20, 24 October 2011

Error message wording improved in r100585.

Status & tagging log