r62840 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62839‎ | r62840 | r62841 >
Date:20:54, 22 February 2010
Author:reedy
Status:reverted (Comments)
Tags:
Comment:
Add a simple Api Module to display the possible parameters (ie an API help) for AssertEdit

Useful for users to tell whether it's enabled on the the target wiki
Modified paths:
  • /trunk/extensions/AssertEdit/AssertEdit.php (modified) (history)
  • /trunk/extensions/AssertEdit/AssertEditApi.php (added) (history)

Diff [purge]

Index: trunk/extensions/AssertEdit/AssertEditApi.php
@@ -0,0 +1,58 @@
 2+<?php
 3+if ( !defined( 'MEDIAWIKI' ) ) {
 4+ // Eclipse helper - will be ignored in production
 5+ require_once ( 'ApiBase.php' );
 6+}
 7+
 8+class AssertEditApi extends ApiBase
 9+{
 10+ public function __construct( $main, $action ) {
 11+ parent :: __construct( $main, $action );
 12+ }
 13+
 14+ public function execute() {
 15+ $this->dieUsage( '', 'assertedit' );
 16+ }
 17+
 18+ public function getDescription() {
 19+ return 'Allows bots to make assertions. Can only be used during of editing';
 20+ }
 21+
 22+ public function isReadMode() {
 23+ return false;
 24+ }
 25+
 26+ public function shouldCheckMaxlag() {
 27+ return false;
 28+ }
 29+
 30+ public function getParamDescription() {
 31+ return array(
 32+ 'user' => 'Verify that bot is logged in, to prevent anonymous edits.',
 33+ 'bot' => 'Verify that bot is logged in and has a bot flag.',
 34+ 'true' => 'Always true; nassert=true will fail if the extension is installed.',
 35+ 'false' => 'Always false; assert=false will fail if the extension is installed.',
 36+ 'exists' => 'Verify that page exists. Could be useful from other extensions, i.e. adding nassert=exists to the inputbox extension.',
 37+ 'test' => 'Verify that this wiki allows random testing. Defaults to false, but can be overridden in LocalSettings.php.'
 38+ );
 39+ }
 40+
 41+ public function getPossibleErrors() {
 42+ return array();
 43+ }
 44+
 45+ public function getAllowedParams() {
 46+ return array(
 47+ 'user' => null,
 48+ 'bot' => null,
 49+ 'true' => null,
 50+ 'false' => null,
 51+ 'exists' => null,
 52+ 'test' => null
 53+ );
 54+ }
 55+
 56+ public function getVersion() {
 57+ return __CLASS__ . ': $Id: $';
 58+ }
 59+}
Property changes on: trunk/extensions/AssertEdit/AssertEditApi.php
___________________________________________________________________
Name: svn:eol-style
160 + native
Index: trunk/extensions/AssertEdit/AssertEdit.php
@@ -32,6 +32,9 @@
3333 $wgHooks['AlternateEdit'][] = 'efAssertEditHook';
3434 $wgHooks['APIEditBeforeSave'][] = 'efAssertApiEditHook';
3535
 36+$wgAutoloadClasses['AssertEditApi'] = $dir . "AssertEditApi.php";
 37+$wgAPIModules['assertedit'] = 'AssertEditApi';
 38+
