Skip to content

Commit

Permalink
Merge pull request #618 from takeit/garbage
Browse files Browse the repository at this point in the history
[CS-5238] - Implement a configurable autodelete mechanism for pending us...
  • Loading branch information
Paweł Mikołajczuk committed Apr 8, 2014
2 parents 24568d6 + a3dba29 commit d64b7c0
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 45 deletions.
36 changes: 13 additions & 23 deletions newscoop/admin-files/articles/editor_load_tinymce.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ function CampsiteSubhead(ed) {

<?php
$translator = \Zend_Registry::get('container')->getService('translator');
if ($p_user->hasPermission('EditorSubhead') && $p_objectType == 'article') { ?>
?>
setup : function(ed) {
var text = '';
var wordcount = false;
<?php if ($p_user->hasPermission('EditorSubhead') && $p_objectType == 'article') { ?>
ed.onInit.add(function(){
ed.controlManager.setDisabled('pasteword', true);
if (tinymce.isIE) {
Expand All @@ -327,35 +327,25 @@ function CampsiteSubhead(ed) {
});
ed.onNodeChange.add(function(){ed.controlManager.setDisabled('pasteword', true);});

ed.onKeyUp.add(function(ed, l) {
var idx = ed.id.lastIndexOf('_');
var buttonId = ed.id.substr(0, idx);
var row = tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row');

if (32 == l.keyCode || 13 == l.keyCode || 46 == l.keyCode || 33 == l.keyCode || 63 == l.keyCode ) {
if (!wordcount) {
tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, '<?php echo $translator->trans("Characters", array(), 'articles'); ?>: ' + '<span id="' + tinyMCE.activeEditor.id + '-wordcount">0</span>');
wordcount = true;
}
text = ed.getContent().replace(/(< ([^>]+)<)/g,"").replace(/\s+/g," ");
text = $.trim(text);
if (row) {
tinymce.DOM.setHTML(tinyMCE.activeEditor.id + '-wordcount', text.length);
}
}

});

ed.addButton('campsite-subhead', {
title : '<?php echo $translator->trans("Newscoop Subhead", array(), 'articles'); ?>',
image : website_url + '/js/tinymce/themes/advanced/img/campsite_subhead.gif',
onclick : function() {
CampsiteSubhead(ed);
}
});
},
<?php } ?>
<?php } ?>

ed.onKeyUp.add(function(ed, l) {
var row = tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row');
if (!wordcount) {
tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, '<?php echo $translator->trans("Characters", array(), 'articles'); ?>: ' + '<span id="' + tinyMCE.activeEditor.id + '-wordcount">0</span>');
wordcount = true;
}
var strip = (tinyMCE.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,"");
tinymce.DOM.setHTML(tinyMCE.activeEditor.id + '-wordcount', strip.length);
});
},
});
});

Expand Down
3 changes: 3 additions & 0 deletions newscoop/application/configs/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ services:
newscoop.cache:
class: Newscoop\Services\CacheService
arguments: ["@system_preferences_service"]
user.garbage:
class: Newscoop\Services\GarbageCollectionService
arguments: ["@em"]

