Extension:Arrays/tr

From MediaWiki.org
Jump to navigation Jump to search
Diğer diller: English  • 日本語 • Türkçe
MediaWiki manüel uzantıları
OOjs UI icon advanced.svg
Arrays
Yayın durumu: sabit
Uygulama Ayrıştırıcı işlevi
Açıklama Ayrıştırıcıyı dizi işlevleriyle geliştirir.
Yazar(lar) Li Ding, Jie Bao ve Daniel Werner
Son sürüm 2.2.0 (2019-02-13)
MediaWiki 1.31+
Veritabanı değişiklikleri Hayır
Lisans MIT Lisansı
İndir
README
CHANGELOG

  • $egArraysCompatibilityMode
  • $egArraysExpansionEscapeTemplates
Arrays uzantısını çevirin translatewiki.net adresinde mevcutsa
Kullanım ve sürüm matrisini kontrol edin.
Sorunlar Görevleri aç · Bir hatayı bildir

Arrays uzantısı (eski adıyla ArrayExtension), ayrıştırıcı işlevi için diziler üzerinde çalışan ek bir grup oluşturur.

İşlevler[edit]

Bu uzantı aşağıdaki ayrıştırıcı işlevlerini tanımlar:

Grup İşlevler
Bir dizi oluşturun (benzersiz, sıralama, yazdırma seçenekleriyle) #arraydefine
Bir diziden bilgi çıkarma #arrayprint, #arrayindex, #arraysize, #arraysearch, #arraysearcharray ve #arrayslice
Dizi değiştirme #arrayreset, #arrayunique ve #arraysort
Birkaç dizi arasındaki etkileşim #arraymerge, #arrayunion, #arrayintersect ve #arraydiff
Extension:HashTables kurulu ise, dizi/karma tablo etkileşimi için
#hashtoarray, #arraytohash

Diziler oluşturma[edit]

arraydefine[edit]

Bu işlev, 'tanımlayıcı' ile ayrılmış bir 'değerler' listesi kullanarak bir dizi ('key' ile tanımlanır) oluşturur. Değişkene daha sonra başka fonksiyonlar ile erişilebilir.

Sözdizimi:

