Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Detailed documentation can be found here : https://www.open-csp.org/DevOps:Doc/P

#### Development

* 2.7.0 rebuild-files-clean. Same as rebuild-files, but will now also physically unused files from the server
* 2.6.10 fix for maintenance script when installing a shared file. Changed pages were counted as unchanged
* 2.6.9 fix for maintenance script rename and removed specific PHP 8.3 usage
* 2.6.8 Added fail catcher for broken pages. Added continue-on-error argument for maintenance script
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PageSync",
"version": "2.6.10",
"version": "2.7.0",
"author": [
"Sen-Sai"
],
Expand Down
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
"wsps-maintenance-analyze-server-wiki-error-slot-missing": "Slot $1 is missing on server.",
"wsps-maintenance-analyze-server-wiki-error-slot-unsynced": "Slot $1 content in Wiki and on Server are not identical",
"wsps-maintenance-analyze-server-wiki-slot": " Slot : ",
"wsps-maintenance-clean-header": "Delete files from server not in index",
"apihelp-wsps-description": "PageSync API module",
"apihelp-wsps-summary": "PageSync API module",
"apihelp-wsps-param-what": "Action to take; valid values are 'add', 'remove', 'gettags' and 'updatetags'",
Expand Down
24 changes: 20 additions & 4 deletions maintenance/WspsMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function __construct() {
'rebuild-files',
'Will take the index file and re-create all files from the database'
);
$this->addOption(
'rebuild-files-clean',
'Same as rebuild-files, but will also physically remove all files not in the PageSync from the server'
);
$this->addOption(
'rebuild-index',
'Will recreate the index file from existing file structure'
Expand All @@ -67,7 +71,7 @@ public function __construct() {
);
$this->addOption(
'force-rebuild-files',
'Used with rebuild-files. This forces rebuild-files without prompting for user interaction'
'Used with rebuild-files and rebuild-files-clean. This forces rebuild-files without prompting for user interaction'
);

$this->addOption(
Expand Down Expand Up @@ -259,6 +263,11 @@ public function execute() {
$silent = true;
}

if ( $this->hasOption( 'special' ) ) {
$special = true;
$silent = true;
}

$skipDifferentUser = false;
if ( $this->hasOption( 'skip-if-page-is-changed-in-wiki' ) ) {
$skipDifferentUser = true;
Expand Down Expand Up @@ -315,15 +324,18 @@ public function execute() {
}
return;
}

if ( $this->hasOption( 'rebuild-files' ) ) {
if ( $this->hasOption( 'rebuild-files' ) || $this->hasOption( 'rebuild-files-clean' ) ) {
// We need to rebuild the index file here.
$continueOnError = $this->hasOption( 'continue-on-error' );
if ( $continueOnError ) {
$errorList = [];
}
if ( $this->hasOption( 'force-rebuild-files' ) === false ) {
echo "\n[Rebuilding files from index]\n";
if ( $this->hasOption( 'rebuild-files-clean' ) ) {
echo "\n[Rebuilding files from index and remove unused files from server. This cannot be undone!]\n";
} else {
echo "\n[Rebuilding files from index]\n";
}
$answer = strtolower( readline( "Are you sure (y/n)" ) );
if ( $answer !== "y" ) {
die( "no action\n\n" );
Expand Down Expand Up @@ -388,6 +400,10 @@ public function execute() {
echo implode( "\n", $errorList ) . "\n";
}
}
if ( $this->hasOption( 'rebuild-files-clean' ) ) {
$cleaner = new \PageSync\Core\PSClean();
$cleaner->cleanServerFiles();
}
die();
}

Expand Down
178 changes: 178 additions & 0 deletions src/Core/PSClean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace PageSync\Core;

use PageSync\Helpers\Colors;

class PSClean {

/**
* @var array
*/
private array $indexList;

/**
* @var array
*/
private array $serverList;

/**
* @var string
*/
private string $exportPath;

public function __construct() {
$serverFullList = PSCore::getFilesFromServer( true );

foreach ( $serverFullList as $file ) {
$pathInfo = pathinfo( $file );
$fileName = $pathInfo['filename'];
if ( $pathInfo['extension'] === 'info' || $pathInfo['extension'] === 'data' ) {
$this->serverList[] = $fileName;
continue;
}
$explodedFileName = explode( '_', $fileName );
$cnt = count( $explodedFileName );
if ( $cnt > 2 ) {
unset( $explodedFileName[ $cnt - 1 ] );
unset( $explodedFileName[ $cnt - 2 ] );
$this->serverList[] = implode( '_', $explodedFileName );
}
}
$this->exportPath = PSConfig::$config['exportPath'];
$indexList = PSCore::getFileIndex();
if ( $indexList !== false ) {
$this->indexList = $indexList;
}
}

/**
* @return void
*/
public function cleanServerFiles(): void {
echo Colors::cEcho(
wfMessage( "wsps-maintenance-clean-header" )->plain(),
"blue+bold",
true,
"",
wfMessage( "wsps-maintenance-analyze-start" )->plain()
);
if ( empty( $this->serverList ) ) {
echo Colors::cEcho(
'No files on server',
"yellow+bold",
true
);
echo Colors::cEcho(
wfMessage( "wsps-maintenance-clean-header" )->plain(),
"blue+bold",
true,
"",
wfMessage( "wsps-maintenance-analyze-end" )->plain()
);

return;
}
$this->serverList = array_unique( $this->serverList );
$cntNotDeleted = 0;
foreach ( $this->serverList as $infoFile ) {
if ( !array_key_exists( $infoFile, $this->indexList ) ) {
$this->deleteInfoFile( $infoFile );
$this->deleteSlotFiles( $infoFile );
$this->deleteDatFiles( $infoFile );
} else {
$cntNotDeleted++;
}
}

echo Colors::cEcho(
wfMessage( "wsps-maintenance-clean-header" )->plain(),
"blue+bold",
true,
"",
wfMessage( "wsps-maintenance-analyze-end" )->plain()
);
}

/**
* @param string $file
* @param string $function
*
* @return void
*/
private function echoDeleted( string $file, string $function ): void {
echo Colors::cEcho(
$file,
"yellow",
false,
$function,
"deleted"
);
}

/**
* @param string $file
* @param string $function
*
* @return void
*/
private function echoNotDeleted( string $file, string $function ): void {
echo Colors::cEcho(
$file,
"red",
false,
'Could not be deleted',
$function
);
}

/**
* @param string $file
*
* @return void
*/
private function deleteInfoFile( string $file ): void {
$infoFile = $this->exportPath . $file . '.info';
if ( file_exists( $infoFile ) ) {
if ( unlink( $infoFile ) ) {
$this->echoDeleted( $infoFile, __function__ );
} else {
$this->echoNotDeleted( $infoFile, __function__ );
}
}
}

/**
* @param string $file
*
* @return void
*/
private function deleteSlotFiles( string $file ): void {
$slotFiles = $this->exportPath . $file . '_slot*.wiki';
$filesList = glob( $slotFiles );
foreach ( $filesList as $singleFile ) {
if ( unlink( $singleFile ) ) {
$this->echoDeleted( $singleFile, __function__ );
} else {
$this->echoNotDeleted( $singleFile, __function__ );
}
}
}

/**
* @param string $file
*
* @return void
*/
private function deleteDatFiles( string $file ): void {
$dataFiles = $this->exportPath . $file . '.data';
if ( file_exists( $dataFiles ) ) {
if ( unlink( $dataFiles ) ) {
$this->echoDeleted( $dataFiles, __function__ );
} else {
$this->echoNotDeleted( $dataFiles, __function__ );

}
}
}
}
7 changes: 6 additions & 1 deletion src/Core/PSCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ public static function cleanFileName( string $fname ) : string {
}

/**
* @param bool $allFiles
*
* @return array
*/
public static function getFilesFromServer(): array {
public static function getFilesFromServer( bool $allFiles = false ): array {
if ( empty( PSConfig::$config ) ) {
self::setConfig();
}
$path = PSConfig::$config['exportPath'];
if ( $allFiles ) {
return glob( $path . '*.*' );
}
return glob( $path . "*.info" );
}

Expand Down