User talk:Sarang/simpleSVGcheck/sandbox.js
see also(link) to P-check, P-cleanup (u); S-check, S-cleanup, S-x-cleanup.
User existence check[edit]
// * ============================================================
// The page for a specified user often does not exist, causing a redlink.
// It is possible to avoid it and link instead to an existing page:
// template:Uc checks whether User talk, user~commonswiki or -talk exists;
// in the worst case up to four checks are necessary - each time when the file description is displayed.
// But page existence check is an expensive access.
// Template:Uc bears the advantage that it will show dynamically the "nearest" substitute valid at run time.
// When the expensive selection is done by the script, future accesses become static:
// the link to a not-existing page is replaced by a valid link.
// A disadvantage of this method against T:Uc is the lack of dynamics - when missing pages are generated later.
// * it is very unlikely that a ~commonswiki talk page will get later its user page.
// * ~commonswiki must never be changed to a correspondent non~commonswiki page (it is another user)
// * but the creation of a missing user page may occur at any time.
// Only in the last case the access to the substituting talk page is not any longer the best choice:
// it is not really wrong, but e.g. author can only be a user and not his talk page.
// It remains the task of a global replacing tool to comply with such changes.
function pageexist( pagename, callback ) { // version with Asynchronous JavaScript+XML
$.ajax( mw.util.getUrl( pagename ), // recommended by ExE Boss
{ method: 'HEAD',
success: function () { callback( true ); },
error: function ( xhr ) { callback( xhr.status === 404 ? false : null ); }
} );
}
// * ============================================================
if (ns === 'U' && lang === '') // user existence check
{ alert(link); { ns =
pageexist("User:" + link, (existU) =>
{ if (existU) {ns = 'U'; alert('0='+ns); } // the user page exists
else if (existU == null) // CommonsMaintenanceBot issue
{ns = 'Uf';alert('0='+ns); } // invalid user ID
else {ns = 'Un';alert('0='+ns); } // no page - continue
}); } alert('1='+ns);
if (ns === 'Un') { ns =
pageexist("User talk:" + link, (existT) =>
{ if (existT) {ns = 'Ur'; alert('0='+ns); } // the user talk page exists
else ns = 'Un';}); alert('2='+ns); }
if (ns === 'Un') { ns =
pageexist("User:" + link + '~commonswiki', (existW) =>
{ if (existW) {ns = 'Uw';alert('w='+ns); } // ~user page exists
else ns = 'Un';}); alert('3='+ns); }
if (ns === 'Un') { ns =
pageexist("User talk:" + link + '~commonswiki', (existX) =>
{ if (existX) ns = 'Uwt'; // ~user talk page exists
else ns = 'Un'; }); alert('4='+ns); }
}
// * ============================================================
For testing, "link" got the following pseudo IDs :
pseudo id | ns | ok | |
---|---|---|---|
Username | U | ✓ | existing user ID |
UserTalkTest | Ut | × | only talk, but was true as user |
Essjay | Uw | ✓ | existing ~commonswiki user ID |
HandigeHarry | Uwt | × | only ~talk, but was true as ~user |
not_a_user | Un | ✓ | result correct (after 4 checks) |
[@*!] | Uf | ✓ | invalid ID |
but when the alert-dialogs are suppressed the results are 'undefined'
Due to my very poor JS knowledge, code may be written too complicated, or may be illogical.
In my believe it would be much easier when the function gets not only the id, but also the ns-value for successfulness,
and the ns-value when not successful (can also be fixed 'Un' for this usage), to return instead of the Boolean results. -- sarang♥사랑 11:19, 27 July 2021 (UTC)
Changes[edit]
The function for general existence check became embedded into the special user page existence
with appropriate return values. This simplifies drastically the repeated usage.
function pageexists( pagename, callback ) {
$.ajax( mw.util.getUrl( pagename ),
{ method: 'HEAD',
success: function () { callback( true ); },
error: function ( xhr ) { callback( xhr.status === 404 ? false : null ); }
} );
}
// * ============================================================
function userexist( username, namespace )
{ retval = 'Un';
pageexists ( username, (boolres) =>
{ if (boolres) retval = namespace;
else
if (boolres == null) // CommonsMaintenanceBot issue
retval = 'Uf'; // else 'Un';
// alert('p='+retval); // does not work without
});
return retval;
}
// ===================================================================
if (ns === 'U' && lang === '') // user existence check
{ ns = userexist("User:" + link, 'U');
if (ns === 'Un')
ns = userexist("User talk:" + link, 'Ut'};
if (ns === 'Un')
ns = userexist("User:" + link + '~commonswiki', 'Uw');
if (ns === 'Un')
ns = userexist("User talk:" + link + '~commonswiki', 'Uwt');
}
But the two problems mentioned above remain. -- sarang♥사랑 05:25, 29 July 2021 (UTC)
Situation at 08-08[edit]
For each user specified, the existence will be checked. The "expensive" extern access may become necessary up to four times for each user. Because a user can be specified multiple times (four, six or more times), prior to the access a table is checked - when the user id is found the namespace is used from the table; otherwise the access is performed and afterwards the user id is inserted into the table. When this id occurs another time it will be taken from the table - no second access happens. Every extern access is counted and the total is displayed finally.
// * ============================================================
if (ns === 'U' && lang === '') // user existence check
{ let linx = link.slice(0, 1).toUpperCase() + link.slice(1); // standardize user id
ns = Usertab[linx] ? ''+Usertab[linx] : 'Un'; // test: with prefix when from Usertab
if (ns === 'Un')
ns = userexist("User:" + linx, 'U');
if (ns === 'Un')
ns = userexist("User talk:" + linx, 'Ut');
if (ns === 'Un')
ns = userexist("User:" + linx + '~commonswiki', 'Uw');
if (ns === 'Un')
ns = userexist("User talk:" + linx + '~commonswiki', 'Uwt');
Usertab[linx] = ns; // new element into Usertab
}
// * ============================================================
But the suggested async option makes problems: it seems to require ECMA script version 6 and 8, and causes other troubles. I tried to put it into another script which can be handled via export and import. But my knowledge is too poor to get this working. So I am using currently the synchronous version with a boring alert which is nevertheless faster than checking the user ids otherwise. A test with setTimeout was not successful.
// * ============================================================
function pageexists( pagename, callback ) {
mw.loader.using( 'mediawiki.api', function () {
( new mw.Api() ).get(
{ action: 'query',
prop: 'info',
titles: pagename,
formatversion: 2
},
{
success: function ( response )
{ var page = response.query.pages[ 0 ];
if ( page.invalid )
{ callback( null );}
else
if ( page.missing )
{ callback( false );}
else
{ callback( true );}
},
error: function () { callback( null ); }
}
);
} );
}
// * ============================================================
function userexist( username, namespace )
{ let retval = 'Un';
++tstex; // add 1 for each one of the expensive accesses
pageexists ( username, (boolres) =>
{ // await;
if (boolres) retval = namespace; // ns when 'true'
else
if (boolres == null) // CommonsMaintenanceBot issue
retval = 'Uf'; // when 'null'
// else retval = 'Un'; // when 'false'
}); // setTimeout ( function() {return;} , 250);
alert('r='+retval+'/'+username); // does not work without that communication
return retval;
}
// * ============================================================
-- sarang♥사랑 13:55, 7 August 2021 (UTC)
Tests show that the above code works well with e.g. FireFox or MicroSoft-edge (both new downloads);
but with browsers as Opera or GoogleChrom the existence is never 'true' (unknown release versions)
I do not know how to check under which browser the script is executed, so I cannot avoid the useless run under Opera or GoogleChrom.
-- sarang♥사랑 12:23, 28 August 2021 (UTC)