Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class CoreSitePluginsPluginContentComponent implements OnInit, DoCheck {
this.jsData = Object.assign(this.data, this.sitePluginsProvider.createDataForJS(this.initResult, result));

// Pass some methods as jsData so they can be called from the template too.
this.jsData.fetchContent = this.fetchContent.bind(this);
this.jsData.openContent = this.openContent.bind(this);
this.jsData.refreshContent = this.refreshContent.bind(this);
this.jsData.updateContent = this.updateContent.bind(this);
Expand Down
2 changes: 1 addition & 1 deletion src/core/siteplugins/providers/siteplugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class CoreSitePluginsProvider {

if (initResult) {
// First of all, add the data returned by the init JS (if any).
data = this.utils.clone(initResult.jsResult || {});
data = Object.assign({}, initResult.jsResult || {});
if (typeof data == 'boolean') {
data = {};
}
Expand Down
87 changes: 54 additions & 33 deletions src/providers/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,16 @@ export class CoreSitesProvider {
* Register a site schema.
*/
registerSiteSchema(schema: CoreSiteSchema): void {
this.siteSchemas[schema.name] = schema;

if (this.currentSite) {
// Site has already been created, it's a schema probably added by site plugins. Add it only to current site.
const schemas: {[name: string]: CoreSiteSchema} = {};
schemas[schema.name] = schema;

this.applySiteSchemas(this.currentSite, schemas);
} else {
this.siteSchemas[schema.name] = schema;
}
}

/**
Expand All @@ -1638,7 +1647,6 @@ export class CoreSitesProvider {
* @return Promise resolved when done.
*/
migrateSiteSchemas(site: CoreSite): Promise<any> {
const db = site.getDb();

if (this.siteSchemasMigration[site.id]) {
return this.siteSchemasMigration[site.id];
Expand All @@ -1647,46 +1655,59 @@ export class CoreSitesProvider {
this.logger.debug(`Migrating all schemas of ${site.id}`);

// First create tables not registerd with name/version.
const promise = db.createTablesFromSchema(this.siteTablesSchemas).then(() => {
// Fetch installed versions of the schema.
return db.getAllRecords(this.SCHEMA_VERSIONS_TABLE).then((records) => {
const versions = {};
records.forEach((record) => {
versions[record.name] = record.version;
});
const promise = site.getDb().createTablesFromSchema(this.siteTablesSchemas).then(() => {
return this.applySiteSchemas(site, this.siteSchemas);
});

const promises = [];
for (const name in this.siteSchemas) {
const schema = this.siteSchemas[name];
const oldVersion = versions[name] || 0;
if (oldVersion >= schema.version) {
continue;
}
this.siteSchemasMigration[site.id] = promise;

this.logger.debug(`Migrating schema '${name}' of ${site.id} from version ${oldVersion} to ${schema.version}`);
return promise.finally(() => {
delete this.siteSchemasMigration[site.id];
});
}

let promise: Promise<any> = Promise.resolve();
if (schema.tables) {
promise = promise.then(() => db.createTablesFromSchema(schema.tables));
}
if (schema.migrate) {
promise = promise.then(() => schema.migrate(db, oldVersion, site.id));
}
/**
* Install and upgrade the supplied schemas for a certain site.
*
* @param site Site.
* @param schemas Schemas to migrate.
* @return Promise resolved when done.
*/
protected applySiteSchemas(site: CoreSite, schemas: {[name: string]: CoreSiteSchema}): Promise<any> {
const db = site.getDb();

// Set installed version.
promise = promise.then(() => db.insertRecord(this.SCHEMA_VERSIONS_TABLE, {name, version: schema.version}));
// Fetch installed versions of the schema.
return db.getAllRecords(this.SCHEMA_VERSIONS_TABLE).then((records) => {
const versions = {};
records.forEach((record) => {
versions[record.name] = record.version;
});

promises.push(promise);
const promises = [];
for (const name in schemas) {
const schema = schemas[name];
const oldVersion = versions[name] || 0;
if (oldVersion >= schema.version) {
continue;
}

return Promise.all(promises);
});
});
this.logger.debug(`Migrating schema '${name}' of ${site.id} from version ${oldVersion} to ${schema.version}`);

this.siteSchemasMigration[site.id] = promise;
let promise: Promise<any> = Promise.resolve();
if (schema.tables) {
promise = promise.then(() => db.createTablesFromSchema(schema.tables));
}
if (schema.migrate) {
promise = promise.then(() => schema.migrate(db, oldVersion, site.id));
}

return promise.finally(() => {
delete this.siteSchemasMigration[site.id];
// Set installed version.
promise = promise.then(() => db.insertRecord(this.SCHEMA_VERSIONS_TABLE, {name, version: schema.version}));

promises.push(promise);
}

return Promise.all(promises);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class CoreUrlUtilsProvider {
if (!this.isPluginFileUrl(url) || url.indexOf(this.textUtils.addEndingSlash(siteUrl)) !== 0) {
return url;
}

if (canUseTokenPluginFile) {
// Use tokenpluginfile.php.
url = url.replace(/(\/webservice)?\/pluginfile\.php/, '/tokenpluginfile.php/' + accessKey);
Expand Down