diff options
author | bill-auger <mr.j.spam.me@gmail.com> | 2018-01-21 11:20:30 -0500 |
---|---|---|
committer | bill-auger <mr.j.spam.me@gmail.com> | 2018-02-13 13:29:34 -0500 |
commit | cbaf04e367ff3fd05b5f8973e55aef90c4debf5f (patch) | |
tree | 653daee8d284e6f509ccd3f67c0a4efd284e9d22 | |
parent | d33f18f36a68acb876ec37cf53ad4b62ac093a86 (diff) |
add categories to wiki
-rw-r--r-- | blacklist-testdata.json | 12 | ||||
-rw-r--r-- | post_fsd_wiki.phantomjs | 114 |
2 files changed, 71 insertions, 55 deletions
diff --git a/blacklist-testdata.json b/blacklist-testdata.json index a58da60..039c1e2 100644 --- a/blacklist-testdata.json +++ b/blacklist-testdata.json @@ -1,10 +1,14 @@ [ { - "original_package": "dummy package 1" , - "short_description": "dummy description 1" + "original_package": "dummy package 71" , + "libre_replacement": "" , + "short_description": "dummy description 71" , + "blacklist_tags": [ "Category A" ] } , { - "original_package": "dummy package 2" , - "short_description": "dummy description 2" + "original_package": "dummy package 72" , + "libre_replacement": "dummy replacement" , + "short_description": "dummy description 72" , + "blacklist_tags": [ "Category A" , "nonfree" ] } ] diff --git a/post_fsd_wiki.phantomjs b/post_fsd_wiki.phantomjs index a6c3d4f..688a65a 100644 --- a/post_fsd_wiki.phantomjs +++ b/post_fsd_wiki.phantomjs @@ -9,7 +9,7 @@ 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 WIKI_TEXT_REGEX = RegExp(WIKI_TEXT_BEGIN.replace(/\((.*)\)/ , '\\($1\\)') + '(.|\n)*' + WIKI_TEXT_END) ; var LOGIN_IMPUT_ID = 'wpName1' ; var PASS_IMPUT_ID = 'wpPassword1' ; var LOGIN_SUBMIT_IMPUT_ID = 'wpLoginAttempt' ; @@ -18,6 +18,7 @@ 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 CATEGORIES_DIV_ID = 'catlinks' ; 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] @@ -56,18 +57,24 @@ function Prepare() blacklist_data.forEach(function(package_data) { - var package_name = package_data[PACKAGE_NAME_KEY].trim() ; - var description = package_data[DESCRIPTION_KEY ].trim() ; - - if (!IsA(package_name , String) || - !IsA(description , String) ) + var package_name = package_data[PACKAGE_NAME_KEY ] && package_data[PACKAGE_NAME_KEY ].trim() ; + var replacement = package_data[REPLACEMENT_KEY ] && package_data[REPLACEMENT_KEY ].trim() ; + var description = package_data[DESCRIPTION_KEY ] && package_data[DESCRIPTION_KEY ].trim() ; + var blacklist_tags = package_data[BLACKLIST_TAGS_KEY] && package_data[BLACKLIST_TAGS_KEY] ; + + if ((!IsA(package_name , String) ) || + (!IsA(replacement , String) && replacement != '') || + (!IsA(description , String) ) || + (!IsA(blacklist_tags , Array ) ) ) { LOG_ERR("invalid package data - ignoring:\n" + JSON.stringify(package_data)) ; return ; } - LOG("found package: " + page_title) ; + LOG("loading package data: " + package_name) ; - var wiki_page = {} ; - wiki_page[PACKAGE_NAME_KEY] = package_name.toLowerCase().replace(/ /g , '_') ; - wiki_page[DESCRIPTION_KEY ] = description ; + var wiki_page = {} ; + wiki_page[PACKAGE_NAME_KEY ] = package_name.toLowerCase().replace(/ /g , '_') ; + wiki_page[REPLACEMENT_KEY ] = (!!replacement) ? '*Parabola Replacement: ' + replacement + '\n\n' : '' ; + wiki_page[DESCRIPTION_KEY ] = description ; + wiki_page[BLACKLIST_TAGS_KEY] = blacklist_tags ; WikiPages.push(wiki_page) ; }) ; @@ -76,19 +83,8 @@ function Prepare() 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) ; - } + if (WikiPages.length > 0) { LOG(WikiPages.length + " WikiPages to consider") ; } + else { ForceQuit("no WikiPages to consider") ; return ; } GetUrl(LOGIN_URL) ; } @@ -151,8 +147,13 @@ function LoadEditPage() function SubmitEdit() { - var page_title = WikiPage[PACKAGE_NAME_KEY] ; - var wiki_text = WikiPage[DESCRIPTION_KEY ] ; + var page_title = WikiPage[PACKAGE_NAME_KEY ] ; + var replacement_text = WikiPage[REPLACEMENT_KEY ] ; + var wiki_text = WikiPage[DESCRIPTION_KEY ] ; + var wiki_categories = WikiPage[BLACKLIST_TAGS_KEY] ; + wiki_text = WIKI_TEXT_BEGIN + replacement_text + wiki_text + '\n\n' ; + wiki_categories.forEach(function(category) { wiki_text += '[[Category:' + category + ']]\n' }) ; + wiki_text += WIKI_TEXT_END if (Page.url != EditPageUrl(page_title)) { ForceQuit("edit page load failed") ; return ; } @@ -180,9 +181,7 @@ DBG('SubmitEditEvalOut')(new_wiki_text , existing_wiki_text , modified_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 , IsA , DBG)) ; - + } , EDIT_TEXT_INPUT_ID , EDIT_SUBMIT_INPUT_ID , WIKI_TEXT_REGEX , wiki_text , IsA , DBG)) ; DBG('SubmitEditOut')(post_wiki_text) ; @@ -191,34 +190,43 @@ DBG('SubmitEditOut')(post_wiki_text) ; 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 ; + var page_title = WikiPage[PACKAGE_NAME_KEY ] ; + var replacement_text = WikiPage[REPLACEMENT_KEY ] ; + var wiki_text = WikiPage[DESCRIPTION_KEY ] ; + var wiki_categories = WikiPage[BLACKLIST_TAGS_KEY] ; + var expected_url = WIKI_BASE_URL + '/' + page_title ; + page_title = page_title.replace(/_/g , ' ') ; + wiki_text = PARABOLA_ENTRY_HEADER.replace(/=/g , '').trim() + '\n' + + replacement_text .replace(/\*/g , '').trim() + '\n' + wiki_text ; + var categories_text = 'Categories: ' + wiki_categories.join('') ; if (DEBUG_VB) DUMPFILE() ; - if (Page.url.toLowerCase() != expected_url) { ForceQuit("edit post failed") ; return ; } + if (Page.url.toLowerCase() != expected_url) { ForceQuit("edit post expected_url failed") ; return ; } - var err = EvalQuitOnErr(Page.evaluate(function(wiki_title_h1_id , wiki_text_div_id , - page_title , new_wiki_text , - IsA , DBG ) + var err = EvalQuitOnErr(Page.evaluate(function(wiki_title_h1_id , wiki_text_div_id , categories_div_id , + page_title , new_wiki_text , new_categories , + IsA , DBG ) { - 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) ; - -DBG('VerifyEdit')(wiki_title_h1 , wiki_text_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , wiki_text_regex) ; - - if (!IsA(wiki_title_h1 , 'H1' ) || existing_wiki_title != page_title || - !IsA(wiki_text_div , 'DIV') || !wiki_text_regex .test(existing_wiki_text )) - return "ERROR: edit post failed" ; + var wiki_title_h1 = document.getElementById(wiki_title_h1_id ) ; + var wiki_text_div = document.getElementById(wiki_text_div_id ) ; + var categories_div = document.getElementById(categories_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 existing_categories = !!categories_div && categories_div.textContent.trim() ; + var wiki_text_regex = RegExp(new_wiki_text ) ; + var categories_regex = RegExp(new_categories) ; + +DBG('VerifyEdit')(wiki_title_h1 , wiki_text_div , categories_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , existing_categories , new_categories , wiki_text_regex , categories_regex) ; + + if (!IsA(wiki_title_h1 , 'H1' ) || existing_wiki_title != page_title || + !IsA(wiki_text_div , 'DIV') || !wiki_text_regex .test(existing_wiki_text ) || + !IsA(categories_div , 'DIV') || !categories_regex.test(existing_categories) ) + return "ERROR: edit post data failed" ; return '' ; - } , WIKI_TITLE_H1_ID , WIKI_TEXT_DIV_ID , page_title , wiki_text , IsA , DBG)) ; + } , WIKI_TITLE_H1_ID , WIKI_TEXT_DIV_ID , CATEGORIES_DIV_ID , + page_title , wiki_text , categories_text , IsA , DBG)) ; if (!err) LOG("successfully updated: " + page_title) ; NextPage() ; } @@ -347,15 +355,19 @@ function DBG(context) Dbg("SubmitEdit() (modified_wiki_text == existing_wiki_text)=" + (modified_wiki_text == existing_wiki_text)) ; Dbg("SubmitEdit() wiki_text_regex.test(existing_wiki_text)=" + wiki_text_regex.test(existing_wiki_text) ) ; } function DbgSubmitEditOut(post_wiki_text) { if (DEBUG_VB) Dbg("SubmitEdit() returned post_wiki_text=" + post_wiki_text) ; } - function DbgVerifyEdit(wiki_title_h1 , wiki_text_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , wiki_text_regex) + function DbgVerifyEdit(wiki_title_h1 , wiki_text_div , categories_div , existing_wiki_title , page_title , existing_wiki_text , new_wiki_text , existing_categories , new_categories , wiki_text_regex , categories_regex) { Dbg("VerifyEdit() wiki_title_h1=" + wiki_title_h1 + " wiki_title_h1.nodeName=" + wiki_title_h1.nodeName ) ; Dbg("VerifyEdit() wiki_text_div=" + wiki_text_div + " wiki_text_div.nodeName=" + wiki_text_div.nodeName ) ; + Dbg("VerifyEdit() categories_div=" + categories_div + " categories_div.nodeName=" + categories_div.nodeName) ; Dbg("VerifyEdit() existing_wiki_title=" + existing_wiki_title ) ; Dbg("VerifyEdit() page_title=" + page_title ) ; Dbg("VerifyEdit() existing_wiki_text=" + existing_wiki_text ) ; Dbg("VerifyEdit() new_wiki_text=" + new_wiki_text ) ; + Dbg("VerifyEdit() existing_categories=" + existing_categories ) ; + Dbg("VerifyEdit() new_categories=" + new_categories ) ; Dbg("VerifyEdit() (existing_wiki_title == page_title )=" + (existing_wiki_title == page_title) ) ; - Dbg("VerifyEdit() wiki_text_regex .test(existing_wiki_text)=" + wiki_text_regex .test(existing_wiki_text) ) ; } + Dbg("VerifyEdit() wiki_text_regex .test(existing_wiki_text)=" + wiki_text_regex .test(existing_wiki_text) ) ; + Dbg("VerifyEdit() categories_regex.test(existing_categories)=" + categories_regex.test(existing_categories) ) ; } function DbgMainLoop() { if (DEBUG_VB) Dbg("MainLoop() ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ; } function DbgGetUrl(get_url) { Dbg("GetUrl() " + get_url) ; } |