八面玲珑是什么意思| 看脱发应该挂什么科| 青蟹什么季节吃最好| 18kgp是什么意思| 表妹是什么关系| 薇诺娜适合什么年龄| 拔河是什么意思| 胰岛素起什么作用| 国家电网是什么单位| 马冬梅是什么意思| 尿酸高是什么原因引起的| 庆大霉素治疗鱼什么病| 胖头鱼又叫什么鱼| 半胱氨酸是什么| 什么是真菌感染| 牛仔裤配什么上衣| 喉咙发炎吃什么水果好| 喉癌是什么原因引起的| 女生体毛多是什么原因| 梦见自己假牙掉了是什么意思| 凭什么| 尿液分析是检查什么| 陈皮不能和什么一起吃| 疱疹性咽峡炎吃什么食物| 夏至吃什么好| 主动脉夹层a型是什么病| 自锁是什么意思| 雅诗兰黛属于什么档次| 金铃子是什么昆虫| 咳嗽吃什么能治好| 羊后马前对是什么生肖| 梦见小婴儿是什么意思| 肝功七项查的是什么| 胡子为什么长得快| 孕妇便秘吃什么| pcl是什么材料| 荔枝与什么不能同吃| 伏脉常见于什么病| 霸王硬上弓什么意思| 蚰蜒吃什么| 芡实适合什么人吃| 857什么意思| 牛津布是什么材质| 思是什么生肖| 世界大战是什么意思| 高铁上不能带什么东西| 验尿白细胞高是什么原因| 怀孕肚子胀是什么原因| 根是什么生肖| 沥水是什么意思| 腰肌劳损是什么症状| 琼花是什么意思| 谷草转氨酶偏低是什么原因| 派特ct主要检查什么| 今年71岁属什么生肖| 南宁晚上有什么好玩的地方| 纤支镜检查是用来查什么的| egfr医学上是什么意思| 肠癌是什么症状| 上火吃什么| 黄连泡水喝能治什么病| 粘胶是什么材质| 寒包火感冒吃什么药| 高反吃什么药| 扁平疣挂什么科| 老咳嗽是什么原因| 布克兄弟什么档次| 女性排卵期一般是什么时候| 10月19日什么星座| 海藻是什么| columbia是什么牌子| 北京大学校长是什么级别| 晚上睡觉容易醒是什么原因| 急性会厌炎吃什么药| 日后好相见的前一句是什么| 定情信物是什么意思| 疤痕体质是什么| 男人皮肤黑穿什么颜色的衣服好看| 软组织挫伤用什么药| 痣长在什么地方不好| 扔枕头有什么忌讳吗| 1981年属什么| 颈椎病有什么特效药| 舌加氏念什么| 什么时候拔罐最好| 口臭什么原因引起的| ac是什么基团| 肺纤维化是什么意思| 男士内裤什么材质的好| camel是什么牌子| 背上长白斑是什么病的症状| 什么雪糕最好吃| 功高震主是什么意思| 看见黄鼠狼有什么预兆| 莲花与荷花有什么区别| 嘴唇紫红色是什么原因| 什么中药补肾| 梦见彩虹是什么征兆| 心源性猝死是什么意思| 穷是什么意思| 碳酸氢钠是什么| 吃什么对肝好怎么养肝| 五彩缤纷是什么意思| 鲍鱼长什么样| 1999年发生了什么| 梦见洗碗是什么预兆| 后颈长痘痘是什么原因| 津津有味什么意思| 为什么医院不用咳特灵| 总胆汁酸是什么意思| 痰多咳嗽是什么原因| 梦见小黑蛇是什么预兆| 过期啤酒有什么用途| 热锅上的蚂蚁是什么意思| 进入icu病房意味着什么| 3月份生日是什么星座| 风信子的花语是什么| 吃什么增肥最快| 腿抽筋吃什么药最好| 月经期适合吃什么水果| 吃什么食物能养肝护肝| 马车标志是什么品牌| la什么牌子| 红茶加枸杞有什么功效| 为什么尽量不打免疫球蛋白| 玉米水喝了有什么好处| 蝴蝶喜欢吃什么| 饭后呕吐是什么原因引起的| 三级警督是什么级别| 经常中暑的人体内缺什么| 三教九流什么意思| 天津市市长是什么级别| 梵音是什么意思| 痒是什么原因引起的| 什么是提示语| 阴毛的作用是什么| 反哺是什么意思| 灰指甲用什么药膏| 刮宫后需要注意什么| 中度贫血吃什么补血最快| 急功近利什么意思| 鱼不能和什么食物一起吃| 人工虎骨粉是什么做的| 人中发红是什么原因| 什么叫阴吹| 速战速决的意思是什么| 什么人不能吃火龙果| 薛字五行属什么| 左下腹疼痛是什么原因女性| 右膝关节退行性变是什么意思| 大姨妈来了喝什么好| 奶粉中的opo是什么| 七什么八什么| 九五年属什么生肖| 梦见两个小男孩是什么意思| 最新奥特曼叫什么| u盾是什么| 天天吃玉米有什么好处和坏处| 对立面是什么意思| 虾不能和什么东西一起吃| 备货是什么意思| 阑尾粪石是什么| 臭屁是什么意思| pvd是什么材料| 一生辛苦不得财是什么生肖| 月经淋漓不尽什么原因| 黑枸杞和什么一起泡水喝比较好| 儿童去火吃什么药| 什么症状吃柏子养心丸| 隐血十一是什么意思| 什么人不能摆放大象| 最近老放屁是什么原因| 狗嚎叫有什么预兆| 春风得意是什么生肖| 妃子是什么意思| 后腰左侧疼痛是什么原因男性| 3月5日什么星座| 信访局是干什么的| 梦见理发是什么意思| 传教士是什么姿势| 舌头鱼又叫什么鱼| 腿部青筋明显是什么原因| 抗衰老吃什么| 胰头占位是什么病| 嘴苦口臭是什么原因造成的| 黄盖代表什么生肖| 半套是什么意思| 不自觉摇头是什么病| 肚子一直咕咕叫是什么原因| 地头蛇比喻什么样的人| 产后42天挂什么科| 梦见别人掉牙齿是什么征兆| sq是什么意思| 脚掌脱皮是什么原因| 市局副局长什么级别| 去三亚需要什么证件| 胃出血有什么症状| 瘿瘤是什么病| 什么的事| 中老年人吃什么油好| 打完升白针有什么反应| 检查怀孕挂什么科| 胡子变白是什么原因| 想什么来什么是什么定律| 宫颈hpv感染是什么病| 出山是什么意思| 肚脐眼为什么会有臭味| 失能是什么意思| 师参谋长是什么军衔| 什么样的充电宝能带上飞机| 缺黄体酮会有什么症状| 缺钙吃什么补得最快| 什么是音爆| 裸钻是什么| 结扎对男人有什么伤害| 心脏病吃什么食物好| 牙齿黑是什么原因| 脚旁边骨头突出叫什么| 高血脂不能吃什么| 妇科tct检查什么| 爱困总想睡觉什么原因| 盆腔积液什么意思| 大致是什么意思| 阳虚吃什么药效果最好| 为什么会得痛风| 专柜是什么意思| AMI是什么病| 什么叫焦虑症| 头一直疼是什么原因| 尿蛋白是什么意思| 血酮体高代表什么| 梦见妈妈出轨预示什么意思| 八面玲珑指什么生肖| 为什么会偏头痛| 粉墙用什么| 正常白带什么样| 青储是什么| 二次报销需要什么条件| 胆囊炎不能吃什么食物| 什么是掌跖脓疱病| 激素6项什么时候查| 什么烟最贵| 龙冲什么生肖| 小孩抵抗力差吃什么提高免疫力| 草龟吃什么蔬菜| 苏州秋裤楼叫什么| 破釜沉舟是什么生肖| 血小板压积是什么意思| 唯心是什么意思| 萎缩性阴道炎用什么药| 葡萄糖升高说明什么| 恶心是什么原因| 哺乳期抽烟对宝宝有什么影响| 什么是沉香| 食谱是什么意思| 声音小是什么原因| 结婚送什么礼物最合适| 中暑头晕吃什么药| 泡黄芪水喝有什么好处| 525什么星座| 血氧仪是干什么用的| 梦见爆炸是什么意思| 自闭症是什么意思| 13岁属什么| 百度Jump to content

