| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
const config = require( './config.json' ); |
| 6 |
const dbName = mw.config.get( 'wgDBname' ) + '_editRecovery'; |
| 7 |
const editRecoveryExpiry = config.EditRecoveryExpiry; |
| 8 |
const objectStoreName = 'unsaved-page-data'; |
| 9 |
|
| 10 |
let db = null; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
function openDatabaseLocal() { |
| 19 |
return new Promise( ( resolve, reject ) => { |
| 20 |
const schemaNumber = 3; |
| 21 |
const openRequest = window.indexedDB.open( dbName, schemaNumber ); |
| 22 |
openRequest.addEventListener( 'upgradeneeded', upgradeDatabase ); |
| 23 |
openRequest.addEventListener( 'success', ( event ) => { |
| 24 |
db = event.target.result; |
| 25 |
resolve(); |
| 26 |
} ); |
| 27 |
openRequest.addEventListener( 'error', ( event ) => { |
| 28 |
reject( 'EditRecovery error: ' + event.target.error ); |
| 29 |
} ); |
| 30 |
} ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
function upgradeDatabase( versionChangeEvent ) { |
| 38 |
const keyPathParts = [ 'pageName', 'section' ]; |
| 39 |
let objectStore; |
| 40 |
|
| 41 |
db = versionChangeEvent.target.result; |
| 42 |
if ( !db.objectStoreNames.contains( objectStoreName ) ) { |
| 43 |
|
| 44 |
objectStore = db.createObjectStore( objectStoreName, { keyPath: keyPathParts } ); |
| 45 |
} else { |
| 46 |
|
| 47 |
objectStore = versionChangeEvent.target.transaction.objectStore( objectStoreName ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
if ( !objectStore.indexNames.contains( 'pageName-section' ) ) { |
| 52 |
objectStore.createIndex( 'pageName-section', keyPathParts, { unique: true } ); |
| 53 |
} |
| 54 |
if ( !objectStore.indexNames.contains( 'expiry' ) ) { |
| 55 |
objectStore.createIndex( 'expiry', 'expiry' ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if ( objectStore.indexNames.contains( 'lastModified' ) ) { |
| 60 |
objectStore.deleteIndex( 'lastModified' ); |
| 61 |
} |
| 62 |
if ( objectStore.indexNames.contains( 'expiryDate' ) ) { |
| 63 |
objectStore.deleteIndex( 'expiryDate' ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
function loadData( pageName, section ) { |
| 76 |
return new Promise( ( resolve, reject ) => { |
| 77 |
if ( !db ) { |
| 78 |
reject( 'DB not opened' ); |
| 79 |
} |
| 80 |
const transaction = db.transaction( objectStoreName, 'readonly' ); |
| 81 |
const key = [ pageName, section || '' ]; |
| 82 |
const findExisting = transaction |
| 83 |
.objectStore( objectStoreName ) |
| 84 |
.get( key ); |
| 85 |
findExisting.addEventListener( 'success', () => { |
| 86 |
resolve( findExisting.result ); |
| 87 |
} ); |
| 88 |
} ); |
| 89 |
} |
| 90 |
|
| 91 |
function loadAllData() { |
| 92 |
return new Promise( ( resolve, reject ) => { |
| 93 |
if ( !db ) { |
| 94 |
reject( 'DB not opened' ); |
| 95 |
} |
| 96 |
const transaction = db.transaction( objectStoreName, 'readonly' ); |
| 97 |
const requestAll = transaction |
| 98 |
.objectStore( objectStoreName ) |
| 99 |
.getAll(); |
| 100 |
requestAll.addEventListener( 'success', () => { |
| 101 |
resolve( requestAll.result ); |
| 102 |
} ); |
| 103 |
} ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
function saveData( pageName, section, pageData ) { |
| 116 |
return new Promise( ( resolve, reject ) => { |
| 117 |
if ( !db ) { |
| 118 |
reject( 'DB not opened' ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
pageData.pageName = pageName; |
| 123 |
pageData.section = section || ''; |
| 124 |
pageData.expiry = getExpiryDate( editRecoveryExpiry ); |
| 125 |
|
| 126 |
const transaction = db.transaction( objectStoreName, 'readwrite' ); |
| 127 |
const objectStore = transaction.objectStore( objectStoreName ); |
| 128 |
|
| 129 |
const request = objectStore.put( pageData ); |
| 130 |
request.addEventListener( 'success', ( event ) => { |
| 131 |
resolve( event ); |
| 132 |
} ); |
| 133 |
request.addEventListener( 'error', ( event ) => { |
| 134 |
reject( 'Error saving data: ' + event.target.errorCode ); |
| 135 |
} ); |
| 136 |
} ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
function deleteData( pageName, section ) { |
| 148 |
return new Promise( ( resolve, reject ) => { |
| 149 |
if ( !db ) { |
| 150 |
reject( 'DB not opened' ); |
| 151 |
} |
| 152 |
|
| 153 |
const transaction = db.transaction( objectStoreName, 'readwrite' ); |
| 154 |
const objectStore = transaction.objectStore( objectStoreName ); |
| 155 |
|
| 156 |
const request = objectStore.delete( [ pageName, section || '' ] ); |
| 157 |
request.addEventListener( 'success', resolve ); |
| 158 |
request.addEventListener( 'error', () => { |
| 159 |
reject( 'Error opening cursor' ); |
| 160 |
} ); |
| 161 |
} ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
function getExpiryDate( diff ) { |
| 172 |
return ( Date.now() / 1000 ) + diff; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
function deleteExpiredData() { |
| 182 |
return new Promise( ( resolve, reject ) => { |
| 183 |
if ( !db ) { |
| 184 |
reject( 'DB not opened' ); |
| 185 |
} |
| 186 |
|
| 187 |
const transaction = db.transaction( objectStoreName, 'readwrite' ); |
| 188 |
const objectStore = transaction.objectStore( objectStoreName ); |
| 189 |
const expiry = objectStore.index( 'expiry' ); |
| 190 |
const now = Date.now() / 1000; |
| 191 |
|
| 192 |
const expired = expiry.getAll( IDBKeyRange.upperBound( now, true ) ); |
| 193 |
|
| 194 |
expired.addEventListener( 'success', ( event ) => { |
| 195 |
const cursors = event.target.result; |
| 196 |
if ( cursors.length > 0 ) { |
| 197 |
const deletions = []; |
| 198 |
cursors.forEach( ( cursor ) => { |
| 199 |
deletions.push( deleteData( cursor.pageName, cursor.section ) ); |
| 200 |
} ); |
| 201 |
Promise.all( deletions ).then( resolve ); |
| 202 |
} else { |
| 203 |
resolve(); |
| 204 |
} |
| 205 |
} ); |
| 206 |
|
| 207 |
expired.addEventListener( 'error', () => { |
| 208 |
reject( 'Error getting filtered data' ); |
| 209 |
} ); |
| 210 |
} ); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
function closeDatabase() { |
| 219 |
if ( db ) { |
| 220 |
db.close(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
module.exports = { |
| 225 |
openDatabase: openDatabaseLocal, |
| 226 |
closeDatabase, |
| 227 |
loadData, |
| 228 |
loadAllData, |
| 229 |
saveData, |
| 230 |
deleteData, |
| 231 |
deleteExpiredData |
| 232 |
}; |