Расширение:Arrays
Arrays Статус релиза: стабильно |
|
---|---|
Реализация | Функция парсера |
Описание | Дополняет функции Парсера работой с массивами. |
Автор(ы) | Li Ding, Jie Bao, Daniel Werner |
Последняя версия | 2.2.1 (2020-12-08) |
MediaWiki | 1.31 |
Изменения в БД | Нет |
Лицензия | MIT License |
Скачать | README CHANGELOG |
|
|
Ежеквартальные загрузки | 38 (Ranked 97th) |
Переведите расширение Arrays, если оно доступно на translatewiki.net | |
Проблемы | Открытые задачи · Сообщить об ошибке |
Расширение Arrays (также известное под старым названием ArrayExtension) создаёт дополнительные функции Парсера для работы с массивами.
Функции
Это расширение определяет следующие функции Парсера:
Группа | Функции |
---|---|
Создание массива (с опциями сортировки, вывода и удаления не уникальных элементов) | #arraydefine
|
Достать информацию из массива | #arrayprint , #arrayindex , #arraysize , #arraysearch , #arraysearcharray , #arrayslice
|
Изменить массив | #arrayreset , #arrayunique , #arraysort
|
Взаимодействие между несколькими массивами | #arraymerge , #arrayunion , #arrayintersect , #arraydiff
|
В случае, если Extension:HashTables установлено, функции для взаимодействия между массивами/хэш-таблицами | #hashtoarray , #arraytohash
|
Создание массива
arraydefine
Эта функция создаёт массив (идентифицируемый ключевым словом '$1'), используя список значений ('$2'), разделённых определённым символом ('$3'). Доступ к значениям может быть получен другими функциями в дальнейшем.
Синтаксис:
{{#arraydefine:key | values | delimiter | options}}
Примечания:
- $1 это список значений, разделённых символом $2.
- Массив в результате является массивом строк.
- По умолчанию, разделителем является ',', если значение не задано. Разделитель может быть строкой (символы пробела и табуляции в начале и конце не будут учитываться) или регулярными выражениями языка Perl. Например
/\s*,\s*/
(см. preg_split). - Пользователь может задать пустой массив (см. примеры).
- Пользователь может указать опции уникальных элементов, сортировки и вывода (см. примеры).
- Параметры будут игнорироваться, пока разделитель не будет задан.
Примеры:
Создание массива с одним элементом под названием 'a' | {{#arraydefine:a|red}} |
Создание массива с 4 элементами под названием 'b', используется разделитель по умолчанию (','). | {{#arraydefine:b|orange, red, yellow, yellow}} |
Создание пустого массива под названием 'c'. | {{#arraydefine:c}} |
Создание массива из 2 элементов под названием 'd', используя ';' как разделитель. | {{#arraydefine:d|apple; pear|;}} |
Создание массива из 3 элементов под названием 'e', используя регулярное выражение /\s*[;,]\s*/ как разделитель.
|
{{#arraydefine:e|apple, pear; orange|/\s*[;,]\s*/}} |
Создание массива из 3 элементов под названием 'f', используя разделитель (',') и опции "unique, sort=desc, print=list" (все элементы массива уникальны, сортированы в убывающем порядке и выводятся). Для того, чтобы узнать больше параметров сортировки, см. #arraysort. | {{#arraydefine:f|orange, red, yellow, yellow |, |unique, sort=desc, print=list}} |
Работа с массивами
Вывод данных
arrayprint
Эта функция выводит значения массива в изменяемом формате.
Синтаксис:
{{#arrayprint:key|delimiter|pattern|subject|options}}
Примечания:
- subject включает в себя ссылки, шаблоны и функции Парсера.
- Внутри subject вам не нужно избегать символа '
|
'. Внутри этого параметра будет поиск pattern, который заменится на текущее значение в массиве (значение под текущим индексом). Наконец, вся строка будет спарсена и записана в массив результатов, который выведется с разделителем delimiter. - В случае когда массив, который должен быть выведен, не существует, будет возвращена пустая строка (добавлено в alpha 1.4, часть режима обратной совместимости).
- Разделитель по умолчанию определяется языком, для английского и русского по умолчанию '
,
' (добавлено в alpha 2.0, часть режима обратной совместимости).
Примеры:
Task | Example code | Output (the array b is defined above) |
---|---|---|
Print - using language dependent default list delimiter | {{#arrayprint:b}} |
|
Print - without delimiter | {{#arrayprint:b | }} |
|
Print - using '<br /> ' (line-break) as delimiter
|
{{#arrayprint:b |<br/> }} |
orange
red yellow yellow |
Pretty list output where the last two elements are chained with an ' and ' (or the local languages equivalent). Even though the delimiter parameter is empty, ', ' (or the languages equivalent) will be used since it wouldn't be pretty otherwise.
|
{{#arrayprint:b ||@ |@ |print=pretty }} |
|
Embed wiki link to categories | {{#arrayprint:b |<br/> |@@@@ |[[:Category:@@@@|@@@@]] }} |
orange |
Define a Semantic MediaWiki property value | {{#arrayprint:b |<br/> |@@@@ |[[prop1::@@@@]] }} |
|
Embed parser function | {{#arrayprint:b |<br/> |@@@@ |length of @@@@:{{#len:@@@@}} }} |
|
Embed template (with parameters) | {{#arrayprint:b|<br/>|@@@@|{{template|prop2|@@@@}} }} |
arrayindex
This function prints the value of an array (identified by key) at position index.
Синтаксис:
{{#arrayindex:key|index|default}}
Примечания:
- Invalid index (non-number, out of bound) will result in printing an empty string.
- The index is 0-based, i.e. the first element's index is 0.
- Negative indexes will return an element that far from the end (e.g.
-1
would be the arrays last element). - default will be returned in case the array doesn't exist, the key doesn't exist within the array or if the value is an empty string.
Примеры:
Third element within array a | {{#arrayindex:a |2 }} |
Last element within array b | {{#arrayindex:b |-1 }} |
Print default value for invalid index | {{#arrayindex:c |foo |bad value }} |
arraysize
This function returns the size (number of elements) of an array.
In case the given array doesn't exist the output of the function will be a void string instead of a number. This allows to check whether the array exists.
Синтаксис:
{{#arraysize:key}}
Примеры:
Size of array a: | {{#arraysize:a}} |
Check whether array a exists or not: | {{#if: {{#arraysize:a}} | ''array exists'' | ''array not defined'' }} |
arraysearch
This function returns the index of the first occurrence of the 'value' in the array (identified by 'key') starting from the position identified by 'index' parameter, and returns an empty string when failed. When yes and/or no specified, this will expand the value set to yes if found, value of no otherwise. See https://php.net/function.array-search
Синтаксис:
{{#arraysearch:key|value|index|yes|no}}
Примеры:
Return index of first occurrence of a value | {{#arraysearch:b|white}} {{#arraysearch:b|red}} use offset {{#arraysearch:b|red|0}} {{#arraysearch:b|red|2}} use preg regular expression match {{#arraysearch:b|/low/}} {{#arraysearch:b|/LOW/i}} - case insensitive {{#arraysearch:b|low}} use yes no print option {{#arraysearch:b|white|0|yes|no}} {{#arraysearch:b|yellow|0|yes|no}} |
arraysearcharray
This function searches an array (identified by key) and creates a new array (identified by new_key) from the search with all the results. The search criteria value can be a string or a regular expression. If index is given the search will start there, limit can define the maximum search results. The parameter identified by transform can be used if value is a regular expression. It can transform the result of the matched entries into the new_key array like PHP preg_replace would do it.
Синтаксис:
{{#arraysearcharray:new_key|key|value|index|limit|transform}}
Примечания:
- If value is a string, the new_key array will only contain entries of exact this string.
- Negative index values like
-n
can be used to search the last n entries only. - If Extension:Regex Fun is available within the wiki, Regex Fun's e modifier can be used within the regex. This has nothing to do with PHPs e modifier (which would be a security breach). With active e modifier the transform string will be parsed after back-refs are inserted, after that it will replace the actual match.
Примеры:
Find all entries in array 'a' that start with A followed by a space and put them into a new array 'x'.
|
{{#arraysearcharray:x |a |/^A\s. / }} |
Searching all entries of array 'a' which end with numbers and put the numbers only into a new array 'y'. | {{#arraysearcharray:y |a |/^.*?(\d )$/ |0 |-1 | $1 }} |
Searching all entries of array 'a' which end with numbers and put the length of these items into the new array (this requires Regex Fun extension). | {{#arraysearcharray:y |y |/^.*?\d $/e |0 |-1 | {{#len:$0}} }} |
Remove empty values from array 'a'. | {{#arraysearcharray:a|a|/\S /}} |
arrayslice
This function extracts a sub-array from an array (identified by 'key') into a new array (identified by 'new_key').
Синтаксис:
{{#arrayslice:new_key|key|offset|length}}
Примечания:
- Offset indicates starting point of slice, it can be non-negative number or negative number for backwards index (e.g. the last element of the array's offset is -1). Offset is different from index (which must be non-negative number)
- Length indicates how many element to extract. If it is omitted, then the sequence will have everything from offset up until the end of the array.
- If offset is no less than array size, empty array will be returned, if offset if no greater than negative array size, a new array with all elements will be returned.
Примеры:
Extract a two-element slice starting from the element at offset 1. | {{#arrayslice:x|b|1|2}} |
Extract a two-element slice starting from the element at offset -2. | {{#arrayslice:x|b|-2|2}} |
Alteration
Functions which alter an array directly instead of creating a new array.
arrayunique
This function converts an array (identified by 'key') into a set (no duplicated members, no empty element).
Синтаксис:
{{#arrayunique:key}}
Пример:
Convert array to set. | {{#arrayunique:b}} |
arrayreset
This function will unset some or all defined arrays.
Синтаксис:
{{#arrayreset:}} <!-- will unset ALL arrays -->
{{#arrayreset:key1 |key2 |... |key-n }}
Примечания:
- Using arraysize on them will return an empty string instead of
0
, so they are really unset, not empty. To simply empty an array one can use{{#arraydefine:key}}
. - Prior to version 1.4 alpha '
,
' is used to separate several arrays which should be unset.
arraysort
This function sorts an array in the following order.
none
- no sort (default)desc
- in descending order (see https://php.net/function.sort)asce
/asc
- in ascending order (see https://php.net/function.rsort)random
- in random order (see https://php.net/function.array-rand)reverse
- in reverse order (see https://php.net/function.array-reverse)
Syntax:
{{#arraysort:key|order}}
Примечание:
- Each array element is being treated as a string, this means numbers might not be ordered as expected.
Примеры:
Sort an array. | {{#arraysort:x|desc}} |
Randomize an array. | {{#arraysort:x|random}} |
Reverse an array. | {{#arraysort:x|reverse}} |
Interaction
Functions which work with more than one array, creating one new array or overwriting an existing one as result. Since version 2.0, these functions can interact with more than just two arrays at a time. In case they deal with only one array, they simply create a copy of that array. Any non-existent arrays will simply be ignored by these functions.
arraymerge
This function merges values of two or more arrays into a new array (identified by new_key).
Синтаксис:
{{#arraymerge:new_key |key1 |key2 |... |key-n }}
Примеры:
Merge two arrays. | {{#arraymerge:x |a |b }} |
Duplicate an array (keep the third argument of arraymerge empty). | {{#arraymerge:x |b }} |
arrayunion
This function merges values of two or more arrays into a new array (identified by new_key) without duplicated values.
Синтаксис:
{{#arrayunion:new_key |key1 |key2 |... |key-n }}
Примечания:
- This is a set operator, i.e., the returned array is a set without duplicated values.
- This is equal to arraymerge with arrayunique afterwards.
Пример:
Union of three arrays. | {{#arrayunion:x |a |b |c }} |
arraydiff
This function computes the (set theoretic) difference of two or more arrays. The result array is identified by new_key. The returned array is a set that contains elements of the first given array (identified by key1) which are not defined within any of the other arrays. See https://php.net/function.array-diff
Syntax:
{{#arraydiff:new_key |key1 |key2 |... |key-n }}
Примечание:
- This is a set operator, i.e. the returned array is a set without duplicated values.
- This function can be used to test sub-class relation.
Примеры:
Diff (b-a) | {{#arraydiff:x |b |a }} |
Diff (a-b) | {{#arraydiff:x |a |b }} |
Diff (a-(b c)) | {{#arraydiff:x |a |b |c }} |
arrayintersect
This function computes the set theoretic intersection of two or more given arrays. The result array is identified by new_key. See https://php.net/function.array-intersect
Синтаксис:
{{#arrayintersect:new_key |key1 |key2 |... |key-n }}
Примечание:
- This is a set operator, i.e., the returned array is a set without duplicated values.
Пример:
Intersect of three arrays put into new array x | {{#arrayintersect:x |a |b |c }} |
Установка
- Скачайте и распакуйте файл(ы) в папку с названием
Arrays
в вашей папкеextensions/
.
Вместо этого разработчикам и соавторам кода следует установить расширение из Git, используя:cd extensions/
git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Arrays - Добавьте следующий код в конце вашего файла LocalSettings.php :
wfLoadExtension( 'Arrays' );
- Configure as required.
- Готово – Перейдите на страницу Special:Version на своей вики, чтобы удостовериться в том, что расширение успешно установлено.
Конфигурация
Arrays 2.0 introduces two configuration variables:
- $egArraysCompatibilityMode
- ($egArrayExtensionCompatbilityMode in 1.4 alpha) Set to true, this will activate the compatibility mode which will bring back the behavior of the old ArrayExtension 1.3.2 as far as possible. This is because in Version 2.0 several breaking changes have been introduced. So using this compatibility mode allows a smooth switch from 1.x to 2.x Arrays extension. By default, compatibility mode is inactive. If you have been using the old ArrayExtension within your wiki before, you might want to take a look at that list and adjust your templates before switching to Arrays without compatibility mode.
- $egArraysExpansionEscapeTemplates
- Contains a key-value pair list of characters that should be replaced by a template or parser function call within array values included into an #arrayprint. By replacing these special characters before including the values into the string which is being expanded afterwards, array values can't distract the surrounding MW code. Otherwise the array values themselves would be parsed as well. By default this will escape the following characters with the following template or parser function calls:
=
={{=}}
("Template:=" should print '=
')|
={{!}}
("Template:!" should print '|
')
- Заметка: Starting with MW 1.24.0 it is no longer necessary to create "Template:!" since its purpose is served by the new
{{!}}
magic word.
{{
={{((}}
("Template:((" should print '{{
')}}
={{))}}
("Template:))" should print '}}
')
- Make sure these templates or parser functions exist within your wiki or change this variable accordingly. If this is not set up properly,
#arrayprint
might print unexpected values in case one of these character sequences is being used within array values. - $egArraysExpansionEscapeTemplates also can simply be set to null, in this case it switches back to pre 2.0 behavior where array values with these character sequences did break the given subject code within
#arrayprint
. If the compatibility mode is active, this will always be treated as set to null.
ЧаВо
Iteratively accessing array elements
It is possible to iteratively access elements of an array using #arrayprint or Расширение:Циклы .
Using arrayprint
<!-- define an array -->
{{#arraydefine:colors|Red,Blue,Yellow}}
{{#arrayprint:colors||@@@@|<nowiki/>
* длина @@@@: {{#len:@@@@}}
}}
Below is the expected output:
|
More examples can be found at the former Tetherless World Wiki.
Reusing keys
Once an array previously defined is printed, the same key can be reused for another array further down the page. As long as this sequence is observed, there is no need to define a unique key for every array.
Using Loops extension
For more complex tasks it is possible to loop through an array using the Loops extension.
{{ #arraydefine: colors | red;#FF0000, green;#00FF00, blue;#0000FF }}
{{
#loop: i
| 0 <!-- loops start value for {{ #var:i }} -->
| {{ #arraysize:colors }} <!-- number of loops -->
| <nowiki/>
* {{
#arraydefine: val | {{ #arrayindex:colors | {{ #var:i }} }} | ;
}}
<span style="color:{{ #arrayindex: val | 1 }}">
{{ #arrayindex: val | 0 }}
</span>
}}
This would output something like:
- red
- green
- blue
Working with Расширение:Semantic MediaWiki
There are two ways populating an array with semantic data. The first solution, using Semantic Result Formats is faster and more reliable, also works with complex data sets including record data and multiple values for one property.
Semantic Result Formats (SRF) introduces the Array format in version 1.6.1. It can be used to query data which will automatically be stored within an Extension:Arrays array. This is the preferred solution dealing with semantic data in arrays. Details can be found on the semantic-mediawiki.org.
Пример:
{{#ask: [[Category:Color]][[: ]] |format=array |name=colors}} {{#arrayprint: colors}}
Using a standard query
If you can't use the SRF solution above, Arrays also allows to populate an array using a SMW query result of the list format:
Пример A: To create a list of instances of the class 'Color'
{{#arraydefine:colors|{{#ask:[[Category:Color]][[: ]] |sep =, |limit=1000}} }}
Пример B: To create a unique list of values of property 'has color'
{{#arraydefine:colors|{{#ask:[[has color:: ]][[: ]] |?color= |mainlabel=- |sep =, |limit=1000}} |,|unique}}
Пример C: To deal with 2D array generated by SWM query (e.g. record-type property)
given a 2D array "red;#da2021, yellow;#fcff00, green;#00ff00" 1. create an array 'colors' {{#arraydefine:colors|red;#da2021, yellow;#fcff00, green;#00ff00}} 2. split the first element of 'colors' into another array 'colors0' {{#arraydefine:color0|{{#arrayindex:colors|0}}|;}}
Примечания:
- semantic query parameters
limit=1000
option is used to exhaust all returned results of the semantic querysep=,
option is used to set the separator for entries of the resultsmainlabel=-
option to cut off the page column
Working with Extension:DynamicPageList3
In a similar way as described above for SMW the Arrays extension can be used to store results of a DPL query. A result list can be inverted. We collect all parameter values which are used by certain pages when they include a given template. We store pairs of template parameter value and pagename. Then we sort the array and print the pairs. If consecutive array elements have the same first part (i.e. the parameter values are identical), the first part is only printed once. Thus we can construct a simple inverted index. The same mechanism could be applied to other problems as well.
См. также
- Extension:HashTables - very similar extension for the use of hash tables in MediaWiki.
- Extension:PhpTags Functions/Functions/Array - includes more than fifty functions for working with arrays using the PHP syntax.
- Расширение:Variables
- Расширение:Page Forms - offers the parser function #arraymap . Since input and output are handled by the same function, it does away with the need to define a key.
- Extension:WSArrays - provides a set of parser functions to operate on multidimensional and associative arrays.
- Расширение:Scribunto - Scribunto which allows you to embed Lua scripts into wikipages, which provides arrays and standard imperative programming language control flow.
- Extension:ArrayFunctions - a Parsoid-compatible alternative, that also works with multidimensional and associative arrays.
Это расширение включено в следующие вики-фермы/хостинги и/или пакеты: Это не исчерпывающий список. Некоторые вики-фермы/хостинги и/или пакеты могут содержать это расширение, даже если они не перечислены здесь. Всегда сверяйтесь со своими вики-фермами/хостингами или комплектами/бандлами для подтверждения. |
- Stable extensions/ru
- Parser function extensions/ru
- MIT licensed extensions/ru
- Extensions in Wikimedia version control/ru
- ParserClearState extensions/ru
- ParserFirstCallInit extensions/ru
- All extensions/ru
- Extensions included in BlueSpice/ru
- Extensions included in Canasta/ru
- Extensions included in Fandom/ru
- Extensions included in Miraheze/ru
- Extensions included in ProWiki/ru
- Extensions included in semantic::core/ru
- Extensions included in wiki.gg/ru
- Extensions included in WikiForge/ru
- Array extensions/ru
- Number extensions/ru
- Modifiable variables extensions/ru