Extension:Widgets/zh

微件扩展允许在普通的wiki页面嵌入原始HTML页面（类似模板）. 您可以通过在Widget命名空间创建页面来完成. 这避免了在可编辑wiki页面中原始HTML的安全性问题，因为在Widget名字空间中进行编辑的权限是受控的. 您可以找到一些预先写好的微件.

下載
要使用Git-{A|zh-hans:获取; zh-hant:取得;}-代码，请输入以下-{A|zh-hans:命令; zh-hant:指令;}-： Composer是一个PHP依赖管理工具. 对于高于1.35.2的Mediawiki版本，您需要把Composer更新到版本2. 指引见此.

安裝
To invoke this extension, add the following to :

Also, the  folder should be made writable by the web server. See Making a directory writable by the webserver. The compiled templates folder is where Smarty stores pre-compiled templates.
 * 文件夹权限

-{zh-hans:配置; zh-hant:組態;}-
本节所述的步骤都是可选的——这个扩展即便没有这些更改也能工作，不过当你有个十分复杂的MediaWiki实例，它们能为你提供更多灵活性.



使用FlaggedRevs进行小部件审查
You can use the FlaggedRevs extension to enable a widget security review process. This will enable the Widgets extension to use only stable versions of the Widget:* pages. To do so, just add the following to your LocalSettings.php file:

This will tell the Widgets extension not to protect Widget:* pages using permissions, but rather require changes to be reviewed for security before they can take effect.

To just use FlaggedRevs to review widgets, the following configuration should suffice:



修改存储已编译的小部件的目录
You can use the  variable to change the directory for storing the compiled widgets ($compile_dir in the code). The default setting is

If you change the location by setting the parameter with another directory, make sure that it exists and has the correct permissions.



用户权限
This extension adds a namespace called "Widget", but due to potential security implications that can result from using insecure widget code, this namespace is only editable by users who have the  permission (the   group is also created to add users to; see Help:User rights management for more details).

用法
To add a widget to your MediaWiki installation, just create a page in the Widget: namespace. You can then use the  parser function to include it in any page in the wiki.



解析器函数
To add a defined widget to pages, users can use the  parser function. The syntax is as follows:

Where WidgetName is a page name in the Widget namespace (e.g. Widget:WidgetName) and param=value pairs are the settable parameters, defined within the widget code.

Parameters can be expanded inside a widget using Smarty syntax, as follows:

The  options specifies how the parameter will be 'escaped', or encoded, in the resultant Widget. It is critically important that all parameters are escaped to prevent cross-site scripting vulnerabilities. Some escape methods are ineffective. In general you should use one of,  ,   or. See http://www.smarty.net/docsv2/en/language.modifier.escape for more information on this.



Widget名字空间的页面
All widgets in the wiki are defined by creating pages in the special "Widget:" namespace, like e.g. "Widget:WidgetName".

To see all Widgets defined in your system, you can simply go to the page "Special:AllPages", select "Widget" in the namespace dropdown and click "Go".

For security reasons, these pages are only editable by wiki administrators - see User rights above for more info.

You can find many pre-defined widgets to install in your wiki at MediaWikiWidgets.org. 如果你对自行创建微件感兴趣，请见下一节.



小部件页面语法
The Widgets extension uses the Smarty PHP templating engine to provide simple templating functionality within widget pages. 传给微件的所有参数都会转化为Smarty参数.

Important: Use escape modifiers on all passed-in parameters to prevent users from passing in raw HTML from normal wiki pages. 不对此进行防护会使托管网站暴露于XSS（以及其它）攻击.

数组
如果你多次使用同一个参数，微件会得到一个包含这些值的数组. You can use to go through the array.



布尔值（true/false）
In addition to PHP's default handling of Boolean conversions, you can (unlike in PHP) use the values "true" or "false" to set the Boolean value. The following would set the  parameter to , for example:

In addition, you can set Boolean parameters to  by just using a parameter name without a value, like this:



点符号
Parameter names can have dots, and Smarty will interpret them as associative arrays, so you can use with both   and   attributes to traverse through them, or you can just use the same name with dots if you want to reference the parameter directly.

Widget:AssocTest might look like this:
 * 示例

...and you might call this Widget as follows:

