Method for deleting discarded stories

Register an Account
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-04-2007, 06:40 PM
Pligg Donor
Pligg Version: 9.8.2 +
Pligg Template: yget
 
Join Date: Jul 2007
Posts: 123
So far, I haven't found a modification on here for completely deleting a story from the database. For example, even though the link may be removed, the votes, comments, etc. remain. Therefore, I've decided to create my own solution and thought I'd share it with you.

I'll warn you upfront that I'm not a developer and I don't know a thing about object oriented programming. Therefore, I'm sure there's a better way to do this other than my little hack. Still, the result is the same so I'm sticking with this until somebody comes up with something better.

Version: 1.2

STEP 1:

Open admin_links_center.tpl (filename corrected from initial post) and find "<a href="javascript:uncheck_all()">Uncheck All</a>". Add the following:

Code:
<br><a href="admin_links.php" onclick="javascript:window.open('admin_delete_stories.php', 'resultwindow', 'width=300, height=200')">Delete discarded stories</a>

STEP 2:

Create a file called "admin_delete_stories.php" and add the following:


Code:
<?php
include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'user.php');
include(mnminclude.'smartyvariables.php');
			
// require user to log in
force_authentication();

// restrict access to god only
$amIgod = 0;
$amIgod = $amIgod + checklevel('god');
$main_smarty->assign('amIgod', $amIgod);

$canIhaveAccess = 0;
$canIhaveAccess = $canIhaveAccess + checklevel('god');
$canIhaveAccess = $canIhaveAccess + checklevel('admin');

if($canIhaveAccess == 0){	
	$main_smarty->assign('tpl_center', $the_template . '/admin_templates/admin_access_denied');
	$main_smarty->display($the_template . '/pligg.tpl');		
	die();
}


function delete_storylink($linkid) {

    $query="SELECT * FROM " . table_links . " WHERE link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}
    else {$sql_array = mysql_fetch_object($result); }

    # delete the story link
    $query="DELETE FROM " . table_links . " WHERE link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}
    
    # delete the story comments
    $query="DELETE FROM " . table_comments . " WHERE comment_link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}
	
    # delete the story pageviews
    $query="DELETE FROM " . table_pageviews . " WHERE (pv_type = 'story' OR pv_type = 'out') AND pv_page_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}
	
    # delete the saved links
    $query="DELETE FROM " . table_saved_links . " WHERE saved_link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}	
	
    # delete the story tags
    $query="DELETE FROM " . table_tags . " WHERE tag_link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}	 		 

    # delete the story trackbacks
    $query="DELETE FROM " . table_trackbacks . " WHERE trackback_link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}    

    # delete the story votes
    $query="DELETE FROM " . table_votes . " WHERE vote_link_id = '$linkid'";
    if (! $result=mysql_query($query)) {die (mysql_error());}   
  
	}

	
$sql_query = "SELECT * FROM " . table_links . " WHERE link_status = 'discard'";
			
$result_storylinks = mysql_query($sql_query);
$num_rows = mysql_num_rows($result_storylinks);
		while($storylink = mysql_fetch_object($result_storylinks))
		{
			delete_storylink($storylink->link_id);
		}
	
# set discards total to zero
$query="UPDATE " . table_totals . " SET total = '0' WHERE name = 'discard'";
if (!mysql_query($query)) {die (mysql_error());}

	
echo $num_rows. " discarded stories deleted";

?>
<p><a href="admin_optimize_database.php">Click here</a> to optimize database</p>

STEP 3 (OPTIONAL):

This next file allows you to optimize your database after deleting your stories. This isn't necessary, but it's a good idea because the deleted stories will create overhead in your MySQL database.

Create a file called "admin_optimize_database.php" and add the following:


Code:
<?php
include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'user.php');
include(mnminclude.'smartyvariables.php');
			
// require user to log in
force_authentication();

// restrict access to god only
$amIgod = 0;
$amIgod = $amIgod + checklevel('god');
$main_smarty->assign('amIgod', $amIgod);

$canIhaveAccess = 0;
$canIhaveAccess = $canIhaveAccess + checklevel('god');
$canIhaveAccess = $canIhaveAccess + checklevel('admin');

if($canIhaveAccess == 0){	
	$main_smarty->assign('tpl_center', $the_template . '/admin_templates/admin_access_denied');
	$main_smarty->display($the_template . '/pligg.tpl');		
	die();
}