{{#arraydefine:key|değerler|tanımlayıcı|seçenekler}}

Notlar:

  • değerler, sınırlayıcı ile ayrılmış dizelerin listesidir
  • Ortaya çıkan dizi bir dizeler dizisidir.
  • Varsayılan sınırlayıcı ',' belirtilmezse, bir sınırlayıcı (i) bir dize (sınırlayıcıyı çevreleyen beyaz boşluklar kırpılır) veya (ii) Perl normal ifadesi, ör. /\s*,\s*/ (preg_split adresine bakın)
  • Kullanıcılar boş bir dizi tanımlayabilir (örneğe bakın)
  • Kullanıcılar benzersiz, sıralama ve yazdırma gibi seçenekleri belirleyebilir (örneğe bakın).
    • Bir sınırlayıcı da belirtilmedikçe seçenekler yok sayılır.

Örnekler:

'a' adlı tek öğeli bir dizi tanımlayın
{{#arraydefine:a|red}}
'B' adlı dört öğeli bir dizi tanımlayın, varsayılan sınırlayıcı (',') kullanın
{{#arraydefine:b|orange, red, yellow, yellow}}
'C' adlı boş bir dizi tanımlayın/ayarlayın
{{#arraydefine:c}}
';' kullanarak 'd' adlı iki elemanlı bir dizi tanımlayın sınırlayıcı olarak
{{#arraydefine:d|apple; pear|;}}
Sınırlayıcı olarak /\s*[;,]\s*/ normal ifadesini kullanarak 'e' adlı üç öğeli bir dizi tanımlayın
{{#arraydefine:e|apple, pear; orange|/\s*[;,]\s*/}}
'f' adlı üç öğeli bir dizi tanımlayın, ayırıcı (','), "unique, sort=desc, print=list" seçeneklerini kullanın (dizi öğeleri benzersizdir, azalan düzende sıralanır ve yazdırılır). daha fazla "sort" seçeneği değeri için #arraysort sayfasına bakın
{{#arraydefine:f|orange, red, yellow, yellow |, |unique, sort=desc, print=list}}

Dizilerle çalışma[edit]

Çıkarma[edit]

arrayprint[edit]

Bu işlev, bir dizinin değerlerini özelleştirilebilir biçimde yazdırır.

Sözdizimi:

{{#arrayprint:key|sınırlayıcı|desen|konu|seçenekler}}

Notlar:

  • 'Konu' viki bağlantılarını, şablonları ve ayrıştırıcı işlevlerini kabul eder.
  • 'Konu' içinde, '|' kanal karakterlerinden kaçmak zorunda değilsiniz! Tüm yapı içinde, desen aranacak ve her döngünün geçerli (kaçan) dizi değeri ile değiştirilecektir. Son olarak, tüm dize ayrıştırılır ve ayırıcı olarak sınırlayıcı ile yerleştirilecek bir sonuç dizisine yerleştirilir.
  • Yazdırılması gereken dizinin mevcut olmaması durumunda, boş bir dize döndürülür. (1.4 alfada, uyumluluk modunun bir parçası olarak tanıtıldı)
  • Varsayılan sınırlayıcı dile bağlıdır, İngilizce için ', '. (2.0'da, uyumluluk modunun bir parçası olarak tanıtıldı)

Örnekler:

Yazdır - dile bağlı varsayılan liste sınırlayıcısını kullanarak
{{#arrayprint:b}}
Yazdır - sınırlayıcı olmadan
{{#arrayprint:b | }}
Yazdır - sınırlayıcı olarak '<br/>' (satır sonu) kullanarak
{{#arrayprint:b |<br/> }}
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:@@@@|@@@@]] }}
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[edit]

This function print the value of an array (identified by key) at position index.

Syntax:

{{#arrayindex:key|index|default}}

Notes:

  • 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.

Examples:

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[edit]

This function returns the size (number of elements) of an array. See: https://secure.php.net/function.count

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.

Syntax:

{{#arraysize:key}}

Examples:

Size of array a:
{{#arraysize:a}}
Check whether array a exists or not:
{{#if: {{#arraysize:b}} | ''array exists'' | ''array not defined'' }}

arraysearch[edit]

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://secure.php.net/function.array-search

Syntax:

{{#arraysearch:key|value|index|yes|no}}


Examples:

Return index of first occurrence of a value
{{#arraysearch:b|white}}
{{#arraysearch:b|red}}
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[edit]

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.

Syntax:

{{#arraysearcharray:new_key|key|value|index|limit|transform}}

Notes:

  • 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.

Examples:

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[edit]

This function extracts a sub-array from an array (identified by 'key') into a new array (identified by 'new_key'). See: https://secure.php.net/function.array-slice

Syntax:

{{#arrayslice:new_key|key|offset|length}}

Notes:

  • Offset indicates starting point of slice, it can be (i) non-negative number (ii) 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

Examples:

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[edit]

Functions which alter an array directly instead of creating a new array.

arrayunique[edit]

This function converts an array (identified by 'key') into a set (no duplicated members, no empty element). see: https://secure.php.net/function.array-unique

Syntax:

{{#arrayunique:key}}

Example:

convert array to set
{{#arrayunique:b}}

arrayreset[edit]

This function will unset some or all defined arrays.

Syntax:

{{#arrayreset:}} <!-- will unset ALL arrays -->
{{#arrayreset:key1 |key2 |... |key-n }}

Notes:

  • 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[edit]

This function sorts an array in the following order.

Syntax:

{{#arraysort:key|order}}

Note:

  • Each array element is being treated as a string, this means numbers might not be ordered as expected.

Examples:

Sort an array
{{#arraysort:x|desc}}
Randomize an array
{{#arraysort:x|random}}
Reverse an array
{{#arraysort:x|reverse}}

Interaction[edit]

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-existant arrays will simply be ignored by these functions.

arraymerge[edit]

This function merges values of two or more arrays into a new array (identified by new_key). See: https://secure.php.net/function.array-merge

Syntax:

{{#arraymerge:new_key |key1 |key2 |... |key-n }}

Examples:

Merge two arrays
{{#arraymerge:x |a |b }}
Duplicate an array (keep the third argument of arraymerge empty
{{#arraymerge:x |b }}

arrayunion[edit]

This function merges values of two or more arrays into a new array (identified by new_key) without duplicated values.

Syntax:

{{#arrayunion:new_key |key1 |key2 |... |key-n }}

Notes:

  • This is a set operator, i.e., the returned array is a set without duplicated values.
  • This is equal to arraymerge with arrayunique afterwards.

Example:

Union of three arrays
{{#arrayunion:x |a |b |c }}

arraydiff[edit]

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://secure.php.net/function.array-diff

Syntax:

{{#arraydiff:new_key |key1 |key2 |... |key-n }}

Note:

  • 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

Examples:

Diff (b-a)
{{#arraydiff:x |b |a }}
Diff (a-b)
{{#arraydiff:x |a |b }}
Diff (a-(b+c))
{{#arraydiff:x |a |b |c }}

arrayintersect[edit]

This function computes the set theoretic intersection of two or more given arrays. The result array is identified by new_key. See: https://secure.php.net/function.array-intersect

Syntax:

{{#arrayintersect:new_key |key1 |key2 |... |key-n }}

Note:

  • This is a set operator, i.e., the returned array is a set without duplicated values.

Example:

Intersect of three arrays put into new array x
{{#arrayintersect:x |a |b |c }}

Installation[edit]

  • İndir ve dosyaları extensions/ klasörünüzdeki Arrays adlı bir dizine yerleştirin.

Configuration[edit]

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. For a list which changes are effected by this, see /Pre_2.0_Documentation#Differences compared to Arrays 2.0. 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:
  • = with {{=}} ("Template:=" should print '=' )
  • | with {{!}} ("Template:!" should print '|' )
Not Not: Starting with MW 1.24.0 it is no longer necessary to create "Template:!" since its purpose is served by the new {{!}} magic word.
  • {{ with {{((}} ("Template:((" should print '{{' )
  • }} with {{))}} ("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 on 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.

FAQ[edit]

Iteratively accessing array elements[edit]

It is possible to iteratively access elements of an array using #arrayprint or Extension:Loops.

Using arrayprint[edit]

<!--define an array-->
{{#arraydefine:colors|Red,Blue,Yellow}}

{{#arrayprint:colors||@@@@|<nowiki/>
* length of @@@@: {{#len:@@@@}}
}}

below is the expected output:

  • length of Red: 3
  • length of Blue: 4
  • length of Yellow: 6

for live examples, follow this URL.

Reusing keys[edit]

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[edit]

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 Extension:Semantic MediaWiki[edit]

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.

Using Extension:Semantic Result Formats[edit]

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.

Example:

{{#ask: [[Category:Color]][[:+]] |format=array |name=colors}}
{{#arrayprint: colors}}

Using a standard query[edit]

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:

Example A: to create a list of instances of the class 'Color'

{{#arraydefine:colors|{{#ask:[[Category:Color]][[:+]] |sep =, |limit=1000}} }}

Example B: To create a unique list of values of property 'has color'

{{#arraydefine:colors|{{#ask:[[has color::+]][[:+]] |?color= |mainlabel=- |sep =, |limit=1000}} |,|unique}}

Example 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}}|;}}

Note(s):

  • semantic query parameters
    • limit=1000 option is used to exhaust all returned results of the semantic query
    • sep=, option is used to set the separator for entries of the results
    • mainlabel=- option to cut off the page column

Working with Extension:DynamicPageList[edit]

In a similar way as described above for SMW the Arrays extension can be used to store results of a DPL query.

In the Example we show how 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.

See also[edit]