HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/ojs/tools/cleanReviewerInterests.php
<?php

/**
 * @file tools/cleanReviewerInterests.php
 *
 * Copyright (c) 2014-2021 Simon Fraser University
 * Copyright (c) 2003-2021 John Willinsky
 * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
 *
 * @class ReviewerInterestsDeletionTool
 * @ingroup tools
 *
 * @brief CLI tool to remove user interests that are not referenced by any user accounts. 
 */

require(dirname(__FILE__) . '/bootstrap.inc.php');

class ReviewerInterestsDeletionTool extends CommandLineTool {

	/**
	 * Constructor.
	 * @param $argv array command-line arguments
	 */
	public function __construct($argv = array()) {
		parent::__construct($argv);

		if (!sizeof($this->argv)) {
			$this->usage();
			exit(1);
		}
		
		$this->parameters = $this->argv;
	}

	/**
	 * Print command usage information.
	 */
	function usage() {
		echo "Permanently removes user interests that are not referenced by any user accounts.  USE WITH CARE.\n"
			. "Usage:\n"
			. "\t{$this->scriptName} --show : Display user interests not referenced\n"
			. "\t{$this->scriptName} --remove  : Permanently delete user interests not referenced\n";
	}

	/**
	 * Remove user interests that are not referenced by any user account
	 */
	function execute() {

		$orphans = $this->_getOrphanVocabInterests();
		if (!count($orphans)) {
			echo "No user interests to remove.\n";
			exit(0);
		}
		
		$command = $this->parameters[0];
		switch($command) {
			case '--show':
				$interests = array_map(function($entry) { return $entry->getData(CONTROLLED_VOCAB_INTEREST); }, $orphans);
				echo "Below are the user interests that are not referenced by any user account.\n";
				echo "\t" . join($interests, "\n\t") . "\n";
				break;
				
			case '--remove':
				$vocabEntryDao = DAORegistry::getDAO('ControlledVocabEntryDAO');
				foreach ($orphans as $orphanVocab) {
					$vocabEntryDao->deleteObject($orphanVocab);
				}
				echo count($orphans) . " entries deleted\n";
				break;

			default:
				echo "Invalid command.\n";
				$this->usage();
				exit(2);
		}
	}
	
	/**
	 * Returns user interests that are not referenced
	 * @return array array of ControlledVocabEntry object
	 */
	protected function _getOrphanVocabInterests() {
		
		$interestDao = DAORegistry::getDAO('InterestDAO');
		$vocabDao = DAORegistry::getDAO('ControlledVocabDAO');
		$vocabEntryDao = DAORegistry::getDAO('ControlledVocabEntryDAO');
		
		$interestVocab = $vocabDao->getBySymbolic(CONTROLLED_VOCAB_INTEREST);
		$vocabEntryIterator = $vocabEntryDao->getByControlledVocabId($interestVocab->getId());
		$vocabEntryList = $vocabEntryIterator->toArray(); 
		
		// list of vocab interests in db
		$allInterestVocabIds = array_map(
			function($entry) {
				return $entry->getId();
			}, 
			$vocabEntryList
		);
		
		// list of vocabs associated to users
		$interests = $interestDao->getAllInterests();
		$userInterestVocabIds = array_map(
			function($interest) {
				return $interest->getId();
			},
			$interests->toArray()
		);
		
		// get the difference
		$diff = array_diff($allInterestVocabIds, $userInterestVocabIds);
		
		$orphans = array_filter(
			$vocabEntryList, 
			function($entry) use($diff) {
				return in_array($entry->getId(), $diff);
			}
		);
		
		return $orphans;
	}
}

$tool = new ReviewerInterestsDeletionTool(isset($argv) ? $argv : array());
$tool->execute();