Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I found myself in a situation where I needed to rebuild the votingapi_cache table (it got deleted). So, here's a script which will do it and some clean ups to README.txt while we're at it.
Comment | File | Size | Author |
---|---|---|---|
#72 | votingapi-382866-71.patch | 8.11 KB | pifagor |
Comments
Comment #1
gregglesComment #2
moshe weitzman CreditAttribution: moshe weitzman commentedWe're using this script on Economist so I'll RTBC it.
I like this change in README: +VotingAPI requires Drupal.
Comment #3
stewsnoozesubscribe
Comment #4
Junro CreditAttribution: Junro commentedsubscribe :)
Comment #5
Scott Reynolds CreditAttribution: Scott Reynolds commentedSo orginal patch works, but this attached patch is a "I think this is a better approach, no offense" patch. This patch makes the operation Batch API'd and adds a form button to the votingapi settings page. Click the button and rebuild your cache.
I have tested it with a combination of about 100,000 votes on a mixture of nodes, comments, and users
Comment #6
gregglesI think the warning that it could take a long time should be on the main form in addition to the confirmation form. People generally ignore text on confirm forms.
Other than that, shell scripts are easier to write and less likely to be run by accident. I'm not sure this needs to be truly "hard to run by accident" so I see this as an improvement. Didn't test or do a thorough review of the code, though..
Comment #7
Scott Reynolds CreditAttribution: Scott Reynolds commentedAlso think that i probably should do a batch_set() for each content type. Removes a lot of the bookkeeping in there.
No harm in adding the warning message again.
Comment #8
Scott Reynolds CreditAttribution: Scott Reynolds commentedattached patch does the two afore mentioned things.
1.) adds the message to the button
2.) uses an operation per content_type in the batch api call
Interestingly,
didn't work even though 'markup' is listed to respect #attributes: http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...
@eaton ^^ ?
Comment #9
Junro CreditAttribution: Junro commentedHi,
just tried the last patch #8 but He doesn't work for me.
I have in /admin/settings/votingapi/rebuild the button "Rebuild all Voting Results" but when I click on it, the page reload without effects. I don't have anything that confirms any changes.
Anyway Voting results is not rebuilt.
And what about 2.) uses an operation per content_type in the batch api call ?
I can't choose node type.
Comment #10
Scott Reynolds CreditAttribution: Scott Reynolds commentedpretty strange. When you run this query:
SELECT DISTINCT(content_type) FROM votingapi_vote
does it return anything? Is your votingapi_vote table empty?Comment #11
Junro CreditAttribution: Junro commentedMy content type is "fiche"
When I run this query: SELECT DISTINCT(fiche) FROM votingapi_vote, it return me:
Erreur
requête SQL:
SELECT DISTINCT (
fiche
)
FROM votingapi_vote
LIMIT 0 , 30
MySQL a répondu:
#1054 - Unknown column 'fiche' in 'field list'
I'm not a big expert in SQL
Comment #12
Scott Reynolds CreditAttribution: Scott Reynolds commentedright, you need to run this query exactly
SELECT DISTINCT(content_type) FROM votingapi_vote
Not
SELECT DISTINCT(fiche) FROM votingapi_vote
Comment #13
Junro CreditAttribution: Junro commentedoh ok, sorry
here the results:
Affichage des enregistrements 0 - 29 (1 306 total, Traitement en 0.0003 sec.)
SELECT DISTINCT (
content_type
)
FROM votingapi_vote
LIMIT 0 , 30
Comment #14
Scott Reynolds CreditAttribution: Scott Reynolds commentedguess its not working still ok.
Comment #15
Junro CreditAttribution: Junro commentedNothing new since the last post?
I'm still here to test some patches :) Really needs to rebuild the votingapi cache ^^
Comment #16
Scott Reynolds CreditAttribution: Scott Reynolds commentedo duh! you have to clear your menu cache. try existing patch, and visit admin/build/modules first before hitting that button
/me kicks himself forgetting that
Comment #17
Junro CreditAttribution: Junro commentedPerfect! This patch is great! I don't know why it didn't work the first time I apllied it...
Comment #18
Scott Reynolds CreditAttribution: Scott Reynolds commentedsweet good to here, marking as reviewed and tested. Im using in other places as well.
Comment #19
Junro CreditAttribution: Junro commentedHum that's wird, it didn't work for one vote (maybe more? I haven't see another one yet).
I cleared view cache, performance cache...
What do you call "menu cache"?
you say "and visit admin/build/modules first before hitting that button", I visit admin/build/modules, to do what? Maybe I haven"t carry out all spots before hitting thaht button! lol
Comment #20
Scott Reynolds CreditAttribution: Scott Reynolds commentedok...
The menu callbacks and urls are cached away to the database. This patch adds a new url, so that cache needs to be rebuilt
By going to admin/build/modules, that cache is rebuilt. You don't have todo anything.
But I use devel module to clear the caches (http://drupal.org/project/devel)
Comment #21
Junro CreditAttribution: Junro commentedHum ok, I don't really understand why this vote is still here if the votingapi cache has been rebuilt successfully. Is this vote was hidding somewhere? lol
I've got votes from 3 users to erase before launch my website, it was votes for tests.
I will report you if I have some news. :)
ps: I don't use Devel module.
Comment #22
thekayra CreditAttribution: thekayra commentedThe patch at #8 worked quite well for me at first glance. Will report if I come accross something strange.
Comment #23
hefox CreditAttribution: hefox commentedHm, it worked, but it did produce the whole double-menu-rebuild warnings and I wasn't clearing cache elsewhere, so I think it's being a bit too much cache clearing though I don't see where normal drupal cache was being cleared atm.
Comment #24
Scott Reynolds CreditAttribution: Scott Reynolds commentedI have sucessfully used this all the time. The major need for it was developing a voting function for a social news site. I have not run into any of the problems mentioned above. It doesn't do anything to menu_rebuild. And it doesn't miscalculate.
Comment #25
Junro CreditAttribution: Junro commentedHello,
Any news about this patch? Any modification?
Every time I rebuilt the cache api, only 3008 pieces of content is recalculated.
My votingapi_cache table contains 8 861 pieces.
So I think the recalculaion is not complete.
And some votes are not erased on my site.
Any ideas? :)
Comment #26
Scott Reynolds CreditAttribution: Scott Reynolds commented3008 nodes were recalculated and resulted in 8,861 different cache results. This is expected.
This has nothing to do with vote erasing....
Essentially, when you calculate vote totals on a node, there are several different totals that are calculated. The standard ones are 'average', 'sum' and 'count'.
Do you have different calculations? Are some points and others percent ? Points have different ones.
Again, I use this almost daily at this point, as I import my database without the votingapi cache table and then rebuild it.
Comment #27
Junro CreditAttribution: Junro commentedOh ok, thanks
Do you have an idea why some votes are still in the voting api cache table? Or some tricks to do about it?
Comment #28
Junro CreditAttribution: Junro commentedI don't use percent but points with 'average', 'sum' and 'count'.
Can I erase things directly in the table (phpmyadmin) without problems? The Voting api cache will be rebuild automatically?
Comment #29
kruser CreditAttribution: kruser commentedsubscribing - I did a manual table conversion from Wordpress WP-PostRatings now I just need to rebuild the votingapi cache to get the imported data to show up.
edit: running the #1 php script got it working for me. The patch didn't seem to do anything.
Comment #30
Scott Reynolds CreditAttribution: Scott Reynolds commentedPlease explain. did you apply the patch, then clear you cache (it adds a new menu item). Did you go to the Votingapi settings and click the big button ?
Did the patch not apply?
Comment #31
kruser CreditAttribution: kruser commentedOkay, I see it now. I just had to clear cache and the new menu link came up for admin/settings/votingapi/rebuild - so the patch does work!
Comment #32
Junro CreditAttribution: Junro commentedHello,
I still have this problem:
My rebuild ajax bar stop à 10% and directly go at 100%
Maybe it's why I don't have all votingapi cache rebuild.
Someone could tell me if he has the same behaviour? or the ajax bar go from 0% to 100%, step by step (with 65%, 66%, 67%...)?
Comment #33
Scott Reynolds CreditAttribution: Scott Reynolds commented3008 x 3 ~= 8 861
the 3 would be 'count', 'sum', 'average'. As mentioned this is expected.
What does that mean ? Votes erased? This doesn't erase votes, it recalculates the totals from all the votes cast by users.
Comment #34
Junro CreditAttribution: Junro commentedAnd some votes are not erased on my site.
I mean some votes are still in the votingapi cache table. They still appear in views.
I clear the cache so many times. (Site cache, view cache, run cron...) No way, still have them in votingapi cache.
the 3 would be 'count', 'sum', 'average'. As mentioned this is expected.
So, you're agree there is only one of those that is recalculated. Is is expected, why? All three should be recalculated, doesn't it?
In my views, I'm using "count" and "average". Not "sum" and not the value of a single vote.
And there is a problem here too: I've got 3800 pieces of content recalculated, but I have 7000 votes on my website. All these votes should be recalculated?
Comment #35
Scott Reynolds CreditAttribution: Scott Reynolds commentedvotingapi cache isn't cleared by "clearing the cache". Its a badly name table because its not really a cache table.
Huh? I don't understand. Every piece of content that has a vote in votingapi_cache table has their three vote results recalculated (count, sum, average). It doesn't delete anything.
There is no way for VotingAPI to know this.
This patch goes through every content that has received at least one vote and updates the sum, average and count values in the votingapi_cache table.
Hopefully that helps you understand. And maybe there is a bug in there.
Comment #36
Junro CreditAttribution: Junro commentedOk, all is cleared for me now.
So the bug is:
Not all nodes are recalculated. Only 50% are recalculated.
Rebuild the voting api cache should affect all votes, isn't it?
Comment #37
Scott Reynolds CreditAttribution: Scott Reynolds commentedSo your saying that there are pieces of content in the system that have at least one vote that are not recalculated ?
It doesn't go "Give me all nodes and recalculate" it does "Give me all nodes that received at least on vote and recalculate".
Comment #38
Junro CreditAttribution: Junro commentedSo your saying that there are pieces of content in the system that have at least one vote that are not recalculated ?
Exactly, no doubt about that.
It doesn't go "Give me all nodes and recalculate" it does "Give me all nodes that received at least on vote and recalculate".
Yes, sure.
If you need some proof, details, screenshots or anything that could be helpfull to understant this problem, let me know :)
Comment #39
Scott Reynolds CreditAttribution: Scott Reynolds commentedneeds to be
Comment #40
Junro CreditAttribution: Junro commentedThe process stopped at 10% before.
Now, it continues until 51% and nothing, I had wait 15 minutes but still nothing, still at 51%.
When I want to go to another page, i have:
When I reload the page:
Finished with an error.
on admin/settings/votingapi page.
Comment #41
DamienMcKennaThis would be *great* as a Drush command :)
Comment #42
Junro CreditAttribution: Junro commentedIt sill doesn't work well for me. The provessus stoppes at 29% now. More I have votes, more the processus stop early (normal).
After x votes recalculated, the processus stoppes.
Comment #43
DamienMcKenna@Scott Reynolds: the correct query should be MAX(), not COUNT(), you're trying to find the highest content_id value, not the number of values.
In my case I had ~1,400 votes across two different "content_types" (one was 'node', another the name of an actual content type) and it worked fine.
Am changing this to RTBC.
Comment #44
DamienMcKennaChanging back to Needs Work after Junro's comment.
Comment #45
jwilson3If votes are attached to cck fields via, for example, fivestar module that stores the votes in the content_type_X tables for voting on other nodes... then this script will not find those votes, and thus the cache may be rebuilt without these tallies.
This could explain why some users (@Junro perhaps?) are not seeing all their votes rebuilt after running this script. This may be related to whats also being described in #1027146: Aggregate functions in views.
Comment #46
jwilson3To summarize...
The remaining issues that need work include the following:
Have I missed anything? (reserve the right to come back and update this comment ;)
Comment #47
incaic CreditAttribution: incaic commentedsubscribing
Comment #48
Junro CreditAttribution: Junro commented@ jwilson3
All my votes (even those with fivestars module) are stored in the votingapi table.
Comment #49
Junro CreditAttribution: Junro commented10736 pieces of content recalculated
First time I have the recalculation completed!! :)
I don't know why I didn't have it before...
Comment #50
InTheLyonsDen CreditAttribution: InTheLyonsDen commentedHas anyone ported this to D7? *banging head on table*
Comment #51
mmilo CreditAttribution: mmilo commentedIndeed, would be great to have this ported to D7. An update:
There *is* a Drush command, but to rebuild the entire voting cache for a node type, we need #1399800: Need to Import 9k Votes - Votingapi_cache not being rebuilt committed.
Things that could be worked on...
Note sure if this is entirely necessary. The code already has
drupal_alter('votingapi_results', $cache, $entity_type, $entity_id);
. Is that enough?Comment #52
mmilo CreditAttribution: mmilo commentedAttached is a patch for D7 based on #8.
Comment #53
torotil CreditAttribution: torotil commentedHi,
thanks for your patches and sorry for the long silence.
I've reviewed the patch:
There is some whitespace errors in this patch - please fix them.
Using the entity_id for navigating through the introduces some flaws:
Comment #54
zhuber CreditAttribution: zhuber commentedI use this module on various sites, and really want this to be ported into the module. It seems to work well for me.
Therefore, I have updated the patch to remove the excess whitespace in hopes that it will get included in the dev release.
I didn't update it to use voting_cache_id, since that would require another table join. We can still do this, but for now I am submitting the cleaned-up version of #52.
Comment #55
DamienMcKenna@zhuber: That's a good direction, but the patch needs some updates to match the Drupal coding standards. Also, you might want to rename the new rebuild page to "Rebuild voting results", and votingapi_rebuild_form() needs a comment.
Comment #56
zhuber CreditAttribution: zhuber commentedIs this better?
I cleaned a lot of things up, but I'm not sure if I missed anything.
Comment #57
zhuber CreditAttribution: zhuber commentedSorry for the spam, let me try that again. The last one was missing the updated menu and page titles.
Comment #58
DamienMcKenna@zhuber: This is really close. A few small nitpicks:
Have you tried the patch on D6? I might try a reroll for one of the sites I maintain.
Comment #59
zhuber CreditAttribution: zhuber commentedHere is an updated version. Thanks for all the direction.
I haven't tried anything with the D6 patches, since I am using fivestar and votingapi heavily on a few D7 sites.
Comment #60
GoofyX CreditAttribution: GoofyX commentedI have applied the patch (by hand, since it threw out some errors) in the latest 2.11 version for the 7.x branch. How am I supposed to invoke the rebuild functionality? I tried to access the link (
admin/config/search/votingapi/rebuild
) but it shows the general options page. What am I doing wrong?Comment #61
zhuber CreditAttribution: zhuber commented@GoofyX:
Do you have the 'administer voting api' permisson on your user account?
You should have the 'Rebuild Settings' tab in addition to the 'General Settings' tab. Either you do not have the correct permissions or the patch was not implemented correctly.
Comment #62
torotil CreditAttribution: torotil commentedThanks to all for putting so much work into this!
This still works only as long as there aren't two entities with the same entity_id but different entity_types. This could be fixed by also sorting (and filtering) by entity_type. I still prefer a voting_cache_id-based solution though. You don't need an extra join for that either: Just use the votingapi_cache table instead.
Also there is a possible issue with setting setting current_entity_id before the result for the entity is saved. What if votingapi_recalculate_results() somehow fails?
Comment #63
torotil CreditAttribution: torotil commentedComment #64
emilymoi CreditAttribution: emilymoi commentedI also couldn't get [#59] to apply--Malformed patch. I ended up applying by hand and recreated the patch so others won't have to do the same.
I've done a bit of testing and it seems like it's doing the job so far. Not sure why votingapi has been without this functionality for so long.
Comment #65
roderikThe initial idea for this patch was to rebuild the votes when the votingapi_cache table was deleted - so we can't do that. I think we need to keep selecting entity_id/entity_type combinations from the votingapi_vote table. (And these are not unique, so we need to use DISTINCT here.)
It's possible... it just doesn't work well with dividing the process up into batches of 100 records. You need to account for the possibility that records 100 & 101 have the same entity ID. I created code to check that, which unfortunately makes things harder to follow...
Also (and I see this was also mentioned in #45 / #46): votes from comments are not re-counted. So I attached help text to say that.
Comment #66
roderikremoving cruft from previous patch...
Comment #67
torotil CreditAttribution: torotil commentedThere is another reason: There might be multiple vote_cache_ids for one combination of entity_type-entity_id-tag since the function may vary. This makes vote_cache_ids less handy than they looked like. The empty table could have been solved easily by pre-populating the table.
What about using
ORDER BY entity_id, type
together withentity_id>:current_entity_id OR (entity_id=:current_entity_id AND entity_type>:current_entity_type)
? That would make each entity_id/entity_type combination unique.The calculation of the progress-bar is still a bit off. Isn't there a better solution for that?
Comment #68
roderikGood idea, I think you're right, would make the first 'if' wrapper inside the 'foreach' unnecessary, and 'current_entity_types' array can be simplified to a 'current_entity_type' scalar.
Sadly it looks like I don't have time to look at / test this code more.
Not one I am aware of. See code comments.
Comment #69
roderikIncorporated comments. I also added some code (min_entity_id) to make the progress bar seems less random. Still not perfect but should be better.
Comment #70
pifagorComment #71
pifagorComment #72
pifagorComment #73
pifagorComment #76
pifagor