Jump to content

User:Luasóg bot/Exclusion compliant

From Wikipedia, the free encyclopedia

allowBots function

[edit]
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+"\\s*(?=[,\\}])[^}]*\\s*\\}\\}", "i").test(text)) ? false : new RegExp("\\{\\{\\s*((?!nobots)|bots(\\s*\\|\\s*allow\\s*=\\s*((?!none)|([^}]*,\\s*)*"+user+"\\s*(?=[,\\}])[^}]*|all))?|bots\\s*\\|\\s*deny\\s*=\\s*(?!all)[^}]*|bots\\s*\\|\\s*optout=(?!all)[^}]*)\\s*\\}\\}", "i").test(text);
}

Test script

[edit]

The following test script uses User:Anomie's bot exclusion test cases. (Don't forget to add the allowBots function from above.)

var luasog = new Luasog("http://wiki.riteme.site/w/api.php");
luasog.speed = 1000;

var testCase = 1;
function doTestCase(){
  luasog.get({page: "User:AnomieBOT/nobots test "+testCase}, function(success, result){
    if (!success) { kill(result.info); return false; }
    trace("Test Case "+testCase+":");
    trace(result.content);
    if (allowBots(result.content, "ExampleBot")) { trace("Result: Allowed"); }
    else { trace("Result: Not allowed"); }
    
    testCase++;
    if (testCase<34) { 
      trace("---");
      doTestCase();
    } else {
      stop();
    }
  });
}
doTestCase();

Results

[edit]

The following output shows a 100% pass rate for the allowBots function on User:Anomie's bot exclusion test cases.

Test Case 1:
{{nobots}}
This page should not be editable, as it has "{{tlx|nobots}}"
Result: Not allowed
---
Test Case 2:
{{bots}}
This page should be editable, {{tlx|bots}} does nothing
Result: Allowed
---
Test Case 3:
{{bots|allow=all}}
This page should be editable, all bots explicitly allowed
Result: Allowed
---
Test Case 4:
{{bots|allow=none}}
This page should not be editable, no bots allowed
Result: Not allowed
---
Test Case 5:
{{bots|allow=ExampleBot}}
This page should be editable, ExampleBot is specifically allowed
Result: Allowed
---
Test Case 6:
{{bots|allow=foo}}
This page should not be editable, only bot "foo" is allowed
Result: Not allowed
---
Test Case 7:
{{bots|deny=all}}
This page should not be editable, all bots denied
Result: Not allowed
---
Test Case 8:
{{bots|deny=none}}
This page should be editable, no bots denied
Result: Allowed
---
Test Case 9:
{{bots|deny=ExampleBot}}
This page should not be editable, ExampleBot specifically denied
Result: Not allowed
---
Test Case 10:
{{bots|deny=foo}}
This page should be editable, only bot "foo" is denied
Result: Allowed
---
Test Case 11:
{{bots|optout=all}}
This page should not be editable if the bot honors any optout code; if the bot does not honor any optout code, this page may or may not be editable
Result: Not allowed
---
Test Case 12:
{{bots|optout=ifd}}
This page should not be editable if the bot honors the "ifd" optout code, otherwise it should be editable
Result: Allowed
---
Test Case 13:
{{nobots}} {{foo}}
This page should not be editable, as it has {{tlx|nobots}}
Result: Not allowed
---
Test Case 14:
{{foo}}
This page should be editable, as it has no bot control template
Result: Allowed
---
Test Case 15:
{{bots|deny=ExampleBot,bar}}
This page should not be editable, as both ExampleBot and bot "bar" are specifically denied
Result: Not allowed
---
Test Case 16:
{{bots|deny=foo,ExampleBot,bar}}
This page should not be editable, as ExampleBot, bot "foo", and bot "bar" are specifically denied
Result: Not allowed
---
Test Case 17:
{{bots|deny=foo,ExampleBot}}
This page should not be editable, as both ExampleBot and bot "foo" are specifically denied
Result: Not allowed
---
Test Case 18:
{{bots|allow=ExampleBot,bar}}
This page should be editable, as both ExampleBot and bot "bar" are specifically allowed
Result: Allowed
---
Test Case 19:
{{bots|allow=foo,ExampleBot,bar}}
This page should be editable, as ExampleBot, bot "foo", and bot "bar" are specifically allowed
Result: Allowed
---
Test Case 20:
{{bots|allow=foo,ExampleBot}}
This page should be editable, as both ExampleBot and bot "foo" are specifically allowed
Result: Allowed
---
Test Case 21:
{{bots|deny=ExampleBot II}}
This page should be editable, as only "ExampleBot II" is denied
Result: Allowed
---
Test Case 22:
{{bots|allow=ExampleBot II}}
This page should not be editable, as only "ExampleBot II" is allowed
Result: Not allowed
---
Test Case 23:
This page should be editable, as it has no bot control templates (and, in fact, no templates at all)
Result: Allowed
---
Test Case 24:
{{bots|deny= ExampleBot }}
This page should not be editable, although it technically doesn't follow the specification by having spaces around the bot names
Result: Not allowed
---
Test Case 25:
{{bots|deny= foo , ExampleBot , bar }}
This page should not be editable, although it technically doesn't follow the specification by having spaces around the bot names
Result: Not allowed
---
Test Case 26:
{{bots|allow= ExampleBot }}
This page should be editable, although it technically doesn't follow the specification by having spaces around the bot names
Result: Allowed
---
Test Case 27:
{{bots|allow= foo , ExampleBot , bar }}
This page should be editable, although it technically doesn't follow the specification by having spaces around the bot names
Result: Allowed
---
Test Case 28:
{{bots|deny=
ExampleBot
}}
This page should not be editable, although it technically doesn't follow the specification by having newlines around the bot names
Result: Not allowed
---
Test Case 29:
{{bots|deny=
foo ,
foo2
, foo3 ,
ExampleBot
, bar
}}
This page should not be editable, although it technically doesn't follow the specification by having newlines around the bot names
Result: Not allowed
---
Test Case 30:
{{bots|allow=
ExampleBot
}}
This page should be editable, although it technically doesn't follow the specification by having newlines around the bot names
Result: Allowed
---
Test Case 31:
{{bots|allow=
foo ,
foo2
, foo3 ,
ExampleBot
, bar
}}
This page should be editable, although it technically doesn't follow the specification by having newlines around the bot names
Result: Allowed
---
Test Case 32:
{{bots|allow=foo}} ,ExampleBot, {{foo}}

This page should not be editable, as only bot "foo" is allowed ("ExampleBot" is outside the template)
Result: Not allowed
---
Test Case 33:
{{bots|deny=foo}} ,ExampleBot, {{foo}}

This page should be editable, as only bot "foo" is denied ("ExampleBot" is outside the template)
Result: Allowed