Templat:Bots/doc: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
←Membuat halaman berisi '{{Documentation subpage}} <!-- Masukan kategori pada bagian bawah yang sudah ditandai di halaman ini. Untuk interwiki gunakan Wikidata (lihat Wikipedia:Wikidata) -...'
 
Tidak ada ringkasan suntingan
Baris 2:
<!-- Masukan kategori pada bagian bawah yang sudah ditandai di halaman ini. Untuk interwiki gunakan Wikidata (lihat [[Wikipedia:Wikidata]]) -->
== Penggunaan ==
 
=== Contoh implementasi ===
==== PHP ====
<syntaxhighlight lang="php" enclose="div">
function allowBots( $text ) {
global $user;
if (preg_match('/\{\{(nobots|bots\|allow=none|bots\|deny=all|bots\|optout=all|bots\|deny=.*?'.preg_quote($user,'/').'.*?)\}\}/iS',$text))
return false;
if (preg_match('/\{\{(bots\|allow=all|bots\|allow=.*?'.preg_quote($user,'/').'.*?)\}\}/iS', $text))
return true;
if (preg_match('/\{\{(bots\|allow=.*?)\}\}/iS', $text))
return false;
return true;
}
</syntaxhighlight>
 
==== Perl ====
 
<source lang="perl">
sub allow_bots {
my($text, $user, $opt) = @_;
return 0 if $text =~ /{{[nN]obots}}/;
return 1 if $text =~ /{{[bB]ots}}/;
if ($text =~ /{{[bB]ots\s*\|\s*allow\s*=\s*(.*?)\s*}}/s){
return 1 if $1 eq 'all';
return 0 if $1 eq 'none';
my @bots = split(/\s*,\s*/, $1);
return (grep $_ eq $user, @bots)?1:0;
}
if ($text =~ /{{[bB]ots\s*\|\s*deny\s*=\s*(.*?)\s*}}/s){
return 0 if $1 eq 'all';
return 1 if $1 eq 'none';
my @bots = split(/\s*,\s*/, $1);
return (grep $_ eq $user, @bots)?0:1;
}
if (defined($opt) && $text =~ /{{[bB]ots\s*\|\s*optout\s*=\s*(.*?)\s*}}/s){
return 0 if $1 eq 'all';
my @opt = split(/\s*,\s*/, $1);
return (grep $_ eq $opt, @opt)?0:1;
}
return 1;
}
</source>
 
==== VB.NET ====
<source lang="vbnet">
Public Shared Function AllowBots(ByVal text As String, ByVal user As String) As Boolean
Return Not Regex.IsMatch(text, "\{\{(nobots|bots\|(allow=none|deny=(?!none).*(" & user.Normalize() & "|all)|optout=all))\}\}", RegexOptions.IgnoreCase)
End Function
</source>
 
==== C# ====
<source lang="csharp">
public static bool AllowBots(string text, string user)
{
return !Regex.IsMatch(text, @"\{\{(nobots|bots\|(allow=none|deny=(?!none).*(" + user.Normalize() + @".*|all)|optout=all))\}\}", RegexOptions.IgnoreCase);
}
</source>
 
==== Java ====
<source lang="java">
public static boolean allowBots(String text, String user)
{
return !text.matches("(?si).*\\{\\{(nobots|bots\\|(allow=none|deny=([^\\}]*?" + user + "[^\\}]*?|all)|optout=all))\\}\\}.*");
}
</source>
 
==== JavaScript ====
<syntaxhighlight lang="javascript">
function allowBots(text, user){
if (!new RegExp("\\{\\{\\s*(nobots|bots[^}]*)\\s*\\}\\}", "i").test(text)) return true;
return (new RegExp("\\{\\{\\s*bots\\s*\\|\\s*deny\\s*=\\s*([^}]*,\\s*)*" + user.replace(/([\(\)\*\+\?\.\-\:\!\=\/\^\$])/g, "\\$1") + "\\s*(?=[,\\}])[^}]*\\s*\\}\\}", "i").test(text)) ? false : new RegExp("\\{\\{\\s*((?!nobots)|bots(\\s*\\|\\s*allow\\s*=\\s*((?!none)|([^}]*,\\s*)*" + user.replace(/([\(\)\*\+\?\.\-\:\!\=\/\^\$])/g, "\\$1") + "\\s*(?=[,\\}])[^}]*|all))?|bots\\s*\\|\\s*deny\\s*=\\s*(?!all)[^}]*|bots\\s*\\|\\s*optout=(?!all)[^}]*)\\s*\\}\\}", "i").test(text);
}
</syntaxhighlight>
 
==== Python ====
This code assumes that the [http://pypi.python.org/pypi/mwparserfromhell/ mwparserfromhell] library was installed and imported in the current script.
<source lang="python">
def allow_bots(text, user):
user = user.lower().strip()
text = mwparserfromhell.parse(text)
for tl in text.filter_templates():
if tl.name in ('bots', 'nobots'):
break
else:
return True
for param in tl.params:
bots = [x.lower().strip() for x in param.value.split(",")]
if param.name == 'allow':
if ''.join(bots) == 'none': return False
for bot in bots:
if bot in (user, 'all'):
return True
elif param.name == 'deny':
if ''.join(bots) == 'none': return True
for bot in bots:
if bot in (user, 'all'):
return False
return True
</source>
 
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
<!-- Tambahkan kategori dibawah baris ini, untuk interwiki gunakan Wikidata -->
[[Kategori:Templat utilitas Wikipedia]]
}}</includeonly>