User:Headbomb/linkclassifier.js

In this article, we will explore the fascinating world of User:Headbomb/linkclassifier.js, a topic that has captured the attention of people of all ages and backgrounds. From its origins to its impact on modern society, we will delve into the most relevant aspects of User:Headbomb/linkclassifier.js, analyzing its evolution over time and its relevance today. Through different perspectives and relevant data, we seek to better understand the importance of User:Headbomb/linkclassifier.js in our days and its influence on various aspects of our daily lives. Get ready to discover all the facets of User:Headbomb/linkclassifier.js in this intriguing article!
/* If you want to use this script, simply add the following line to your ]:

importScript('User:Headbomb/linkclassifier.js'); // Linkback: ], based on ]

* add some CSS classes, such as those at ].
*/

/* If you want this to run "on demand" instead of on every page, set "LinkClassifierOnDemand=true" and
 * use mw.util.addPortletLink() or the like to add a button calling LinkClassifier.onDemand().
 */

var LinkClassifier = {
	/* This object maps classes to the categories for which to apply them. Values may be an array of strings or a regex. */
	cats: {
		deletion: [
			'Category:All articles proposed for deletion',
			'Category:All books proposed for deletion',
			'Category:All categories for discussion',
			'Category:All disputed non-free Wikipedia files',
			'Category:All files proposed for deletion',
			'Category:All orphaned non-free use Wikipedia files',
			'Category:All redirects for discussion',
			'Category:All replaceable non-free use Wikipedia files',
			'Category:All Wikipedia files with no non-free use rationale',
			'Category:All Wikipedia files with unknown copyright status',
			'Category:All Wikipedia files with unknown source',
			'Category:Articles for deletion',
			'Category:Articles for deletion using wrong syntax',
			'Category:Articles on deletion review',
			'Category:Articles to be merged after an Articles for deletion discussion',
			'Category:Candidates for speedy deletion',
			'Category:Candidates for undeletion',
			'Category:Categories for conversion',
			'Category:Categories for deletion',
			'Category:Categories for listifying',
			'Category:Categories for merging',
			'Category:Categories for renaming',
			'Category:Categories for speedy renaming',
			'Category:Categories to be listified then deleted',
			'Category:Duplicate or hardcoded templates awaiting deletion',
			'Category:Empty categories awaiting deletion',
			'Category:Items pending OTRS confirmation of permission for over 30 days',
			'Category:Miscellaneous pages for deletion',
			'Category:Templates for deletion',
			'Category:Templates for merging',
			'Category:Templates for discussion',
			'Category:Wikipedia files for discussion'
		].sort(),
		disambiguation: [
			'Category:All disambiguation pages'
		].sort(),
		'set-index': [
			'Category:All set index articles'
		].sort(),
		'featured-content': [
			'Category:Featured articles',
			'Category:Featured lists',
			'Category:Featured pictures',
			'Category:Featured sounds',
			'Category:Featured videos',
			'Category:Featured portals'
		].sort(),
		'good-content': [
			'Category:Good articles'
		].sort(),
		'soft-redirect-cats': [
			'Category:Wikipedia soft redirected categories'
		].sort(),
		'spoken-articles': [
			'Category:Spoken articles'
		].sort(),
		stubcls: /^Category:.* stubs$/,
		'nonfree-media': [
			'Category:All non-free media'
		].sort(),
		unprintworthy: [
			'Category:Unprintworthy redirects',
			'Category:Middle-earth redirects from redundant titles'
		].sort(),
		'unprintworthy-shortcut': [
			'Category:Redirects from shortcuts'
		].sort(),
		'linked-misspellings': [
			'Category:Redirects from misspellings',
			'Category:Redirects from incorrect hyphenation',
			'Category:Redirects from miscapitalisations',
			'Category:Redirects from incorrect names',
			'Category:Redirects from incorrect disambiguation',
			'Category:Redirects from titles without diacritics',
			'Category:Redirects from titles with diacritics',
			'Category:Redirects from database entries'
		].sort(),
		'linked-other-capitalisations': [
			'Category:Redirects from other capitalisations'
		].sort(),
		'linked-ISO4': [
			'Category:Redirects from ISO 4 abbreviations'
		].sort(),
		'linked-mathscinet': [
			'Category:Redirects from MathSciNet abbreviations'
		].sort(),
		'linked-nlm': [
			'Category:Redirects from NLM abbreviations'
		].sort(),
		'linked-bluebook': [
			'Category:Redirects from Bluebook abbreviations'
		].sort()
	},

	/* This object maps page props to CSS classes for which to apply them. Values may be an array of strings or a function returning such. */
	props: {
		disambiguation: [
			'disambiguation'
		]
	},

	/* This regex matches page titles to be marked as intentional links to disambiguation pages */
	intentionaldab: / \(disambiguation\)$/,

	/* Was it run already? */
	wasRun: false,

	onAjaxError: function ( xhr, textStatus, errorThrown ) {
		mw.log.error( 'AJAX error: ' + textStatus + ' ' + errorThrown );
		return;
	},

	callback: function ( r ) {
		var i, j, k, k2, v, node, alist, q, prefix, seen, cls,
			redir = {},
			redirlist = ,
			cats = {},
			missing = {},
			classes = {};

		if ( !r.query ) {
			if ( !window.console || !$.isFunction( window.console.error ) ) {
				mw.log.error( 'Bad response' );
				return;
			}
			window.console.error( 'Bad response', r );
			return;
		}
		if ( r ) {
			q = this.rawdata;
			for ( k in r ) {
				for ( k2 in r ) {
					q = r;
				}
			}
			$.ajax( {
				url: mw.util.wikiScript( 'api' ),
				dataType: 'json',
				type: 'POST',
				data: q,
				rawdata: this.rawdata,
				success: LinkClassifier.callback,
				error: LinkClassifier.onAjaxError
			} );
		}
		r = r.query;

		node = document.getElementById( 'wikiPreview' );
		if ( !node ) {
			node = document.getElementById( 'bodyContent' );
		}
		if ( !node ) {
			mw.log.error( 'Huh? No body content?' );
			return;
		}
		alist = node.getElementsByTagName( 'A' );
		if ( alist.length === 0 ) {
			return;
		}

		if ( r.redirects ) {
			for ( i = r.redirects.length - 1; i >= 0; i-- ) {
				redir.from] = r.redirects.to;
				redirlist.push( r.redirects.from );
			}
		}
		if ( redirlist.length > 0 ) {
			q = {
				format: 'json',
				action: 'query',
				titles: redirlist.join( '|' ),
				prop: 'categories|info',
				inprop: 'protection',
				cllimit: 'max',
				rawcontinue: 1
			};
			$.ajax( {
				url: mw.util.wikiScript( 'api' ),
				dataType: 'json',
				type: 'POST',
				data: q,
				rawdata: q,
				success: LinkClassifier.callback,
				error: LinkClassifier.onAjaxError
			} );
		}

		prefix = this.rawdata.redirects ? '' : 'redir-';
		if ( r.pages ) {
			for ( i in r.pages ) {
				classes.title] = ;
				missing.title] = r.pages.missing !== undefined;
				if ( r.pages.categories ) {
					cats.title] = r.pages.categories.map( function ( a ) {
						return a.title;
					} ).sort();
				}
				if ( r.pages.pageprops ) {
					for ( k in r.pages.pageprops ) {
						if ( !LinkClassifier.props ) {
							continue;
						}
						v = LinkClassifier.props;
						if ( $.isFunction( v ) ) {
							v = v( r.pages.pageprops, k, r.pages.title );
						}
						classes.title].push.apply( classes.title], v );
					}
				}
				if ( r.pages.protection ) {
					seen = {};
					for ( j = r.pages.protection.length - 1; j >= 0; j-- ) {
						cls = prefix + 'protection-' + r.pages.protection.type + '-' + r.pages.protection.level;
						if ( !seen ) {
							seen = 1;
							classes.title].push( cls );
						}
						if ( r.pages.protection.expiry === 'infinity' ) {
							cls += '-indef';
							if ( !seen ) {
								seen = 1;
								classes.title].push( cls );
							}
						}
					}
				}
				if ( r.pages.flagged ) {
					if ( r.pages.lastrevid !== r.pages.flagged.stable_revid ) {
						classes.title].push( 'needs-review' );
					}
				}
			}
		}
		Array.prototype.forEach.call( alist, function ( a ) {
			var cns, cls, m, i, j, pageCats, matchCats,
				$a = $( a );

			if ( a.wikipage === undefined ) {
				return;
			}
			if ( redir ) {
				$a.addClass( 'redirect' );
				a.wikipage = redir;
				a.title = a.wikipage;
				cns = mw.config.get( 'wgCanonicalNamespace' );
				if ( a.wikipage === ( cns ? cns + ':' : '' ) + mw.config.get( 'wgTitle' ) ) {
					$a.addClass( 'self-redirect' );
				}
				if ( missing ) {
					$a.addClass( 'broken-redirect' );
				}
			}
			m = a.href.match( /#.*/ );
			if ( m && m.substr( 0, 10 ) !== '#cite_note' ) {
				a.title = a.title.replace( /#.*/, '' ) +
					m.replace( /_/g, ' ' ).replace( /\.()/gi, function ( x, n ) {
						return String.fromCharCode( parseInt( n, 16 ) );
					} );
			}
			if ( LinkClassifier.intentionaldab.test( a.origwikipage ) ) {
				$a.addClass( 'intentional-disambiguation' );
			}
			if ( classes ) {
				for ( j = classes.length - 1; j >= 0; j-- ) {
					$a.addClass( classes );
				}
			}
			if ( a.wikipage !== a.origwikipage && classes ) {
				for ( j = classes.length - 1; j >= 0; j-- ) {
					$a.addClass( classes );
				}
			}

			pageCats = ;
			if ( cats ) {
				pageCats = pageCats.concat( cats );
			}
			if ( a.wikipage !== a.origwikipage && cats ) {
				pageCats = pageCats.concat( cats );
			}
			if ( pageCats.length > 0 ) {
				pageCats = pageCats.sort();
				for ( cls in LinkClassifier.cats ) {
					i = pageCats.length - 1;
					matchCats = LinkClassifier.cats;
					if ( matchCats instanceof RegExp ) {
						while ( i >= 0 ) {
							if ( matchCats.test( pageCats ) ) {
								$a.addClass( cls );
								break;
							}
							i--;
						}
					} else {
						j = matchCats.length - 1;
						while ( i >= 0 && j >= 0 ) {
							if ( pageCats === matchCats ) {
								$a.addClass( cls );
								break;
							}
							if ( pageCats > matchCats ) {
								--i;
							} else {
								--j;
							}
						}
					}
				}
			}
		} );
	},

	draftsCallback: function ( r ) {
		var i, node, alist,
			found = {};

		if ( !r.query ) {
			if ( !window.console || !$.isFunction( window.console.error ) ) {
				mw.log.error( 'Bad response' );
				return;
			}
			window.console.error( 'Bad response', r );
			return;
		}
		r = r.query;

		node = document.getElementById( 'wikiPreview' );
		if ( !node ) {
			node = document.getElementById( 'bodyContent' );
		}
		if ( !node ) {
			mw.log.error( 'Huh? No body content?' );
			return;
		}
		alist = node.getElementsByTagName( 'A' );
		if ( alist.length === 0 ) {
			return;
		}

		if ( r.pages ) {
			for ( i in r.pages ) {
				found.title] = r.pages.missing === undefined;
			}
		}
		Array.prototype.forEach.call( alist, function ( a ) {
			if ( a.wikipage !== undefined && found ) {
				$( a ).addClass( 'has-draft' );
			}
		} );
	},

	getPageName: function ( url ) {
		var t, m = url.match( /\/wiki\/(+)/ );
		if ( !m ) {
			m = url.match( /\/w\/index.php\?(?:.*&)?title=(+)/ );
		}
		if ( !m ) {
			return '';
		}
		t = decodeURIComponent( m ).replace( /_/g, ' ' );
		if ( t.substr( 0, 6 ) === 'Image:' ) {
			t = 'File:' + t.substr( 6 );
		}
		if ( t.substr( 0, 11 ) === 'Image talk:' ) {
			t = 'File talk:' + t.substr( 6 );
		}
		if ( t.substr( 0, 8 ) === 'Special:' ) {
			t = '';
		}
		return t;
	},

	classifyChildren: function ( node ) {
		mw.loader.using( , function () {
			var alist, titles, draftTitles, re, self, props, i, k;

			LinkClassifier.wasRun = true;
			alist = node.getElementsByTagName( 'A' );
			if ( !alist.length ) {
				return;
			}
			self = LinkClassifier.getPageName( location.href );
			titles = Array.prototype.map.call( alist, function ( a ) {
				a.wikipage = '';
				if ( /(^|\s)(external|extiw)(\s|$)/.test( a.className ) ) {
					return '';
				}
				if ( !/(^|\s)(image)(\s|$)/.test( a.className ) ) {
					a.className += ' nonimage';
				}
				a.wikipage = LinkClassifier.getPageName( a.href );
				if ( a.wikipage === self ) {
					a.wikipage = '';
				}
				a.origwikipage = a.wikipage;
				return a.wikipage;
			} ).sort().filter( function ( e, i, a ) {
				return e !== '' && ( i === 0 || a !== e );
			} );

			re = ;
			for ( k in mw.config.get( 'wgNamespaceIds' ) ) {
				if ( k !== '' ) {
					re.push( k.replace( /_/g, ' ' ) );
				}
			}
			re = new RegExp( '^(' + re.join( '|' ) + '):', 'i' );
			draftTitles = ;
			for ( i = titles.length - 1; i >= 0; i-- ) {
				if ( !re.test( titles ) ) {
					draftTitles.push( 'Draft:' + titles )
				}
			}

			props = ;
			for ( k in LinkClassifier.props ) {
				props.push( k );
			}

			function processLinks( limit ) {
				var q;
				while ( titles.length > 0 ) {
					q = {
						format: 'json',
						action: 'query',
						titles: titles.splice( 0, limit ).join( '|' ),
						prop: 'categories|pageprops|info|flagged',
						redirects: 1,
						cllimit: 'max',
						inprop: 'protection',
						rawcontinue: 1
					};
					if ( props.length <= limit ) {
						q.ppprop = props.join( '|' );
					}
					$.ajax( {
						url: mw.util.wikiScript( 'api' ),
						dataType: 'json',
						type: 'POST',
						data: q,
						rawdata: q,
						success: LinkClassifier.callback,
						error: LinkClassifier.onAjaxError
					} );
				}

				while ( draftTitles.length > 0 ) {
					q = {
						format: 'json',
						action: 'query',
						titles: draftTitles.splice( 0, limit ).join( '|' ),
						rawcontinue: 1
					};
					$.ajax( {
						url: mw.util.wikiScript( 'api' ),
						dataType: 'json',
						type: 'POST',
						data: q,
						rawdata: q,
						success: LinkClassifier.draftsCallback,
						error: LinkClassifier.onAjaxError
					} );
				}
			}

			if ( titles.length <= 100 ) {
				// Not worth querying the API to see if the user has apihighlimits
				processLinks( 50 );
			} else {
				// Note mw.user.getRights queries the API
				mw.user.getRights( function ( rights ) {
					processLinks( ( rights.indexOf( 'apihighlimits' ) >= 0 ) ? 500 : 50 );
				} );
			}
		} );
	},

	onLoad: function () {
		if ( window.LinkClassifierOnDemand ) {
			return;
		}
		if ( window.AJAXPreview ) {
			window.AJAXPreview.AddOnLoadHook( LinkClassifier.classifyChildren );
		}
		LinkClassifier.onDemand();
	},

	onDemand: function () {
		mw.hook( 'LinkClassifier' ).fire( this );
		var node = document.getElementById( 'wikiPreview' );
		if ( !node ) {
			node = document.getElementById( 'bodyContent' );
		}
		if ( node ) {
			LinkClassifier.classifyChildren( node );
		}
	},

	rerun: function () {
		if ( LinkClassifier.wasRun ) {
			LinkClassifier.onDemand();
		}
	}
};

if ( !window.LinkClassifierOnDemand ) {
	$( document ).ready( LinkClassifier.onLoad );
}