Last active
December 10, 2015 06:18
-
-
Save mcrider/4393650 to your computer and use it in GitHub Desktop.
Remove a list of spam words from the user interest database in OJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** This version works on 2.2.x versions of OJS. | |
* The ways interests are stored has changed a lot so tweaks will need to be made depending on the version of OJS | |
The following method needs to be added to lib/pkp/classes/user/InterestDAO.inc.php | |
function getInterestObjects($userId) { | |
$interests = $this->build($userId); | |
$interestEntryDao =& DAORegistry::getDAO('InterestEntryDAO'); | |
$userInterests = $interestEntryDao->getByControlledVocabId($interests->getId()); | |
return $userInterests; | |
} | |
*/ | |
/** | |
* @file deleteInterests.php | |
* | |
* Copyright (c) 2003-2012 John Willinsky | |
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING. | |
* | |
* @class deleteInterests | |
* @ingroup tools | |
* | |
* @brief CLI tool for removing (spammy) reviewing interests | |
*/ | |
// $Id$ | |
require(dirname(__FILE__) . '/bootstrap.inc.php'); | |
class deleteInterests extends CommandLineTool { | |
/** @var $inputFile string */ | |
var $inputFile; | |
/** | |
* Constructor. | |
* @param $argv array command-line arguments | |
*/ | |
function deleteInterests($argv = array()) { | |
parent::CommandLineTool($argv); | |
if (!isset($this->argv[0])) { | |
$this->usage(); | |
exit(1); | |
} | |
$this->inputFile = $this->argv[0]; | |
} | |
/** | |
* Print command usage information. | |
*/ | |
function usage() { | |
echo "OJS 2 interest removal tool\n" | |
. "Use this tool to remove spammy user interest.\n\n" | |
. "Usage: {$this->scriptName} [filename] \n" | |
. "username1 The file with spam words to delete (comma separated).\n"; | |
} | |
/** | |
* Execute the merge users command. | |
*/ | |
function execute() { | |
//load file into array | |
ini_set('auto_detect_line_endings', true); | |
$fileContents = file_get_contents($this->inputFile, true); | |
$badInterests = array_map('trim', explode(',', $fileContents)); | |
$interestDao =& DAORegistry::getDAO('InterestDAO'); /* @var $interestDao InterestDAO */ | |
$interestEntryDao =& DAORegistry::getDAO('InterestEntryDAO'); /* @var $interestDao InterestDAO */ | |
$userDao =& DAORegistry::getDAO('UserDAO'); | |
$allUsers =& $userDao->getUsersByField(); | |
$counter = 0; | |
while($user =& $allUsers->next()) { | |
$userInterests = $interestDao->getInterestObjects($user->getId()); | |
while($interest =& $userInterests->next()) { | |
if(strpos($interest->getInterest(), 'forums') !== false || strpos($interest->getInterest(), 'href') !== false || in_array($interest->getInterest(), $badInterests)) { | |
$interestEntryDao->deleteObject($interest); | |
printf("'%s' found and deleted.\n", $interest->getInterest()); | |
$counter++; | |
} | |
unset($interest); | |
} | |
unset($user); | |
unset($userInterests); | |
} | |
// Iterate over all interests and check if they're in our spam list | |
printf("Process completed: %s items removed.\n", $counter); | |
} | |
} | |
$tool = new deleteInterests(isset($argv) ? $argv : array()); | |
$tool->execute(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment