summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2018-01-16 10:00:52 -0500
committerbill-auger <mr.j.spam.me@gmail.com>2018-02-13 13:29:34 -0500
commit414c87a6ba3b2694ed282e3f6de23da42b49e572 (patch)
treeed7ac8f98f1c508afa3958bab79f1a853391edb6
parent729eb56815dbd9b3c9d397b3d78f0e3ca58ed219 (diff)
stash
-rw-r--r--blacklist-testdata.json10
-rw-r--r--post_fsd_wiki.phantomjs244
2 files changed, 152 insertions, 102 deletions
diff --git a/blacklist-testdata.json b/blacklist-testdata.json
new file mode 100644
index 0000000..a58da60
--- /dev/null
+++ b/blacklist-testdata.json
@@ -0,0 +1,10 @@
+[
+ {
+ "original_package": "dummy package 1" ,
+ "short_description": "dummy description 1"
+ } ,
+ {
+ "original_package": "dummy package 2" ,
+ "short_description": "dummy description 2"
+ }
+]
diff --git a/post_fsd_wiki.phantomjs b/post_fsd_wiki.phantomjs
index 97eaf1f..003c80f 100644
--- a/post_fsd_wiki.phantomjs
+++ b/post_fsd_wiki.phantomjs
@@ -1,30 +1,40 @@
-var WIKI_BASE_URL = 'http://localhost/mediawiki/index.php' ;
+var BLACKLIST_DATA_FILE = './blacklist-testdata.json' ;
+var WIKI_BASE_URL = 'http://localhost/mediawiki/index.php' ;
// var WIKI_BASE_URL = 'https://directory.fsf.org/wiki?title=' ;
// var LOGIN_URL = WIKI_URL + '/index.php?title=Special:Userlogin&action=submitlogin' ;
-var LOGIN_URL = WIKI_BASE_URL + '?title=Special:UserLogin' ;
-var EDIT_URL = WIKI_BASE_URL + '?title=_TITLE_&action=edit&redlink=1' ;
-var TITLE_URL_REGEX = '/_TITLE_/' ;
-var USAGE_MSG = "USAGE: phantomjs ./post_fsd_wiki.phantomjs <WIKI_LOGIN> <WIKI_PASS>" ;
-var LOGIN_IMPUT_ID = 'wpName1' ;
-var PASS_IMPUT_ID = 'wpPassword1' ;
-var SUBMIT_IMPUT_ID = 'wpLoginAttempt' ;
-var USERNAME_LI_ID = 'pt-userpage' ;
-var CONTENT_INPUT_ID = 'wpTextbox1' ;
-var EDIT_FORM_ID = 'editform' ;
-var STEP_FUNCTION_KEY = 'step-finction' ;
-var PAGELOAD_WAIT_KEY = 'wait-for-pageload' ;
+var LOGIN_URL = WIKI_BASE_URL + '?title=Special:UserLogin' ;
+var EDIT_URL = WIKI_BASE_URL + '?title=_TITLE_&action=edit&redlink=1' ;
+var USAGE_MSG = "USAGE: phantomjs ./post_fsd_wiki.phantomjs <WIKI_LOGIN> <WIKI_PASS>" ;
+var TITLE_URL_REGEX = /_TITLE_/ ;
+var WIKI_CONTENT_BEGIN = '<!-- PARABOLA BLACKLIST BEGIN (DO NOT EDIT) -->Parabola Blacklist Description:' ;
+var WIKI_CONTENT_END = '<!-- PARABOLA BLACKLIST END -->' ;
+var WIKI_CONTENT_REGEX = RegExp(WIKI_CONTENT_BEGIN + '.*' + WIKI_CONTENT_END) ;
+var LOGIN_IMPUT_ID = 'wpName1' ;
+var PASS_IMPUT_ID = 'wpPassword1' ;
+var SUBMIT_IMPUT_ID = 'wpLoginAttempt' ;
+var USERNAME_LI_ID = 'pt-userpage' ;
+var CONTENT_INPUT_ID = 'wpTextbox1' ;
+var EDIT_FORM_ID = 'editform' ;
+var PACKAGE_NAME_KEY = 'original_package' ; var PAGE_TITLE_KEY = PACKAGE_NAME_KEY ;
+// var REPLACEMENT_KEY = 'libre_replacement' ;
+// var REFERENCE_KEY = 'ref' ;
+// var ENTRY_ID_KEY = 'id' ;
+var DESCRIPTION_KEY = 'short_description' ; var PAGE_TEXT_KEY = DESCRIPTION_KEY ;
+var STEP_FUNCTION_KEY = 'step-function' ;
+var PAGELOAD_WAIT_KEY = 'wait-for-pageload' ;
+var STEP_TIMEOUT = 3000 ; var StepTimeout ;
var System = require('system') ;
var Args = System.args ;
var Page = require('webpage').create() ;
var WIKI_LOGIN = Args[1] || '' ;
var WIKI_PASS = Args[2] || '' ;
-var Steps = [] ;
-var Step = {} ;
+var WikiPages = [] ; var WikiPage ;
+var Steps = [] ; var Step ;
var StepN = 0 ;
-var IsLoading = false ;
+var IsReady = true ;
var ShouldQuit = false ;
-var MainIvl ;
+
if (WIKI_LOGIN == '' || WIKI_PASS == '') { DBG(USAGE_MSG) ; phantom.exit() ; }
@@ -33,57 +43,58 @@ if (WIKI_LOGIN == '' || WIKI_PASS == '') { DBG(USAGE_MSG) ; phantom.exit() ; }
function prepare()
{
- var title = 'a title' ;
- var page_title = JSON.encode(title.eplace(' ' , '_')) ;
-}
+ LOG("loading BLACKLIST_DATA_FILE") ; var blacklist_data = require(BLACKLIST_DATA_FILE) ;
-function login()
-{
-DBG("in") ;
- // Page.settings.userAgent = 'SpecialAgent';
+ if (!IsA(blacklist_data , Array )) { ForceQuit("failed to load package data") ; return ; }
- OpenUrl(LOGIN_URL , function()
+ blacklist_data.forEach(function(package_data)
{
-DBG("login() open in") ;
+ var page_title = package_data[PACKAGE_NAME_KEY] ;
+ var page_text = package_data[DESCRIPTION_KEY ] ;
- QuitOnErr(Page.evaluate(function(login_input_id , pass_input_id , submit_input_id ,
- wiki_login , wiki_pass )
- {
-console.log("login() eval in" + login_input_id) ;
+ if (!IsA(page_title , String)) { ForceQuit("invalid package data") ; return ; }
+ if (!IsA(page_text , String)) { ForceQuit("invalid package data") ; return ; }
- var login_input = document.getElementById(login_input_id ) ;
- var pass_input = document.getElementById(pass_input_id ) ;
- var submit_input = document.getElementById(submit_input_id) ;
- var submit_form = submit_input.form ;
+ LOG("found package: " + page_title) ;
+ var wiki_page = {} ;
+ wiki_page[PAGE_TITLE_KEY] = page_title.trim().replace(' ' , '_') ;
+ wiki_page[PAGE_TEXT_KEY ] = page_text .trim() ;
+ WikiPages.push(wiki_page) ;
+ }) ;
-console.log("login_input=" + ((!!login_input) ? login_input : 'NFG')) ;
-console.log("submit_input=" + ((!!submit_input) ? submit_input : 'NFG')) ;
+ IsReady = true ;
+}
- if (!login_input || !login_input || !submit_input || !submit_form)
- return "invalid login page" ;
+function loadLoginPage()
+{
+ if (WikiPages.length == 0) { ForceQuit("no WikiPages to consider") ; return ; }
- login_input.value = wiki_login ;
- pass_input.value = wiki_pass ;
+ OpenUrl(LOGIN_URL) ;
+}
-console.log("login() login_input.value=" + login_input.value) ;
-console.log("login() pass_input.value=" + pass_input.value) ;
-console.log("login() submit_input.value=" + submit_input.value) ;
+function submitLogin()
+{
+ QuitOnErr(Page.evaluate(function(login_input_id , pass_input_id , submit_input_id ,
+ wiki_login , wiki_pass )
+ {
+ var login_input = document.getElementById(login_input_id ) ;
+ var pass_input = document.getElementById(pass_input_id ) ;
+ var submit_input = document.getElementById(submit_input_id) ;
+ var login_form = submit_input.form ;
- submit_form.submit() ;
+ if (!login_input || !login_input || !submit_input || !login_form)
+ return "invalid login page" ;
-console.log("login() eval out") ;
- } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;
+ login_input.value = wiki_login ;
+ pass_input.value = wiki_pass ;
-DBG("login() open out") ;
- }) ;
-DBG("out") ;
+ login_form.submit() ;
+
+ } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;
}
function verifyLogin()
{
-DBG("in") ;
-// console.log("verifyLogin() Page.content=" + Page.content) ;
-
QuitOnErr(Page.evaluate(function(username_li_id , wiki_login)
{
var username_li = document.getElementById(username_li_id) ;
@@ -93,101 +104,130 @@ DBG("in") ;
console.info("signed in as: " + username_li.textContent) ;
} , USERNAME_LI_ID , WIKI_LOGIN)) ;
-DBG("out") ;
+}
+
+function loadEditPage()
+{
+ WikiPage = WikiPages.shift ;
+ var page_title = WikiPage[PAGE_TITLE_KEY] ;
+
+ OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title)) ;
}
function submitEditPage()
{
-DBG("in") ;
+if (DEBUG_VB) DBG("in") ;
- var page_title = 'New Page' ;
+ var content_text = WikiPage[PAGE_TEXT_KEY] ; if (content_text == '') { ForceQuit("!BUG!") ; return ; }
- OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title) , function()
+ QuitOnErr(Page.evaluate(function(content_input_id , edit_form_id ,
+ wiki_content_regex , content_text )
{
- QuitOnErr(Page.evaluate(function(content_input_id , edit_form_id , content_text)
- {
- var content_input = document.getElementById(content_input_id) ;
- var edit_form = content_input.form ;
- var existing_content = content_input.textContent ;
+ var content_input = document.getElementById(content_input_id) ;
+ var edit_form = content_input.form ;
+ var existing_content = content_input.textContent ;
-console.log("submitEditPage() content_input.textContent=" + existing_content) ;
+ if (!content_input || !edit_form) return "invalid edit page" ;
- if (!content_input || !edit_form) return "invalid edit page" ;
- } , CONTENT_INPUT_ID , EDIT_FORM_ID)) ;
- }) ;
+ var modified_content = existing_content.replace(wiki_content_regex , content_text) ;
-DBG("out") ;
-}
+console.log("submitEditPage() content_input.textContent IN=" + existing_content) ;
+console.log("submitEditPage() (modified_content == existing_content)=" + (modified_content == existing_content)) ;
+ if (modified_content != existing_content || !wiki_content_regex.test(existing_content))
+ { content_input.value = modified_content ; edit_form.submit() ; }
-/* main loop */
+console.log("submitEditPage() content_input.textContent OUT=" + existing_content) ;
-function DefineStep(step_function , should_wait_for_pageload)
-{
- var step_data = {} ;
- step_data[STEP_FUNCTION_KEY] = step_function ;
- step_data[PAGELOAD_WAIT_KEY] = should_wait_for_pageload ;
- Steps.push(step_data) ;
+ } , CONTENT_INPUT_ID , EDIT_FORM_ID ,
+ WIKI_CONTENT_REGEX , WIKI_CONTENT_BEGIN + content_text + WIKI_CONTENT_END)) ;
+
+if (DEBUG_VB) DBG("out") ;
}
+
+/* main loop */
+
function MainLoop()
{
- if (IsLoading) return ;
+if (DEBUG_VB) DBG("ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ;
+
+ if (!!ShouldQuit) { ERR(ShouldQuit) ; Done() ; return ; }
+ else if (!IsReady ) { PumpMainLoop() ; return ; }
- var step_data = Steps[StepN] ;
- var step = step_data && step_data[STEP_FUNCTION_KEY] ;
- var wait = step_data && step_data[PAGELOAD_WAIT_KEY] ;
+ clearTimeout(StepTimeout) ;
- if (ShouldQuit) LOG("forced quit") ;
- if (typeof step === 'function') ++StepN ; else ShouldQuit = true ;
+ var step = Steps.shift() ;
+ StepTimeout = setTimeout(function() { ForceQuit("timeout executing step") ; } , STEP_TIMEOUT) ;
+ IsReady = false ;
- if (ShouldQuit) { clearInterval(MainIvl) ; LOG("done") ; phantom.exit() ; }
- else { LOG("Step " + StepN + ": " + step.name) ; step() ; }
+ if (typeof step !== 'function') Done() ;
+ else { LOG("Step: " + step.name) ; step() ; PumpMainLoop() ; }
}
+function PumpMainLoop() { setTimeout(MainLoop , 250) ; }
+
+function ForceQuit(quit_msg) { ShouldQuit = quit_msg ; PumpMainLoop() ; }
+
+function Done() { LOG("done") ; phantom.exit() ; }
+
/* helpers */
-function OpenUrl(url , callback)
+function OpenUrl(url)
{
DBG("url=" + url) ;
- Page.open(url , function(status)
- {
- if (status != 'success') { ERR("status: " + status) ; ShouldQuit = true ; }
- else { callback() ; }
- }) ;
+ Page.open(url , function(status) { if (status != 'success') ForceQuit("status: " + status) ; }) ;
}
-function QuitOnErr(err) { if (!!err) { ERR(err) ; ShouldQuit = true ; } }
+function QuitOnErr(error_msg)
+{
+ if (!!error_msg) ForceQuit(error_msg) ; else IsReady = !Step[PAGELOAD_WAIT_KEY] ;
+}
+
+function IsA(an_object , expected_type)
+{
+ switch(expected_type)
+ {
+ case Array: expected_type = '[object Array]' ; break ;
+ case String: expected_type = '[object String]' ; break ;
+ }
+
+ return (Object.prototype.toString.call(an_object) === expected_type) ;
+}
/* event hendlers */
-Page.onLoadStarted = function() { IsLoading = true ; ARGS.apply("Page.onLoadStarted" , arguments) ; } ;
+Page.onLoadStarted = function() { IsReady = false ; ARGS.apply("Page.onLoadStarted" , arguments) ; } ;
-Page.onLoadFinished = function() { IsLoading = false ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;
+Page.onLoadFinished = function() { IsReady = true ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;
-Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChanged" , arguments) ; } ;
+Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChanged" , arguments) ; } ;
/* logging */
Page.onConsoleMessage = function(msg) { LOG(msg) ; } ;
+var DEBUG = true ; var DEBUG_EVS = DEBUG && false ; var DEBUG_VB = DEBUG && false ;
function LOG(log , color) { console.log((color || '\033[01;34m') + log + '\033[00m' ) ; }
function ERR(err) { LOG("ERROR: " + err , '\033[00;31m') ; }
-function DBG(dbg) { var fn_name = (!!DBG.caller.name) ? DBG.caller.name + "() " : '' ;
- LOG("DEBUG: " + fn_name + dbg , '\033[00;32m') ; }
-
-function ARGS() { LOG("EVENT: " + this) ; for (arg in arguments) LOG(" arg: " + JSON.stringify(arguments[arg])) ; }
+function DBG(dbg) { if (!DEBUG) return ;
+ dbg = ((!!DBG.caller.name) ? DBG.caller.name + "() " : '') + dbg ;
+ LOG("DEBUG: " + dbg , '\033[00;32m') ; }
+function ARGS() { if (!DEBUG_EVS) return ;
+ LOG("EVENT: " + this) ; var args = arguments ;
+ for (arg in args) LOG(" arg: " + JSON.stringify(args[arg])) ; }
/* main entry */
-DefineStep(prepare , false) ;
-DefineStep(login , true ) ;
-DefineStep(verifyLogin , false) ;
-DefineStep(submitEditPage , true ) ;
-
-MainIvl = setInterval(MainLoop , 250) ;
+Steps.push(prepare ) ;
+Steps.push(loadLoginPage ) ;
+Steps.push(submitLogin ) ;
+Steps.push(verifyLogin ) ;
+Steps.push(loadEditPage ) ;
+Steps.push(submitEditPage) ;
+MainLoop() ;