User:PerfektesChaos/js/externalLinkProblem/raw/d.js

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/raw/d.js
/// 2018-08-24 [email protected]
// Sub-module: Look for template on talk page, and analyze parameters
// ResourceLoader:  compatible;
//       dependencies: user, user.options, mediawiki.user, 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      =  "raw";
   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;
   if ( typeof ELP.talk  !==  "object" ) {
      ELP.talk  =  { };
   }



   function facilitated() {
      // Mark sub-module as ready
      // Uses:
      //    >  .signature
      //    >  Sub
      //    >  .type
      //    mw.loader.getState()
      //    mw.loader.state()
      //    mw.hook()
      // 2018-08-24 [email protected]
      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 fact( assign, assigned ) {
      // Interprete flags
      // Precondition:
      //    assign    -- string, with flags, or null
      //    assigned  -- object, with attributes
      // Postcondition:
      //    Returns object, with attributes
      // Uses:
      //    >< ELP.raw.regExps
      // 2015-10-22 [email protected]
      var r  =  assigned,
          got, i, p, sign, stuff, v;
      if ( assign ) {
         p =  assign.split( " " );
         if ( typeof ELP.raw.regExps  !==  "object" ) {
            ELP.raw.regExps  =  { cit:    "(-?cit=[0-9A-Za-z]+)",
                                  link:   "(proto)",
                                  listed: "(sbl)",
                                  less:   "(-)",
                                  mode:   "([3-9][0-9][0-9])",
                                  other:  "\\+([0-9]+(?:,[0-9]+)*)",
                                  since:  "(2[01][0-9][0-9])",
                                  wba:    "(-?wba=[12][09][0-9]+)"
                                };
            for ( sign in ELP.raw.regExps ) {
               stuff                    =  "^ *" +
                                           ELP.raw.regExps[ sign ] +
                                           " *$";
               ELP.raw.regExps[ sign ]  =  new RegExp( stuff );
            }   // for s
         }
         for ( i = 0;  i < p.length;  i++ ) {
            stuff  =  p[ i ];
            for ( sign in ELP.raw.regExps ) {
               got  =  ELP.raw.regExps[ sign ].exec( stuff );
               if ( got ) {
                  v  =  got[ 1 ];
                  if ( v.charCodeAt( 0 ) === 45 ) {   // '-'
                     v       =  v.substr( 1 );
                     r.less  =  true;
                  }
                  switch ( sign ) {
                     case "cit" :
                        v  =  v.substr( 4 );
                        break;
                     case "less" :
                     case "link" :
                     case "listed" :
                        v  =  true;
                        break;
                     case "mode" :
                        v  =  parseInt( v, 10 );
                        break;
                     case "other" :
                        v  =  v.split( "," );
                        break;
                     case "since" :
                        break;
                     case "wba" :
                        v  =  v.substr( 4 );
                        if ( v.length < 10 ) {
                           v  =  false;
                        }
                        break;
                  }   // switch sign
                  r[ sign ]  =  v;
                  break;   // for s
               }
            }   // for s
         }   // for i
      }
      return r;
   }   // fact()



   function factory( at ) {
      // Analyze talk page section, create list of URL of current
      // Precondition:
      //    at  -- point to begin searching
      // Postcondition:
      //    Returns object, with .problems, or index to continue
      // Uses:
      //    >  .project.template.seek
      //    >  .talk.story
      //    >  .project.template.sign
      //    >< .raw.reEntry
      //    >< .raw.reRun
      //     < .problems
      //     < .talk.last
      //    fact()
      // 2016-06-04 [email protected]
      var r  =  -1,
          s  =  ELP.talk.story,
          t  =  ELP.project.template,
          got, i, k, learnt, n, p, ul, v;
      i  =  s.indexOf( "{{" + t.seek,  at );
      if ( i >= 0 ) {
         n  =  t.seek.length + i + 2;
         k  =  s.charCodeAt( n );
         if ( k === 124  ||  k === 10 ) {
            r  =  s.indexOf( "\n}}", n );
            if ( r > 0 ) {
               s  =  s.substring( n, r );
               p  =  s.split( "|" );
               n  =  p.length;
               if ( n > 1 ) {
                  if ( typeof ELP.raw.reEntry  !==  "object" ) {
                     ELP.raw.reEntry  =  "^\\s*[0-9]+\\s*=\\s*"
                                         + "(?:"
                                            + "([^:/\n]+) +)?"
                                         + "((?:https?|ftps?|mailto)"
                                          + ":\\S+)"
                                         + "(?:\\s+"
                                            + "([^ \n}].+)?"
                                          + "|\\s*"
                                           + "(?:\\}\\})?)$";
                     ELP.raw.reEntry  =  new RegExp( ELP.raw.reEntry );
                     ELP.raw.reRun    =  "^ *" + t.sign + " *= *"
                                         + "([-0-9/A-Z.a-z:]+)"
                                         + "(?:[ \n]| *}})";
                     ELP.raw.reRun    =  new RegExp( ELP.raw.reRun );
                  }
                  for ( i = 1;  i < n;  i++ ) {
                     got  =  ELP.raw.reRun.exec( p[ i ] + " " );
                     if ( got ) {
                        s  =  got[ 1 ];
                        if ( s >= t.stamp ) {
                           r  =  -9;
                           break;   // for i
                        }
                     }
                  }   // for i
                  if ( r === -9 ) {
                     ul  =  [ ];
                     for ( i = 0;  i < n;  i++ ) {
                        got  =  ELP.raw.reEntry.exec( p[ i ] + " " );
                        if ( got ) {
                           s       =  got[ 2 ].replace( /</g, "%3C" );
                           learnt  =  ( got[ 3 ]  ?  true  :  false );
                           v       =  fact( got[ 1 ],
                                            { url:    s,
                                              learnt: learnt } );
                           ul.push( v );
                        }
                     }   // for i
                     if ( ul.length ) {
                        r  =  ul;
                     }
                  }
               }
            } else {
               r  =  n;
            }
         } else {
            r  =  n;
        }
      } else if ( ! i ){
         r  =  -8;
      }
      return r;
   }   // factory()



   function feed() {
      // Analyze talk page content, create list of URL
      // Precondition:
      //    All resources have been loaded
      // Uses:
      //    >  .project.raw.skip
      //    >  .project.raw.old
      //    >  .raw.follow
      //     < .problems
      //     < .talk.last
      //    factory()
      //    fresh()
      //    fixing()
      // 2015-11-24 [email protected]
      var i  =  0,
          p;
      ELP.problems  =  null;
      while ( i >= 0 ) {
         p  =  factory( i );
         switch ( typeof p ) {
            case "number" :
               i  =  p;
               break;
            case "object" :
               ELP.problems  =   p;
               i             =  -2;
               break;
            default:
               i  =  -3;
         }   // switch  typeof p
      }   // while i > 0
      if ( typeof ELP.project.raw  ===  "object"
           &&     ELP.project.raw ) {
         p  =  ELP.project.raw;
         if ( typeof p.skip  ===  "string"    &&
              ELP.talk.story.indexOf( p.skip )  >  0 ) {
            // OBSOLETING
            ELP.talk.last  =  true;
         }
         if ( typeof p.old  ===  "object"
              &&     p.old   &&
              typeof p.old.length  ===  "number" ) {
            for ( i = 0;  i < p.old.length;  i++ ) {
               if ( ELP.talk.story.indexOf( p.old[ i ] )  >=  0 ) {
                  ELP.talk.last  =  true;
               }
            }   // for i
         }
      }
      if ( ELP.raw.follow ) {
         ELP.raw.follow();
      }
   }   // feed()



   function fetched( arrived ) {
      // Postprocess after ajax request for talk page content
      // Precondition:
      //    arrived  -- JSON result of ajax query
      // Uses:
      //    >  .talk.id
      //    feed()
      // 2014-01-20 [email protected]
      var q;
      if ( typeof arrived  ===  "object"   &&
           typeof arrived.query  ===  "object"   &&
           typeof arrived.query.pages  ===  "object" ) {
         if ( ! ELP.talk.id   &&
              typeof arrived.query.pageids  ===  "object" ) {
            ELP.talk.id  =  arrived.query.pageids[ 0 ];
         }
         q  =  arrived.query.pages[ ELP.talk.id ];
         if ( typeof q  ===  "object"   &&
              typeof q.revisions  ===  "object" ) {
            q  =  q.revisions[ 0 ];
            if ( typeof q  ===  "object"   &&
                 typeof q[ "*" ]  ===  "string" ) {
               ELP.talk.story  =  q[ "*" ];
               feed();
            }
         }
      }
   }   // fetched()



   function fire() {
      // Retrieve talk page content
      // Precondition:
      //    All resources have been loaded.
      //    "mediawiki.api"
      // Uses:
      //    >  .nsubject
      //    >  .talk.leader
      //    >< .talk.id
      //    >< .spot
      //    >< .envNS
      //     < .sibling
      //     < .talk.spot
      //    mw.Api()
      //    mw.config.get()
      //    (fetched)
      // 2015-10-22 [email protected]
      var q  =  new mw.Api(),
          w  =  { action:       "query",
                  "continue":   "",
                  indexpageids: true,
                  prop:         "revisions",
                  rvprop:       "content"
                };
      if ( ! ELP.talk.id ) {
         if ( ELP.talk.leader ) {
            if ( ! ELP.spot ) {
               ELP.spot  =  mw.config.get( "wgTitle" );
            }
            if ( typeof ELP.nsubject  ===  "number" ) {
               if ( ! ELP.envNS ) {
                  ELP.envNS  =  mw.config.get( "wgFormattedNamespaces" );
               }
               ELP.sibling  =  ELP.envNS[ ELP.nsubject + 1 ];
            }
         } else {
            ELP.talk.id  =  mw.config.get( "wgArticleId" );
         }
      }
      if ( ELP.talk.id ) {
         w.pageids  =  ELP.talk.id;
      } else if ( ELP.sibling && ELP.spot ) {
         ELP.talk.spot  =  ELP.sibling + ":" + ELP.spot;
         w.titles       =  ELP.talk.spot.replace( /\s/g, "_" );
      } else {
         w  =  false;
      }
      if ( w ) {
         q.get( w ).done( fetched );
      }
   }   // fire()



   ELP[ Sub ].fire  =  function ( action ) {
      // Start action
      // Precondition:
      //    Template is used on talk page.
      //    action  -- callback function to continue, or false
      // Uses:
      //    >  .project
      //    >  .talk
      //     < .raw.follow
      //    facilitated()
      //    mw.loader.using()
      //    (fire)
      // Remark: May be used as event handler -- 'this' is not accessed
      // 2015-10-22 [email protected]
      facilitated();
      if ( typeof ELP.project  ===  "object"
           &&     ELP.project    &&
           typeof ELP.talk  ===  "object"
           &&     ELP.talk ) {
         ELP.raw.follow  =  action;
         mw.loader.using( [ "mediawiki.api" ],
                          fire );
      }
   };   // .raw.fire()



   function first() {
      // Initialize sub-module
      // Uses:
      //    facilitated()
      // 2015-10-22 [email protected]
      facilitated();
   }   // first()
   first();   // autorun
}( window.mediaWiki, window.jQuery ) );



// Emacs
// Local Variables:
// coding: utf-8-dos
// fill-column: 80
// End:

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