..which would be displayed as:


 * foo set to bar
 * bar set to oni



验证修饰符
In addition to standard Smarty modifiers (like the heavily used ), the Widgets extension implements the   modifier, that uses PHP Data filtering to allow for validating widget parameters. Validating a parameter is not a replacement for escaping a parameter. You should still use an escape modifier even when validating.

The following values for the validate are supported (mapping to PHP's validation filters):


 * Validate as url. Staring in Widgets 1.4.2 this uses MediaWiki's url validation instead of PHP's validation. This only allows url schemes listed in $wgUrlProtocols and unlike PHP validation does not allow urls with characters dangerous in HTML. Prior to Widgets 1.4.2 this used PHP's FILTER_VALIDATE_URL which is much less strict.
 * (FILTER_VALIDATE_URL) Warning: Validating as a URL still allows JavaScript URLs that can lead to XSS. It also allows urls containing characters that are unsafe in HTML.
 * (FILTER_VALIDATE_INT)
 * or  (FILTER_VALIDATE_BOOLEAN)
 * (FILTER_VALIDATE_FLOAT)
 * (FILTER_VALIDATE_EMAIL) Warning: Valid email address can still contain characters that are unsafe in HTML, be sure to escape in addition to validating
 * (FILTER_VALIDATE_IP)
 * (FILTER_VALIDATE_DOMAIN) Warning: Valid domains can still contain characters that are unsafe in HTML, be sure to escape in addition to validating.
 * (FILTER_VALIDATE_MAC)



刷新小部件页面
If you're using a call to the widget within the widget page itself, then you will not see the updated widget (and no widget at all when you just created a page). This happens because the page contents are not available to the Widgets extension until a page is saved, but the call to the  parser function is made before the page is saved. After the page is saved, it's cached by MediaWiki, so you won't see the result even if you reload the page via the browser. To make the latest edits to the widget code appear, you need to refresh the page in the cache; to do this, you just need to use the  action (see also Purge extension), or wait a certain amount of time (up to 24 hours).



小部件与模板
Placing widgets within templates makes them an invaluable tool for creating complex displays of data with minimal lines of code.

It is particularly helpful if you want to preset some parameters of the widget while allowing users to modify others (e.g. video ID for the YouTube widget or username for the Twitter widget).

作者
The widgets extension was created and designed by Sergey Chernyshev. It is currently maintained by Yaron Koren, who has also contributed to the code base.

Other important contributions have been provided by Alexandre Emsenhuber, Jeroen De Dauw, Joshua Lerner, Majr, Sam Reed and Tim Starling.



版本历史

 * 1.4.2 (February 14, 2023) - Improved language conversion, improved validation, removed Widgets.php entry point
 * 1.4.1 (December 10, 2020) - Improved Smarty validation
 * 1.4.0 (October 10, 2018) - Added extension.json file, dropped compatibility with MW 1.29 and earlier, Smarty downloaded via Composer rather than Git submodule, removed Makefile
 * 1.3.0 (August 3, 2017) - Removed i18n php shim, dropped compatibility with MW 1.22 and earlier
 * 1.2.2 (April 14, 2017) - Restored compatibility with MW 1.27 and later
 * 1.2.1 (November 9, 2015) - Minor changes: improved debugging, license to show on "Special:Version"
 * 1.2.0 (April 29, 2015) - Possibility of arbitrary HTML output removed, support added for PHP 5.3
 * 1.1.0 (May 28, 2014) - i18n messages moved into JSON files, fixes for MediaWiki 1.21+, new parameter for setting the storage location of compiled widgets, removed support for PHP 5.2 and earlier
 * 1.0 (February 21, 2013) - Security leak fixed.
 * 0.10.1 (February 20, 2013) - Smarty added as a Git submodule, and updated to version 3.1.7.
 * 0.10.0 (January 19, 2012) - 'editwidgets' permission given to sysops by default, support removed for MediaWiki < 1.16.
 * 0.9.2 (January 12, 2011) - PHP fixes.
 * 0.9.1 (September 19, 2010) - Some code structure changes and fix to a bug 25219.
 * 0.9 (April 16, 2010) - Added support for FlaggedRev extension controlling Widget review.
 * 0.8.10 (November 27, 2009) - Security Release: Fixed a security hole in error message.
 * 0.8.9 (November 11, 2009) - Bugfix release: base64 wasn't identified properly which caused some widgets not to be displayed.
 * 0.8.8 (November 2, 2009) - HTML is inserted as-is, thanks to Joshua C. Lerner.
 * 0.8.7 (June 19, 2009) - namespaces issues and i18n issues fixed by ialex.
 * 0.8.6 (May 22, 2009) - some important security fixes by Tim Starling.
 * 0.8.5 (March 12, 2009) - Allowing parameters without values to work as bulean "true" and converting test "false" into boolean false (which is not the case in PHP itself).
 * 0.8.4 (March 11, 2009) - Minor security release - compiled Smarty templates can't be accessed directly. Also fixed a problem with compiled_templates being empty in archives and therefore not extractable by some software.
 * 0.8.3 (February 10, 2009) - Added 'validate' modifier that makes sure variable value conforms to validation rules (using https://php.net/filter).
 * 0.8.2 (January 27, 2009) - Fixed a bug when widgets were not showing up correctly on old versions of the pages. Thanks to Max Ingleton for a bug report.
 * 0.8.1 (June 25, 2008) - Worked around MediaWiki bug that inserts in front of widget output.
 * 0.8.0 (June 10, 2008) - Fixed a bug where variables were carried over from one widget to another on the same page
 * 0.7.0 (May 23, 2008) - Added support for arrays and Smarty's dotted notation
 * 0.6.0 (May 9, 2008) - Minor tweaks and bug fixes, release Makefile
 * 0.5.0 (Feb 11, 2008) - First public release



贡献小部件
If you created a widget and would like to share it, feel free to post it to MediaWikiWidgets.org website and to add a reference to it to the Widget library on this page.



错误和功能请求
If you found a problem, would like to contribute a patch or request a new feature, feel free to open a bug in the Wikimedia bug tracker:

https://phabricator.wikimedia.org/maniphest/task/create/?projects=MediaWiki-extensions-Widgets

故障排除
There are a few common problems that users encounter when they start to use Widgets extension - we'll try to document them here:


 * On a widget page, right after you just created it (or copied from MediaWikiWidgets.org) you see the message:

Warning: Smarty error: unable to read resource: "Widget:" /../extensions/Widgets/ smarty/Smarty.class.php on line 1095


 * This is most likely caused by the widget not yet existing at the moment when the widget page itself is being processed - to solve this simply purge the page, e.g. add &action=purge (or ?action=purge if you have nice URLs) to the URL.
 * It's also possible that you called the Widget incorrectly. Widget page names are case sensitive and must match the name of the widget you're calling. E.g. don't use when the widget is called "Widget:YouTube", or vice versa.

PHP Fatal error: Smarty error: unable to write to $compile_dir '/../extensions/Widgets/compiled_templates'. Be sure $compile_dir is writable by the web server user. in /../extensions/Widgets/smarty/Smarty.class.php on line 1095, referer: https://your-wiki.com/Widget:
 * If the page doesn't load and you see the following error message in the log file:
 * Check if you changed permissions and owner for Smarty to store compiled templates in. See also this post for further details.


 * If your wiki began returning totally white pages or 500 errors when you updated MediaWiki to 1.20 or a later version, try setting the permissions on /../extensions/Widgets/compiled_templates to 777.

<span id="Widget_library">

小部件库
MediaWikiWidgets.org contains a full library of ready-made widgets, including support for most of the major video sites. Any widget can be used simply by copying over the contents of the page.

To get the most up-to-date list of widgets, click here.

<span id="Extensions_that_can_be_replaced_with_widgets">

可以被小部件替换的扩展
Let's collect a list of extensions that can be replaced with widgets because all they do is output some HTML/JS/CSS with parameters and simple logic that can be done using Smarty templates.

Maybe someone will come and create a widget for that to simplify deployment. Also, these lists of extensions are a good source for action:



<span id="Frequently_asked_questions">

常见问题
Use something like this. Note that the line break tags will have to be added manually. 180px is used for width because that is the default for thumbnails. 150px is used for height because that keeps the same ratio as for the default 350x420.
 * How do I get a video to align to the right with the other images?