This can be fixed either by reinstalling Drupal or by copying the table from an ideally fresh installation of Drupal into your broken instance.
Or you can try the following workaround (only do it when your Drupal instance is already broken):
Backup the old registry table first (just in case):
drush sqlq "CREATE TABLE registry_bak LIKE registry; INSERT INTO registry_bak SELECT * FROM registry;"
drush sqlq "CREATE TABLE system_bak LIKE system; INSERT INTO system_bak SELECT * FROM system;"
Clear the bootstrap cache and registry table.
drush sqlq "TRUNCATE cache_bootstrap; TRUNCATE registry"
Insert the basic data into registry table:
drush sqlq 'INSERT INTO registry (name, type, filename) VALUES ("SelectQueryExtender", "class", "includes/database/select.inc"), ("DrupalDefaultEntityController", "class", "includes/entity.inc");'
drush eval "registry_update();"
Clear the caches:
drush -y cc all
If clear caches in 4. failed, because of some other missing classes, you can either:
a) Add the missing classes manually, in example:
$ drush -y cc all
Fatal error: Class 'Entity' not found in profile2.module on line 593
$ grep -Rwl "^class Entity" .
./sites/all/modules/entity/includes/entity.inc
$ drush sqlq 'INSERT INTO registry (name, type, filename) VALUES ("Entity", "class", "sites/all/modules/entity/includes/entity.inc");'
$ drush -y cc all # testing...
# If Fatal error:, repeat 5a. again.
or:
b) Try to disable these failing contrib modules (e.g. profile2, rules) by:
drush sqlq 'UPDATE system SET status = 0 WHERE name = "failing_module"'
and repeat the steps starting from 4.
If something is more broken than it was, you can restore your tables to the original point (which was done in 1. step) by:
drush sqlq "TRUNCATE registry; INSERT INTO registry SELECT * FROM registry_bak;"
drush sqlq "TRUNCATE system; INSERT INTO system SELECT * FROM system_bak;"
and try again.
See also: How to move installed modules from /sites/all/modules/* to /sites/all/contrib/modules/*
Rebuilding registry
table can be also achieved by the following script ran in Drupal webroot:
grep -ERo "^(\s+)?(abstract )?class (\S+)" . | tr ':' ' ' | sed "s/abstract //g" | awk '{print "INSERT INTO registry (filename, type, name) VALUES (\x27"$1"\x27,\x27"$2"\x27,\x27"$3"\x27);"}' | $(drush sql-connect) -f
Thanks to Mike's help with regex.