Jump to content

User:PerfektesChaos/js/citoidWikitext/d.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/// User:PerfektesChaos/js/citoidWikitext.js
// Run citoid on wikitext source editing
/// 2018-08-30 PerfektesChaos@de.wikipedia
// ResourceLoader:  compatible;
//    dependencies: --
/// Fingerprint: #0#0#
/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/// <nowiki>
/* global window: false                                                */
/* jshint forin:false,
          bitwise:true, curly:true, eqeqeq:true, latedef:true,
          laxbreak:true,
          nocomma:true, strict:true, undef:true, unused:true           */



( function ( mw ) {
   "use strict";
   var Version   = -4.2,
       Signature = "citoidWikitext",
       HELP      = { site:    "w:en",
                     support: "user:PerfektesChaos/js/" + Signature },
       PREGO     = { maxage:    604812,
                     signature: "preferencesGadgetOptions",
                     site:      "w:en",
                     store:     "User:PerfektesChaos/js/",
                     sub:       "/r.js" },
       REPOS     = { maxage: 86400,
                     site:   "w:en",
                     store:  "User:PerfektesChaos/js/"  },
       CITWT;



   function feed( assigned, access ) {
      // Run subscript
      // Precondition:
      //    assigned  -- signature
      //    access    -- script
      // Uses:
      //    >  .suite
      //    >  Version
      //    >  REPOS.maxage
      //    >  REPOS.site
      //    >  REPOS.store
      //    >< .suite
      //    mw.loader.getState()
      //    mw.loader.state()
      //    PREGO.feed()
      //    REPOS.fire()
      // 2018-08-30 PerfektesChaos@de.wikipedia
      var p, rls;
      if ( ! mw.loader.getState( assigned ) ) {
         rls = { };
         rls[ assigned ] = "loading";
         mw.loader.state( rls );
         p = { bcache: 1 };
         if ( typeof CITWT.suite  ===  "string" ) {
            CITWT.suite = ( CITWT.suite === "r"  ?  "r"  :  "d" );
         } else {
            CITWT.suite = ( Version > 1  ?  "r"  :  "d" );
         }
         p.maxage = ( CITWT.suite === "r"  ?  REPOS.maxage  :  100 );
         REPOS.fire( REPOS.site,
                     REPOS.store + access,
                     "/" + CITWT.suite + ".js",
                     p );
      }
   }   // feed()



   function fetch( access ) {
      // Run subscripts
      // Precondition:
      //    access  -- subscript
      // Uses:
      //    >  CITWT.signature
      //    >  Signature
      //    PREGO.feed()
      //    feed()
      // 2018-07-14 PerfektesChaos@de.wikipedia
      var sub = "/" + access;
      PREGO.feed();
      feed( CITWT.signature + sub,  Signature + sub );
   }   // fetch()



   function finesse( assigned, alike ) {
      // Create sandbox root memorizer
      // Precondition:
      //    assigned  -- object, with config
      //    alike     -- true, if sandbox subpages are to be cut off
      // 2015-05-29 PerfektesChaos@de.wikipedia
      var s = assigned.wgTitle;
      if ( alike ) {
         s = s.replace( /\/.*$/, "" );
      }
      return "|sandbox." + assigned.wgDBname + "="
                         + assigned.wgNamespaceNumber + ":" + s + "|";
   }   // finesse()



   function fire() {
      // Start investigation for meaningful usage
      // Uses:
      //    >  CITWT
      //    >  .type
      //    >  .pages
      //    >  .signature
      //     < .bibRecord
      //    mw.config.get()
      //    fetch()
      //    finesse()
      // 2018-07-28 PerfektesChaos@de.wikipedia
      var env = mw.config.get( [ "wgAction",
                                 "wgArticleId",
                                 "wgCanonicalSpecialPageName",
                                 "wgDBname",
                                 "wgIsProbablyEditable",
                                 "wgNamespaceNumber",
                                 "wgPageContentModel",
                                 "wgTitle",
                                 "wgUserName",
                                 "wgWikibaseItemId" ] ),
          load, re, seek, store, stored;
      switch ( env.wgAction ) {
         case "edit":
         case "submit":
            switch ( env.wgNamespaceNumber ) {
               case 0:
                  load = true;
                  break;
               case 2:
                  load = ( ! env.wgTitle.indexOf( env.wgUserName
                                                  + "/" ) );
                  break;
               case 10:
                  load = ( env.wgTitle.substr( 0, 3 )  ===  "Bib" );
                  if ( load ) {
                     re   = "^Bib(DOI|ISBN|PMID)/([0-9].*)$";
                     re   = new RegExp( re );
                     load = re.exec( env.wgTitle );
                     if ( load ) {
                        CITWT.bibRecord = { launch:  ( env.wgArticleId
                                                                ? false
                                                                : true ),
                                            sign:    load[ 1 ],
                                            subject: load[ 2 ] };
                     }
                  }
                  break;
               default:
                  if ( env.wgNamespaceNumber % 2  ===  0   &&
                       typeof window.sessionStorage  ===  "object" ) {
                     store = window.sessionStorage.getItem( CITWT.type );
                     if ( store ) {
                        seek = finesse( env, true );
                        load = ( store.indexOf( seek )  >=  0 );
                     }
                  }
            }   // switch wgNamespaceNumber
            if ( ! load   &&
                 typeof CITWT.pages  ===  "object"   &&
                 CITWT.pages ) {
               switch ( typeof CITWT.pages[ env.wgNamespaceNumber ] ) {
                  case "object":
                     re = CITWT.pages[ env.wgNamespaceNumber ];
                     if ( re   &&   typeof re.test  !==  "function" ) {
                        re = false;
                     }
                     break;
                  case "string":
                     re = CITWT.pages[ env.wgNamespaceNumber ];
                     if ( re ) {
                        re = new RegExp( re );
                     }
                     break;
               }   // switch typeof .pages[env.wgNamespaceNumber]
               if ( re ) {
                  load = ( re.test( env.wgTitle ) );
               }
            }
            if ( load   &&
                 env.wgIsProbablyEditable   &&
                 env.wgPageContentModel === "wikitext" ) {
                fetch( "core" );
            }
            break;
         case "view":
            if ( env.wgNamespaceNumber === -1 ) {
               if ( env.wgCanonicalSpecialPageName === "Blankpage" ) {
                  //                                   "Gadgets"
                  fetch( "prego" );
               }
            } else if ( env.wgNamespaceNumber > 3   &&
                        typeof env.wgWikibaseItemId  ===  "string"   &&
                        env.wgWikibaseItemId.toUpperCase() === "Q3938"
                        &&
                        typeof window.sessionStorage  ===  "object" ) {
               store = finesse( env );
               try {
                  stored = window.sessionStorage.getItem( CITWT.type )
                     ||  "|";
                  if ( stored.indexOf( store )  <  0 ) {
                     store = stored + store.substr( 1 );
                     window.sessionStorage.setItem( CITWT.type, store );
                  }
               } catch (e) {
               }
            }
            break;
      }   // switch wgAction
   }   // fire()



   function first() {
      // Initialize scout module
      // Uses:
      //    >  Signature
      //    >  Sub
      //    >  HELP.site
      //    >  HELP.support
      //     < CITWT
      //     < .signature
      //     < .vsn
      //     < .type
      //    mw.loader.getState()
      //    mw.loader.state()
      //    fire()
      //    mw.hook()
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var signature = "ext.gadget." + Signature,
          s         = mw.loader.getState( signature ),
          rls;
      if ( s !== "loaded"  &&  s !== "ready" ) {
         rls = { };
         rls[ signature ] = "loaded";
         mw.loader.state( rls );
         if ( typeof mw.libs[ Signature ]  !==  "object"
              ||   ! mw.libs[ Signature ] ) {
            mw.libs[ Signature ] = { };
         }
         CITWT = mw.libs[ Signature ];
         if ( typeof CITWT.vsn  ===  "string" ) {
            CITWT.vsn = "*" + Version + " " + CITWT.vsn;
         } else {
            CITWT.vsn = "" + Version;
         }
         CITWT.type      = Signature;
         CITWT.signature = "ext.gadget." + Signature;
         CITWT.support   = "[[" + HELP.site + ":" + HELP.support + "]]";
         fire();
         rls[ signature ] = "ready";
         mw.loader.state( rls );
         mw.hook( CITWT.type + ".ready" ).fire();
      }
   }   // first()



   PREGO.feed = function () {
      // Provide PREGO
      // Uses:
      //    >  PREGO.signature
      //    >  PREGO.site
      //    >  PREGO.store
      //    >  PREGO.sub
      //    >  PREGO.maxage
      //    mw.loader.getState()
      //    mw.loader.state()
      //    REPOS.fire()
      // 2018-08-30 PerfektesChaos@de.wikipedia
      var sign = "ext.gadget." + PREGO.signature,
          rls;
      if ( ! mw.loader.getState( sign ) ) {
         rls = { };
         rls[ sign ] = "loading";
         mw.loader.state( rls );
         REPOS.fire( PREGO.site,
                     PREGO.store + PREGO.signature + PREGO.sub,
                     false,
                     { action: "raw",
                       ctype:  "text/javascript",
                       bcache: 1,
                       maxage: PREGO.maxage } );
      }
   };   // PREGO.feed()



   REPOS.fire = function ( at, access, append, alter ) {
      // Load from external URL
      // Precondition:
      //    at      -- Wikimedia Foundation site code, or not
      //    access  -- string with basic page name
      //    append  -- string with subpage, or not
      //    alter   -- parameter object, or MIME string, or not
      // Uses:
      //    >< REPOS.requests
      //    REPOS.foundation()
      //    mw.loader.load()
      // 2018-03-21 PerfektesChaos@de.wikipedia
      var source, syntax;
      if ( typeof REPOS.requests  !==  "object" ) {
         REPOS.requests = { };
      }
      if ( typeof REPOS.requests[ access ]  !==  "boolean" ) {
         REPOS.requests[ access ] = true;
         if ( append ) {
            source = access + append;
         } else {
            source = access;
         }
         if ( at ) {
            source = REPOS.foundation( at, source, alter );
            if ( typeof alter  ===  "object"
                 &&     alter   &&
                 typeof alter.ctype  ===  "string" ) {
               syntax = alter.ctype;
            }
         } else {
            syntax = alter;
         }
         mw.loader.load( source, syntax );
      }
   };   // REPOS.fire()



   REPOS.foundation = function ( at, access, alter ) {
      // Create URL within Wikimedia Foundation
      // Precondition:
      //    at      -- site code, or not
      //    access  -- string with page name
      //    alter   -- parameter object, or not
      // Postcondition:
      //    Returns full URL
      // 2018-03-21 PerfektesChaos@de.wikipedia
      var s = access,
          r = encodeURI( s );
      if ( typeof alter  ===  "object"
           &&     alter ) {
         r = "/w/index.php?title=" + r;
         if ( access.substr( -3 ) === ".js" ) {
            alter.ctype = "text/javascript";
         } else if ( access.substr( -4 ) === ".css" ) {
            alter.ctype = "text/css";
         }
         alter.action = "raw";
         for ( s in alter ) {
            r = r + "&" + s + "=" + encodeURI( alter[ s ] );
         }   // for s in alter
      } else {
         r = "/wiki/" + r;
      }
      if ( typeof at  ===  "string"
           &&     at ) {
         switch ( at ) {
            case "meta":
               r = "meta.wikimedia.org" + r;
               break;
            case "mw":
               r = "www.mediawiki.org" + r;
               break;
            case "w:en":
               r = "en.wikipedia.org" + r;
               break;
            default:
               r = window.location.host + r;
         }   // switch at
         r = "https://" + r;
      }
      return r;
   };   // REPOS.foundation()



   first();
}( window.mediaWiki ) );



// Emacs
// Local Variables:
// coding: us-ascii
// fill-column: 80
// End:

/// EOF </nowiki>   citoidWikitext/d.js