// $message = "";

    $query = "SHOW TABLE STATUS";
    $result=mysql_query($query);
    $table_list = "";
    while ($cur_table = mysql_fetch_object($result)) {
        $table_list .= $cur_table->Name.", ";
    }

    if (!empty($table_list)) {
        $table_list = substr($table_list, 0, -2);
        $query = "OPTIMIZE TABLE ".$table_list;
        mysql_query($query);
        $message = "Database optimized";
    }

echo $message

?>

Version changes:
Version 1.1 (12-31-2007): Made accessible to administrators only.
Version 1.2 (5-4-2008): Removed need for custom error page.

Last edited by fingerprn; 05-04-2008 at 02:38 PM. Reason: Removed need for custom error page.
Reply With Quote
  #2 (permalink)  
Old 11-04-2007, 08:01 PM
chuckroast's Avatar
Pligg Developer/Coder/Designer
Pligg Version: SVN
Pligg Template: Social Pro Kit
 
Join Date: Jun 2006
Posts: 5,448
Nice work, and thank you for sharing.
Reply With Quote
  #3 (permalink)  
Old 11-04-2007, 11:33 PM
New Pligger
Pligg Version: 9.8.2
Pligg Template: Default
 
Join Date: Oct 2007
Posts: 28
Thanks for this. I had just posted the other day asking if there was any reason partially submitted stories are logged to the database with a status of discard. This will also help take care of these. Thanks!
Reply With Quote
  #4 (permalink)  
Old 11-05-2007, 12:06 AM
crakez's Avatar
Constant Pligger/Designer
Pligg Version: 9.9.5
Pligg Template: Fresh Template
 
Join Date: Jul 2007
Location: Australia
Posts: 119
Send a message via MSN to crakez Send a message via Skype™ to crakez
Thanks a lot for sharing.
Reply With Quote
  #5 (permalink)  
Old 11-10-2007, 08:45 AM
New Pligger
 
Join Date: Sep 2007
Posts: 10
Wow.. Thanks.

I used to delete the data directly in phpmyadmin.

This works great.
Reply With Quote
  #6 (permalink)  
Old 11-10-2007, 01:20 PM
Pligg Donor
Pligg Version: 9.9.5
 
Join Date: Sep 2007
Posts: 192
In your STEP 1, are you actually referring to admin_links_center.tpl instead of admin_links_center.php? Because there isn't a admin_links_center.php file in my version (v. 9.8.2).
Reply With Quote
  #7 (permalink)  
Old 11-10-2007, 01:36 PM
Constant Pligger
 
Join Date: Jun 2006
Posts: 108
nice work

cool!

noyP
Reply With Quote
  #8 (permalink)  
Old 11-10-2007, 02:55 PM
Donor
 
Join Date: Jan 2007
Posts: 377
this method rocks, and you're good with database stuffs!
Reply With Quote
  #9 (permalink)  
Old 11-10-2007, 04:36 PM
Casual Pligger
 
Join Date: Jan 2007
Posts: 46
Thanks so much!
Reply With Quote
  #10 (permalink)  
Old 11-10-2007, 05:02 PM
nzbullet's Avatar
Constant Pligger/Coder
 
Join Date: Jun 2007
Location: New Zealand
Posts: 115
Quote:
Originally Posted by blaze View Post
In your STEP 1, are you actually referring to admin_links_center.tpl instead of admin_links_center.php? Because there isn't a admin_links_center.php file in my version (v. 9.8.2).
Yes - The file is actually admin_links_center.tpl - well at least it is in 9.8.2

fingerprn - Awesome mod. Nice and easy to add to site, following instructions you posted. Also easy to use.

One comment the file, incase anyone can't find it, is located as follows:

/pligg/templates/yget/admin_templates/admin_links_center.tpl

The new files should just be added to the root directory of your site along with the other "admin_.......php" files.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Url Method 2 works for everything except stories coreyvf Questions and Comments 2 10-11-2009 02:07 AM
Voting method 2, displaying stories in order of rating radaro Questions and Comments 4 02-03-2008 02:44 AM
v9.5: Comments to discarded stories tbones Questions and Comments 6 01-28-2008 07:49 AM
Clicking on a tag reveals stories that have been discarded kbeeveer46 Questions and Comments 0 08-07-2006 01:36 AM
User vote count shouldn't include discarded stories gragland Questions and Comments 0 07-26-2006 09:14 PM


Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development