User:PerfektesChaos/js/resultListSort/d.js

/// mw:User:PerfektesChaos/js/resultListSort/d.js /// 2012-10-29 PerfektesChaos@de.wikipedia // Sort result lists of "Whatlinkshere", "Search" or other special pages. // ResourceLoader: compatible; dependencies: user, mediawiki.util /// Fingerprint: #0#0# /// @license GPL [//www.mediawiki.org/w/COPYING] (+GFDL, LGPL, CC-BY-SA) /// /* jshint curly:true, eqeqeq:true, undef:true, white:false            */ /* global window:false, document:false, jQuery:false, mediaWiki:false */ /*jslint plusplus: true, vars: true, white: true                      */ /*globals document, jQuery, mediaWiki, window                         */

( function ( mw, $ ) {  "use strict";   var RLS;   if ( typeof mw.libs.resultListSort !== "object"  || ! mw.libs.resultListSort ) {     mw.libs.resultListSort  =  { };   }   RLS             =  mw.libs.resultListSort;   RLS.vsn         =  -1.10;   RLS.type        =  "resultListSort";   RLS.canonicals  =  [ // General Purpose                        "Contributions",                        "LinkSearch",                        "Newpages",                        "Recentchanges",                        "Search",                        "Watchlist",                        "Whatlinkshere",                        // Maintenance                        "BrokenRedirects",                        "Deadendpages",                        "Fewestrevisions",                        "DoubleRedirects",                        "Lonelypages",                        "Longpages",                        "Protectedpages",                        "Protectedtitles",                        "Shortpages",                        "Uncategorizedcategories", "Uncategorizedimages", "Uncategorizedpages", "Uncategorizedtemplates", "Unusedcategories", "Unusedimages", "Unusedtemplates", "Wantedcategories", "Wantedfiles", "Wantedpages", "Wantedtemplates" ]; RLS.script     =  "javascript"; RLS.modeDirect =   0;  // direct child RLS.modeContr  =   1;  // Contributions RLS.modeLink2  =   2;  // LinkSearch RLS.modeSRH    =   3;  // .mw-search-result-heading RLS.modeSize   =  -1;  // Newpages size RLS.modeSumm   =  -2;  // Newpages summary RLS.modeURL    =  -3;  // LinkSearch URL RLS.modeUser   =  -4;  // Newpages user RLS.unsorted   =  [ "Search", "LinkSearch", "Whatlinkshere" ]; if ( typeof RLS.debugging !== "object" ) { RLS.debugging =  { loud: true, say: "RLS" }; } //mw.log({loud:true},".",0);

// User options: // .auto // .casing // .group // .portlet

/*   * 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 */

// Requires: JavaScript 1.3 //          ECMA        262-3 § 11.8.5  (string comparison operators) //          MediaWiki   1.18 (mw.libs, jQuery core)

RLS.texts =  { // 2012-10-29 PerfektesChaos@de.wikipedia "byName":   {"en": "Sort results", "de": "Sortiere Ergebnisse"}, "bySize":   {"en": "Sort by size", "de": "Sortiere nach Größe"}, "bySummary": {"en": "Sort by summary", "de": "Sortiere nach BK"}, "byURL":    {"en": "URL Sort", "de": "Sortiere nach URL"}, "byUser":   {"en": "Sort by user", "de": "Sortiere nach Benutzer"} };  // .texts

RLS.factory =  function ( access, action, ahead ) { // Insert portlet link // Precondition: //   access  -- text distinguisher "by" //   action  -- function name //   ahead   -- ID of element to prepend to      // Uses: //   this //   >  .slang //   >  .texts //   >  .script //   >  .vsn //   mw.util.addPortletLink //   (.fresh) // 2012-10-29 PerfektesChaos@de.wikipedia var s     =  "by" + access; var show  =  this.texts[ s ][ this.slang ]; if ( ! show ) { show =  this.texts[ s ].en; }     mw.util.addPortletLink( "p-cactions",                              this.script                               +  ":mw.libs.resultListSort.fresh(" + ( action ? action : "" )  +  ");"                                 +  "void(0);",                              show,                              "ca-sort-by" + access,                              show + " * resultListSort " + this.vsn,                              null,                              ( ahead  ? "#" + ahead :  null ) ); };  // .factory

RLS.fair =  function ( $access ) { // Make sortable element from jQuery list item // Precondition: //   $access  -- jQuery list item // Postcondition: //   Returns   [ $access, ns, title, talkmod, oldid ] // Uses: //   this //   >  .mode //   >  .modeURL //   >  .reNS //   >  .space2num //   >  .group //   >  .casing //   >  .modeContr //   .fetch // 2012-07-29 PerfektesChaos@de.wikipedia var g;     var k   =  0; var m  =  0; var n  =  0; var p;     var s   =  this.fetch( $access ); if ( s ) { if ( RLS.mode !== RLS.modeURL ) { g =  this.reNS.exec( s ); if ( g ) { p =  g[ 1 ].toLowerCase; n =  this.space2num[ p ]; s =  s.substr( p.length + 1 );   // decodeURI if ( RLS.group ) { m =  n % 2; n =  ( n - m )  /  2; }           }            if ( RLS.casing ) { s =  s.toLowerCase; }           if ( RLS.mode === RLS.modeContr ) { p =  $access.children( "a" ).attr( "href" ); p =  /[&?]oldid=([0-9]+)(?:&.*)?$/.exec( p ); if (p) { k =  parseInt( p[1], 10 ); }           }         }      } else { s =  ""; }     return  [ $access, n, s, m, k ]; };  // .fair

RLS.favorite =  function  { // Guess user language // Uses: //   this //   >< .slang //   mw.config.get // 2012-10-29 PerfektesChaos@de.wikipedia if ( ! this.slang ) { this.slang =  mw.config.get( "wgUserLanguage" ).toLowerCase; switch ( this.slang ) { case "de" : case "de-at" : case "de-ch" : case "de-formal" : case "als" : case "nds" : this.slang =  "de"; break; default: this.slang =  "en"; }  // switch wgUserLanguage }  };   // .favorite

RLS.fetch =  function ( $access ) { // Retrieve distinguisher from jQuery list item // Precondition: //   $access  -- jQuery list item // Postcondition: //   Returns namespace:title string // Uses: //   this //   >  .mode //   >  .modeDirect //   >  .modeContr //   >  .modeLink2 //   >  .modeSize //   >  .modeSRH //   >  .modeSumm //   >  .modeURL //   >  .modeUser // 2012-10-29 PerfektesChaos@de.wikipedia var j   =  0; var r   =  false; var sub =  "a"; var $s  =  false; switch ( this.mode ) { case this.modeContr : case this.modeDirect : $s =  $access; break; case this.modeLink2 : $s =  $access; j  =  1; break; case this.modeSize : r =  $access.children( ".mw-newpages-length" ).text; if ( r ) { r =  r.replace( /\[?([.0-9]+)[^0-9].*/, "$1" ); r =  "            ".substr( 0, 13 - r.length )  +  r; /*                    123456789012 r =  r.replace( /\./g, "" );   parseint(,10); }           break; case this.modeSRH : $s =  $access.children( ".mw-search-result-heading" ); break; case this.modeSumm : r =  $access.children( ".comment" ).text; break; case this.modeURL : r =  $access.children( "a" ).attr( "href" ); break; case this.modeUser : $s  =  $access.children( ".mw-userlink" ); sub =  false; break; }  // switch .mode if ( $s ) { if ( $s.length ) { if ( sub ) { $s =  $s.children( sub ); if ( j ) { $s =  $s.eq( j ); }           }            r  =  $s.attr( "title" ); }     }      return  r;   };   // .fetch

RLS.fiat =  function  { // Ensure namespace RegExp and list objects // Postcondition: //   configuration has been set // Uses: //   this //   >< .reNS //   >< .space2num //   mw.config.get // Requires: JavaScript 1.3  charCodeAt // 2012-07-21 PerfektesChaos@de.wikipedia var k, o, re, s;     if ( ! this.reNS ) { o              =  mw.config.get( "wgNamespaceIds" ); re             =  ""; this.space2num =  { }; for ( s in o ) { if ( o.hasOwnProperty( s ) ) { k =  o[ s ]; if ( k ) {  // not main s                   =  s.replace( /_/, " " ); this.space2num[ s ] =  k;                  re                   =  re  +  ( re ? "|" : "" )  +  s;               } }        }   // for s in this.space2num this.reNS =  new RegExp( "^(" + re + "):",  "i" ); }  };   // .fiat */

RLS.find =  function ( album, allow ) { // Is element matching? // Precondition: //   album  -- collection or single item //   allow  -- default for album===true // Postcondition: //   Returns false iff not matching // Uses: //   this //   >  .specialPage // 2012-07-27 PerfektesChaos@de.wikipedia var i;     var o  =  album; var r =  false; switch ( typeof o ) { case "number" : case "string" : r =  ( o === this.specialPage ); break; case "boolean" : if ( ! o ) { break; }           o  =  allow; // fall through case "object" : // if ( o ) { if ( typeof o.length ===  "number" ) {   // Array for ( i = 0; i < o.length;  i++ ) { r =  ( o[i] === this.specialPage ); if ( r ) { break;  // for i                     } }  // for i               } else { r =  o[ this.specialPage ]; }           }            break; }  // switch typeof album return r;   };   // .find

RLS.fire =  function  { // Start possible actions on current page // Precondition: //   Current page is special page // Uses: //   >  .auto //   >  .unsorted //   >  .portlet //   >  .canonicals //   .find //   .fresh //   .furnish // Remark: May be used as event handler -- 'this' is not accessed // 2012-07-27 PerfektesChaos@de.wikipedia if ( RLS.find( RLS.auto, RLS.unsorted ) ) { RLS.fresh; }     if ( RLS.find( ( RLS.portlet === undefined  ?  RLS.canonicals                                                  :  RLS.portlet ), RLS.canonicals ) ) { RLS.furnish; }  };   // .fire

RLS.first =  function ( arg1, arg2 ) { // Compare two list elements //   element:  [ *, ns, title, talkmod, oldid, ... ]     // Precondition: //   arg1  -- first element //   arg2  -- second element // Postcondition: //   Returns number  -1 :  arg2 -> arg1 //                    0 :  arg1 == arg2 //                    1 :  arg1 -> arg2 // 2012-07-23 PerfektesChaos@de.wikipedia var r =  arg1[ 1 ]  -  arg2[ 1 ]; if ( ! r ) { if ( arg1[ 2 ] ===  arg2[ 2 ] ) { if ( arg1[ 3] ===  arg2[ 3 ] ) { r =  arg1[ 4 ]  -  arg2[ 4 ]; } else { r =  arg1[ 3 ]  -  arg2[ 3 ]; }        } else if ( arg1[ 2 ]  <  arg2[ 2 ] ) { r =  -1; } else { r =  1; }     }      return  r;   };   // .first

RLS.fresh =  function ( alter ) { // Launch sorting process on current page // Precondition: //   alter  -- optional distinguisher // Uses: //   >  .specialPage //   >  .modeContr //   >  .modeURL //   >  .modeLink2 //   >  .modeDirect //   >  .modeSRH //   >  mw.util.$content //   >< .lean //    < .mode //   jQuery.find //   jQuery.children //   .fiat //   .fair //   (.first) // Remark: May be used as event handler -- 'this' is not accessed // 2012-10-29 PerfektesChaos@de.wikipedia var g, i, n, $lis; var select =  false; var $l     =  false; switch ( RLS.specialPage ) { case "Contributions" : RLS.mode =  RLS.modeContr; select   =  "#mw-content-text ul"; break; case "LinkSearch" : RLS.mode =  ( alter ? RLS.modeURL : RLS.modeLink2 ); select   =  "ol.special"; break; case "Newpages" : RLS.mode =  ( alter ? alter : RLS.modeDirect ); select   =  "#mw-content-text ul"; break; case "Recentchanges" : case "Watchlist" : RLS.mode =  ( alter ? alter : RLS.modeDirect ); select   =  ".special"; if ( ! RLS.lean ) { $l =  mw.util.$content.find( "#mw-content-text" ); $l.find( "h4" ).remove; $l.find( ".mw-changeslist-date" ).remove; RLS.lean =  true; }           break; case "Recentchangeslinked" : RLS.mode =  RLS.modeDirect; select   =  ".special"; break; case "Search" : RLS.mode =  RLS.modeSRH; select   =  ".mw-search-results"; break; case "Whatlinkshere" : RLS.mode =  RLS.modeDirect; select   =  "#mw-whatlinkshere-list"; break; }  // switch .specialPage if ( select ) { $l =  mw.util.$content.find( select ); if ( $l.length ) { // "Recentchangeslinked" --  $l.length > 1; loop $lis =  $l.children( "li" ); n    =  $lis.length; if ( n > 1 ) { g =  new Array( n ); RLS.fiat; for ( i = 0; i < n;  i++ ) { g[ i ] =  RLS.fair( $lis.eq( i ) ); }  // for i               g.sort( RLS.first ); $lis.detach; for ( i = 0; i < n;  i++ ) { $l.append( g[ i ][ 0 ] ); }  // for i            } }     }   };   // .fresh

RLS.furnish =  function  { // Equip current page with portlet links // Uses: //   this //   >  .specialPage //   >  .modeURL //   >  .modeUser //   >  .modeSize //   >  .modeSumm //   .favorite //   .factory // 2012-10-29 PerfektesChaos@de.wikipedia this.favorite; this.factory( "Name", 0, null );  // "ca-history" ever? switch ( this.specialPage ) { case "LinkSearch" : this.factory( "URL", this.modeURL, null ); break; case "Newpages" : this.factory( "User", this.modeUser, null ); this.factory( "Size", this.modeSize, null ); this.factory( "Summary", this.modeSumm, null ); break; case "Recentchanges" : case "Watchlist" : this.factory( "User", this.modeUser, null ); this.factory( "Summary", this.modeSumm, null ); break; }  // switch .specialPage };  // .furnish

if ( ! RLS.loaded ) { RLS.loaded =  true; if ( mw.config.get( "wgNamespaceNumber" ) ===  -1 ) { $( document ).ready( function {               RLS.specialPage  =                           mw.config.get( "wgCanonicalSpecialPageName" );               mw.loader.using( [ "user", "mediawiki.util" ], RLS.fire );                                         } );   // .ready }  }   mw.loader.state( "ext.gadget.resultListSort", "ready" ); }( mediaWiki, jQuery ) );

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

/// EOF  resultListSort/d.js