summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--post_fsd_wiki.phantomjs156
1 files changed, 102 insertions, 54 deletions
diff --git a/post_fsd_wiki.phantomjs b/post_fsd_wiki.phantomjs
index 003c80f..29b3424 100644
--- a/post_fsd_wiki.phantomjs
+++ b/post_fsd_wiki.phantomjs
@@ -6,20 +6,21 @@ 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 WIKI_TEXT_BEGIN = '<!-- PARABOLA BLACKLIST BEGIN (DO NOT EDIT) -->Parabola Blacklist Description:' ;
+var WIKI_TEXT_END = '<!-- PARABOLA BLACKLIST END -->' ;
+var WIKI_TEXT_REGEX = RegExp(WIKI_TEXT_BEGIN + '.*' + WIKI_TEXT_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 WIKITEXT_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 PACKAGE_NAME_KEY = 'original_package' ; // BLACKLIST_DATA_FILE per blacklist SYNTAX
+var REPLACEMENT_KEY = 'libre_replacement' ; // BLACKLIST_DATA_FILE per blacklist SYNTAX
+var REFERENCE_KEY = 'ref' ; // BLACKLIST_DATA_FILE per blacklist SYNTAX
+var ENTRY_ID_KEY = 'id' ; // BLACKLIST_DATA_FILE per blacklist SYNTAX
+var DESCRIPTION_KEY = 'short_description' ; // BLACKLIST_DATA_FILE per blacklist SYNTAX
+var BLACKLIST_TAGS_KEY = 'blacklist_tags' // BLACKLIST_DATA_FILE
var STEP_FUNCTION_KEY = 'step-function' ;
var PAGELOAD_WAIT_KEY = 'wait-for-pageload' ;
var STEP_TIMEOUT = 3000 ; var StepTimeout ;
@@ -33,6 +34,7 @@ var WikiPages = [] ; var WikiPage ;
var Steps = [] ; var Step ;
var StepN = 0 ;
var IsReady = true ;
+var ShouldWait = false ;
var ShouldQuit = false ;
@@ -47,18 +49,21 @@ function prepare()
if (!IsA(blacklist_data , Array )) { ForceQuit("failed to load package data") ; return ; }
+ LOG("found " + blacklist_data.length + " packages") ;
+
blacklist_data.forEach(function(package_data)
{
var page_title = package_data[PACKAGE_NAME_KEY] ;
var page_text = package_data[DESCRIPTION_KEY ] ;
- if (!IsA(page_title , String)) { ForceQuit("invalid package data") ; return ; }
- if (!IsA(page_text , String)) { ForceQuit("invalid package data") ; return ; }
+ if (!page_title || !IsA(page_title , String) ||
+ !page_text || !IsA(page_text , String) )
+ { ForceQuit("invalid package data: " + JSON.stringify(package_data)) ; return ; }
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() ;
+ wiki_page[PACKAGE_NAME_KEY] = page_title.trim().replace(' ' , '_') ;
+ wiki_page[DESCRIPTION_KEY ] = page_text .trim() ;
WikiPages.push(wiki_page) ;
}) ;
@@ -69,13 +74,25 @@ function loadLoginPage()
{
if (WikiPages.length == 0) { ForceQuit("no WikiPages to consider") ; return ; }
+ for (page_n in WikiPages)
+ {
+ var wiki_page_data = WikiPages[page_n] ;
+ var page_title = wiki_page_data[PACKAGE_NAME_KEY] ;
+ var page_text = wiki_page_data[DESCRIPTION_KEY ] ;
+
+ if (!page_title || !IsA(page_title , String)) { ForceQuit("invalid wiki page data") ; return ; }
+ if (!page_text || !IsA(page_text , String)) { ForceQuit("invalid wiki page data") ; return ; }
+
+ LOG("loaded package data: " + page_title) ;
+ }
+
OpenUrl(LOGIN_URL) ;
}
function submitLogin()
{
QuitOnErr(Page.evaluate(function(login_input_id , pass_input_id , submit_input_id ,
- wiki_login , wiki_pass )
+ wiki_login , wiki_pass )
{
var login_input = document.getElementById(login_input_id ) ;
var pass_input = document.getElementById(pass_input_id ) ;
@@ -108,46 +125,76 @@ function verifyLogin()
function loadEditPage()
{
- WikiPage = WikiPages.shift ;
- var page_title = WikiPage[PAGE_TITLE_KEY] ;
+ WikiPage = WikiPages.shift() ;
+ var page_title = WikiPage[PACKAGE_NAME_KEY] ;
OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title)) ;
}
-function submitEditPage()
+function submitEdit()
{
if (DEBUG_VB) DBG("in") ;
- var content_text = WikiPage[PAGE_TEXT_KEY] ; if (content_text == '') { ForceQuit("!BUG!") ; return ; }
+ var page_title = WikiPage[PACKAGE_NAME_KEY] ;
+ var wiki_text = WikiPage[DESCRIPTION_KEY ] ;
- QuitOnErr(Page.evaluate(function(content_input_id , edit_form_id ,
- wiki_content_regex , content_text )
+ QuitOnErr(Page.evaluate(function(wiki_text_input_id , edit_form_id ,
+ wiki_text_regex , new_wiki_text)
{
- var content_input = document.getElementById(content_input_id) ;
- var edit_form = content_input.form ;
- var existing_content = content_input.textContent ;
+ var wiki_text_input = document.getElementById(wiki_text_input_id) ;
+ var edit_form = wiki_text_input.form ;
+ var existing_wiki_text = wiki_text_input.value ;
+
+ if (!wiki_text_input || !edit_form) return "invalid edit page" ;
- if (!content_input || !edit_form) return "invalid edit page" ;
+ var modified_wiki_text = existing_wiki_text.replace(wiki_text_regex , new_wiki_text) ;
+ var concatenated_wiki_text = existing_wiki_text + new_wiki_text ;
- var modified_content = existing_content.replace(wiki_content_regex , content_text) ;
+console.log("submitEditPage() new_wiki_text=" + new_wiki_text) ;
+console.log("submitEditPage() wiki_text_input.value IN=" + wiki_text_input.value) ;
+console.log("submitEditPage() (modified_wiki_text == existing_wiki_text)=" + (modified_wiki_text == existing_wiki_text)) ;
+console.log("submitEditPage() wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text)) ;
-console.log("submitEditPage() content_input.textContent IN=" + existing_content) ;
-console.log("submitEditPage() (modified_content == existing_content)=" + (modified_content == existing_content)) ;
+ if (!wiki_text_regex.test(existing_wiki_text)) wiki_text_input.value = concatenated_wiki_text ;
+ else if (modified_wiki_text != existing_wiki_text ) wiki_text_input.value = modified_wiki_text ;
+ else return '' ;
- if (modified_content != existing_content || !wiki_content_regex.test(existing_content))
- { content_input.value = modified_content ; edit_form.submit() ; }
+console.log("submitEditPage() wiki_text_input.value OUT=" + wiki_text_input.value) ;
-console.log("submitEditPage() content_input.textContent OUT=" + existing_content) ;
+ edit_form.submit() ;
- } , CONTENT_INPUT_ID , EDIT_FORM_ID ,
- WIKI_CONTENT_REGEX , WIKI_CONTENT_BEGIN + content_text + WIKI_CONTENT_END)) ;
+ } , WIKITEXT_INPUT_ID , EDIT_FORM_ID ,
+ WIKI_TEXT_REGEX , WIKI_TEXT_BEGIN + wiki_text + WIKI_TEXT_END)) ;
if (DEBUG_VB) DBG("out") ;
}
+function verifyEdit()
+{
+ QuitOnErr(Page.evaluate(function(wiki_text_input_id , edit_form_id , new_wiki_text)
+ {
+ var wiki_text_input = document.getElementById(wiki_text_input_id) ;
+ var existing_wiki_text = wiki_text_input.value ;
+ var wiki_text_regex = RegExp(new_wiki_text) ;
+
+ if (!wiki_text_input || !wiki_text_regex.test(existing_wiki_text))
+ return "edit failed" ;
+
+ } , WIKITEXT_INPUT_ID , EDIT_FORM_ID , WIKI_TEXT_BEGIN + wiki_text + WIKI_TEXT_END)) ;
+}
+
/* main loop */
+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) ;
+}
+
+
function MainLoop()
{
if (DEBUG_VB) DBG("ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ;
@@ -157,12 +204,14 @@ if (DEBUG_VB) DBG("ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ;
clearTimeout(StepTimeout) ;
- var step = Steps.shift() ;
- StepTimeout = setTimeout(function() { ForceQuit("timeout executing step") ; } , STEP_TIMEOUT) ;
- IsReady = false ;
+ StepTimeout = setTimeout(function() { ForceQuit("timeout executing step") ; } , STEP_TIMEOUT) ;
+ var step_data = Steps.shift() ;
+ var step = step_data && step_data[STEP_FUNCTION_KEY] ;
+ ShouldWait = step_data && step_data[PAGELOAD_WAIT_KEY] ;
+ IsReady = false ;
if (typeof step !== 'function') Done() ;
- else { LOG("Step: " + step.name) ; step() ; PumpMainLoop() ; }
+ else { LOG_STEP(step.name) ; step() ; PumpMainLoop() ; }
}
function PumpMainLoop() { setTimeout(MainLoop , 250) ; }
@@ -181,10 +230,7 @@ DBG("url=" + url) ;
Page.open(url , function(status) { if (status != 'success') ForceQuit("status: " + status) ; }) ;
}
-function QuitOnErr(error_msg)
-{
- if (!!error_msg) ForceQuit(error_msg) ; else IsReady = !Step[PAGELOAD_WAIT_KEY] ;
-}
+function QuitOnErr(error_msg) { if (!!error_msg) ForceQuit(error_msg) ; else IsReady = !ShouldWait ; }
function IsA(an_object , expected_type)
{
@@ -212,22 +258,24 @@ Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChang
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) { 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])) ; }
+function LOG (log , color) { console.log((color || '\033[00;32m') + log + '\033[00m' ) ; }
+function LOG_STEP(name) { LOG("Step: " + name , '\033[01;32m') ; }
+function ERR (err) { LOG("ERROR: " + err , '\033[00;31m') ; }
+function DBG (dbg) { if (!DEBUG) return ;
+ dbg = ((!!DBG.caller.name) ? DBG.caller.name + "() " : '') + dbg ;
+ LOG("DEBUG: " + dbg , '\033[00;34m') ; }
+function ARGS () { if (!DEBUG_EVS) return ;
+ LOG("EVENT: " + this) ; var args = arguments ;
+ for (arg in args) LOG(" arg: " + JSON.stringify(args[arg])) ; }
/* main entry */
-Steps.push(prepare ) ;
-Steps.push(loadLoginPage ) ;
-Steps.push(submitLogin ) ;
-Steps.push(verifyLogin ) ;
-Steps.push(loadEditPage ) ;
-Steps.push(submitEditPage) ;
+DefineStep(prepare , false) ;
+DefineStep(loadLoginPage , true ) ;
+DefineStep(submitLogin , true ) ;
+DefineStep(verifyLogin , false) ;
+DefineStep(loadEditPage , true ) ;
+DefineStep(submitEdit , true ) ;
+DefineStep(verifyEdit , false) ;
MainLoop() ;