diff --git a/amd/build/filter_bibleversesnwt.min.js b/amd/build/filter_bibleversesnwt.min.js index 562b6c1..89d380c 100644 --- a/amd/build/filter_bibleversesnwt.min.js +++ b/amd/build/filter_bibleversesnwt.min.js @@ -1,3 +1,3 @@ -define("filter_bibleversesnwt/filter_bibleversesnwt",["exports","core/ajax","core/modal","core/modal_factory","core/modal_events","core/notification"],(function(_exports,_ajax,_modal,_modal_factory,_modal_events,_notification){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function bible_link(event){const target=event.target,info=JSON.parse(target.getAttribute("data-verseinfo")),bibleblock_heading=document.querySelector("#block_bibleblock_heading"),bibleblock_quote=document.querySelector("#block_bibleblock_content");return bibleblock_quote&&(bibleblock_heading&&(bibleblock_heading.innerHTML=""),bibleblock_quote.innerHTML='
'),(0,_ajax.call)([{methodname:"filter_bibleversesnwt_get_biblequote",args:{lang:info.lang,book:info.book,ch:info.ch,verse:info.verse,book_end:info.book,ch_end:info.ch,verse_end:info.verse_end}}])[0].then((quote=>{var modalconfig;console.info("Got bible quote",quote),bibleblock_quote?(console.info("Attempting to place in verses block",bibleblock_heading,bibleblock_quote),bibleblock_heading&&(bibleblock_heading.innerHTML=quote.heading),bibleblock_quote.innerHTML=quote.quote):(console.info("Attempting to place in new modal"),(modalconfig={title:quote.heading,body:quote.quote},_modal.default.create?_modal.default.create(modalconfig):_modal_factory.default.create(modalconfig)).then((modal=>(modal.getRoot().on(_modal_events.default.hidden,(()=>{modal.destroy()})),modal.show(),modal))).catch(_notification.default.exception))})).catch((error=>{console.error("Error retrieving bible quote:",error),window.open(target.getAttribute("href"),"_bible_","width=500,height=800")})),event.preventDefault(),!1}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(!initialized){initialized=!0;const elements=document.querySelectorAll("a.bible-link[data-verseinfo]");console.info("Found elements",elements),elements.forEach((el=>{console.info("Handling element",el),el.addEventListener("click",bible_link)}))}},_modal=_interopRequireDefault(_modal),_modal_factory=_interopRequireDefault(_modal_factory),_modal_events=_interopRequireDefault(_modal_events),_notification=_interopRequireDefault(_notification);let initialized=!1})); +define("filter_bibleversesnwt/filter_bibleversesnwt",["exports","core/ajax","core/modal","core/modal_factory","core/modal_events","core/notification"],(function(_exports,_ajax,_modal,_modal_factory,_modal_events,_notification){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(!initialized){initialized=!0;const elements=document.querySelectorAll("a.bible-link[data-verseinfo]");console.info("Found elements",elements),elements.forEach((el=>{console.info("Handling element",el),el.addEventListener("click",bible_link);getQuote(JSON.parse(el.getAttribute("data-verseinfo")))}))}},_modal=_interopRequireDefault(_modal),_modal_factory=_interopRequireDefault(_modal_factory),_modal_events=_interopRequireDefault(_modal_events),_notification=_interopRequireDefault(_notification);const Preloaded={};function getQuote(info){const key=`${info.lang}|${info.book}.${info.ch}:${info.verse}-${info.book_end}.${info.ch_end}:${info.verse_end}`;return new Promise(((resolve,reject)=>{key in Preloaded?resolve(Preloaded[key]):(0,_ajax.call)([{methodname:"filter_bibleversesnwt_get_biblequote",args:{lang:info.lang,book:info.book,ch:info.ch,verse:info.verse,book_end:info.book,ch_end:info.ch_end,verse_end:info.verse_end}}])[0].then((quote=>{Preloaded[key]=quote,resolve(quote)})).catch((error=>{reject(error)}))}))}function bible_link(event){const target=event.target,info=JSON.parse(target.getAttribute("data-verseinfo")),bibleblock_heading=document.querySelector("#block_bibleblock_heading"),bibleblock_quote=document.querySelector("#block_bibleblock_content");return bibleblock_quote&&(bibleblock_heading&&(bibleblock_heading.innerHTML=""),bibleblock_quote.innerHTML='
'),getQuote(info).then((quote=>{var modalconfig;console.info("Got bible quote",quote),bibleblock_quote?(console.info("Attempting to place in verses block",bibleblock_heading,bibleblock_quote),bibleblock_heading&&(bibleblock_heading.innerHTML=quote.heading),bibleblock_quote.innerHTML=quote.quote):(console.info("Attempting to place in new modal"),(modalconfig={title:quote.heading,body:quote.quote},_modal.default.create?_modal.default.create(modalconfig):_modal_factory.default.create(modalconfig)).then((modal=>(modal.getRoot().on(_modal_events.default.hidden,(()=>{modal.destroy()})),modal.show(),modal))).catch(_notification.default.exception))})).catch((error=>{console.error("Error retrieving bible quote:",error),window.open(target.getAttribute("href"),"_bible_","width=500,height=800")})),event.preventDefault(),!1}let initialized=!1})); //# sourceMappingURL=filter_bibleversesnwt.min.js.map \ No newline at end of file diff --git a/amd/build/filter_bibleversesnwt.min.js.map b/amd/build/filter_bibleversesnwt.min.js.map index 7e64a2c..7614765 100644 --- a/amd/build/filter_bibleversesnwt.min.js.map +++ b/amd/build/filter_bibleversesnwt.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filter_bibleversesnwt.min.js","sources":["../src/filter_bibleversesnwt.js"],"sourcesContent":["/*eslint no-var: \"error\" */\n/*eslint no-unused-vars: \"off\" */\n/*eslint no-console: \"off\" */\n/*eslint linebreak-style: \"off\" */\n/*eslint no-trailing-spaces: \"off\" */\n/*eslint-env es6*/\n// Put this file in path/to/plugin/amd/src\n// You can call it anything you like\nimport {call} from 'core/ajax';\nimport Modal from 'core/modal';\nimport ModalFactory from 'core/modal_factory';\nimport ModalEvents from 'core/modal_events';\nimport Notification from 'core/notification';\n\n/**\n * \n * @param {*} modalconfig \n * @returns Promise of modal\n */\nfunction modalcreate(modalconfig) {\n if(Modal.create) {\n return Modal.create(modalconfig);\n } else {\n return ModalFactory.create(modalconfig);\n }\n}\n/**\n * \n * @param {*} lang \n * @param {*} book \n * @param {*} ch \n * @param {*} verse \n * @param {*} book_end \n * @param {*} ch_end \n * @param {*} verse_end \n */\nfunction getBibleQuote(lang,book,ch,verse,book_end, ch_end, verse_end) {\n return new Promise((resolve, reject) => {\n let range = \"00000000\";\n let url = \"\";\n\n range = book.toString() + ch.toString().padStart(3,'0') + verse.toString().padStart(3,'0');\n\n if(book_end && ch_end && verse_end) {\n range = range + \"-\" + book_end.toString() + ch_end.toString().padStart(3,'0') + verse_end.toString().padStart(3,'0');\n }\n\n if (lang == \"nl\") {\n url = `https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/${range}`;\n } else { // if $lang == 'en'\n url = `https://www.jw.org/en/library/bible/nwt/books/json/html/${range}`;\n }\n\n window.fetch(url).then((response) => {\n try {\n const o = JSON.parse();\n resolve(o.ranges[range]);\n } catch (error) {\n reject(\"Error parsing json response\",error, response);\n }\n }).catch((error) => {\n reject(\"Error retrieving url\",error);\n });\n });\n}\n\n/**\n * \n * @param {*} event \n */\nfunction bible_link(event) {\n const target = event.target;\n const info = JSON.parse(target.getAttribute(\"data-verseinfo\"));\n const bibleblock_heading = document.querySelector(\"#block_bibleblock_heading\");\n const bibleblock_quote = document.querySelector(\"#block_bibleblock_content\");\n if( bibleblock_quote ) {\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = \"\";\n }\n bibleblock_quote.innerHTML = `
`;\n }\n call([{\n methodname: 'filter_bibleversesnwt_get_biblequote',\n args: { \n lang: info.lang,\n book: info.book,\n ch: info.ch,\n verse: info.verse,\n book_end: info.book,\n ch_end: info.ch,\n verse_end: info.verse_end}\n }])[0].then((quote) => {\n console.info(\"Got bible quote\",quote);\n if( bibleblock_quote ) {\n console.info(\"Attempting to place in verses block\",bibleblock_heading,bibleblock_quote);\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = quote.heading;\n }\n bibleblock_quote.innerHTML = quote.quote;\n } else {\n console.info(\"Attempting to place in new modal\",);\n modalcreate({\n title: quote.heading,\n body: quote.quote,\n }).then(modal => {\n // Handle hidden event.\n modal.getRoot().on(ModalEvents.hidden, () => {\n // Destroy when hidden.\n modal.destroy();\n });\n // Show the modal.\n modal.show();\n return modal;\n })\n .catch(Notification.exception);\n\n }\n }).catch((error) => {\n console.error(\"Error retrieving bible quote:\", error);\n window.open(target.getAttribute(\"href\"),\"_bible_\",\"width=500,height=800\");\n });\n\n event.preventDefault();\n return false;\n}\n\n/**\n * \n */\nlet initialized = false;\n/**\n * Initialize the Page\n */\nexport function init() {\n // Make sure this function is only called once on the page. May be called multiple times because of filter weirdness\n if (!initialized) {\n initialized = true;\n\n const elements = document.querySelectorAll(\"a.bible-link[data-verseinfo]\");\n console.info(\"Found elements\",elements);\n elements.forEach((el) => {\n console.info(\"Handling element\",el);\n el.addEventListener(\"click\", bible_link);\n });\n }\n}\n\n\n"],"names":["bible_link","event","target","info","JSON","parse","getAttribute","bibleblock_heading","document","querySelector","bibleblock_quote","innerHTML","methodname","args","lang","book","ch","verse","book_end","ch_end","verse_end","then","quote","modalconfig","console","heading","title","body","Modal","create","ModalFactory","modal","getRoot","on","ModalEvents","hidden","destroy","show","catch","Notification","exception","error","window","open","preventDefault","initialized","elements","querySelectorAll","forEach","el","addEventListener"],"mappings":"8TAsESA,WAAWC,aACVC,OAASD,MAAMC,OACfC,KAAOC,KAAKC,MAAMH,OAAOI,aAAa,mBACtCC,mBAAqBC,SAASC,cAAc,6BAC5CC,iBAAmBF,SAASC,cAAc,oCAC5CC,mBACGH,qBACCA,mBAAmBI,UAAY,IAEnCD,iBAAiBC,UAAa,+DAE7B,CAAC,CACFC,WAAY,uCACZC,KAAM,CACFC,KAAMX,KAAKW,KACXC,KAAMZ,KAAKY,KACXC,GAAIb,KAAKa,GACTC,MAAOd,KAAKc,MACZC,SAAUf,KAAKY,KACfI,OAAQhB,KAAKa,GACbI,UAAWjB,KAAKiB,cACpB,GAAGC,MAAMC,YAxEIC,YAyEbC,QAAQrB,KAAK,kBAAkBmB,OAC3BZ,kBACAc,QAAQrB,KAAK,sCAAsCI,mBAAmBG,kBACnEH,qBACCA,mBAAmBI,UAAYW,MAAMG,SAEzCf,iBAAiBC,UAAYW,MAAMA,QAEnCE,QAAQrB,KAAK,qCAjFJoB,YAkFG,CACRG,MAAOJ,MAAMG,QACbE,KAAML,MAAMA,OAnFrBM,eAAMC,OACED,eAAMC,OAAON,aAEbO,uBAAaD,OAAON,cAiFpBF,MAAKU,QAEJA,MAAMC,UAAUC,GAAGC,sBAAYC,QAAQ,KAEnCJ,MAAMK,aAGVL,MAAMM,OACCN,SAEVO,MAAMC,sBAAaC,eAGzBF,OAAOG,QACNjB,QAAQiB,MAAM,gCAAiCA,OAC/CC,OAAOC,KAAKzC,OAAOI,aAAa,QAAQ,UAAU,2BAGtDL,MAAM2C,kBACC,uFAYFC,YAAa,CACdA,aAAc,QAERC,SAAWtC,SAASuC,iBAAiB,gCAC3CvB,QAAQrB,KAAK,iBAAiB2C,UAC9BA,SAASE,SAASC,KACdzB,QAAQrB,KAAK,mBAAmB8C,IAChCA,GAAGC,iBAAiB,QAASlD,yNAbrC6C,aAAc"} \ No newline at end of file +{"version":3,"file":"filter_bibleversesnwt.min.js","sources":["../src/filter_bibleversesnwt.js"],"sourcesContent":["/*eslint no-var: \"error\" */\n/*eslint no-unused-vars: \"off\" */\n/*eslint no-console: \"off\" */\n/*eslint linebreak-style: \"off\" */\n/*eslint no-trailing-spaces: \"off\" */\n/*eslint-env es6*/\n// Put this file in path/to/plugin/amd/src\n// You can call it anything you like\nimport {call} from 'core/ajax';\nimport Modal from 'core/modal';\nimport ModalFactory from 'core/modal_factory';\nimport ModalEvents from 'core/modal_events';\nimport Notification from 'core/notification';\n\n/**\n * \n * @param {*} modalconfig \n * @returns Promise of modal\n */\nfunction modalcreate(modalconfig) {\n if(Modal.create) {\n return Modal.create(modalconfig);\n } else {\n return ModalFactory.create(modalconfig);\n }\n}\n\nconst Preloaded = {};\n\n/**\n * \n * @param {*} info \n */\nfunction getQuote(info) {\n const key = `${info.lang}|${info.book}.${info.ch}:${info.verse}-${info.book_end}.${info.ch_end}:${info.verse_end}`;\n return new Promise((resolve,reject) => {\n if (key in Preloaded) {\n resolve(Preloaded[key]);\n } else {\n call([{\n methodname: 'filter_bibleversesnwt_get_biblequote',\n args: { \n lang: info.lang,\n book: info.book,\n ch: info.ch,\n verse: info.verse,\n book_end: info.book,\n ch_end: info.ch_end,\n verse_end: info.verse_end}\n }])[0].then((quote) => {\n Preloaded[key] = quote;\n resolve(quote);\n }).catch((error) => {\n reject(error);\n });\n }\n });\n}\n\n/**\n * \n * @param {*} event \n */\nfunction bible_link(event) {\n const target = event.target;\n const info = JSON.parse(target.getAttribute(\"data-verseinfo\"));\n const bibleblock_heading = document.querySelector(\"#block_bibleblock_heading\");\n const bibleblock_quote = document.querySelector(\"#block_bibleblock_content\");\n if( bibleblock_quote ) {\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = \"\";\n }\n bibleblock_quote.innerHTML = `
`;\n }\n\n getQuote(info).then((quote) => {\n console.info(\"Got bible quote\",quote);\n if( bibleblock_quote ) {\n console.info(\"Attempting to place in verses block\",bibleblock_heading,bibleblock_quote);\n if(bibleblock_heading) {\n bibleblock_heading.innerHTML = quote.heading;\n }\n bibleblock_quote.innerHTML = quote.quote;\n } else {\n console.info(\"Attempting to place in new modal\",);\n modalcreate({\n title: quote.heading,\n body: quote.quote,\n }).then(modal => {\n // Handle hidden event.\n modal.getRoot().on(ModalEvents.hidden, () => {\n // Destroy when hidden.\n modal.destroy();\n });\n // Show the modal.\n modal.show();\n return modal;\n })\n .catch(Notification.exception);\n\n }\n }).catch((error) => {\n console.error(\"Error retrieving bible quote:\", error);\n window.open(target.getAttribute(\"href\"),\"_bible_\",\"width=500,height=800\");\n });\n\n event.preventDefault();\n return false;\n}\n\n/**\n * \n */\nlet initialized = false;\n/**\n * Initialize the Page\n */\nexport function init() {\n // Make sure this function is only called once on the page. May be called multiple times because of filter weirdness\n if (!initialized) {\n initialized = true;\n\n const elements = document.querySelectorAll(\"a.bible-link[data-verseinfo]\");\n console.info(\"Found elements\",elements);\n elements.forEach((el) => {\n console.info(\"Handling element\",el);\n el.addEventListener(\"click\", bible_link);\n const info = JSON.parse(el.getAttribute(\"data-verseinfo\"));\n getQuote(info);\n });\n }\n}\n\n\n"],"names":["initialized","elements","document","querySelectorAll","console","info","forEach","el","addEventListener","bible_link","getQuote","JSON","parse","getAttribute","Preloaded","key","lang","book","ch","verse","book_end","ch_end","verse_end","Promise","resolve","reject","methodname","args","then","quote","catch","error","event","target","bibleblock_heading","querySelector","bibleblock_quote","innerHTML","modalconfig","heading","title","body","Modal","create","ModalFactory","modal","getRoot","on","ModalEvents","hidden","destroy","show","Notification","exception","window","open","preventDefault"],"mappings":"0YAuHSA,YAAa,CACdA,aAAc,QAERC,SAAWC,SAASC,iBAAiB,gCAC3CC,QAAQC,KAAK,iBAAiBJ,UAC9BA,SAASK,SAASC,KACdH,QAAQC,KAAK,mBAAmBE,IAChCA,GAAGC,iBAAiB,QAASC,YAE7BC,SADaC,KAAKC,MAAML,GAAGM,aAAa,mBACxC,gNArGNC,UAAY,YAMTJ,SAASL,YACRU,IAAO,GAAEV,KAAKW,QAAQX,KAAKY,QAAQZ,KAAKa,MAAMb,KAAKc,SAASd,KAAKe,YAAYf,KAAKgB,UAAUhB,KAAKiB,mBAChG,IAAIC,SAAQ,CAACC,QAAQC,UACpBV,OAAOD,UACPU,QAAQV,UAAUC,qBAEb,CAAC,CACFW,WAAY,uCACZC,KAAM,CACFX,KAAMX,KAAKW,KACXC,KAAMZ,KAAKY,KACXC,GAAIb,KAAKa,GACTC,MAAOd,KAAKc,MACZC,SAAUf,KAAKY,KACfI,OAAQhB,KAAKgB,OACbC,UAAWjB,KAAKiB,cACpB,GAAGM,MAAMC,QACTf,UAAUC,KAAOc,MACjBL,QAAQK,MAAR,IACDC,OAAOC,QACNN,OAAOM,MAAP,gBAUPtB,WAAWuB,aACVC,OAASD,MAAMC,OACf5B,KAAOM,KAAKC,MAAMqB,OAAOpB,aAAa,mBACtCqB,mBAAqBhC,SAASiC,cAAc,6BAC5CC,iBAAmBlC,SAASiC,cAAc,oCAC5CC,mBACGF,qBACCA,mBAAmBG,UAAY,IAEnCD,iBAAiBC,UAAa,gDAGlC3B,SAASL,MAAMuB,MAAMC,YAxDJS,YAyDblC,QAAQC,KAAK,kBAAkBwB,OAC3BO,kBACAhC,QAAQC,KAAK,sCAAsC6B,mBAAmBE,kBACnEF,qBACCA,mBAAmBG,UAAYR,MAAMU,SAEzCH,iBAAiBC,UAAYR,MAAMA,QAEnCzB,QAAQC,KAAK,qCAjEJiC,YAkEG,CACRE,MAAOX,MAAMU,QACbE,KAAMZ,MAAMA,OAnErBa,eAAMC,OACED,eAAMC,OAAOL,aAEbM,uBAAaD,OAAOL,cAiEpBV,MAAKiB,QAEJA,MAAMC,UAAUC,GAAGC,sBAAYC,QAAQ,KAEnCJ,MAAMK,aAGVL,MAAMM,OACCN,SAEVf,MAAMsB,sBAAaC,eAGzBvB,OAAOC,QACN3B,QAAQ2B,MAAM,gCAAiCA,OAC/CuB,OAAOC,KAAKtB,OAAOpB,aAAa,QAAQ,UAAU,2BAGtDmB,MAAMwB,kBACC,MAMPxD,aAAc"} \ No newline at end of file diff --git a/amd/src/filter_bibleversesnwt.js b/amd/src/filter_bibleversesnwt.js index 55abb1e..6d44fb9 100644 --- a/amd/src/filter_bibleversesnwt.js +++ b/amd/src/filter_bibleversesnwt.js @@ -24,43 +24,36 @@ function modalcreate(modalconfig) { return ModalFactory.create(modalconfig); } } + +const Preloaded = {}; + /** * - * @param {*} lang - * @param {*} book - * @param {*} ch - * @param {*} verse - * @param {*} book_end - * @param {*} ch_end - * @param {*} verse_end + * @param {*} info */ -function getBibleQuote(lang,book,ch,verse,book_end, ch_end, verse_end) { - return new Promise((resolve, reject) => { - let range = "00000000"; - let url = ""; - - range = book.toString() + ch.toString().padStart(3,'0') + verse.toString().padStart(3,'0'); - - if(book_end && ch_end && verse_end) { - range = range + "-" + book_end.toString() + ch_end.toString().padStart(3,'0') + verse_end.toString().padStart(3,'0'); +function getQuote(info) { + const key = `${info.lang}|${info.book}.${info.ch}:${info.verse}-${info.book_end}.${info.ch_end}:${info.verse_end}`; + return new Promise((resolve,reject) => { + if (key in Preloaded) { + resolve(Preloaded[key]); + } else { + call([{ + methodname: 'filter_bibleversesnwt_get_biblequote', + args: { + lang: info.lang, + book: info.book, + ch: info.ch, + verse: info.verse, + book_end: info.book, + ch_end: info.ch_end, + verse_end: info.verse_end} + }])[0].then((quote) => { + Preloaded[key] = quote; + resolve(quote); + }).catch((error) => { + reject(error); + }); } - - if (lang == "nl") { - url = `https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/${range}`; - } else { // if $lang == 'en' - url = `https://www.jw.org/en/library/bible/nwt/books/json/html/${range}`; - } - - window.fetch(url).then((response) => { - try { - const o = JSON.parse(); - resolve(o.ranges[range]); - } catch (error) { - reject("Error parsing json response",error, response); - } - }).catch((error) => { - reject("Error retrieving url",error); - }); }); } @@ -79,17 +72,8 @@ function bible_link(event) { } bibleblock_quote.innerHTML = `
`; } - call([{ - methodname: 'filter_bibleversesnwt_get_biblequote', - args: { - lang: info.lang, - book: info.book, - ch: info.ch, - verse: info.verse, - book_end: info.book, - ch_end: info.ch, - verse_end: info.verse_end} - }])[0].then((quote) => { + + getQuote(info).then((quote) => { console.info("Got bible quote",quote); if( bibleblock_quote ) { console.info("Attempting to place in verses block",bibleblock_heading,bibleblock_quote); @@ -141,6 +125,8 @@ export function init() { elements.forEach((el) => { console.info("Handling element",el); el.addEventListener("click", bible_link); + const info = JSON.parse(el.getAttribute("data-verseinfo")); + getQuote(info); }); } } diff --git a/build/bibleversesnwt-2023112000.zip b/build/bibleversesnwt-2023112000.zip new file mode 100644 index 0000000..343db3e Binary files /dev/null and b/build/bibleversesnwt-2023112000.zip differ diff --git a/build/bibleversesnwt-2023122800.zip b/build/bibleversesnwt-2023122800.zip new file mode 100644 index 0000000..cece1e1 Binary files /dev/null and b/build/bibleversesnwt-2023122800.zip differ diff --git a/build/bibleversesnwt-2023122801.zip b/build/bibleversesnwt-2023122801.zip new file mode 100644 index 0000000..521ecce Binary files /dev/null and b/build/bibleversesnwt-2023122801.zip differ diff --git a/classes/bible.php b/classes/bible.php index cd6c857..714e3f1 100644 --- a/classes/bible.php +++ b/classes/bible.php @@ -16,17 +16,17 @@ class bible { return $languages; } - private function wolhref($book,$chapter,$verse,$verse_end=0){ + private function wolhref($book,$chapter,$verse,$chapter_end,$verse_end=0){ $book = ($book > 0)?$book:1; $chapter = ($chapter >0)?$chapter:1; $verse = ($verse > 0)?$verse:1; $verse_end = ($verse_end > $verse)?$verse_end:$verse; if($this->lang == "nl"){ - return "https://wol.jw.org/nl/wol/b/r18/lp-o/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter}:{$verse_end}"; + return "https://wol.jw.org/nl/wol/b/r18/lp-o/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter_end}:{$verse_end}"; } else { // if $this->lang == 'en' - return "https://wol.jw.org/nl/wol/b/r1/lp-e/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter}:{$verse_end}"; + return "https://wol.jw.org/nl/wol/b/r1/lp-e/nwtsty/{$book}/{$chapter}#v={$book}:{$chapter}:{$verse}-{$book}:{$chapter_end}:{$verse_end}"; } } @@ -185,21 +185,24 @@ class bible { $names = $this->books[$booknum]; foreach($names as $bookname){ - $re = "/([1-3]?\s?\b{$bookname})\s*?(\d*):(\d+)(?:\s*?[-,]\s*?(\d+))?/i"; + $re = "/(\b{$bookname})\h*?(\d*):(\d+)(?:\h*[-,]\h*(\d+)(?::(\d+))?)?/i"; // First check the presence of the bookname before - if( ($ix = stripos($content,$bookname)) !== false){ - $matches = []; + if ( ($ix = stripos($content,$bookname)) !== false ) { $lang = $this->lang; $content = preg_replace_callback($re,function ($m) use ($lang,$booknum, $bookname){ $book = strtolower(trim($m[1])); $ch = $m[2]; $verse = $m[3]; - $verse_to = (count($m)>4)?$m[4]:0; + $ch_end = (count($m)>5)?$m[4]:$ch; + $verse_to = (count($m)>5)?$m[5]:((count($m)>4)?$m[4]:0); if($book == $bookname){ // Check if name matches - $href = $this->wolhref($lang, $booknum, $ch, $verse, $verse_to); - $versedata = json_encode(["lang" => $lang, "book" => $booknum, "ch" => $ch, "verse" => $verse, "verse_end" => $verse_to, "wolhref" => $href]); + $href = $this->wolhref($booknum, $ch, $verse, $ch_end, $verse_to); + $versedata = json_encode([ "lang" => $lang, "book" => $booknum, + "ch" => $ch, "verse" => $verse, + "ch_end" => $ch_end, "verse_end" => $verse_to, + "wolhref" => $href]); return "".$m[0].""; } else { return $m[0]; diff --git a/classes/bibleservice.php b/classes/bibleservice.php index 5c6973a..622fd08 100644 --- a/classes/bibleservice.php +++ b/classes/bibleservice.php @@ -63,33 +63,72 @@ class bibleservice extends \external_api { * @return array */ public static function get_biblequote($lang,$book,$ch,$verse,$book_end = null,$ch_end = null,$verse_end = null) { - $range = $book .str_pad($ch,3,'0',STR_PAD_LEFT) . str_pad($verse,3,'0',STR_PAD_LEFT); + global $DB; + $TABLE = "filter_bibleversesnwt"; - if(!empty($book_end) && !empty($ch_end) && !empty($verse_end)) { - $range .= "-" . $book_end .str_pad($ch_end,3,'0',STR_PAD_LEFT) . str_pad($verse_end,3,'0',STR_PAD_LEFT); - } + $search = [ + "lang" => $lang, + "book" => $book, + "ch"=> $ch, + "verse" => $verse, + "book_end" => $book_end, + "ch_end" => $ch_end, + "verse_end" => $verse_end, + ]; - if ($lang == "nl") { - $url = "https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/{$range}"; - } else { // if $lang == 'en' - $url = "https://www.jw.org/en/library/bible/nwt/books/json/html/{$range}"; - } + $cache = $DB->get_record($TABLE,$search); + $now = time(); - $ch = curl_init(); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_SSH_COMPRESSION, true); - $result = curl_exec($ch); - - $o = json_decode($result,true); - if(isset($o["ranges"]) && isset($o["ranges"][$range])) { - $r = $o["ranges"][$range]; - return [ - "heading" => $r["citation"], - "quote" => preg_replace('#(.*?)#i', '\1', $r["html"]), - ]; + if($cache && ($now - $cache->time_retrieved) < (24*3600)) { + // Return from cache. + $quote = json_decode($cache->content,true); + return $quote; } else { - throw new \moodle_exception("Unexpected JSON code",'','',null,$result); + + $range = $book .str_pad($ch,3,'0',STR_PAD_LEFT) . str_pad($verse,3,'0',STR_PAD_LEFT); + + if(!empty($book_end) && !empty($ch_end) && !empty($verse_end)) { + $range .= "-" . $book_end .str_pad($ch_end,3,'0',STR_PAD_LEFT) . str_pad($verse_end,3,'0',STR_PAD_LEFT); + } + + if ($lang == "nl") { + $url = "https://www.jw.org/nl/bibliotheek/bijbel/nwt/boeken/json/html/{$range}"; + } else { // if $lang == 'en' + $url = "https://www.jw.org/en/library/bible/nwt/books/json/html/{$range}"; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSH_COMPRESSION, true); + $result = curl_exec($ch); + + $o = json_decode($result,true); + if(isset($o["ranges"]) && isset($o["ranges"][$range])) { + $r = $o["ranges"][$range]; + + $quote = [ + "heading" => $r["citation"], + "quote" => preg_replace('#(.*?)#i', '\1', $r["html"]), + ]; + + // Store bible verse into cache. + $quotejson = json_encode($quote); + if($cache) { + $cache->time_retrieved = time(); + $cache->content = $quotejson; + $DB->update_record($TABLE,$cache); + } else { + $cache = (object)$search; + $cache->time_retrieved = time(); + $cache->content = $quotejson; + $DB->insert_record($TABLE,$cache); + } + + return $quote; + } else { + throw new \moodle_exception("Unexpected JSON code",'','',null,$result); + } } } diff --git a/db/install.xml b/db/install.xml new file mode 100644 index 0000000..1df5d89 --- /dev/null +++ b/db/install.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + +
+
+
diff --git a/db/upgrade.php b/db/upgrade.php new file mode 100644 index 0000000..4799d1e --- /dev/null +++ b/db/upgrade.php @@ -0,0 +1,92 @@ +. +/** + * Database upgrade script + * @package filter_bibleversesnwt + * @copyright 2023 P.M. Kuipers + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Hook to upgrade database upon plugin upgrade + * @param mixed $oldversion Version of plugin database before upgrade + * @return bool Upgrade success status + * + */ +function xmldb_filter_bibleversesnwt_upgrade($oldversion) { + global $DB; + $dbman = $DB->get_manager(); + + if ($oldversion < 2024030800) { + + // Define table filter_bibleversesnwt to be created. + $table = new xmldb_table('filter_bibleversesnwt'); + + // Adding fields to table filter_bibleversesnwt. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('book', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('ch', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('verse', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('book_end', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('ch_end', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('verse_end', XMLDB_TYPE_INTEGER, '5', null, null, null, null); + $table->add_field('content', XMLDB_TYPE_TEXT, null, null, null, null, null); + + // Adding keys to table filter_bibleversesnwt. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + + // Conditionally launch create table for filter_bibleversesnwt. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Bibleversesnwt savepoint reached. + upgrade_plugin_savepoint(true, 2024030800, 'filter', 'bibleversesnwt'); + } + if ($oldversion < 2024030801) { + + // Define field time_retrieved to be added to filter_bibleversesnwt. + $table = new xmldb_table('filter_bibleversesnwt'); + $field = new xmldb_field('time_retrieved', XMLDB_TYPE_INTEGER, '20', null, null, null, null, 'content'); + + // Conditionally launch add field time_retrieved. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Bibleversesnwt savepoint reached. + upgrade_plugin_savepoint(true, 2024030801, 'filter', 'bibleversesnwt'); + } + + + if ($oldversion < 2024030802) { + + // Define field lang to be added to filter_bibleversesnwt. + $table = new xmldb_table('filter_bibleversesnwt'); + $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '12', null, null, null, null, 'time_retrieved'); + + // Conditionally launch add field lang. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Bibleversesnwt savepoint reached. + upgrade_plugin_savepoint(true, 2024030802, 'filter', 'bibleversesnwt'); + } + + + return true; +} diff --git a/version.php b/version.php index c39a5f3..3161654 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023112000; +$plugin->version = 2024030802; $plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11) $plugin->component = 'filter_bibleversesnwt'; $plugin->maturity = MATURITY_STABLE;