2017全球(上海)人工智能创新峰会

From mediawiki.org
GetPreferences
Available from version 1.16.0
Modify user preferences.
Define function:
百度 ,第二十届新概念作文大赛宣布启动,主办方《萌芽》杂志社社长孙甘露的开场白开启时光隧道。
public static function onGetPreferences( User $user, array &$preferences ) { ... }
Attach hook: In extension.json:
{
	"Hooks": {
		"GetPreferences": "MediaWiki\\Extension\\MyExtension\\Hooks::onGetPreferences"
	}
}
Called from: File(s): preferences/DefaultPreferencesFactory.php
Interface: GetPreferencesHook.php

For more information about attaching hooks, see Manual:Hooks .
For examples of extensions using this hook, see Category:GetPreferences extensions.


Since REL1_35:

Define function:
public function onGetPreferences( $user, &$preferences ) { ... }
Attach hook: In extension.json:
{
	"Hooks": {
		"GetPreferences": "example_tag"
	},
	"HookHandlers": {
		"example_tag": {
			"class": "MediaWiki\\Extension\\ExampleExtension\\Hooks",
			"services": [
				"MainConfig",
				"UserOptionsLookup"
			]
		}
	}
}
Interface: GetPreferencesHook.php

Usage

[edit]

Parameters

