MediaWiki:Gadget-show-quality-on-cats.js

From WikiMSK

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (โŒ˜-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (โŒ˜-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.hook('wikipage.content').add(function hook_handler() {

const cats = {
  stub: 'Stubs',
  part: 'Partially complete articles',
  good: 'Articles Awaiting Peer Review',
  done: 'Peer Reviewed Articles',
  ported: 'Ported Articles',
  best: 'Exemplary articles',
  warn: 'Articles in need of attention',
  curriculum: 'Curriculum Articles',
  meta: 'Meta',
}
// SEE: [[MediaWiki:Gadget-quality.css]] for icons and colors

const css_class = Object.fromEntries(Object.entries(cats).map(function map_cats_to_classes(kv) { return [kv[1],'quality-' + kv[0]]; }))

// only on categories
//if (mw.config.get('wgCanonicalNamespace') != 'Category') return
// not on quality cats (pointless)
//if (Object.values(cats).includes(mw.config.get('wgTitle'))) return
// TODO: only ignore 

// add specials for keepers
//if (mw.config.get('wgUserGroups').includes('keeper')) {
//  document.querySelector('#mw-pages').classList.add('keeper-highlight-good')
//}

console.warn('gadget:', 'show-quality-markers')

const query = {
  prop: "categories",
    indexpageids: true,
    cllimit: "max",
    clcategories: Object.values(cats).filter(function (cat) { return cat != mw.config.get('wgTitle') }).map(function(cat) { return 'Category:'+ cat }),
  generator: "categorymembers",
    gcmtitle: mw.config.get('wgPageName'),
    gcmlimit: "max",
    gcmtype: "page",
}

const api = new mw.Api({ parameters: query, })

// page => [cat,...]
const categories = {}

function paginate(resp) {
  resp.query.pageids.forEach(function each_pageid(id) {
    const page = resp.query.pages[id]
    if (!categories[page.title])
      categories[page.title] = []
    if (!page.categories) return
    page.categories.forEach(function each_cat(row) {
      const cat = row.title.replace('Category:','')
      categories[page.title].push(cat)			
    })
  })
  if (!resp.continue) {
    return categories
  }
  console.debug('continuing:', resp.continue)
  return api.get(resp.continue).then(paginate)
}

api.get().then(paginate).then(function all_results(all) {

console.debug('article-qualities:', all)

document.querySelectorAll('#mw-pages a').forEach(function each_link(el) {
  const title = el.getAttribute('title')
  all[title].forEach(function each_quality_cat(cat) {
    const li = el.parentElement
    if (!css_class[cat]) return
    li.classList.add('quality')
    li.classList.add(css_class[cat])
  })
})

document.querySelectorAll('.quality-meta').forEach(function clean_meta(el) {
  el.classList.remove('quality')
})

mw.hook('quality-on-cats').fire()

}) // end of API query

}) // end of MW hook