From c4bff23bf6abc370fc9fa23c04338f6fdedc4d4b Mon Sep 17 00:00:00 2001 From: Universal Omega <54654040+Universal-Omega@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:50:27 -0600 Subject: [PATCH] Use database update maintenance script for creating VIEW (#165) --- extension.json | 3 +- includes/Hooks.php | 9 ++---- maintenance/createView.php | 58 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 maintenance/createView.php diff --git a/extension.json b/extension.json index e941b66b..d2df36c6 100644 --- a/extension.json +++ b/extension.json @@ -32,7 +32,8 @@ "DynamicPageList3Magic": "DynamicPageList3Magic.php" }, "AutoloadClasses": { - "MediaWiki\\Extension\\DynamicPageList3\\Maintenance\\CreateTemplate": "maintenance/createTemplate.php" + "MediaWiki\\Extension\\DynamicPageList3\\Maintenance\\CreateTemplate": "maintenance/createTemplate.php", + "MediaWiki\\Extension\\DynamicPageList3\\Maintenance\\CreateView": "maintenance/createView.php" }, "AutoloadNamespaces": { "MediaWiki\\Extension\\DynamicPageList3\\": "includes/" diff --git a/includes/Hooks.php b/includes/Hooks.php index 2ca084bc..f63c8ffc 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -4,6 +4,7 @@ use DatabaseUpdater; use MediaWiki\Extension\DynamicPageList3\Maintenance\CreateTemplate; +use MediaWiki\Extension\DynamicPageList3\Maintenance\CreateView; use Parser; use PPFrame; @@ -626,12 +627,6 @@ public static function endEliminate( $parser, &$text ) { */ public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) { $updater->addPostDatabaseUpdateMaintenance( CreateTemplate::class ); - - $db = $updater->getDB(); - if ( !$db->tableExists( 'dpl_clview' ) ) { - // PostgreSQL doesn't have IFNULL, so use COALESCE instead - $sqlNullMethod = ( $db->getType() === 'postgres' ? 'COALESCE' : 'IFNULL' ); - $db->query( "CREATE VIEW {$db->tablePrefix()}dpl_clview AS SELECT $sqlNullMethod(cl_from, page_id) AS cl_from, $sqlNullMethod(cl_to, '') AS cl_to, cl_sortkey FROM {$db->tablePrefix()}page LEFT OUTER JOIN {$db->tablePrefix()}categorylinks ON {$db->tablePrefix()}page.page_id=cl_from;" ); - } + $updater->addPostDatabaseUpdateMaintenance( CreateView::class ); } } diff --git a/maintenance/createView.php b/maintenance/createView.php new file mode 100644 index 00000000..8e8a525b --- /dev/null +++ b/maintenance/createView.php @@ -0,0 +1,58 @@ +addDescription( 'Handle creating DPL3\'s dpl_clview VIEW.' ); + } + + /** + * Get the unique update key for this logged update. + * + * @return string + */ + protected function getUpdateKey() { + return 'dynamic-page-list-3-create-view'; + } + + /** + * Message to show that the update was done already and was just skipped + * + * @return string + */ + protected function updateSkippedMessage() { + return 'VIEW already created.'; + } + + /** + * Handle creating DPL3's dpl_clview VIEW. + * + * @return bool + */ + protected function doDBUpdates() { + $dbw = $this->getDB( DB_PRIMARY ); + if ( !$dbw->tableExists( 'dpl_clview' ) ) { + // PostgreSQL doesn't have IFNULL, so use COALESCE instead + $sqlNullMethod = ( $dbw->getType() === 'postgres' ? 'COALESCE' : 'IFNULL' ); + $dbw->query( "CREATE VIEW {$dbw->tablePrefix()}dpl_clview AS SELECT $sqlNullMethod(cl_from, page_id) AS cl_from, $sqlNullMethod(cl_to, '') AS cl_to, cl_sortkey FROM {$dbw->tablePrefix()}page LEFT OUTER JOIN {$dbw->tablePrefix()}categorylinks ON {$dbw->tablePrefix()}page.page_id=cl_from;" ); + } + + return true; + } +} + +$maintClass = CreateView::class; +require_once RUN_MAINTENANCE_IF_MAIN;