Delete pages after x time

Permalink 3 users found helpful
So, I'm trying to do something with classified ads right now. Since there's been no real update on the classified ads block (can't see why this hasn't been added even though the post was made over a year ago that it was in progress)... I need to work something out myself.

The forums system would work, although I do not just wanted thousands upon thousands of old listings. Sure, Craigslist does, but even that has pages expire after a certain time.

So this is what I am trying to do: after a set time length the pages get deleted. I don't mind a cron job for this, but I can't find where the page creation date would be. The DB has the posts listed, and it would be easy to do a set delete by parent ID, but that would delete all of the posts in that category. How could I check both parent ID and time created to delete old posts?

Looking for something like once a day running a job to delete the old posts.

(By the way - where is the actual data for each page stored?)

 
Zudane replied on at Permalink Reply
Okay, trying to set this up as a job, but having no luck with it - I know there are problems, but the help here isn't too helpful on it.

Can someone take a look at this code and tell me what all is wrong with it?

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class DeleteOldAds extends Job {
   public function getJobName() {
      return t('Delete Old Ads');
   }
   public function getJobDescription() {
      return t("Deletes all old ads from Classified ads section");
   }
   function run() {
      $expiry = date('Y-m-d', strtotime('-2 days'));
      Loader::model('page_list');
         $pl = new PageList();
         $pl->filterByPath('/discuss', $includeAllChildren = true);
         $pl->filterByCollectionTypeID(6);


Just to clarify on it - the path is "mysite.com/discuss", and the typeID is 6 (this is the discussion post type for me).

I do know that the expiry variable is correct, but I am not sure about the rest of the function.
jordanlev replied on at Permalink Reply
jordanlev
Try changing this line:
$pl->filterByDateAdded($expiry, $comparison = '<=');

to this:
$pl->filterByDateAdded($expiry, '<=');


Also try changing the format of your dates -- perhaps it needs to be a different format somehow?

(And you're probably already aware of this, but just in case... whatever you do, don't test this out on your live site because you could wind up losing tons of pages you didn't intend to!)
Zudane replied on at Permalink Reply
I am trying it on a live site - although it's still in production and I've been backing up the database and files prior to testing.

Although I really have no idea how to test it beyond running the job, but when I try the job just runs and runs and runs, even though something like making the site map only takes 5 seconds.

I've tested it the way you suggested - it actually was the first way I tried, but after checking the documentation for it I ended up changing it to what I have above.
jordanlev replied on at Permalink Reply
jordanlev
If the job just runs and runs it's probably because there's an error in your php code. It's going to be *really* difficult to test this by running jobs -- you need to run it somehow so that any errors are outputted to the screen. So I suggest learning how to create a single_page, and creating one of those in the dashboard and trying to run your code from there -- that way, you will see an error message for any problems with the code. Once you've got it figured out and working, THEN move it over to a job like you have now (and remember to delete the dashboard page!)
Zudane replied on at Permalink Reply
I had thought of that and attempted, although with a single page it is not loading the page_list handler as it should.

The way C5 runs is a bit confusing to me to be honest, I have done php before but not like this.
Zudane replied on at Permalink Reply
Welcoming any thoughts on this though, especially a good way to test it. I'm thinking my only real way is to make a new template for a page list - or find out how to get the singlepage to work with the handler for a page_list, just getting an error that it is an undefined class.
jordanlev replied on at Permalink Best Answer Reply 1 Attachment
jordanlev
I'm attaching a zip file containing a single_page and a controller. Unzip it, put the files in their corresponding locations in your site (in your site's top-level "controllers" and "single_pages" directories, NOT in "concrete/controllers" or "concrete/single_pages").

Go to your dashboard, click "Pages and Themes", then click the "Single Pages" tab up top. Scroll to the bottom of the list and enter this into the "add new single page" textbox:

dashboard/system/testdelete

Now click on "System & Maintenance" in the left sidebar, and click the "testdelete" up top.

Click the "Test Delete" button, and it should show you a list of pages that it thinks it should delete. I have commented out the actual deletion code in this so it can just be used for testing.

First you want to make sure the pages that you want deleted are actually coming up. If they're not, then there's a problem with your query parameters (although they look good to me -- but I dunno, maybe you have the wrong page type id or path or something?).

Once you have that working, undelete the deletion code line, and run it again. Now check and see if the pages actually got deleted (NOTE that you may want to increase your expiry time to farther back in the past for these tests so that if the deletion actually works, you can still have some more pages to delete as part of your next step of tests).

Okay, so once you know that code works, move it into your job. If it doesn't work there, then you have a problem with the job-running code. That I can't help you with, but you can try putting in some other code into a test job (like something that creates a file with a timestamp on it maybe) so you can make sure the actual job code itself is working. Then once you know that's working, put this deletion code back in there.

Hopefully that will do it!

-Jordan
Zudane replied on at Permalink Reply
That worked great, and I was able to get the pages to show and delete properly, so I know the code is correct!

Making a few tweaks I got the job to run and delete the pages, unfortunately the job still did not end when I put in an exit command... removed that and it works! This should probably be posted somewhere as it could be a very useful job:

<?php 
defined('C5_EXECUTE') or die("Access Denied.");
class DeleteOldPosts extends Job {
   public function getJobName() {
      return t('Delete Old Posts');
   }
   public function getJobDescription() {
      return t("Deletes all old posts from specified directory");
   }
   public function run() {
      $expiry = date('Y-m-d', strtotime('-7 days')); // change '-7 days' to the length back you wish deleted, use days, weeks, months or years
      Loader::model('page_list');
         //creates page list      
         $pl = new PageList();
         //filters down to '/directory' change to what you need, set true/false for including children pages (true to include, false to not include)


And a very big thank you to Jordan for helping me with this!
MrGrowBizz replied on at Permalink Reply
MrGrowBizz
Jordan, Will this method work on deleting old page versions. I have been trying to find a solution in doing so. I have read another post where you indicated it may. I don't care if I remove all old versions, but do not want to end up with any orphan blocks where I have read could occur. Any input on this would be greatly appreciated as have been searching and spending time on for a while with no solution found. Thanks!
jordanlev replied on at Permalink Reply
jordanlev
Unfortunately I have no idea. I honestly don't even remember posting this :)
I think what I did was take code someone else wrote and put it into a single_page for them, but I didn't actually use the functionality.

I'd suggest testing it out yourself on a test site, and see if it does what you want it to.
ddkooij replied on at Permalink Reply
Is it maybe possible to apply this script to delete previous versions of pages? I would find this very helpful. Code in this format is already helping me out anyway. Thank you.
Zudane replied on at Permalink Reply
Sadly no, I've tried to work it out like that. But page version cannot be searched the same way, it can only find whether a page is approved or not - and if you try to remove unapproved page you remove all versions of that page.
niakool replied on at Permalink Reply
For delete not approved version you can replace this loop :
foreach ($pages as $page) {
  $page->delete();
}



by

foreach ($pages as $page) {
  $vl = new VersionList($page);
  $vlArray = $vl->getVersionListArray();  
  foreach ($vlArray as $v) {
    if (!$v->isApproved()) {      
      $v->delete();
    }
  }
}