def text_history(ref, version, lang): """ Return a complete list of changes to a segment of text (identified by ref/version/lang) """ ref = texts.norm_ref(ref) refRe = '^%s$|^%s:' % (ref, ref) changes = texts.db.history.find({"ref": {"$regex": refRe}, "version": version, "language": lang}).sort([['revision', -1]]) history = [] for i in range(changes.count()): rev = changes[i] log = { "ref": rev["ref"], "revision": rev["revision"], "date": rev["date"], "user": rev["user"], "rev_type": rev["rev_type"], "method": rev.get("method", "Site"), "diff_html": rev["diff_html"], "text": text_at_revision(ref, version, lang, rev["revision"]) } history.append(log) """ # create a fake revision 0 for initial work that was unrecorded rev0 = { "revision": 0, "date": "Date Unknown", "user": "******", "rev_type": "add text", "diff_html": text_at_revision(ref, version, lang, 0) } history.append(rev0) """ return history
def text_history(ref, version, lang): """ Return a complete list of changes to a segment of text (identified by ref/version/lang) """ ref = texts.norm_ref(ref) changes = texts.db.history.find({ "ref": ref, "version": version, "language": lang }).sort([['revision', -1]]) history = [] for i in range(changes.count()): rev = changes[i] log = { "revision": rev["revision"], "date": rev["date"], "user": rev["user"], "rev_type": rev["rev_type"], "diff_html": rev["diff_html"], "text": text_at_revision(ref, version, lang, rev["revision"]) } history.append(log) # create a fake revision 0 for initial work that was unrecorded rev0 = { "revision": 0, "date": "Date Unknown", "user": "******", "rev_type": "add text", "diff_html": text_at_revision(ref, version, lang, 0) } history.append(rev0) return history
def record_text_change(ref, version, lang, text, user, **kwargs): """ Record a change to a text (ref/version/lang) by user. """ # unpack text into smaller segments if necessary (e.g. chapter -> verse) if isinstance(text, list): for i in reversed(range(len(text))): n = i + 1 record_text_change("%s.%d" % (ref, n), version, lang, text[i], user, **kwargs) return # get the current state of the text in question current = texts.get_text(ref, context=0, commentary=False, version=version, lang=lang) if "error" in current and current["error"].startswith("No text found"): current = "" elif "error" in current: return current elif lang == "en" and current["text"]: current = current["text"] elif lang == "he" and current["he"]: current = current["he"] else: current = "" # Don't record anything if there's no change. if not text: text = "" if text == current: return # create a patch that turns the new version back into the old backwards_diff = dmp.diff_main(text, current) patch = dmp.patch_toText(dmp.patch_make(backwards_diff)) # get html displaying edits in this change. forwards_diff = dmp.diff_main(current, text) dmp.diff_cleanupSemantic(forwards_diff) diff_html = dmp.diff_prettyHtml(forwards_diff) # give this revision a new revision number revision = next_revision_num() log = { "ref": texts.norm_ref(ref), "version": version, "language": lang, "diff_html": diff_html, "revert_patch": patch, "user": user, "date": datetime.now(), "revision": revision, "message": kwargs.get("message", ""), "rev_type": kwargs.get("type", None) or "edit text" if len(current) else "add text", "method": kwargs.get("method", "Site") } db.history.save(log)
def record_text_change(ref, version, lang, text, user, **kwargs): """ Record a change to a text (ref/version/lang) by user. """ # unpack text into smaller segments if necessary (e.g. chapter -> verse) if isinstance(text, list): for i in reversed(range(len(text))): n = i + 1 record_text_change("%s.%d" % (ref, n), version, lang, text[i], user, **kwargs) return # get the current state of the text in question current = texts.get_text(ref, context=0, commentary=False, version=version, lang=lang) if "error" in current and current["error"].startswith("No text found"): current = "" elif "error" in current: return current elif lang == "en" and current["text"]: current = current["text"] elif lang == "he" and current["he"]: current = current["he"] else: current = "" # Don't record anything if there's no change. if not text: text = "" if text == current: return # create a patch that turns the new version back into the old backwards_diff = dmp.diff_main(text, current) patch = dmp.patch_toText(dmp.patch_make(backwards_diff)) # get html displaying edits in this change. forwards_diff = dmp.diff_main(current, text) dmp.diff_cleanupSemantic(forwards_diff) diff_html = dmp.diff_prettyHtml(forwards_diff) # give this revision a new revision number revision = next_revision_num() log = { "ref": texts.norm_ref(ref), "version": version, "language": lang, "diff_html": diff_html, "revert_patch": patch, "user": user, "date": datetime.now(), "revision": revision, "message": kwargs.get("message", ""), "rev_type": kwargs.get("type", None) or "edit text" if len(current) else "add text", "method": kwargs.get("method", "Site") } texts.db.history.save(log)
def text_history(ref, version, lang, filter_type=None): """ Return a complete list of changes to a segment of text (identified by ref/version/lang) """ ref = texts.norm_ref(ref) refRe = '^%s$|^%s:' % (ref, ref) query = {"ref": {"$regex": refRe}, "version": version, "language": lang} query.update(filter_type_to_query(filter_type)) return get_activity(query, page_size=0, page=1, filter_type=filter_type)
def get_last_edit(ref, lang, version): """ Returns the last edit or addition to ref/lang/version """ ref = texts.norm_ref(ref) refRe = '^%s$|^%s:' % (ref, ref) query = {"ref": {"$regex": refRe}, "language": lang, "version": version, "rev_type": {"$in": ["edit text", "add text", "revert text"]}} edit = texts.db.history.find(query).sort([["date", -1]]).limit(1) if edit.count(): return edit[0] return None
def get_reviews(ref, lang, version): """ Returns a list of reviews pertaining to ref/lang/version """ reviews = [] ref = texts.norm_ref(ref) refRe = '^%s$|^%s:' % (ref, ref) cursor = texts.db.history.find({"ref": {"$regex": refRe}, "language": lang, "version": version, "rev_type": "review"}).sort([["date", -1]]) for r in cursor: r["_id"] = str(r["_id"]) r["userLink"] = user_link(r["user"]) reviews.append(r) return reviews
def index_text(ref, version=None, lang=None): """ Index the text designated by ref. If no version and lang are given, this functon will be called for each availble version. Currently assumes ref is at section level. """ ref = texts.norm_ref(unicode(ref)) # Recall this function for each specific text version, if non provided if not (version and lang): for v in texts.get_version_list(ref): index_text(ref, version=v["versionTitle"], lang=v["language"]) return # Index each segment of this document individually pRef = texts.parse_ref(ref) if len(pRef["sections"]) < len(pRef["sectionNames"]): text = texts.get_text(ref, context=0, commentary=False, version=version, lang=lang) if "error" in text: print text["error"] else: for i in range(max(len(text["text"]), len(text["he"]))): index_text("%s:%d" % (ref, i + 1)) # Don't try to index docs with depth 3 if len(pRef["sections"]) < len(pRef["sectionNames"]) - 1: return # Index this document as a whole doc = make_text_index_document(ref, version, lang) if doc: try: es.index(doc, 'sefaria', 'text', make_text_doc_id(ref, version, lang)) global doc_count doc_count += 1 except Exception, e: print "Error indexing %s / %s / %s" % (ref, version, lang) print e
def index_text(ref, version=None, lang=None): """ Index the text designated by ref. If no version and lang are given, this functon will be called for each availble version. Currently assumes ref is at section level. """ ref = texts.norm_ref(unicode(ref)) # Recall this function for each specific text version, if non provided if not (version and lang): for v in texts.get_version_list(ref): index_text(ref, version=v["versionTitle"], lang=v["language"]) return # Index each segment of this document individually pRef = texts.parse_ref(ref) if len(pRef["sections"]) < len(pRef["sectionNames"]): text = texts.get_text(ref, context=0, commentary=False, version=version, lang=lang) if "error" in text: print text["error"] else: for i in range(max(len(text["text"]), len(text["he"]))): index_text("%s:%d" % (ref, i+1)) # Don't try to index docs with depth 3 if len(pRef["sections"]) < len(pRef["sectionNames"]) - 1: return # Index this document as a whole doc = make_text_index_document(ref, version, lang) if doc: try: global doc_count if doc_count % 5000 == 0: print "[%d] Indexing %s / %s / %s" % (doc_count, ref, version, lang) es.index('sefaria', 'text', doc, make_text_doc_id(ref, version, lang)) doc_count += 1 except Exception, e: print "ERROR indexing %s / %s / %s" % (ref, version, lang) pprint(e)