User:Avanidhar/PersistenceManager.js

/** * Persistence Manager * **/ PersistenceManager = {}

/**    * Word List *    * Builds a persistent WordList using the Wikipedia API and forwards * that list to the provided onsuccess function. If an error occurs * while building the persistent WordList, onerror will be called. *    * @param  pageTitle      string    The title of the page to build the persistent WordList for * @param onsuccess      function  A function to be called with the persistent WordList * @param onerror        function  A function that accepts visible and logging error messages in case an error occurs * @param startingRevId  int       (Optional) a revision_id at which to stop building the WordList */   PersistenceManager.getWordList = function(pageTitle, onsuccess, onerror, options){ params = { "action":   "query", "prop":     "revisions", "titles":   pageTitle, "rvprop":   "user|content", "rvlimit":  25, "format":   "json" }       if(typeof(options) != "undefined"){ if(options.startingRevId){ params.rvstartid = options.startingRevId }           if(options.section){ params.rvsection = options.section }       }        args = { "onsuccess": onsuccess, "onerror": onerror }       WPAPIHandler.performGET(            params,            function(result, args){                //Is this result JSON?                try{                    res = eval("(" + result + ")")                }catch(e){                    args.onerror("Could not interpret the Wikipedia API's response", result)                    return                }                //Is it an error result                if(res.error){                    args.onerror("Error message received from Wikipedia's API", res.error.code + ": " + res.error.info)                    return                }                //Get the page information                var page = undefined                try{                    for(pageId in res.query.pages){                        page = res.query.pages[pageId]                        break                    }                }catch(e){                    args.onerror("Could not find a page in the Wikipedia API's response", result) return }               //Is the page missing if(page.missing){ args.onerror("Could not find page with title '" + res.page.title + "'", result) return }               //Get the revisions if(page.revisions){ revisionList = page.revisions }               else{ args.onerror("Could not get previous revisions from Wikipedia's response", result) return }               //build WordList and call onsuccess args.onsuccess(PersistenceManager.buildWordList(revisionList)) },           args, function(message){ onerror("Request failed to get list of previous revisions", message) }       )    }    /**     * Build Word List     *     * Constructs a persistent WordList from a list of revisions     *     * @param  revisionList  A list of {"user":, "*": } objects that represent a sequence of revisions     *     * @return a persistent WordList     */    PersistenceManager.buildWordList = function(revisionList){        recentRevs  = new ListDict(15)        var lastRev     = undefined        for(var i=revisionList.length-1;i>=0;i--){            //Reverse walk through revisionList (since it is returned to us backwards [WTF?])            var curRev = revisionList[i]            curRev.text = curRev['*']            curRev.checksum = CRC32(curRev.text)            curRev.words = Diff.splitText(curRev.text)            if(lastRev){                if(lastRev.checksum == curRev.checksum){                    //Noop - just copy the last word list                    curRev.wordList = lastRev.wordList }               else if(recentRevs.find(curRev.checksum)){ //Revert - bring back the old word list curRev.wordList = recentRevs.find(curRev.checksum) }else{ //Regular edit //Do a diff diff = Diff.diffWordLists(lastRev.words, curRev.words) curRev.wordList = lastRev.wordList.applyDiff(diff, curRev) }           }else{ //This is the first revision diff = Diff.diffWordLists([], curRev.words) curRev.wordList = (new WordList).applyDiff(diff, {"user": "???"}) }           try{ curRev.wordList.increment }catch(e){ str = "" for(key in curRev.wordList){ str += key + ": " + curRev.wordList[key] + "\n" }               alert(str) }           recentRevs.insert(curRev.checksum, curRev.wordList) lastRev = curRev }       if(lastRev.wordList){ return lastRev.wordList }       else{ return new WordList }   }