Jump to content

User:PerfektesChaos/js/watchCategories

From Wikipedia, the free encyclopedia

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.wiki.x.io/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

[edit]

On 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

[edit]

The 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
object with detailed specification
Component Default Description
cat mandatory
  • string with category title
  • number with curid of category description page
min 1 Minimum number of entries in category to trigger display.
  • min=0 shows green box if empty; ensures succesful query.
ns -1 Namespace number of target page
title Watchlist
  • Title of target page
  • For special pages (ns=-1): Canonical page name
append Selector or jQuery object of the element after which the box is to be inserted.
before
  • mw.util.$content
  • #mw-content-text
    (for Watchlist only)
Selector or jQuery object of the element before which the box is to be inserted.
  • Overriden by definition of append.
  • If not Watchlist request and nothing specified, the box will be inserted on top of content area.
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

[edit]

Most 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.wiki.x.io/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.wiki.x.io/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.

Codes

[edit]
Source code
ResourceLoader
  • ext.gadget.watchCategories for mw:Extension:Gadgets
  • Dependencies: user, mediawiki.api, mediawiki.util
Namespaces
  • -1   (Watchlist)
  • Any page, defined by namespace number and title.
mw.libs watchCategories

Other languages

[edit]

This gadget is prepared for multilingual support.

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.