[edit]
Parameter/Option Description
$user User whose preferences are being modified
&$preferences Preferences description array, to be fed to an HTMLForm object

Tabs and sections

[edit]

The section array key specifies which tab and section of Preferences contains your preferences. If your section value is foo/bar, this means your preference will appear on the foo tab (named by system message prefs-foo) within the bar section (named by system message prefs-bar). If no such tab or section exists, it is created automatically.

List of default tabs

[edit]
Identifier Displays as
personal User profile
rendering Appearance
editing Editing
rc Recent changes
watchlist Watchlist
misc Misc

Supported types

[edit]

Visible types

[edit]

The type can take on various values found in the HTMLForm::$typeMappings array in the file includes/htmlform/HTMLForm.php, including info, multiselect, radio, etc.

Most preferences are stored in the same format as is used by the HTMLFormField, but in the case of 'type' => 'usersmultiselect' a transformation should be carried out from a newline-separated list of usernames (which is what the form widget works with) and a newline-separated list of user IDs (which is what gets stored in the database). See the treatment of email-blacklist (in core) or echo-notifications-blacklist (in Echo ) for examples of this.

Floats

[edit]

For float types, you can set min and max, which will be validated on save.

API preferences

[edit]

API preferences use type api. They are not displayed in Special:Preferences. They are usually set via custom front-end interfaces that call the API.

Note that you should not use 'type' => 'hidden' for API preferences (that type exists for HTML forms, not preferences).

Default preferences

[edit]