# Add this event listener registering to newscoop/application/configs/parameters/custom_parameters.yml
# search_indexer:
Expand Down
1 change: 1 addition & 0 deletions newscoop/application/console
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ $cli->addCommands(array(
new \Newscoop\Tools\Console\Command\InstallNewscoopCommand(),
new \Newscoop\Tools\Console\Command\CreateOAuthClientCommand(),
new \Newscoop\Tools\Console\Command\GenerateORMSchemaCommand(),
new \Newscoop\Tools\Console\Command\UserGarbageCollectionCommand(),
new \Newscoop\Tools\Console\Command\AutopublishCommand(),
new \Newscoop\Tools\Console\Command\ClearOldStatisticsCommand(),
new \Newscoop\Tools\Console\Command\IndexerCommand(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
30 0 * * * {{ bin_directory }}/../application/console user:garbage
2 changes: 1 addition & 1 deletion newscoop/library/Newscoop/Entity/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Attachment

/**
* @ORM\OneToOne(targetEntity="Newscoop\Entity\Translation")
* @ORM\JoinColumn(name="fk_description_id", referencedColumnName="Id", nullable=true, onDelete="SET NULL")
* @ORM\JoinColumn(name="fk_description_id", referencedColumnName="phrase_id", nullable=true, onDelete="SET NULL")
* @var Newscoop\Entity\Translation
*/
protected $description;
Expand Down
21 changes: 2 additions & 19 deletions newscoop/library/Newscoop/Entity/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ class Translation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="Id", type="integer")
* @ORM\Column(name="phrase_id", type="integer")
* @var int
*/
protected $id;
protected $phrase_id;

/**
* @ORM\ManyToOne(targetEntity="\Newscoop\Entity\Language")
Expand All @@ -41,12 +40,6 @@ class Translation
*/
protected $translationText;

/**
* @ORM\Column(name="phrase_id", type="integer")
* @var int
*/
protected $phrase_id;

/**
* @param int $phraseId Current phrase id for translations from autoid table.
*/
Expand All @@ -55,16 +48,6 @@ public function __construct($phraseId)
$this->setPhraseId($phraseId);
}

/**
* Gets the value of id.
*
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* Gets the value of language.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function installAssets()
public function saveCronjobs()
{
$binDirectory = realpath($this->newscoopDir.'/bin');
$appDirectory = realpath($this->newscoopDir.'/application/console');

$crontab = new Crontab();

Expand Down Expand Up @@ -135,6 +136,11 @@ public function saveCronjobs()
$job->setMinute('0')->setHour('5')->setDayOfMonth('*')->setMonth('*')->setDayOfWeek('*')
->setCommand($binDirectory.'/newscoop-autopublish');
$crontab->addJob($job);

$job = new Job();
$job->setMinute('30')->setHour('0')->setDayOfMonth('*')->setMonth('*')->setDayOfWeek('*')
->setCommand($appDirectory.' user:garbage');
$crontab->addJob($job);
$crontab->write();

return true;
Expand Down
78 changes: 78 additions & 0 deletions newscoop/library/Newscoop/Services/GarbageCollectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @package Newscoop
* @copyright 2012 Sourcefabric o.p.s.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\Services;

use Doctrine\ORM\EntityManager;
use Newscoop\Entity\User;

/**
* Deletes inactive users after defined days
*/
class GarbageCollectionService
{
/**
* @var Doctrine\ORM\EntityManager
*/
protected $em;

/**
* @param Doctrine\ORM\EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}

/**
* Run users garbage collection
*
* @param string $days
*
* @return void
*/
public function run($days)
{
$this->gcUsers($days);
$this->gcTokens();
}

/**
* Remove obsolete users
*
* @param string $days
*
* @return void
*/
private function gcUsers($days)
{
$query = $this->em->createQueryBuilder()
->delete('Newscoop\Entity\User', 'u')
->where('u.created < :ttl')
->andWhere('u.status = :status')
->getQuery();

$query->setParameter('ttl', new \DateTime('-'.$days.' days'));
$query->setParameter('status', User::STATUS_INACTIVE);
$query->execute();
}

/**
* Remove obsolete tokens
*
* @return void
*/
private function gcTokens()
{
$query = $this->em->createQueryBuilder()
->delete('Newscoop\Entity\UserToken', 't')
->where('t.user NOT IN (SELECT u.id FROM Newscoop\Entity\User u)')
->getQuery();

$query->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @package Newscoop
* @author Rafał Muszyński <[email protected]>
* @copyright 2014 Sourcefabric o.p.s.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\Tools\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Removes obsolete pending users data
*/
class UserGarbageCollectionCommand extends ContainerAwareCommand
{
/**
*/
protected function configure()
{
$this
->setName('user:garbage')
->setDescription('Users Garbage Collection')
->setHelp("Removes obsolete pending users data")
->addOption('force', null, InputOption::VALUE_NONE, 'If set, foreces command execution omitting system preferences settings.');
}

/**
*/
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{
$systemPreferences = $this->getContainer()->getService('system_preferences_service');
$text = '<info>Obsolete pending users successfuly removed.</info>';

try {
if ($input->getOption('force')) {
$this->getContainer()->getService('user.garbage')->run($systemPreferences->get('userGarbageDays'));
if ($input->getOption('verbose')) {
$output->writeln($text);
}
} elseif ($systemPreferences->get('userGarbageActive') === 'Y' && !is_null($systemPreferences->get('userGarbageActive'))) {
$this->getContainer()->getService('user.garbage')->run($systemPreferences->get('userGarbageDays'));
if ($input->getOption('verbose')) {
$output->writeln($text);
}
}
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public function indexAction(Request $request)
'recaptchaPublicKey' => $preferencesService->RecaptchaPublicKey,
'recaptchaPrivateKey' => $preferencesService->RecaptchaPrivateKey,
'recaptchaSecure' => $preferencesService->RecaptchaSecure,
'userGarbageActive' => $preferencesService->userGarbageActive ?: 'N',
'userGarbageDays' => $preferencesService->userGarbageDays ?: 90,
)
, array(
'cacheService' => $this->container->get('newscoop.cache')
Expand Down Expand Up @@ -237,6 +239,9 @@ public function indexAction(Request $request)
$this->imageResizing($data['image_ratio'], $data['image_width'], $data['image_height'], $data['zoom']);
// template filter
$this->templateFilter($data['template_filter']);
// users garbage settings
$preferencesService->set('userGarbageActive', $data['userGarbageActive']);
$preferencesService->set('userGarbageDays', $data['userGarbageDays']);
}
// General Settings
$this->generalSettings($data['siteonline'], $data['title'], $data['meta_keywords'], $data['meta_description'], $data['timezone'], $data['cache_image'], $data['allow_recovery'], $data['email_from'],
Expand Down
14 changes: 14 additions & 0 deletions newscoop/src/Newscoop/NewscoopBundle/Form/Type/PreferencesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'multiple' => false,
'expanded' => true,
'required' => true,
))
->add('userGarbageActive', 'choice', array(
'choices' => array(
'Y' => 'newscoop.preferences.label.yesoption',
'N' => 'newscoop.preferences.label.nooption'
),
'error_bubbling' => true,
'multiple' => false,
'expanded' => true,
'required' => true,
))
->add('userGarbageDays', 'integer', array(
'attr' => array('max' => 999, 'min' => 1),
'required' => true
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ newscoop:
yesoption: "Yes"
nooption: "No"
geosearch: "Use Local GeoNames Search:"
usergarbage: "Remove obsolete pending users data?"
usergarbagedaysinfo: "Define a certain number of days after which the pending users will be deleted."
usergarbagedays: "Days number"
popover:
cacheengine: "Enables or disables system cache cache, which can improve Newscoop performance."
cacheenginehost: "Provide cache host (for Memcache, Memcached or Redis)"
Expand All @@ -124,4 +127,5 @@ newscoop:
recaptcha:
publickey: "Use this in the JavaScript code that is served to your users."
privatekey: "Use this when communicating between your server and reCAPTCHA server. Be sure to keep it a secret."
secure: "If you use secure url for reCAPTCHA choose Yes option."
secure: "If you use secure url for reCAPTCHA choose Yes option."
garbage: "If enabled, this allows the publisher to define a certain number of days after which the pending users are being deleted. By default 90 days number is set."
Loading

0 comments on commit d64b7c0

Please sign in to comment.