#9843 closed enhancement (fixed)
Duplicate autosave/revisions clutter the database
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | low | Milestone: | 3.6 |
| Component: | Revisions | Version: | 2.8 |
| Severity: | minor | Keywords: | has-patch |
| Cc: | AmbushCommander, scribu@…, erick@…, adamsilverstein@…, kovshenin |
Description
We shouldn't be storing revisions and autosaves that are identical to the original post.
It generates a bug, to start with. If you open a post, and you leave it alone until the autosave fires, and then close the window, you end up with a notice -- a newer version of the post exists.
Then, it clutters the list of actual revisions with junk that cannot be removed without a plugin or by using SQL.
Attachments (2)
Change History (22)
comment:1
Denis-de-Bernardy
— 4 years ago
- Keywords needs-patch added
- Milestone changed from 2.8 to Future Release
- Priority changed from normal to low
- Type changed from defect (bug) to enhancement
comment:2
Denis-de-Bernardy
— 4 years ago
I've this function on the save_post hook, in case there is any interest for a patch writer:
function save_post_revision($rev_id) {
if ( wp_is_post_autosave($rev_id) ) {
return;
} elseif ( $post_id = wp_is_post_revision($rev_id) ) {
# do nothing
} else {
$post_id = $rev_id;
}
global $wpdb;
$post = get_post($rev_id);
# drop dup revs
$kill_ids = $wpdb->get_col("
SELECT ID
FROM $wpdb->posts
WHERE post_type = 'revision'
AND ID <> " . intval($rev_id) . "
AND post_parent = " . intval($post_id) . "
AND post_content = '" . $wpdb->escape($post->post_content) . "'
");
if ( $kill_ids ) {
foreach ( $kill_ids as $kill_id ) {
wp_delete_post_revision($kill_id);
}
}
# stop here for real posts
if ( $post_id == $rev_id )
return;
# drop other potential dup revs
$kill_ids = $wpdb->get_col("
SELECT p2.ID
FROM $wpdb->posts as p2
JOIN $wpdb->posts as p1
ON p1.post_parent = p2.post_parent
AND p1.post_type = p2.post_type
WHERE p1.post_type = 'revision'
AND p1.post_parent = " . intval($post_id) . "
AND p1.post_content = p2.post_content
AND p1.ID > p2.ID
");
if ( $kill_ids ) {
foreach ( $kill_ids as $kill_id ) {
wp_delete_post_revision($kill_id);
}
}
# drop near-empty revs
$kill_ids = $wpdb->get_col("
SELECT ID
FROM $wpdb->posts
WHERE post_type = 'revision'
AND post_parent = " . intval($post_id) . "
AND LENGTH(post_content) <= 50
");
if ( $kill_ids ) {
foreach ( $kill_ids as $kill_id ) {
wp_delete_post_revision($kill_id);
}
}
# drop adjascent revs
$kill_ids = $wpdb->get_col("
SELECT p2.ID
FROM $wpdb->posts as p2
JOIN $wpdb->posts as p1
ON p1.post_parent = p2.post_parent
AND p1.post_type = p2.post_type
WHERE p1.post_type = 'revision'
AND p1.post_parent = " . intval($post_id) . "
AND DATEDIFF(p1.post_date, p2.post_date) < 1
AND p1.post_date >= p2.post_date
AND p1.ID <> p2.ID
");
if ( $kill_ids ) {
foreach ( $kill_ids as $kill_id ) {
wp_delete_post_revision($kill_id);
}
}
# drop near-identical revs
$kill_ids = $wpdb->get_col("
SELECT p2.ID
FROM $wpdb->posts as p2
JOIN $wpdb->posts as p1
ON p1.post_parent = p2.post_parent
AND p1.post_type = p2.post_type
WHERE p1.post_type = 'revision'
AND p1.post_parent = " . intval($post_id) . "
AND DATEDIFF(p1.post_date, p2.post_date) <= 7
AND ABS( LENGTH(p1.post_content) - LENGTH(p2.post_content) ) <= 50
AND p1.post_date >= p2.post_date
AND p1.ID <> p2.ID
");
if ( $kill_ids ) {
foreach ( $kill_ids as $kill_id ) {
wp_delete_post_revision($kill_id);
}
}
} # save_post_revision()
comment:3
caesarsgrunt
— 4 years ago
Duplicate of #7392.
comment:4
Denis-de-Bernardy
— 4 years ago
similar, but not 100% dup. #7392 only covers autosaves. here, it's more like: don't create a revision if only a post's tags have been edited.
comment:5
Denis-de-Bernardy
— 4 years ago
see #11324
comment:6
AmbushCommander
— 3 years ago
- Cc AmbushCommander added
comment:8
solarissmoke
— 2 years ago
I've posted a patch on #7392 which I think should resolve both of these.
comment:9
westi
— 5 months ago
- Keywords revisions-3.6 added
Adding to the list of things to consider for the 3.6 work on Revisions.
comment:10
westi
— 5 months ago
- Keywords revisions-3.6 removed
- Milestone changed from Future Release to 3.6
This need careful consideration and we will take a look for 3.6.
comment:11
adamsilverstein
— 5 months ago
i think solving #7392 will solve this issue; the initial autosave firing is what creates the extra revision record mentioned by the author in this post. by the way, if you create a post, but go immediately to publish button from the title field without entering any content the extra revision is not created.
comment:12
adamsilverstein
— 5 months ago
- Keywords has-patch added; needs-patch removed
verified, #7392 patch fixes this.
comment:13
ethitter
— 5 months ago
- Cc erick@… added
comment:14
adamsilverstein
— 5 months ago
- Cc adamsilverstein@… added
comment:15
westi
— 4 months ago
In 1211/tests:
comment:16
westi
— 4 months ago
- Owner set to westi
- Resolution set to fixed
- Status changed from new to closed
In 23414:
comment:17
westi
— 4 months ago
In 1214/tests:
comment:18
westi
— 4 months ago
In 23415:
comment:19
soulseekah
— 4 months ago
Breaks post/revisions/test_revision_restore_caps_before_publish, since content hasn't changed a revision was not created, expected 2 revisions after second "edit", getting one.
Test was written for #16847 http://core.trac.wordpress.org/changeset/1213/tests and should probably change content the second time to create a second revision, or expect 1 revision.
soulseekah
— 4 months ago
comment:20
kovshenin
— 4 months ago
- Cc kovshenin added
Mm, actually, the autosave issue described above seems fixed in trunk. But we're still cluttering the database with useless revisions. It's minor, too, so punting pending patch.