To set the default value for a preference (i.e. the value that is set for a new user that hasn't customized their preferences yet), add the setting to the $wgDefaultUserOptions global variable. Use the same key name as you use for $preferences in the hook.

Alternatively, if you're writing an extension, you can add to the DefaultUserOptions section of the file extensions.json.

Example since REL1_35

[edit]

See: Handling hooks in MediaWiki 1.35 and later

extension.json

[edit]

In extension.json:

	"Hooks": {
		"GetPreferences": "main"
	},
	"HookHandlers": {
		"main": {
			"class": "MediaWiki\\Extension\\ExampleExtension\\Hooks",
			"services": [
				"MainConfig",
				"UserOptionsLookup"
			]
		}
	},
	"AutoloadClasses": {
		"MediaWiki\\Extension\\ExampleExtension\\Hooks": "includes/Hooks.php",
	},
	"config": {
		"PersonalSettingsEnabledPageId": {
			"type": "boolean",
			"value": false
		},
		"PersonalSettingsNumberOfMostViewedPages": {
			"type": "int",
			"value": 50
		},
		"PersonalSettingsPeriodForLastViewedPages": {
			"type": "string",
			"value": "year"
		}
	},
	"manifest_version": 2

Hooks.json

[edit]

In includes/Hooks.php:

namespace MediaWiki\Extension\ExampleExtension;

use MediaWiki\Preferences\Hook\GetPreferencesHook;

use GlobalVarConfig;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserOptionsLookup;

class Hooks implements GetPreferencesHook {
	private GlobalVarConfig $config;
	private UserOptionsLookup $userOptionsLookup;

	public function __construct(
		GlobalVarConfig $config,
		UserOptionsLookup $userOptionsLookup
	) {
		$this->config = $config;
		$this->userOptionsLookup = $userOptionsLookup;
	}

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

		$your_new_extensions_section = 'exampleextension';

		// A checkbox
		$preferences_key = 'hitcounters-pageid';
		$preferences_default = $this->userOptionsLookup->getOption(
						$user,
						$preferences_key,
						$this->config->get( 'PersonalSettingsEnabledPageId' ) );
		$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->config->get( 'PersonalSettingsNumberOfMostViewedPages' ) );
		$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->config->get( 'PersonalSettingsPeriodForLastViewedPages' );
		$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
		];

		// A set of radio buttons. Notice that in the 'options' array,
		// the keys are the text (not system messages), and the values are the HTML values.
		// They keys/values might be the opposite of what you expect. PHP's array_flip()
		// can be helpful here.
		$preferences_key = 'exampleextension-exampleselect';
		$key_base = 'exampleextension-select';
		$itemKey1 = 'choice1';
		$itemName1 = $ctx->msg( "$key_base-$itemKey1" )->text();
		$itemKey2 = 'choice2';
		$itemName2 = $ctx->msg( "$key_base-$itemKey2" )->text();
		$itemKey3 = 'choice3';
		$itemName3 = $ctx->msg( "$key_base-$itemKey3" )->text();

		// A 'default' key is required, ...
		$preferences_default = $itemKey1;
		// ..., but respect user's choice!
		$usersItem = $this->userOptionsLookup->getOption(
						$user,
						$preferences_key,
						$preferences_default );

		$preferences[$preferences_key] = [
			'type' => 'radio',
			'help-message' => 'exampleextension-exampleselect-help', // a system message (optional)
			'label-message' => 'exampleextension-exampleselect-label', // a system message
			// Array of options. Key = text to display. Value = HTML <option> value.
			'options' => [
				$itemName1 => $itemKey1,
				$itemName2 => $itemKey2,
				$itemName3 => $itemKey3
			],
			'default' => $usersItem,  // A 'default' key is required!
			'section' => $your_new_extensions_section
		];
	}
<!-- [...] -->
}

i18n

[edit]

In i18n/en.json:

{
	"exampleextension-select-help": "Put a help message here!",
	"exampleextension-select-label": "Select an item:",
	"exampleextension-select-choice1": "Pick me please",
	"exampleextension-select-choice2": "No, pick me!",
	"exampleextension-select-choice3": "Seriously, pick me right now",
	"hitcounters-pageid-label": "Show page ID",
	"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",
}

Example Old School

[edit]

extension.json

[edit]

In extension.json:

	"Hooks": {
		"GetPreferences": "ExampleExtensionHooks::onGetPreferences",
	},
	"AutoloadClasses": {
		"ExampleExtensionHooks": "includes/Hooks.php",
	},
	"config": {
		"PersonalSettingsEnabledPageId": false,
		"PersonalSettingsNumberOfMostViewedPages": 50,
		"PersonalSettingsPeriodForLastViewedPages": "year"
	},
	"manifest_version": 1

Hooks.json

[edit]

In includes/Hooks.php:

class ExampleExtensionHooks extends Hooks {

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

		global $wgPersonalSettingsEnabledPageId, $PersonalSettingsNumberOfMostViewedPages, $PersonalSettingsPeriodForLastViewedPages;

		$your_new_extensions_section = 'exampleextension';

