Jump to content

手册:用户参数设置

From mediawiki.org
This page is a translated version of the page Manual:User preferences and the translation is 72% complete.

Retrieving user preferences

To retrieve all of a user's preferences, use UserOptionsLookup::getOptions() with a user identity object as a parameter.

To retrieve a specific preference of a user, use UserOptionsLookup::getOption() with a user identity and the name of the preference as a parameter, for example:

$emailFrequency = MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption( $this->getUser(), 'echo-email-frequency' );

If the preference is of the multiselect or checkmatrix type, the parameter will be <preference-name><option-value>. For example, if the preference name is 'searchNs' and the option value is '2', the parameter for getOption will be 'searchNs2'. There is an exception to this, however: If the preference specifies an explicit option prefix, that prefix will be used instead of the preference name (<prefix-name><option-name>). See the Gadgets extension for an example.

To retrieve it in JavaScript, use the user.options module.

设置默认参数设置

有关如何设置所有用户的默认参数设置,参见手册:$wgDefaultUserOptions

修改参数设置界面

可以通过Options API操作更改首选项。

创建参数设置界面

有关为功能创建首选项界面的信息,请参阅手册:钩子/GetPreferences

小工具和用户脚本参数设置

Any gadget or user script can define a preference, the name of which must start with "userjs-". Such a preference will not appear in Special:Preferences or in API:用户信息 responses, and it will not be validated. It can be read from user.options , and set through API:Options .

隐藏API参数设置

API preferences are also defined through the GetPreferences hook, with the type set to 'api'. They are validated and readable the normal ways, but are not part of the Special:Preferences form.

禁用用户参数设置

按偏好

MediaWiki版本:
1.16

To disable individual preferences for all users, add preference names to the $wgHiddenPrefs configuration variable. For example, to prevent everyone from being able to mark their edits minor by default, set the following in your LocalSettings.php:

$wgHiddenPrefs[] = 'minordefault';

按用户组

MediaWiki版本:
1.22

To prevent individual user groups from editing their preferences, you can use the 'editmyoptions' user right. With this line in your LocalSettings.php, users in the group 'user' (which contains all logged in users) can't edit their user preferences:

$wgGroupPermissions['user']['editmyoptions'] = false;

按用户

无法仅禁用特定用户的参数设置。

Add user preferences in extensions

In extension.json:

extension.json

	"Hooks": {
		"GetPreferences": "main"
	},
	"HookHandlers": {
		"main": {
			"class": "MediaWiki\\Extension\\ExampleExtension\\Hooks",
			"services": [
				"MainConfig",
				"UserOptionsLookup"
			]
		}
	},
	"config": {
		"PersonalSettingsEnabledPageId": {
			"type": "boolean",
			"value": false
		},
		"PersonalSettingsNumberOfMostViewedPages": {
			"type": "int",
			"value": 50
		},
		"PersonalSettingsNumberOfMostViewedPages": {
			"type": "string",
			"value": "year"
		}

In includes/Hooks.php:

Hooks.php

namespace MediaWiki\Extension\ExampleExtension;

class Hooks implements GetPreferencesHook {
	private UserOptionsLookup $userOptionsLookup;
	private bool $enabledPageId;
	private int $numberOfMostViewedPages;
	private string $periodForLastViewedPages;

	public function __construct(
		GlobalVarConfig $config,
		UserOptionsLookup $userOptionsLookup
	) {
		$this->userOptionsLookup = $userOptionsLookup;
		$this->enabledPageId = $config->get( 'PersonalSettingsEnabledPageId' );
		$this->numberOfMostViewedPages = $config->get( 'PersonalSettingsNumberOfMostViewedPages' );
		$this->periodForLastViewedPages = $config->get( 'PersonalSettingsPeriodForLastViewedPages' );
	}

	public function onGetPreferences( $user, &$preferences ) {

		$your_new_extensions_section = 'hitcounters';

		// A checkbox
		$preferences_key = 'hitcounters-pageid';
		$preferences_default = $this->userOptionsLookup->getOption( $user, $preferences_key, $this->enabledPageId );
		$preferences[$preferences_key] = [
			'type' => 'toggle',
			'label-message' => 'hitcounters-pageid-label',
			'default' => $preferences_default,
			'section' => $your_new_extensions_section
		];

		// An int input box
		$preferences_key = 'hitcounters-numberofmostviewedpages';
		$preferences_default = $this->userOptionsLookup->getOption( $user, $preferences_key, $this->numberOfMostViewedPages );
		$preferences[$preferences_key] = [
			'type' => 'int',
			'help-message' => 'hitcounters-numberofmostviewedpages-help',
			'label-message' => 'hitcounters-numberofmostviewedpages-label',
			'maxLength' => 4,
			'default' => $preferences_default,
			'section' => $your_new_extensions_section
		];

		// A select box
		$ctx = RequestContext::getMain();
		$preferences_key = 'hitcounters-periodforlastviewedpages';
		$key_base = 'hitcounters-statistics-mostpopular';

		// Ensure that 'default' is always the 1st array item
		$preferences_default = $period = $this->periodForLastViewedPages;
		$itemDisplayName = $ctx->msg( "$key_base-$period" )->text();
		$itemArray = [ $itemDisplayName => $period ];

		$period = 'week';
		$itemDisplayName = $ctx->msg( "$key_base-$period" )->text();
		$itemArray[$itemDisplayName] = $period;

		$period = 'month';
		$itemDisplayName = $ctx->msg( "$key_base-$period" )->text();
		$itemArray[$itemDisplayName] = $period;

		$period = 'year';
		$itemDisplayName = $ctx->msg( "$key_base-$period" )->text();
		$itemArray[$itemDisplayName] = $period;

		$usersItem = $this->userOptionsLookup->getOption( $user, $preferences_key, $preferences_default );

		$preferences[$preferences_key] = [
			'type' => 'select',
			'options' => $itemArray,
			'default' => $usersItem,
			'label-message' => "$preferences_key-label",
			'section' => $your_new_extensions_section
		];
	}
<!-- [...] -->
}

In i18n/en.json:

i18n/en.json

{
	"hitcounters-pageid-label": "Show page ID",
	"hitcounters-numberofmostviewedpages-help": "Hide with the entry of “0”.",
	"hitcounters-numberofmostviewedpages-label": "Number of most viewed pages",
	"hitcounters-periodforlastviewedpages-label": "Period for last viewed pages:",
	"hitcounters-statistics-mostpopular-week": "Most viewed pages in the current week",
	"hitcounters-statistics-mostpopular-month": "Most viewed pages in the current month",
	"hitcounters-statistics-mostpopular-year": "Most viewed pages in the current year",
}


参见