User:PerfektesChaos/js/watchCategories
JavaScript gadget – display number of entries within a category.
This is aiming at maintenance categories which are supposed to be empty. If not, you might feel prompted to intervene and clear the causes.
Usage
edit- If your project has registered this as a gadget, just activate on your Preferences page.
- Otherwise include the following lines into your common.js or skin dependant like vector.js:
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:PerfektesChaos/js/watchCategories/r.js&action=raw&bcache=1&maxage=604800&ctype=text/javascript",
"text/javascript");
Furthermore you will need to put the following lines before a statement mentioned above, if any:
if ( typeof mw.libs.watchCategories !== "object" || ! mw.libs.watchCategories ) {
mw.libs.watchCategories = { };
}
This block should be followed by definition of your particular categories and options. In the most simplified case this is one line:
mw.libs.watchCategories.cats = "All articles with HTML markup";
That one triggers the Category:All articles with HTML markup which has currently #0 entries (cache delay might occur on this static page).
Effects
editOn the requested target page a box is displayed, with a link to the category and the number of entries.
- By default the target page is the watchlist (changes). The box is appearing on top of the watchlist.
- By default only categories with at least one member are visible.
- Only pages which are put directly into the category will be counted; neither sub-categories nor their pages nor files.
Format of definitions
editThe configuration element mw.libs.watchCategories.cats
is supposed to be either
- a string with the category title
- a number with the
curid
of that category description - an Array with a collection of
- string with category title
- number with
curid
- object with a detailed specification
Component | Default | Description |
---|---|---|
cat
|
mandatory |
|
min
|
1
|
Minimum number of entries in category to trigger display.
|
ns
|
-1
|
Namespace number of target page |
title
|
Watchlist
|
|
append
|
Selector or jQuery object of the element after which the box is to be inserted. | |
before
|
|
Selector or jQuery object of the element before which the box is to be inserted.
|
text
|
category title | Displayed link title |
style1
|
red box, red text | CSS, if members in category |
style0
|
green box | CSS, if no members in category (min=0) |
id
|
Selector for generated element |
Examples
editMost simplified case:
if ( typeof mw.libs.watchCategories !== "object" || ! mw.libs.watchCategories ) {
mw.libs.watchCategories = { };
}
mw.libs.watchCategories.cats = "All articles with HTML markup";
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:PerfektesChaos/js/watchCategories/r.js&action=raw&bcache=1&maxage=604800&ctype=text/javascript",
"text/javascript");
A more complex example, which puts conditions on call to accelerate page loading on all other pages:
if ( typeof mw.libs.watchCategories !== "object" || ! mw.libs.watchCategories ) {
mw.libs.watchCategories = { };
}
mw.libs.watchCategories.myFunction = function () {
"use strict";
this.cats = [ "All articles with HTML markup",
{ cat: "All NRHP articles with dead external links",
ns: 4,
title: "WikiProject National Register of Historic Places/maintenance",
text: "NRHP dead external links"
}
];
mw.loader.load("//en.wikipedia.org/w/index.php?title=User:PerfektesChaos/js/watchCategories/r.js&action=raw&bcache=1&maxage=604800&ctype=text/javascript",
"text/javascript");
}; // .myFunction()
switch ( mw.config.get( "wgNamespaceNumber" ) ) {
case -1 : // Special page
if ( mw.config.get( "wgCanonicalSpecialPageName" ) === "Watchlist" ) {
mw.libs.watchCategories.myFunction();
}
break;
case 4 : // WPNR
if ( mw.config.get( "wgTitle" ) === "WikiProject National Register of Historic Places/maintenance" ) {
mw.libs.watchCategories.myFunction();
}
} // switch current number of namespace
- A function
myFunction
is defined, which does the real business. - By number of namespace it can be decided efficiently, whether further examination will be meaningful.
- Only if the page title matches,
myFunction
is called and will load the gadget. - The same analysis is made within the gadget according to the
.cats
definitions.
Thereby on all other pages (articles and discussions) loading of the gadget is suppressed and execution time minimized.
- Within the function an Array with two elements is defined:
- Category:All articles with HTML markup on watchlist
- Category:All NRHP articles with dead external links on Wikipedia:WikiProject National Register of Historic Places/maintenance
- Link title is abbreviated.
Codes
editSource code |
|
ResourceLoader |
|
Namespaces |
|
mw.libs
|
watchCategories
|
Other languages
editThis gadget is prepared for multilingual support.
- If you like a version in your own language, please forward translations to me:
- This documentation page.
- No adaption of source code necessary.
Other Scripts
edit- User:Ais523/catwatch.js
- Similar goal, but different appearance at other place with single pages rather a total number.
- Last code revision 2008.