Jump to content

User:PerfektesChaos/js/externalLinkProblem/extlinks/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/externalLinkProblem/extlinks/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
// Sub-module: Look for active external links in subject page
// ResourceLoader:  compatible;
//                  dependencies: mediawiki.util
/// 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  =  -2.8,
       ELP      =  "externalLinkProblem",
       Sub      =  "extlinks";
   if ( typeof mw.libs[ ELP ]  !==  "object"   ||   ! mw.libs[ ELP ] ) {
      mw.libs[ ELP ]  =  { };
   }
   mw.libs[ ELP ].type  =  ELP;
   ELP                  =  mw.libs[ ELP ];



   /*
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License as
    * published by the Free Software Foundation; either version 2 of the
    * License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program;
    * if not, write to the Free Software Foundation, Inc.,
    * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    * http://www.gnu.org/copyleft/gpl.html
    */



   if ( typeof ELP[ Sub ]  !==  "object" ) {
      ELP[ Sub ]  =  { };
   }
   ELP[ Sub ].vsn  =  Version;



   function facilitated() {
      // Mark sub-module as ready
      // Uses:
      //    >  .signature
      //    >  Sub
      //    >  .type
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.hook()
      // 2018-08-24 PerfektesChaos@de.wikipedia
      var rls, signature, sub;
      if ( typeof ELP.signature  ===  "string" ) {
         sub        =  "/" + Sub;
         signature  =  ELP.signature + sub;
         if ( mw.loader.getState( signature )  !==  "ready" ) {
            rls = { };
            rls[ signature ] = "ready";
            mw.loader.state( rls );
            mw.hook( ELP.type + sub + ".ready" ).fire();
         }
      }
   }   // facilitated()



   function fair() {
      // Check whether EL still active in subject page
      // Uses:
      //    >  .extlinks.url
      //    >  .project.template.stamp
      //    >< .problems
      //          >  .learnt
      //           < .live
      //    mw.hook
      //    (fixings)
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var lapsus, i, j, m, n, o, s;
      if ( typeof ELP.problems  ===  "object"
           &&     ELP.problems ) {
         if ( ELP.extlinks.url ) {
            m  =  ELP.extlinks.url.length;
         } else {
            m  =  0;
         }
         n  =  ELP.problems.length;
         for ( i = 0;  i < n;  i++ ) {
            o       =  ELP.problems[ i ];
            o.live  =  false;
            s       =  o.url;
            for ( j = 0;  j < m;  j++ ) {
               if ( ELP.extlinks.url[ j ] === s ) {
                  o.live  =  true;
                  break;   // for m
               }
            }   // for m
            if ( o.live ) {
               lapsus  =  ( lapsus || o.learnt );
            } else if ( ! o.learnt ) {
               lapsus  =  true;
            }
         }   // for i
/*
         if ( lapsus && ELP.project.template.stamp ) {
            mw.hook( "wikipage.content" ).add( fixings );
         }
*/
      }
   }   // fair()



   function fixing( arrived ) {
      // Postprocess after ajax request for active EL in subject page
      // Precondition:
      //    arrived  -- JSON result of ajax query
      // Uses:
      //    >  .extlinks.follow
      //     < .id
      //     < .extlinks.url
      //    fair()
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var i, n, q;
      if ( typeof arrived  ===  "object"   &&
           typeof arrived.query  ===  "object"  ) {
         q  =  arrived.query;
         if ( typeof q.pageids  ===  "object"   &&
              typeof q.pages  ===  "object" ) {
            ELP.id  =  q.pageids[ 0 ];
            q       =  q.pages[ ELP.id ];
            if ( q   &&   typeof q.extlinks  ===  "object" ) {
               q  =  q.extlinks;
               n  =  q.length;
               if ( n > 0 ) {
                  ELP.extlinks.url  =  new Array( n );
                  for ( i = 0;  i < n;  i++ ) {
                     ELP.extlinks.url[ i ]  =  q[ i ][ "*" ];
                  }   // for i
               } else {
                  ELP.extlinks.url  =  null;
               }
            }
         }
      }
      fair();
      if ( typeof ELP.extlinks.follow  ===  "function" ) {
         ELP.extlinks.follow();
      }
   }   // fixing()



   function fixings() {
      // Retrieve active EL in subject page
      // Precondition:
      //    All resources have been loaded.
      //    Current page is a talk page.
      // Uses:
      //    >  .nsubject
      //    >< .spot
      //    >< .envNS
      //    mw.Api()
      //    (fixing)
      // 2015-10-22 PerfektesChaos@de.wikipedia
      var q  =  new mw.Api(),
          w  =  { action:       "query",
                  indexpageids: true,
                  prop:         "extlinks",
                  ellimit:      500
                };
      if ( ! ELP.spot ) {
         ELP.spot  =  mw.config.get( "wgTitle" );
      }
      if ( ELP.nsubject ) {
         if ( ! ELP.envNS ) {
            ELP.envNS  =  mw.config.get( "wgFormattedNamespaces" );
         }
         w.titles  =  ELP.envNS[ ELP.nsubject ] + ":" + ELP.spot;
      } else {
         w.titles  =  ELP.spot;
      }
      w.titles  =  w.titles.replace( / /g, "_" );
      q.get( w ).done( fixing );
   }   // fixings()



   ELP[ Sub ].fire  =  function ( action ) {
      // Start possible actions on current page
      // Precondition:
      //    action  -- calback function
      // Uses:
      //    >  .project
      //    >< .request
      //    >< .extlinks.url
      //     < .extlinks.follow
      //    facilitated()
      //    mw.loader.using()
      //    (fixings)
      // Remark: May be used as event handler -- 'this' is not accessed
      // 2015-10-28 PerfektesChaos@de.wikipedia
      facilitated();
      if ( action    &&
           typeof ELP.nsubject  ===  "number"    &&
           typeof ELP.extlinks.url  !==  "object" ) {
         ELP.extlinks.follow  =  action;
         ELP.extlinks.url     =  null;
         if ( true ) {
            mw.loader.using( [ "mediawiki.api" ],
                             fixings );
         }
      }
   };   // .extlinks.fire()



   function first() {
      // Initialize sub-module
      // Uses:
      //    facilitated()
      // 2015-10-22 PerfektesChaos@de.wikipedia
      facilitated();
   }   // first()
   first();   // autorun
}( window.mediaWiki, window.jQuery ) );



// Emacs
// Local Variables:
// coding: iso-8859-1-dos
// fill-column: 80
// End:

/// EOF </nowiki>   externalLinkProblem/extlinks/d.js