My bot does one of two things: either removes a category from an article, or moves an article from one category to another.
The code is different in each situation:
I use RemoveFromCategory() for the removes, and
pl3[i].text = pl3[i].text.Replace("[[Category:" + pl[0].title,"[[Category:" + pl[1].title);
for the moves (to keep the sort keys intact).
This all works fine unless the category has been added to the article using an initial lowercase or underscores.
For example, if
[[Category:foo bar]]
or
[[Category:Foo_bar]]
are added to an article, it will show up in [[Category:Foo bar]], but my code won't remove or move either, because of the initial lowercase or the underscore. When this happens, I have to make the edits manually.
Is there any way to fix this? I'm stumped.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Two last statements do the trick. Firstly, you prepare the regular expression pattern (regexCategoryName) and then you modify the page text with it (text = Regex.Replace(text, pattern, replacement)). Try it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok, I get the premise. Unfortunately, I'm getting error CS1501: No overload for method 'ToLower' takes '0' arguments (also with ToUpper). I'll see if I can figure it out.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Will that work? The way I read that (and apologies if I'm wrong), the code will take what I give it (string categoryName = "Foo bar";) and change it (regexCategoryName = ...), not make the change in what is in the article.
For example, with the code above I can say "remove [[Category:foo bar]]" or "remove [[Category:Foo_bar]]" and it will remove [[Category:Foo bar]] from the article.
Whereas I need,
I will say "remove [[Category:Foo bar]]" and it will remove [[Category:Foo bar]] or [[Category:foo bar]] or [[Category:Foo_bar]] from the article.
I hope I'm explaining this right.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My bot does one of two things: either removes a category from an article, or moves an article from one category to another.
The code is different in each situation:
I use RemoveFromCategory() for the removes, and
pl3[i].text = pl3[i].text.Replace("[[Category:" + pl[0].title,"[[Category:" + pl[1].title);
for the moves (to keep the sort keys intact).
This all works fine unless the category has been added to the article using an initial lowercase or underscores.
For example, if
[[Category:foo bar]]
or
[[Category:Foo_bar]]
are added to an article, it will show up in [[Category:Foo bar]], but my code won't remove or move either, because of the initial lowercase or the underscore. When this happens, I have to make the edits manually.
Is there any way to fix this? I'm stumped.
Only manually for now, using regular expressions, e.g.:
string categoryName = "Foo bar";
string regexCategoryName = Regex.Escape(categoryName);
regexCategoryName = regexCategoryName.Replace(" ", "[_ ]");
regexCategoryName = "[" + regexCategoryName[0].ToLower() +
regexCategoryName[0].ToUpper() + "]";
pl[i].text = Regex.Replace(pl[i].text, @"\[\[(" + site.namespaces["14"] + "|" +
Site.wikiNSpaces["14"] + "):" + regexCategoryName +
@"(\|.*?)?]]\r?\n?", ""); // Removing category from page text
pl[i].text = Regex.Replace(pl[i].text, @"\[\[(" + site.namespaces["14"] + "|" +
Site.wikiNSpaces["14"] + "):" + regexCategoryName,
"[[Category:New foo bar"); // Replacing category in page text
Two last statements do the trick. Firstly, you prepare the regular expression pattern (regexCategoryName) and then you modify the page text with it (text = Regex.Replace(text, pattern, replacement)). Try it.
Ok, I get the premise. Unfortunately, I'm getting error CS1501: No overload for method 'ToLower' takes '0' arguments (also with ToUpper). I'll see if I can figure it out.
Yes, sorry, I forgot that it's a static function. That line must be:
regexCategoryName = "[" + char.ToLower(regexCategoryName[0]) +
char.ToUpper(regexCategoryName[0]) + "]";
No, sorry again, it must be:
string categoryName = "Foo bar";
string regexCategoryName = Regex.Escape(categoryName);
regexCategoryName = regexCategoryName.Replace("_", "\\ ").Replace("\\ ", "[_\\ ]");
regexCategoryName = "[" + regexCategoryName[0].ToLower() +
regexCategoryName[0].ToUpper() + "]" + regexCategoryName.Substring(1);
Don't bother. It seems to be a hard thing, I'd better include it in next release by myself.
You're the greatest, thanks for all the help.
Sorry for this one, I didn't think it was going to be such a hassle.
Will that work? The way I read that (and apologies if I'm wrong), the code will take what I give it (string categoryName = "Foo bar";) and change it (regexCategoryName = ...), not make the change in what is in the article.
For example, with the code above I can say "remove [[Category:foo bar]]" or "remove [[Category:Foo_bar]]" and it will remove [[Category:Foo bar]] from the article.
Whereas I need,
I will say "remove [[Category:Foo bar]]" and it will remove [[Category:Foo bar]] or [[Category:foo bar]] or [[Category:Foo_bar]] from the article.
I hope I'm explaining this right.