Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recreate option to CreateView #299

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
36 changes: 30 additions & 6 deletions maintenance/createView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MediaWiki\Extension\DynamicPageList3\Maintenance;

use Exception;
use LoggedUpdateMaintenance;

$IP = getenv( 'MW_INSTALL_PATH' );
Expand All @@ -15,16 +16,16 @@ class CreateView extends LoggedUpdateMaintenance {

public function __construct() {
parent::__construct();

$this->addDescription( 'Handle creating DPL3\'s dpl_clview VIEW.' );
$this->addOption( 'recreate', 'Drop and recreate the view if it already exists', false, false );
}

/**
* Get the unique update key for this logged update.
*
* @return string
*/
protected function getUpdateKey() {
protected function getUpdateKey(): string {
return 'dynamic-page-list-3-create-view';
}

Expand All @@ -33,7 +34,7 @@ protected function getUpdateKey() {
*
* @return string
*/
protected function updateSkippedMessage() {
protected function updateSkippedMessage(): string {
return 'VIEW already created.';
}

Expand All @@ -42,12 +43,35 @@ protected function updateSkippedMessage() {
*
* @return bool
*/
protected function doDBUpdates() {
protected function doDBUpdates(): bool {
$dbw = $this->getDB( DB_PRIMARY );
if ( !$dbw->tableExists( 'dpl_clview' ) ) {
$recreate = $this->hasOption( 'recreate' );

if ( $recreate || !$dbw->tableExists( 'dpl_clview' ) ) {
// Drop the view if --recreate option is set
if ( $recreate ) {
try {
$dbw->query( "DROP VIEW IF EXISTS {$dbw->tablePrefix()}dpl_clview" );
$this->output( "Dropped existing view dpl_clview.\n" );
} catch ( Exception $e ) {
$this->output( "Failed to drop existing view: " . $e->getMessage() . "\n" );
return false;
}
}

// 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;" );

// Create the view
try {
$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;" );
$this->output( "Created view dpl_clview.\n" );
} catch ( Exception $e ) {
$this->output( "Failed to create view: " . $e->getMessage() . "\n" );
return false;
}
} else {
$this->output( "VIEW already exists. Use --recreate to drop and recreate it.\n" );
}

return true;
Expand Down
Loading