User:Yair rand/IncubatorSimplify.js

From Wikimedia Incubator

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// This script adds some simplifying bits to the Wikimedia Incubator:
// * For users that have not set their test project preferences, the script 
//   adds proper prefixed links to the search page, based on previously viewed
//   test project (or manually entered prefix if available), so that setting
//   preferences before editing is not an absolute requirement for creating 
//   pages.
// * Adds "Set language" button to the sidebar, to easily change interface 
//   language preferences to that of the viewed test project.
// * Adds "Set (test project) as your test project" button to the sidebar.

( function () {
	mw.loader.using( "mediawiki.user", function () {
		var userTestProjectWiki = mw.user.options.values[ "incubatortestwiki-project" ],
			userTestProjectLang = mw.user.options.values[ "incubatortestwiki-code" ],
			userTestProject = "W" + userTestProjectWiki + "/" + userTestProjectLang,
			prefixRegexp = /^(?:.+?:)?((W[pbnqyt])\/(.+?))(?:\/.*)?$/,
			pageName = mw.config.get( 'wgPageName' ),
			isSearch = pageName === "Special:Search",
			searched = isSearch && mw.util.getParamValue( "search" ),
			// If searched term has a namespace, don't do anything fancy with the
			// search box. Non-mainspace pages can be either prefixed or unprefixed.
			searchedHasNamespace = isSearch && searched && searched.split( ":" ).length > 1,
			searchPrefix = isSearch && mw.util.getParamValue( "incprefix" ),
			// If searched title already has a prefix, don't add searchPrefix.
			fullSearchedTitle = isSearch && ( 
				prefixRegexp.test( searched ) ? 
					searched : 
					searchPrefix + "/" + searched
			),
			userLang = mw.config.get( 'wgUserLanguage' ),
			pageNameParts = prefixRegexp.exec( searchPrefix || pageName ),
			prefix = pageNameParts && pageNameParts[ 1 ],
			testProject = pageNameParts && pageNameParts[ 2 ],
			testLang = pageNameParts && pageNameParts[ 3 ],
			testLangName = window.wpAvailableLanguages && wpAvailableLanguages[ testLang ] || testLang,
			messages = {
				en: {
					setLangToProject: "Set language: $1",
					setLangToProjectTooltip: "Set your interface language to $1. ",
					setProjectToCurrent: "Set $1 as your test project",
					setProjectToCurrentTooltip: "Set $1 to be your test project, so that new pages you create will be part of this project."
				}
			};
		
		mw.messages.set( messages[ userLang ] || messages.en );
		
		if ( userTestProjectWiki === "none" ) {
			// Add to the search page a page link with the prefix, based on
			// previously viewed test project.
			if ( isSearch && search && !searchedHasNamespace ) {
				var $createLink = $( ".mw-search-createlink" );
				if ( $createLink.length ) {
					// Determine of the searched page exists.
					var redLinkRequest = $.get( "/w/api.php", {
						format: "json",
						action: "query",
						titles: fullSearchedTitle,
						formatversion: 2
					} );
					// Retrieve the relevant search response messages.
					var messageRequest = $.get( "/w/api.php", {
						format: "json",
						action: 'query',
						meta: 'allmessages',
						amlang: userLang,
						ammessages: "wminc-search-nocreate-suggest|searchmenu-exists",
						maxage: 60 * 60 * 24 * 30,
						smaxage: 60 * 60 * 24 * 30
					} );
					$.when( messageRequest, redLinkRequest ).done( function ( msgdata, linkdata ) {
						var missing = linkdata[ 0 ].query.pages[ 0 ].missing,
							message = msgdata[ 0 ].query.allmessages[ missing ? 0 : 1 ];
						mw.messages.set( message.name, message['*'] );
						if ( testProject && testLang ) {
							$( function () {
								// Replace the search response text.
								$createLink.html(
									mw.message(
										message.name, 
										missing ? searched : fullSearchedTitle, 
										0, 
										fullSearchedTitle
									).parse()
								);
								if ( missing ) {
									// Make the link a proper red link if page does
									// not exist.
									$createLink.find( "a" )
										.addClass( "new" )
										.attr( "href", function ( i, href ) {
											return href + "?action=edit&redlink=1";
										} );
								}
							} );
						}
					} );
				}
			}
			// Add hidden inputs to search field, so that a link can be added to the
			// search page with the correct prefix.
			if ( prefix ) {
				$( "#searchform, #search" ).append( $( "<input>", { 
					type: "hidden", name: "incprefix", value: prefix 
				} ) );
			}
		}
		
		if ( !mw.user.isAnon() ) {
			$( function () {
				// Add a sidebar link to switch interface language to that of the
				// currently viewed test project.
				testLang && testLang !== userLang && mw.util.addPortletLink(
					"p-navigation",
					( 
						location.search.length > 1 ? 
							// Don't add overlapping "setlang="s.
							location.search.replace( /([?&])setlang=[^&]*&?/, '$1' ).replace( /([^&?]$)/, "$1&" ) : 
							"?"
					) + "setlang=" + testLang,
					mw.msg( "setLangToProject", testLangName ), 
					"t-setlang",
					mw.msg( "setLangToProjectTooltip", testLangName )
				);
				
				// Add a sidebar link to make the currently viewed test project the
				// user's test project.
				prefix && prefix !== userTestProject && $( mw.util.addPortletLink(
					"p-navigation",
					"#set-project-" + prefix, // Link to this anchor, no particular reason.
					mw.msg( "setProjectToCurrent", prefix ),
					"t-sethomewiki",
					mw.msg( "setProjectToCurrentTooltip", prefix )
				) ).click( function () {
					// Send the preferences change to the server.
					$.post( "/w/api.php", {
						format: "json",
						action: "options",
						change: 
							"incubatortestwiki-project=" + testProject.substr( 1 ) + "|" +
							"incubatortestwiki-code=" + testLang,
						token: mw.user.tokens.values.csrfToken
					}, function () {
						location.reload();
					} );
				});
			} );
		}
	} );
} )();