Jump to content

User:PerfektesChaos/js/isbnLib/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.
/// PerfektesChaos/js/isbnLib/d.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
/// Fingerprint: #0#0#
/// ISBN utility package
/* 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 LIB      =  "isbnLib",
       Version  =  -2.8,
       PRIVATE  =  { wstmBB:  "SU" };
   if ( typeof mw.libs[ LIB ]  !==  "object"   ||   ! mw.libs[ LIB ] ) {
      mw.libs[ LIB ]  =  { };
   }
   mw.libs[ LIB ].vsn   =  Version;
   mw.libs[ LIB ].type  =  LIB;
   LIB                  =  mw.libs[ LIB ];



/// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA)
/*
 * 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 LIB.doc  !==  "string" ) {
      LIB.doc  =  "[[w:en:User:PerfektesChaos/js/" + LIB.type + "]]";
   }
   // Requires: JavaScript 1.5   RegExp non-capturing parenthese



   PRIVATE.catalogs = {
      de:  [ "http://swb.bsz-bw.de/DB=2.1/CMD?ACT=SRCHA&IKT=1007&TRM=#",
             // "http://gso.gbv.de/DB=2.1/CMD?ACT=SRCHA&IKT=1007&TRM=#",
             "https://portal.dnb.de/"
                              + "opac.htm?method=simpleSearch&query=#" ],
      en:  [ "http://catalog.loc.gov/vwebv/search?"
                          + "searchCode=ISBL&searchType=1&searchArg=#" ],
             // "http://catalog.loc.gov/vwebv/search?"
             //              + "searchCode=STNO&searchType=1&searchArg=#"
             // British Library requires Login
             // "http://explore.bl.uk/"
             //          + "primo_library/libweb/action/search.do"
             //          + "?fn=search&vl(freeText0)=#"
             // DEPRECATED:
             //    "http://catalog.loc.gov/"
             //           + "cgi-bin/Pwebrecon.cgi?DB=local&CNT=10&CMD=#"
      fr:  [ "http://catalogue.bnf.fr/search.do?"
                                     + "mots0=NRC;-1;0;#&pageRech=rav" ],
      nl:  [ "http://opc4.kb.nl/LNG=NE/CMD?ACT=SRCH&IKT=1007&TRM=#" ],
      pl:  [ "http://koha.biblos.pk.edu.pl/cgi-bin/koha/"
                                         + "opac-search.pl?idx=nb&q=#" ],
      pt:  [ "http://aleph20.letras.up.pt/F/?"
                                + "func=find-b&find_code=ISBN&request=#",
             "http://porbase.bnportugal.pt/ipac20/ipac.jsp?"
                                     + "menu=search&index=ISBN&term=#" ],
      sv:  [ "http://libris.kb.se/hitlist?q=linkisxn:#" ],
      qqq: [ "http://www.worldcat.org/search?q=isbn:#" ]
   };   // 2017-05-18



   PRIVATE.countries978  =  [
      [ "bg", 954 ],
      [ "cs", 80 ],
      [ "da", 87 ],
      [ "de", 3 ],
      [ "el", 960, 9963 ],
      [ "en", 0, 1 ],
      [ "es", 84, 950, 956, 958, 959, 968, 970, 980, 987,
              9942, 9945, 9962, 9968, 9972, 9974, 9977, 9978,
              99905, 99922, 99923, 99924, 99925, 99926,
              99934, 99939, 99953, 99954 ],
      [ "et", 9949, 9985 ],
      [ "fi", 951, 952 ],
      [ "fr", 2 ],
      [ "he", 965 ],
      [ "hu", 963 ],
      [ "it", 88 ],
      [ "ja", 4 ],
      [ "ko", 89, 9946 ],
      [ "nl", 90 ],
      [ "nn", 82 ],
      [ "no", 82 ],
      [ "pl", 83 ],
      [ "pt", 85, 972, 989 ],
      [ "ru", 5 ],
      [ "sk", 80 ],
      [ "sl", 961 ],
      [ "sv", 91 ],
      [ "th", 974 ],
      [ "tr", 975, 9944 ],
      [ "uk", 966 ],
      [ "zh", 7 ]
   ];   // .countries978   2015-05-01



   function feed( about ) {
      /**
       Callback from external library
       @version 2018-08-24 PerfektesChaos@de.wikipedia
       @param   about  Array with details; about[0] = "U" or "S"
      */
      var rls, s;
      if ( typeof about  ===  "object"   &&
           typeof about[ 0 ]  ===  "string" ) {
         LIB.loaded  =  true;
         LIB.wstm[ about[ 0 ] ]  =  true;
         for ( s in LIB.wstm ) {
            LIB.loaded  =  LIB.loaded  &&  LIB.wstm[ s ];
         }  //  for s in LIB.wstm
         if ( LIB.loaded ) {
            rls   =  { };
            rls[ "ext.gadget." + LIB.type + "/*" ] = "ready";
            mw.loader.state( rls );
            mw.hook( "isbnLib.ready" ).fire( LIB );
         }
      }
   }   // feed()



   function fill() {
      /**
       Ensure minimal catalogs
       @version 2016-03-20 PerfektesChaos@de.wikipedia
      */
      if ( typeof LIB.catalogs  !==  "object"
           ||   ! LIB.catalogs   ||
           typeof LIB.catalogs.qqq  !==  "object"
           ||   ! LIB.catalogs.qqq   ||
           typeof LIB.catalogs.qqq.length  !==  "number"   ||
           typeof LIB.catalogs.qqq[ 0 ]  !==  "string" ) {
         LIB.catalogs  =  PRIVATE.catalogs;
      }
    }   // fill()



   function fine() {
      /**
       Retrieve pointer to external library
       @version 2016-03-22 PerfektesChaos@de.wikipedia
       @return  object with external library, or undefined
      */
      var r;
      if ( typeof LIB.loaded  ===  "boolean"
           &&     LIB.loaded   &&
           typeof mw.libs.WikiSyntaxTextMod  ===  "object"   &&
           typeof mw.libs.WikiSyntaxTextMod.util  ===  "object"   &&
           typeof mw.libs.WikiSyntaxTextMod.util.isbn  ===  "object" ) {
         r  =  mw.libs.WikiSyntaxTextMod.util.isbn;
      }
      return  r;
   }   // fine()



   function first() {
      /**
       Internal initialization, last attempt to attach libraries
       @version 2018-08-24 PerfektesChaos@de.wikipedia
      */
      var i, rls, s, self, signature, state, sub;
      if ( typeof LIB.wstm  !==  "object" ) {
         rls   =  { };
         self  =  "ext.gadget." + LIB.type;
         rls[ self + "/*" ] = "loading";
         mw.loader.state( rls );
         LIB.wstm  =  { };
         sub       =  ( Version > 0  ?  "r"  :  "d" );
         for ( i = 0;  i < PRIVATE.wstmBB.length;  i++ ) {
            s              =  PRIVATE.wstmBB.substr( i, 1 );
            LIB.wstm[ s ]  =  false;
            signature      =  "user:PerfektesChaos/WikiSyntaxTextMod/"
                              + s;
            state          =  mw.loader.getState( signature );
            if ( ! state ) {
               rls  =  { };
               rls[ signature ] = "loading";
               mw.loader.state( rls );
               mw.loader.load( "https://en.wikipedia.org/w/index.php?"
                               + "title=User:PerfektesChaos/js/"
                               + "WikiSyntaxTextMod/" + sub + s + ".js"
                               + "&bcache=1&maxage=604812"
                               + "&action=raw&ctype=text/javascript" );
            }
            if ( state === "ready" ) {
               feed( [ s ] );
            } else {
               mw.hook( "WikiSyntaxTextMod/" + s + ".ready" )
                 .add( feed );
            }
         }   // for i
         rls          =  { };
         rls[ self ]  =  "ready";
         mw.loader.state( rls );
      }
   }   // first()



   LIB.factory  =  function ( attribute ) {
      /**
       Make regexp string for ISBN detection
       @version 2013-11-04 PerfektesChaos@de.wikipedia
       @param   attribute  true: URLs are excluded from detection
       @return  string for RegExp; null if no libraries
      */
      var wstmUisbn  =  fine(),
          r;
      if ( wstmUisbn ) {
         r  =  wstmUisbn.factory( attribute );
      } else {
         r  =  null;
      }
      return  r;
   };   // .factory()



   LIB.finder = function ( assign ) {
      /**
       Retrieve set of library catalog URLs for particular languages
       @version 2016-04-14 PerfektesChaos@de.wikipedia
       @param   assign  string with language code (not "qqq"), or false
       @return  Array of URL strings, "qqq" will always precede the list
      */
      var r = [ ],
          b, g, i, k, s, u;
      fill();
      for ( i = 0;  i < LIB.catalogs.qqq.length;  i++ ) {
         r.push( LIB.catalogs.qqq[ i ] );
      }   // for i
      if ( typeof assign  ===  "string"
           &&     assign !== "qqq" ) {
         b  =  assign.split( " " );
         for ( k = 0;  k < b.length;  k++ ) {
            s  =  b[ k ];
            if ( typeof LIB.catalogs[ s ]  ===  "object" ) {
               g  =  LIB.catalogs[ s ];
               for ( i = 0;  i < g.length;  i++ ) {
                  u  =  g[ i ];
                  if ( $.inArray( u, r )  <  0 ) {
                     r.push( u );
                  }
               }   // for i
            }
         }   // for k
      }
      return r;
   };   // .finder()



   LIB.flip  =  function ( adjust ) {
      /**
       Retrieve corresponding ISBN-10 for ISBN-13, or ISBN-13 for ISBN-10
       @version 2013-11-04 PerfektesChaos@de.wikipedia
       @param   adjust  string with ISBN (ID only, freestyle, no keyword)
       @return  string, if valid; false if invalid sequence; null else
      */
      var wstmUisbn  =  fine(),
          r;
      if ( wstmUisbn ) {
         r  =  wstmUisbn.flip( adjust,  function () { } );
      } else {
         r  =  null;
      }
      return  r;
   };   // .flip()



   LIB.focus  =  function ( about ) {
      /**
       Guess language(s) from ISBN: space separated ISO 639 codes
       @version 2016-03-22 PerfektesChaos@de.wikipedia
       @param   about  string with ISBN
       @return  string, if valid; false if not detected
      */
      var r = false,
          s = about.replace( /-/g, "" ),
          k = s.length,
          g, i, n;
      if ( k === 13 ) {
         k  =  s.substr( 0, 3 );
         if ( k === "979" ) {
            switch ( s.substr( 3, 2 ) ) {
               case "10":   r = "fr";   break;
               case "11":   r = "ko";   break;
               case "12":   r = "it";   break;
            }   // switch substr( 3, 2 )
         } else if ( k === "978" ) {
            k  =  10;
            s  =  s.substr( 3 );
         }
      }
      if ( k === 10 ) {
         switch ( s.substr( 0, 1 ) ) {
            case "0":
            case "1":   r = "en";   break;
            case "2":   r = "fr";   break;
            case "3":   r = "de";   break;
            case "4":   r = "ja";   break;
            case "5":   r = "ru";   break;
            case "6":
               n  =  s.substr( 0, 3 );
               break;
            case "7":   r = "zh";   break;
            case "8":
               n  =  s.substr( 0, 2 );
               break;
            case "9":
               switch ( s.substr( 0, 2 ) ) {
                  case "90":
                  case "94":   r = "nl";   break;
                  case "91":   r = "sv";   break;
                  case "92":   r = "mul";   break;
                  default:
                     if ( s.substr( 1, 1 )  ===  "9" ) {
                        if ( s.substr( 2, 1 )  ===  "9" ) {
                           n  =  5;
                        } else {
                           n  =  4;
                        }
                     } else {
                        n  =  3;
                     }
                     n  =  s.substr( 0, n );
               }   // switch substr( 0, 2 )
               break;
         }   // switch substr( 0, 1 )
         if ( n ) {
            n  =  parseInt( n, 10 );
            for ( i = 0;  i < PRIVATE.countries978.length;  i++ ) {
               g  =  PRIVATE.countries978[ i ];
               for ( k = 1;  k < g.length;  k++ ) {
                  if ( g[ k ] === n ) {
                     r  =  g[ 0 ];
                     break;   // for k
                  }
               }   // for k
               if ( r ) {
                  break;   // for i
               }
            }   // for i
         }
      }
      return r;
   };   // .focus()



   LIB.format  =  function ( adjust, additional ) {
      /**
       Retrieve formatted ISBN
       @version 2016-04-11 PerfektesChaos@de.wikipedia
       @param   adjust      string with ISBN (ID only, freestyle, no keyword)
       @param   additional  true: return Array rather than string
       @return  string/Array if valid; false if invalid sequence; null else
      */
      var wstmUisbn  =  fine(),
          r;
      if ( wstmUisbn ) {
         r  =  wstmUisbn.format( adjust,  false,  function () { } );
         if ( r[ 0 ] ) {
            if ( ! additional ) {
               r  =  r[ 1 ];
            }
         } else {
            r  =  false;
         }
      } else {
         r  =  null;
      }
      return  r;
   };   // .format()



   LIB.furnish = function ( add ) {
      /**
       Add resolvers to default catalogs
       @version 2016-03-22 PerfektesChaos@de.wikipedia
       @param   add  object with language dependant resolvers
      */
      var cat, g, i, j, s, slang, user;
      switch ( typeof add ) {
         case "string":
            user  =  { qqq: [ add ] };
            break;
         case "object":
            if ( add ) {
               if ( add.length  ===  "number" ) {
                  user  =  { qqq: add };
               } else {
                  user  =  add;
               }
            }
            break;
      }   // switch typeof add
      if ( user ) {
         fill();
         if ( typeof LIB.reLang !==  "object" ) {
            LIB.reLang  =  new RegExp( "^[a-z][a-z]+$" );
            LIB.reURL1  =  new RegExp( "^\\S+$" );
            LIB.reURL2  =  new RegExp( "^https?://[^/#]+\\.[^/#]+/.*#" );
         }
         for ( slang in user ) {
            if ( typeof slang  ===  "string"  &&
                 LIB.reLang.test( slang ) ) {
               g  =  user[ slang ];
               if ( typeof g  ===  "object"   &&   g
                    &&   typeof g.length  ===  "number" ) {
                  if ( typeof LIB.catalogs[ slang ]  !==  "object" ) {
                     LIB.catalogs[ slang ]  =  [ ];
                  }
                  cat  =  LIB.catalogs[ slang ];
                  for ( i = 0;  i < g.length;  i++ ) {
                     s  =  g[ i ];
                     if ( typeof s  ===  "string"   &&
                          LIB.reURL1.test( s )   &&
                          LIB.reURL2.test( s ) ) {
                        for ( j = 0;  j < cat.length;  j++ ) {
                           if ( cat[ j ] === s ) {
                              s  =  false;
                              break;   // for j
                           }
                        }   // for j
                        if ( s ) {
                           cat.push( s );
                        }
                     }
                 }   // for i
               }
            }
         }   // for slang in user
      }   // user defined
   };   // .furnish()



   // autoInit onLoad
   first();
}( window.mediaWiki, window.jQuery ) );



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

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