summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2018-01-15 05:39:29 -0500
committerbill-auger <mr.j.spam.me@gmail.com>2020-03-14 19:11:01 -0400
commit80eb62d02f60fc5c7c5c1713f27f1cdd13d8181c (patch)
tree11500d94095ddbfec1fcd70832b116115b615686
parent06b1e1c5895949aa74ae92b782204f327a49c183 (diff)
[post_fsd_wiki.phantomjs]: add wiki post script
-rw-r--r--.gitignore2
-rw-r--r--blacklist-testdata.json10
-rw-r--r--post_fsd_wiki.phantomjs354
3 files changed, 366 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 76b6c71..2f300aa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,10 +1,12 @@
*
!.gitignore
!aur-blacklist.txt
+!blacklist-testdata.json
!blacklist.txt
!check.sh
!COPYING
!find-replacements
+!post_fsd_wiki.phantomjs
!README
!SYNTAX
!your-freedom_emu-blacklist.txt
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
new file mode 100644
index 0000000..f6ee73f
--- /dev/null
+++ b/post_fsd_wiki.phantomjs
@@ -0,0 +1,354 @@
+var BLACKLIST_DATA_FILE = './blacklist-testdata.json' ;
+var WIKI_TEMPLATE_FILE = './wiki-template.json' ;
+var WIKI_BASE_URL = 'http://localhost/mediawiki/index.php' ;
+// var WIKI_BASE_URL = 'https://directory.fsf.org/wiki?title=' ;
+var LOGIN_URL = WIKI_BASE_URL + '?title=Special:UserLogin' ;
+var EDIT_URL = WIKI_BASE_URL + '?title=_TITLE_&action=edit' ;
+var USAGE_MSG = "USAGE: phantomjs ./post_fsd_wiki.phantomjs <WIKI_LOGIN> <WIKI_PASS>" ;
+var TITLE_URL_REGEX = /_TITLE_/ ;
+var PARABOLA_ENTRY_HEADER = '== Parabola Blacklist Description ==\n' ;
+var WIKI_TEXT_BEGIN = '<!-- PARABOLA BLACKLIST BEGIN (DO NOT EDIT) -->' + PARABOLA_ENTRY_HEADER ;
+var WIKI_TEXT_END = '\n<!-- PARABOLA BLACKLIST END -->\n' ;
+var WIKI_TEXT_REGEX = RegExp(WIKI_TEXT_BEGIN.replace(/\((.*)\)/ , '\\($1\\)') + '.*' + WIKI_TEXT_END) ;
+var LOGIN_IMPUT_ID = 'wpName1' ;
+var PASS_IMPUT_ID = 'wpPassword1' ;
+var LOGIN_SUBMIT_IMPUT_ID = 'wpLoginAttempt' ;
+var USERNAME_LI_ID = 'pt-userpage' ;
+var EDIT_TEXT_INPUT_ID = 'wpTextbox1' ;
+var EDIT_SUBMIT_INPUT_ID = 'wpSave' ;
+var WIKI_TITLE_H1_ID = 'firstHeading' ;
+var WIKI_TEXT_DIV_ID = 'mw-content-text' ;
+var PACKAGE_NAME_KEY = 'original_package' ; // BLACKLIST_DATA_FILE JSON per ./SYNTAX CSV[0]
+var REPLACEMENT_KEY = 'libre_replacement' ; // BLACKLIST_DATA_FILE JSON per ./SYNTAX CSV[1]
+var REFERENCE_KEY = 'ref' ; // BLACKLIST_DATA_FILE JSON per ./SYNTAX CSV[2]
+var ENTRY_ID_KEY = 'id' ; // BLACKLIST_DATA_FILE JSON per ./SYNTAX CSV[3]
+var DESCRIPTION_KEY = 'short_description' ; // BLACKLIST_DATA_FILE JSON per ./SYNTAX CSV[4]
+var BLACKLIST_TAGS_KEY = 'blacklist_tags' // BLACKLIST_DATA_FILE JSON
+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 WikiPages = [] ; var WikiPage ;
+var Steps = [] ; var Step ;
+var StepN = 0 ;
+var IsReady = true ;
+var ShouldWait = false ;
+var ShouldQuit = false ;
+
+
+if (WIKI_LOGIN == '' || WIKI_PASS == '') { LOG(USAGE_MSG) ; phantom.exit() ; } LOG('') ;
+
+
+/* steps */
+
+function Prepare()
+{
+ LOG("loading BLACKLIST_DATA_FILE") ; var blacklist_data = require(BLACKLIST_DATA_FILE) ;
+
+ 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].trim() ;
+ var page_text = package_data[DESCRIPTION_KEY ].trim() ;
+
+ 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[PACKAGE_NAME_KEY] = page_title.toLowerCase().replace(/ /g , '_') ;
+ wiki_page[DESCRIPTION_KEY ] = page_text ;
+ WikiPages.push(wiki_page) ;
+ }) ;
+
+ IsReady = true ;
+}
+
+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) ;
+ }
+
+ GetUrl(LOGIN_URL) ;
+}
+
+function SubmitLogin()
+{
+ EvalQuitOnErr(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_button = document.getElementById(submit_input_id) ;
+ var login_form = submit_button.form ;
+
+ if (!login_input || login_input.nodeName != 'INPUT' ||
+ !pass_input || pass_input.nodeName != 'INPUT' ||
+ !submit_button || submit_button.nodeName != 'BUTTON' ||
+ !login_form || login_form.nodeName != 'FORM' )
+ return "ERROR: invalid login page" ;
+
+ login_input.value = wiki_login ;
+ pass_input.value = wiki_pass ;
+
+ login_form.submit() ;
+
+ return '' ;
+ } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , LOGIN_SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;
+
+ IsReady = true ;
+}
+
+function VerifyLogin()
+{
+ EvalQuitOnErr(Page.evaluate(function(username_li_id , wiki_login)
+ {
+ var username_li = document.getElementById(username_li_id) ;
+ var username = !!username_li && username_li.textContent.toLowerCase() ;
+
+ if (!username_li || username_li.nodeName != 'LI' ||
+ username != wiki_login.toLowerCase() )
+ return "ERROR: login failed" ;
+
+ console.info("signed in as: " + username) ; return '' ;
+ } , USERNAME_LI_ID , WIKI_LOGIN)) ;
+
+ IsReady = true ;
+}
+
+function LoadWikiData()
+{
+ LOG(WikiPages.length + " remaining") ;
+
+ if (!!(WikiPage = WikiPages.shift())) IsReady = true ; else Done() ;
+}
+
+function LoadEditPage()
+{
+ var page_title = WikiPage[PACKAGE_NAME_KEY] ;
+
+ GetUrl(EditPageUrl(page_title)) ;
+}
+
+function SubmitEdit()
+{
+ var page_title = WikiPage[PACKAGE_NAME_KEY] ;
+ var wiki_text = WikiPage[DESCRIPTION_KEY ] ;
+
+ if (Page.url != EditPageUrl(page_title)) { ForceQuit("edit page load failed") ; return ; }
+
+ var post_wiki_text = EvalQuitOnErr(Page.evaluate(function(wiki_text_input_id , wiki_submit_button_id ,
+ wiki_text_regex , new_wiki_text )
+ {
+ var wiki_text_input = document.getElementById(wiki_text_input_id ) ;
+ var wiki_submit_button = document.getElementById(wiki_submit_button_id) ;
+ var existing_wiki_text = !!wiki_text_input && wiki_text_input.value ;
+
+// console.log("submitEdit() wiki_text_input=" + wiki_text_input ) ;
+// console.log("submitEdit() wiki_text_input .nodeName" + wiki_text_input .nodeName) ;
+// console.log("submitEdit() wiki_submit_button" + wiki_submit_button) ;
+// console.log("submitEdit() wiki_submit_button.nodeName" + wiki_submit_button.nodeName) ;
+
+ if (!wiki_text_input || wiki_text_input .nodeName != 'TEXTAREA' ||
+ !wiki_submit_button || wiki_submit_button.nodeName != 'INPUT' )
+ return "ERROR: invalid edit page" ;
+
+ var modified_wiki_text = existing_wiki_text.replace(wiki_text_regex , new_wiki_text) ;
+ var concat_wiki_text = existing_wiki_text + new_wiki_text ;
+ var post_wiki_text = (!wiki_text_regex.test(existing_wiki_text)) ? concat_wiki_text :
+ (modified_wiki_text != existing_wiki_text ) ? modified_wiki_text : '' ;
+
+// console.log("submitEdit() new_wiki_text=" + new_wiki_text) ;
+// console.log("submitEdit() existing_wiki_text=" + existing_wiki_text) ;
+// console.log("submitEdit() modified_wiki_text=" + modified_wiki_text) ;
+// console.log("submitEdit() (modified_wiki_text == existing_wiki_text)=" + (modified_wiki_text == existing_wiki_text)) ;
+// console.log("submitEdit() wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text)) ;
+// console.log("submitEdit() post_wiki_text=" + post_wiki_text) ;
+
+ if (!!post_wiki_text) { wiki_text_input.value = post_wiki_text ; wiki_submit_button .click() ; }
+
+ return post_wiki_text ;
+
+ } , EDIT_TEXT_INPUT_ID , EDIT_SUBMIT_INPUT_ID ,
+ WIKI_TEXT_REGEX , WIKI_TEXT_BEGIN + wiki_text + WIKI_TEXT_END)) ;
+
+// DBG("returned post_wiki_text=" + post_wiki_text) ;
+
+ if (post_wiki_text == '') { LOG("wiki text has not changed - skipping") ; NextPage() ; }
+}
+
+function VerifyEdit()
+{
+ var page_title = WikiPage[PACKAGE_NAME_KEY] ;
+ var wiki_text = WikiPage[DESCRIPTION_KEY ] ;
+ var expected_url = WIKI_BASE_URL + '/' + page_title ;
+ page_title = page_title.replace(/_/g , ' ') ;
+ wiki_text = PARABOLA_ENTRY_HEADER.replace(/=/g , '').trim() + '\n' + wiki_text ;
+
+// DBG("Page.url=" + Page.url.toLowerCase()) ; DUMPFILE(Page.content) ;
+
+ if (Page.url.toLowerCase() != expected_url) { ForceQuit("edit post failed") ; return ; }
+
+ var err = EvalQuitOnErr(Page.evaluate(function(wiki_title_h1_id , wiki_text_div_id ,
+ page_title , new_wiki_text )
+ {
+ var wiki_title_h1 = document.getElementById(wiki_title_h1_id) ;
+ var wiki_text_div = document.getElementById(wiki_text_div_id) ;
+ var existing_wiki_title = !!wiki_title_h1 && wiki_title_h1.textContent.toLowerCase() ;
+ var existing_wiki_text = !!wiki_text_div && wiki_text_div.textContent.trim() ;
+ var wiki_text_regex = RegExp(new_wiki_text) ;
+
+console.log("wiki_title_h1=" + wiki_title_h1 + " wiki_title_h1.nodeName=" + wiki_title_h1.nodeName) ;
+console.log("wiki_text_div=" + wiki_text_div + " wiki_text_div.nodeName=" + wiki_text_div.nodeName) ;
+console.log("existing_wiki_title=" + existing_wiki_title) ;
+console.log("page_title=" + page_title) ;
+console.log("existing_wiki_text=" + existing_wiki_text) ;
+console.log("new_wiki_text=" + new_wiki_text) ;
+console.log("(existing_wiki_title == page_title )=" + (existing_wiki_title == page_title)) ;
+console.log("wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text)) ;
+
+ if (!wiki_title_h1 || wiki_title_h1.nodeName != 'H1' || existing_wiki_title != page_title ||
+ !wiki_text_div || wiki_text_div.nodeName != 'DIV' || !wiki_text_regex.test(existing_wiki_text))
+ return "ERROR: edit post failed" ;
+
+ return '' ;
+ } , WIKI_TITLE_H1_ID , WIKI_TEXT_DIV_ID , page_title , wiki_text)) ;
+
+ if (!err) LOG("successfully updated: " + page_title) ; NextPage() ;
+}
+
+
+/* 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) ;
+
+ if (!!ShouldQuit) { ERR(ShouldQuit) ; Done() ; return ; }
+ else if (!IsReady ) { PumpMainLoop() ; return ; }
+
+ clearTimeout(StepTimeout) ;
+
+ StepTimeout = setTimeout(function() { ForceQuit("timeout executing step") ; } , STEP_TIMEOUT) ;
+ var step_data = Steps[StepN++] ;
+ 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() ; }
+}
+
+function PumpMainLoop() { setTimeout(MainLoop , 250) ; }
+
+function ForceQuit(quit_msg) { ShouldQuit = quit_msg || 'script error' ; PumpMainLoop() ; }
+
+function Done() { LOG("done") ; phantom.exit() ; LOG('') ; }
+
+
+/* helpers */
+
+function GetUrl(get_url)
+{
+DBG("get_url=" + get_url) ;
+
+ Page.open(get_url , function(status) { if (status != 'success') ForceQuit("status: " + status) ; }) ;
+}
+
+function EvalQuitOnErr(result)
+{
+// CAVEATS: this function is intended to handle the return value of 'Page.evaluate' callbacks
+// on success, the 'Page.evaluate' callback must return a string not matching /^ERROR:.*/
+// the Step function must set IsReady to true sometime after this function returns
+// unless it is waiting for a page load, in which case, 'Page.onLoadFinished' will flag it
+
+ if (typeof result != 'string' || !result.indexOf('ERROR:')) ForceQuit(result) ;
+ else IsReady = !ShouldWait ;
+
+ return result ;
+}
+
+function NextPage() { StepN = NEXT_PAGE_STEP ; IsReady = true ; }
+
+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) ;
+}
+
+function EditPageUrl(page_title) { return EDIT_URL.replace(TITLE_URL_REGEX , page_title) ; }
+
+
+/* event hendlers */
+
+Page.onLoadStarted = function() { IsReady = false ; ARGS.apply("Page.onLoadStarted" , arguments) ; } ;
+
+Page.onLoadFinished = function() { IsReady = true ; ARGS.apply("Page.onLoadFinished " + Page.url , arguments) ; } ;
+
+Page.onUrlChanged = function() { ARGS.apply("Page.onUrlChanged" , arguments) ; } ;
+
+
+/* logging */
+
+Page.onConsoleMessage = function(msg) { LOG(msg) ; } ;
+
+var DEBUG = false ; var DEBUG_EVS = DEBUG && false ; var DEBUG_VB = DEBUG && false ;
+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])) ; }
+function DUMPFILE(content) { require('fs').write('dump.txt' , content , 'w') ; }
+
+
+/* main entry */
+
+DefineStep(Prepare , false) ;
+DefineStep(LoadLoginPage , true ) ;
+DefineStep(SubmitLogin , true ) ;
+DefineStep(VerifyLogin , false) ;
+DefineStep(LoadWikiData , false) ;
+DefineStep(LoadEditPage , true ) ;
+DefineStep(SubmitEdit , true ) ;
+DefineStep(VerifyEdit , false) ;
+for (step_n in Steps) if (Steps[step_n][STEP_FUNCTION_KEY].name == 'LoadWikiData') NEXT_PAGE_STEP = step_n ;
+
+MainLoop() ;