summaryrefslogtreecommitdiff
path: root/post_fsd_wiki.phantomjs
blob: 003c80fe2d763a333ef3cce5a4daeaf9576e499c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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 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 WikiPages  = [] ; var WikiPage ;
var Steps      = [] ; var Step ;
var StepN      = 0 ;
var IsReady    = true ;
var ShouldQuit = false ;


if (WIKI_LOGIN == '' || WIKI_PASS == '') { DBG(USAGE_MSG) ; phantom.exit() ; }


/* 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 ; }

  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 ; }

    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) ;
  }) ;

  IsReady = true ;
}

function loadLoginPage()
{
  if (WikiPages.length == 0) { ForceQuit("no WikiPages to consider") ; return ; }

  OpenUrl(LOGIN_URL) ;
}

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 ;

    if (!login_input  || !login_input || !submit_input || !login_form)
      return "invalid login page" ;

    login_input.value = wiki_login ;
    pass_input.value  = wiki_pass ;

    login_form.submit() ;

  } , LOGIN_IMPUT_ID , PASS_IMPUT_ID , SUBMIT_IMPUT_ID , WIKI_LOGIN , WIKI_PASS)) ;
}

function verifyLogin()
{
  QuitOnErr(Page.evaluate(function(username_li_id , wiki_login)
  {
    var username_li = document.getElementById(username_li_id) ;

    if (!username_li || username_li.textContent.lowercase != wiki_login.lowercase)
      return "login failed" ;

    console.info("signed in as: " + username_li.textContent) ;
  } , USERNAME_LI_ID , WIKI_LOGIN)) ;
}

function loadEditPage()
{
  WikiPage       = WikiPages.shift ;
  var page_title = WikiPage[PAGE_TITLE_KEY] ;

  OpenUrl(EDIT_URL.replace(TITLE_URL_REGEX , page_title)) ;
}

function submitEditPage()
{
if (DEBUG_VB) DBG("in") ;

  var content_text = WikiPage[PAGE_TEXT_KEY] ; if (content_text == '') { ForceQuit("!BUG!") ; return ; }

  QuitOnErr(Page.evaluate(function(content_input_id   , edit_form_id ,
                                   wiki_content_regex , content_text )
  {
    var content_input    = document.getElementById(content_input_id) ;
    var edit_form        = content_input.form ;
    var existing_content = content_input.textContent ;

    if (!content_input || !edit_form) return "invalid edit page" ;

    var modified_content = existing_content.replace(wiki_content_regex , content_text) ;

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() ; }

console.log("submitEditPage() content_input.textContent  OUT=" + existing_content) ;

  } , 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 (DEBUG_VB) DBG("ShouldQuit=" + ShouldQuit + " IsReady=" + IsReady) ;

  if      (!!ShouldQuit) { ERR(ShouldQuit) ; Done() ; return ; }
  else if (!IsReady    ) { PumpMainLoop() ;           return ; }

  clearTimeout(StepTimeout) ;

  var step    = Steps.shift() ;
  StepTimeout = setTimeout(function() { ForceQuit("timeout executing step") ; } , STEP_TIMEOUT) ;
  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 ; PumpMainLoop() ; }

function Done() { LOG("done") ; phantom.exit() ; }


/* helpers */

function OpenUrl(url)
{
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 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() { 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 = 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])) ;      }


/* main entry */

Steps.push(prepare       ) ;
Steps.push(loadLoginPage ) ;
Steps.push(submitLogin   ) ;
Steps.push(verifyLogin   ) ;
Steps.push(loadEditPage  ) ;
Steps.push(submitEditPage) ;
MainLoop() ;