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 findSQLPatches and findPossibleUpgradeScripts scripts #434

Closed
wants to merge 5 commits into from
Closed
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
117 changes: 117 additions & 0 deletions maintenance/findPossibleUpgradeScripts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* List new or updated maintenance scripts between two MediaWiki versions.
*
* Usage:
* php findPossibleUpgradeScripts.php --from-version=current_version --to-version=new_version
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
* @author Universal Omega
* @author Paladox
* @version 1.0
*/

require_once __DIR__ . '/../../../maintenance/Maintenance.php';

class FindPossibleUpgradeScripts extends Maintenance {
public function __construct() {
parent::__construct();

$this->addDescription( 'List new or updated maintenance scripts between two MediaWiki versions.' );

$this->addOption( 'from-version-path', 'Path to MediaWiki version to start from', true, true );
$this->addOption( 'to-version-path', 'Path to MediaWiki version to end with', true, true );
}

public function execute() {
$fromVersionPath = $this->getOption( 'from-version-path' );
$toVersionPath = $this->getOption( 'to-version-path' );
$scripts = [];
$this->findScripts( $fromVersionPath, $toVersionPath, $scripts );
foreach ( $scripts as $script ) {
$this->output( $script . $this->findExtendedClass( $script ) . "\n" );
}

$this->output( "\nCount: " . count( $scripts ) . "\n" );
}

private function findScripts( $fromVersion, $toVersion, &$scripts ) {
$fromScripts = $this->findMaintenanceScripts( $fromVersionPath );
$toScripts = $this->findMaintenanceScripts( $toVersionPath );
foreach ( $toScripts as $script ) {
$filename = basename( $script );
if ( array_key_exists( $filename, $fromScripts ) ) {
if ( $this->isScriptUpdated( $fromScripts[$filename], $script ) ) {
$scripts[] = $script;
}
} else {
$scripts[] = $script;
}
}
}

private function findMaintenanceScripts( $path ) {
$scripts = [];

$files1 = $this->findFilesRecursively( $path . '/**/**/maintenance/*.php' );
$files2 = $this->findFilesRecursively( $path . '/**/**/**/maintenance/*.php' );
$coreFiles = $this->findFilesRecursively( $path . '/maintenance/*.php' );

foreach ( array_merge( $files1, $files2, $coreFiles ) as $file ) {
if ( str_contains( $file, '/tests/' ) ) {
continue;
}

$scripts[basename( $file )] = $file;
}

return $scripts;
}

private function isScriptUpdated( $oldScript, $newScript ) {
// TO-DO
}

private function findFilesRecursively( $pattern ) {
$files = glob( $pattern );
foreach ( glob( dirname( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
$files = array_merge(
[],
...[ $files, $this->findFilesRecursively( $dir . '/' . basename( $pattern ) ) ]
);
}

return $files;
}

private function findExtendedClass( $script ) {
$file = file_get_contents( $script );
if ( preg_match( '/extends\s+([^\s]+)\s+/', $file, $matches ) ) {
if ( $matches[1] !== Maintenance::class ) {
return ' (' . $matches[1] . ')';
}
}

return '';
}
}

$maintClass = FindPossibleUpgradeScripts::class;
require_once RUN_MAINTENANCE_IF_MAIN;
108 changes: 108 additions & 0 deletions maintenance/findSQLPatches.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* List new or updated SQL patches between two MediaWiki versions.
*
* Usage:
* php findSQLPatches.php --from-version=current_version --to-version=new_version
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
* @author Universal Omega
* @author Paladox
* @version 1.0
*/

require_once __DIR__ . '/../../../maintenance/Maintenance.php';

class FindSQLPatches extends Maintenance {
public function __construct() {
parent::__construct();

$this->addDescription( 'List new or updated SQL patches between two MediaWiki versions.' );

$this->addOption( 'from-version', 'Path for MediaWiki version to start from', true, true );
$this->addOption( 'to-version', 'Path for MediaWiki version to end with', true, true );
}

public function execute() {
$fromVersionPath = $this->getOption( 'from-version-path' );
$toVersionPath = $this->getOption( 'to-version-path' );
$patches = [];
$this->findPatches( $fromVersionPath, $toVersionPath, $patches );
foreach ( $patches as $patch ) {
$this->output( $patch . "\n" );
}

$this->output( "\nCount: " . count( $patches ) . "\n" );
}

private function findPatches( $fromVersionPath, $toVersionpath, &$patches ) {
$fromPatches = $this->findSqlPatches( $fromVersionPath );
$toPatches = $this->findSqlPatches( $toVersionpath );
foreach ( $toPatches as $patch ) {
$filename = basename( $patch );
if ( array_key_exists( $filename, $fromPatches ) ) {
if ( $this->isPatchUpdated( $fromPatches[$filename], $patch ) ) {
$patches[] = $patch;
}
} else {
$patches[] = $patch;
}
}
}

private function findSqlPatches( $path ) {
$patches = [];

$files = $this->findFilesRecursively( $path );

foreach ( $files as $file ) {
if (
str_contains( $file, '/postgres/' ) ||
str_contains( $file, '/sqlite/' ) ||
str_contains( $file, '/tests/' )
) {
continue;
}

$patches[basename( $file )] = $file;
}

return $patches;
}

private function isPatchUpdated( $oldPatch, $newPatch ) {
// TO-DO
}

private function findFilesRecursively( $pattern ) {
$files = glob( $pattern );
foreach ( glob( dirname( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
$files = array_merge(
[],
...[ $files, $this->findFilesRecursively( $dir . '/' . basename( $pattern ) ) ]
);
}

return $files;
}
}

$maintClass = FindSQLPatches::class;
require_once RUN_MAINTENANCE_IF_MAIN;