		// A checkbox
		$preferences_key = 'hitcounters-pageid';
		$preferences_default = $this->userOptionsLookup->getOption(
						$user,
						$preferences_key,
						$wgPersonalSettingsEnabledPageId );
		$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,
						$PersonalSettingsNumberOfMostViewedPages );
		$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 = $PersonalSettingsPeriodForLastViewedPages;
		$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
		];

		// A set of radio buttons. Notice that in the 'options' array,
		// the keys are the text (not system messages), and the values are the HTML values.
		// They keys/values might be the opposite of what you expect. PHP's array_flip()
		// can be helpful here.
		$preferences_key = 'exampleextension-exampleselect';
		$key_base = 'exampleextension-select';
		$itemKey1 = 'choice1';
		$itemName1 = $ctx->msg( "$key_base-$itemKey1" )->text();
		$itemKey2 = 'choice2';
		$itemName2 = $ctx->msg( "$key_base-$itemKey2" )->text();
		$itemKey3 = 'choice3';
		$itemName3 = $ctx->msg( "$key_base-$itemKey3" )->text();

		// A 'default' key is required, ...
		$preferences_default = $itemKey1;
		// ..., but respect user's choice!
		$usersItem = $this->userOptionsLookup->getOption(
						$user,
						$preferences_key,
						$preferences_default );

		$preferences[$preferences_key] = [
			'type' => 'radio',
			'help-message' => 'exampleextension-exampleselect-help', // a system message (optional)
			'label-message' => 'exampleextension-exampleselect-label', // a system message
			// Array of options. Key = text to display. Value = HTML <option> value.
			'options' => [
				$itemName1 => $itemKey1,
				$itemName2 => $itemKey2,
				$itemName3 => $itemKey3
			],
			'default' => $usersItem,  // A 'default' key is required!
			'section' => $your_new_extensions_section
		];
	}
<!-- [...] -->
}

i18n

[edit]

In i18n/en.json:

{
	"exampleextension-select-help": "Put a help message here!",
	"exampleextension-select-label": "Select an item:",
	"exampleextension-select-choice1": "Pick me please",
	"exampleextension-select-choice2": "No, pick me!",
	"exampleextension-select-choice3": "Seriously, pick me right now",
	"hitcounters-pageid-label": "Show page ID",
	"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",
}
属兔和什么属相最配 梦到自己被蛇咬是什么意思 上颚起泡是什么原因 血糖高看什么科室 晚餐吃什么好
怀孕感冒可以吃什么药 经期可以喝什么茶 低血压头晕吃什么药 什么是情人 黑马比喻什么样的人
水痘有什么症状 生理期没来是什么原因 举世无双什么意思 飞机杯是什么意思 空调嗡嗡响是什么原因
戒腊什么意思 拉黑便是什么原因 kobe是什么意思 拔罐红色是什么原因 7月24日什么星座
生化有什么症状和反应hcv8jop4ns0r.cn 代字五行属什么hcv9jop1ns0r.cn nokia是什么牌子的手机hcv8jop9ns8r.cn 瘢痕子宫是什么意思ff14chat.com 月完念什么hcv9jop3ns0r.cn
肝功能不全是什么意思hcv9jop5ns1r.cn 女人喜欢什么类型男人hcv8jop9ns0r.cn 早搏是什么原因引起的bysq.com 吃什么避孕药可以推迟月经hcv9jop4ns7r.cn 病是什么偏旁hcv8jop9ns5r.cn
什么和什么丽hkuteam.com 妈妈的奶奶叫什么adwl56.com 为什么会有跳蚤hcv9jop2ns2r.cn 回民为什么不吃猪肉hcv7jop6ns0r.cn 梦到拆房子是什么意思hcv9jop8ns1r.cn
大便出血是什么原因hcv9jop4ns4r.cn 早上出汗是什么原因hcv7jop9ns3r.cn 刷牙牙龈出血是什么原因hcv8jop4ns4r.cn tf是什么意思hcv8jop1ns0r.cn 直接胆红素偏高是什么原因hcv9jop6ns2r.cn
百度