-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef9a5b2
commit 44f3a1d
Showing
10 changed files
with
423 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
service-account.json | ||
node_modules | ||
wp-api-etl/lib/.http-cache | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import dotenv from "dotenv"; | ||
|
||
class Config { | ||
constructor(envPath) { | ||
if (envPath) { | ||
dotenv.config({ path: envPath }); | ||
} | ||
const env = process.env; | ||
|
||
this.host = env.HOST || "http://localhost:3000"; | ||
this.apiPath = env.API_PATH || "/wp-json/wp/v2"; | ||
this.useCache = env.USE_CACHE === 'false' ? false : true; | ||
if ( this.useCache ) { | ||
this.writeToCache = true; | ||
} else { | ||
this.writeToCache = env.WRITE_TO_CACHE === 'false' ? false : true; | ||
} | ||
} | ||
|
||
} | ||
|
||
export default Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fs from 'fs'; | ||
import { fileURLToPath } from 'url'; | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
|
||
class HTTPCache { | ||
|
||
constructor() { | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
this.localCacheDir = path.join(__dirname, '.http-cache'); | ||
if( !fs.existsSync(this.localCacheDir) ) { | ||
fs.mkdirSync(this.localCacheDir); | ||
} | ||
|
||
this.cache = {}; | ||
} | ||
|
||
get(url) { | ||
url = this.hash(url); | ||
if( this.cache[url] ) { | ||
return this.cache[url].response; | ||
} | ||
|
||
let cacheFile = this.getCacheFile(url); | ||
if( fs.existsSync(cacheFile) ) { | ||
let cache = JSON.parse(fs.readFileSync(cacheFile)); | ||
this.cache[url] = cache; | ||
return cache.response; | ||
} | ||
return null; | ||
} | ||
|
||
set(url, response) { | ||
let hash = this.hash(url); | ||
this.cache[hash] = {url, response}; | ||
fs.writeFileSync(this.getCacheFile(hash), JSON.stringify(this.cache[hash])); | ||
} | ||
|
||
getCacheFile(hash) { | ||
return path.join(this.localCacheDir, hash+'.json'); | ||
} | ||
|
||
hash(url) { | ||
return crypto.createHash('md5').update(url).digest('hex'); | ||
} | ||
|
||
clearCache(){ | ||
fs.rmdirSync(this.localCacheDir, { recursive: true }); | ||
fs.mkdirSync(this.localCacheDir); | ||
this.cache = {}; | ||
} | ||
|
||
} | ||
|
||
const instance = new HTTPCache(); | ||
export default instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import fetch from "node-fetch"; | ||
|
||
import Config from "./config.js"; | ||
import cache from '../lib/http-cache.js'; | ||
|
||
class WpApi { | ||
constructor(config) { | ||
if ( config ) { | ||
this.config = config; | ||
} else { | ||
this.config = new Config(); | ||
} | ||
} | ||
|
||
/** | ||
* @method getTypeName | ||
* @description make plural, api creaat endpoint hits the list endpoint with a POST | ||
* | ||
* @param {String} type | ||
* @returns | ||
*/ | ||
getTypeName(type) { | ||
if( type === 'media' ) return type; | ||
if( type === 'exhibit' ) return type; | ||
if( type === 'exhibits' ) return 'exhibit'; | ||
if( type === 'library' ) return type; | ||
if( type === 'libraries' ) return 'library'; | ||
if( type === 'expertise' ) return type; | ||
if( type === 'department' ) return type; | ||
if( type === 'departments' ) return 'department'; | ||
if( type === 'person' ) return type; | ||
if( type === 'directory-tag' ) return type; | ||
if( type === 'directory-tags' ) return 'directory-tag'; | ||
if( type === 'exhibit-location' ) return type; | ||
if( type === 'exhibit-locations' ) return 'exhibit-location'; | ||
if( type === 'curator' ) return type; | ||
if( type === 'curators' ) return 'curator'; | ||
|
||
if( !type.match(/s$/) ) { | ||
if( type.match(/y$/) ) type = type.replace(/y$/, 'ies'); | ||
else type = type+'s'; | ||
} | ||
return type; | ||
} | ||
|
||
async getPostsByType(postType, params = {}) { | ||
postType = this.getTypeName(postType); | ||
const queryParams = this.toSearchParamsObject(params.query); | ||
|
||
let host = this.config.host; | ||
if ( params.host ) { | ||
host = params.host; | ||
} | ||
|
||
let apiPath = this.config.apiPath; | ||
if ( params.apiPath ) { | ||
apiPath = params.apiPath; | ||
} | ||
|
||
let useCache = this.config.useCache; | ||
if ( params.hasOwnProperty('useCache') ) { | ||
useCache = params.useCache; | ||
} | ||
|
||
let writeToCache = this.config.writeToCache; | ||
if ( params.hasOwnProperty('writeToCache') ) { | ||
writeToCache = params.writeToCache; | ||
} | ||
|
||
const q = queryParams.toString(); | ||
let url = `${host}${apiPath}/${postType}${q ? '?' + q : ''}`; | ||
console.log(`Fetching ${url}`); | ||
|
||
if ( useCache ){ | ||
const cached = cache.get(url); | ||
if ( cached ) { | ||
return cached; | ||
} | ||
} | ||
|
||
const response = await fetch(url); | ||
if ( !response.ok ) { | ||
throw new Error(`HTTP error retrieving ${url}! status: ${response.status}`); | ||
} | ||
|
||
const json = await response.json(); | ||
if ( writeToCache ) { | ||
cache.set(url, json); | ||
} | ||
|
||
return json; | ||
|
||
} | ||
|
||
async getPostsByTypeAll(postType, params = {}) { | ||
const out = []; | ||
let page = 1; | ||
const queryParams = this.toSearchParamsObject(params.query); | ||
queryParams.set('per_page', 100); | ||
while ( true ) { | ||
queryParams.set('page', page); | ||
const p = {...params, query: queryParams} | ||
const list = await this.getPostsByType(postType, p); | ||
out.push(...list); | ||
if ( list.length < 100 ) { | ||
break; | ||
} | ||
page++; | ||
} | ||
} | ||
|
||
toSearchParamsObject(params) { | ||
params = new URLSearchParams(params); | ||
params.sort(); | ||
return params; | ||
} | ||
} | ||
|
||
export default WpApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Config from "../lib/config.js"; | ||
import { dirname, join } from 'path'; | ||
import { fileURLToPath, URLSearchParams } from 'url'; | ||
import { existsSync, mkdirSync } from 'fs'; | ||
import WpApi from "../lib/wp-api.js"; | ||
|
||
class LightBoxImageCheck { | ||
|
||
constructor(){ | ||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const envFile = join(__dirname, '.env'); | ||
if ( existsSync(envFile) ) { | ||
this.config = new Config(envFile); | ||
} else { | ||
console.warn(`No .env file found at ${envFile}`); | ||
this.config = new Config(); | ||
} | ||
|
||
this.api = new WpApi(this.config); | ||
|
||
} | ||
|
||
async run(){ | ||
this.findPostsWithMissingImageIds(); | ||
} | ||
|
||
async findPostsWithMissingImageIds(postTypes = ['pages', 'exhibit']){ | ||
if ( !Array.isArray(postTypes) ) { | ||
postTypes = [postTypes]; | ||
} | ||
const out = {}; | ||
const p = await this.api.getPostsByTypeAll('exhibits'); | ||
} | ||
} | ||
|
||
const run = async () => { | ||
console.log("Running LightBoxImageCheck"); | ||
( new LightBoxImageCheck() ).run(); | ||
} | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Image Correction for Lightbox Plugin. | ||
|
||
The WP Gallery block has functionality that allows a user to bulk set the "link to" property on all image blocks. However, in some cases, [this does not work on our site](https://github.com/UCDavisLibrary/main-wp-website/issues/85#issuecomment-1535287882). The issue seems to be that when the gallery was originally imported, the images were uploaded, but the `data-id` attribute was not set. |
Oops, something went wrong.