Use database update maintenance script for creating VIEW (#165)

This commit is contained in:
Universal Omega 2022-04-14 11:50:27 -06:00 committed by GitHub
parent 7ad00427e6
commit c4bff23bf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 8 deletions

View file

@ -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/"

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\DynamicPageList3;
use DatabaseUpdater;
use MediaWiki\Extension\DynamicPageList3\Maintenance\CreateTemplate;
use MediaWiki\Extension\DynamicPageList3\Maintenance\CreateView;
use Parser;
use PPFrame;
@ -626,12 +627,6 @@ class Hooks {
*/
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 );
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace MediaWiki\Extension\DynamicPageList3\Maintenance;
use LoggedUpdateMaintenance;
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class CreateView extends LoggedUpdateMaintenance {
public function __construct() {
parent::__construct();
$this->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;