3639 function efAssertEditHook( $editpage ) {
3740 global $wgOut, $wgRequest;
3841

Follow-up revisions

RevisionCommit summaryAuthorDate
r62851Followup to r62840...reedy22:09, 22 February 2010
r62874Followup r62840, rename file as per Gurchs commentreedy12:31, 23 February 2010
r68214Revert r62840reedy11:01, 18 June 2010
r83181Redo r62840 properly...reedy23:47, 3 March 2011

Comments

#Comment by Catrope (talk | contribs)   22:03, 22 February 2010

svn:keywords=Id ?

#Comment by Jack Phoenix (talk | contribs)   22:31, 22 February 2010

Some code style issues here.

+<?php
+if ( !defined( 'MEDIAWIKI' ) ) {
+	// Eclipse helper - will be ignored in production
+	require_once ( 'ApiBase.php' );
+}

Too much spacing, it should be just require_once( 'ApiBase.php' );

+class AssertEditApi extends ApiBase
+{
+	public function __construct( $main, $action ) {
+		parent :: __construct( $main, $action );
+	}

Braces are placed on the same line as the start of the function, or in this case, class. And a bit too much spacing in the call to parent constructor — parent::__construct( $main, $action ); is the current standard.

#Comment by Reedy (talk | contribs)   22:44, 22 February 2010

I copypasted that from somewhere...

Most of the API seems to fall fowl to these.


More reason stylize should do these things, maybe even on like a commit hook...

#Comment by Gurch (talk | contribs)   08:57, 23 February 2010

Class should be named ApiAssertEdit rather than AssertEditApi for consistency with other API classes.

#Comment by Gurch (talk | contribs)   13:47, 23 February 2010

AssertEdit hooks into ApiEditBeforeSave and thus has the effect of adding parameters, 'assert' and 'nassert', to action=edit. Unfortunately I can't see any way to have the extension incorporate this into the generated API help page alongside the other action=edit parameters.

I'm assuming doing what this module does, i.e. adding an 'action=assertedit' that doesn't do anything other than die with an error, is a workaround for this. As it stands, though, I can see people trying to call action=assertedit and then complaining it doesn't do anything.

At the least the description should make it clear that clients aren't meant to use the assertedit action itself, instead passing one of the specified values as assert or nassert parameter to action=edit. Better would be to find a way of having the parameter documented under action=edit where it should be. Besides which is this really needed? There is already a machine-friendly way to check if the extension is installed (meta=siteinfo + siprop=extensions) and documentation on the extension page, which anyone intending to use the assertions in a client ought to be reading anyway. (Said extension page was somewhat out of date and didn't mention the API at all -- I've updated it).

#Comment by Tim Starling (talk | contribs)   01:31, 18 June 2010

API help screens are meant to document API modules, not other random code. I think you should use a README file instead.

#Comment by Umherirrender (talk | contribs)   15:49, 18 June 2010

You can use Manual:Hooks/APIGetParamDescription and Manual:Hooks/APIGetAllowedParams to add Informationen to the API help page.

(Untested)

$wgHooks['APIGetAllowedParams'][] = 'onAPIGetAllowedParams';
$wgHooks['APIGetParamDescription'][] = 'onAPIGetParamDescription';

function onAPIGetAllowedParams( &$module, &$params ) {
	if ( !$module instanceof ApiEditPage ) {
		return true;
	}
	
	$options = array(
		'user',
		'bot',
		'true',
		'false',
		'exists',
		'test'
	);
	$params['assert'][ApiBase::PARAM_TYPE] = $options;
	$params['nassert'][ApiBase::PARAM_TYPE] = $options;
	
	return true;
}

function onAPIGetParamDescription( &$module, &$desc ) {
	if ( !$module instanceof ApiEditPage ) {
		return true;
	}
	
	$options = array(
		' user   - Verify that bot is logged in, to prevent anonymous edits.',
		' bot    - Verify that bot is logged in and has a bot flag.',
		' true   - Always true; nassert=true will fail if the extension is installed.',
		' false  - Always false; assert=false will fail if the extension is installed.',
		' exists - Verify that page exists. Could be useful from other extensions, i.e. adding nassert=exists to the inputbox extension.',
		' test   - Verify that this wiki allows random testing. Defaults to false, but can be overridden in LocalSettings.php.'
	);
	$desc['assert'] = array_merge( array( 'Allows bots to make assertions.' ), $options );
	$desc['nassert'] = array_merge( array( 'Allows bots to make negative assertions.' ), $options );

	return true;
}

Option exists looks like is a dupe to nocreate.

#Comment by Reedy (talk | contribs)   23:49, 3 March 2011

Thanks for that. Not sure why I didn't pay attention before. Refactored a little bit, and applied in r83181 :)

Status & tagging log