// <syntaxhighlight lang="javascript">
/*
QuickPortal.js is intended to apply \{\{subst:Basic portal start page\}\} to
create a portal, or overwrite an existing portal.
When you ctrl-click on a portal redlink (in Firefox or Chrome), a new portal
is created in a new tab in preview mode waiting to be saved. So, if
you ctrl-click on 20 such redlinks, you get 20 tabs each with a new
portal in preview mode waiting for inspection. Regular clicking on
a portal redlink creates a new portal for that subject in the current
window.
The script also presents two menu items:
The "Restart portal" menu item appears when a portal base page is displayed.
Clicking on that puts the portal in edit mode. (Development note: Clicking
on that when a portal is in edit mode will replace the portal's content with
the new portal design, same as the feature that creates a new portal above.)
The "Matching portal" menu item doesn't do anything yet, but shows up
when an article is displayed. (Development note: It will go to the matching
portal, or if there isn't one, create it. But, when the redlink shows up
on the screen to create the page, how do you click it automatically?)
Development notes:
1) For portal put into edit mode by the script, while ignoring edit pages
arrived at by other means, process the portal (replace contents with
subst:quickportal). This will require localstorage.
2) Make "Matching portal" menu item do its thing - When clicked, should
jump to portal with matching name. If no portal is there, it makes one.
Development notes:
After portal is saved, place links to the portal from:
1) Cooresponding root article's See also section, but only if it does not already exist
2) Cooresponding category page, but only if it does not already exist
3) The bottom of the corresponding navigation footer template,
but only if it does not already exist
*/
// ============== Set up ==============
// Start off with a bodyguard function to reserve the aliases mw and $ within
// the scope of this function, so that we can rely on them meaning "mediawiki"
// and "jQuery", respectively: these alias definitions follow this function's
// closing curly bracket at the end of the script.
( function ( mw, $ ) {
// ============== Load dependencies ==============
// For support of mw.util.addPortletLink
mw.loader.using( ['mediawiki.util'], function () {
// ============== ready() event listener/handler ==============
// below is jQuery short-hand for $(document).ready(function() { ... });
// it makes the rest of the script wait until the page's DOM is loaded and ready
$(function() {
// End of set up
// ============== CORE PROGRAM ==============
// ============== Conditionals ==============
// Run the insertQuickPortal function if "Creating Portal:" is in the page title
if (document.title.indexOf("Creating Portal:") != -1) {
// But only if it is empty
if ($('#wpTextbox1').is(':empty')){
insertQuickPortal();
}
}
// Create the "Restart portal" menu item for when a portal is displayed
if (document.title.indexOf("Portal:") === 0) {
// But not for a subpage (page with a forward slash in its title) or a search results page
if ((document.title.indexOf("/") == -1) && (document.title.indexOf("Search results") == -1)) {
//Create linked menu item on side bar menu (in toolbox section)
var menuItem1 = mw.util.addPortletLink( 'p-tb', '#', 'Restart portal', 'tb-restartportal', 'Replace with Basic portal start page');
// Bind click handler
$( menuItem1 ).click( function ( event ) {
event.preventDefault();
// above line prevents any default action,
// as we want only the following action to run:
// Do some stuff when clicked...
// Development note: store pagename in local storage
if (document.title.indexOf("Editing Portal:") === 0) {
// Invoke a function by its name
// (The function itself is defined further down the page, using the word "function").
insertQuickPortal();
} else if (document.title.indexOf("Portal:") === 0) {
// Invoke a function by its name; this one is like clicking "edit source"
// (The function itself is defined further down the page, using the word "function").
invokeEditPage();
}
});
}
}
// Development note:
// Run the insertQuickPortal function if on editing page of portal
// that we designated to be restarted in local memory. And we clear
// that memory.
// This section has 2 problems:
// 1) It runs in a perpetual loop (check a timestamp, and run only if
// a certain amount of time has elapsed?) (When and how do you remove the
// localstorage item?)
// 2) It runs on any portal edit page (needs to know which one)
//if (document.title.indexOf("Editing Portal:") === 0) {
// insertQuickPortal();
//}
// Create the "Matching portal" menu item for when an article is displayed
if (mw.config.get('wgNamespaceNumber') === 0) {
// But not for a list, index, outline, timeline, or subpage
if ((document.title.indexOf("List of ") == -1) && (document.title.indexOf("Index of ") == -1) && (document.title.indexOf("Outline of ") == -1) && (document.title.indexOf("Timeline of ") == -1) && (document.title.indexOf("/") == -1)) {
//Create linked menu item on side bar menu (in toolbox section)
var menuItem2 = mw.util.addPortletLink( 'p-tb', '#', 'Matching portal', 'tb-matchingportal', 'Jump to matching portal, or create one');
// Bind click handler
$( menuItem2 ).click( function ( event ) {
event.preventDefault();
// above line prevents any default action,
// as we want only the following action to run:
// Do some stuff when clicked...
// Development note: store pagename in local storage
// Invoke a function by its name; this one is like clicking "edit source"
// (The function itself is defined further down the page, using the word "function").
matchingPortal();
});
}
}
} );
} );
// ============== Subroutines ==============
function insertQuickPortal() {
var wpTextbox1 = document.getElementById('wpTextbox1');
// Insert \{\{subst:Basic portal start page\}\} into the editing box
// The following is a modification from "Your first script" in
// https://en.wikipedia.org/w/index.php?title=Wikipedia:User_scripts/Guide&oldid=852316213
document.editform.wpTextbox1.value = "{" + "{Construction}}" + "{" + "{subst:Basic portal start page}}";
// Generate a preview of the page.
$('#wpPreviewWidget > input').click();
//document.editform.submit(); //this line saves the page
}
function invokeEditPage() {
// Get edit page
window.___location = window.___location.href.substr(0, window.___location.href.indexOf('#'))+"?action=edit";
// Development note: remove corresponding localstorage item
// localStorage.removeItem('myCat');
}
function matchingPortal() {
// Development note: goto matching portal, otherwise, create it
alert(window.___location);
}
}) ( mediaWiki, jQuery ); //the end of bodyguard function
// END OF PROGRAM
// </syntaxhighlight>