user.install 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the user module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function user_schema() {
  10. $schema['authmap'] = array(
  11. 'description' => 'Stores distributed authentication mapping.',
  12. 'fields' => array(
  13. 'aid' => array(
  14. 'description' => 'Primary Key: Unique authmap ID.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'uid' => array(
  20. 'type' => 'int',
  21. 'not null' => TRUE,
  22. 'default' => 0,
  23. 'description' => "User's {users}.uid.",
  24. ),
  25. 'authname' => array(
  26. 'type' => 'varchar',
  27. 'length' => 128,
  28. 'not null' => TRUE,
  29. 'default' => '',
  30. 'description' => 'Unique authentication name.',
  31. ),
  32. 'module' => array(
  33. 'type' => 'varchar',
  34. 'length' => 128,
  35. 'not null' => TRUE,
  36. 'default' => '',
  37. 'description' => 'Module which is controlling the authentication.',
  38. ),
  39. ),
  40. 'unique keys' => array(
  41. 'authname' => array('authname'),
  42. ),
  43. 'primary key' => array('aid'),
  44. 'foreign keys' => array(
  45. 'user' => array(
  46. 'table' => 'users',
  47. 'columns' => array('uid' => 'uid'),
  48. ),
  49. ),
  50. 'indexes' => array(
  51. 'uid_module' => array('uid', 'module'),
  52. ),
  53. );
  54. $schema['role_permission'] = array(
  55. 'description' => 'Stores the permissions assigned to user roles.',
  56. 'fields' => array(
  57. 'rid' => array(
  58. 'type' => 'int',
  59. 'unsigned' => TRUE,
  60. 'not null' => TRUE,
  61. 'description' => 'Foreign Key: {role}.rid.',
  62. ),
  63. 'permission' => array(
  64. 'type' => 'varchar',
  65. 'length' => 128,
  66. 'not null' => TRUE,
  67. 'default' => '',
  68. 'description' => 'A single permission granted to the role identified by rid.',
  69. ),
  70. 'module' => array(
  71. 'type' => 'varchar',
  72. 'length' => 255,
  73. 'not null' => TRUE,
  74. 'default' => '',
  75. 'description' => "The module declaring the permission.",
  76. ),
  77. ),
  78. 'primary key' => array('rid', 'permission'),
  79. 'indexes' => array(
  80. 'permission' => array('permission'),
  81. ),
  82. 'foreign keys' => array(
  83. 'role' => array(
  84. 'table' => 'role',
  85. 'columns' => array('rid' => 'rid'),
  86. ),
  87. ),
  88. );
  89. $schema['role'] = array(
  90. 'description' => 'Stores user roles.',
  91. 'fields' => array(
  92. 'rid' => array(
  93. 'type' => 'serial',
  94. 'unsigned' => TRUE,
  95. 'not null' => TRUE,
  96. 'description' => 'Primary Key: Unique role ID.',
  97. ),
  98. 'name' => array(
  99. 'type' => 'varchar',
  100. 'length' => 64,
  101. 'not null' => TRUE,
  102. 'default' => '',
  103. 'description' => 'Unique role name.',
  104. 'translatable' => TRUE,
  105. ),
  106. 'weight' => array(
  107. 'type' => 'int',
  108. 'not null' => TRUE,
  109. 'default' => 0,
  110. 'description' => 'The weight of this role in listings and the user interface.',
  111. ),
  112. ),
  113. 'unique keys' => array(
  114. 'name' => array('name'),
  115. ),
  116. 'primary key' => array('rid'),
  117. 'indexes' => array(
  118. 'name_weight' => array('name', 'weight'),
  119. ),
  120. );
  121. // The table name here is plural, despite Drupal table naming standards,
  122. // because "user" is a reserved word in many databases.
  123. $schema['users'] = array(
  124. 'description' => 'Stores user data.',
  125. 'fields' => array(
  126. 'uid' => array(
  127. 'type' => 'int',
  128. 'unsigned' => TRUE,
  129. 'not null' => TRUE,
  130. 'description' => 'Primary Key: Unique user ID.',
  131. 'default' => 0,
  132. ),
  133. 'name' => array(
  134. 'type' => 'varchar',
  135. 'length' => 60,
  136. 'not null' => TRUE,
  137. 'default' => '',
  138. 'description' => 'Unique user name.',
  139. ),
  140. 'pass' => array(
  141. 'type' => 'varchar',
  142. 'length' => 128,
  143. 'not null' => TRUE,
  144. 'default' => '',
  145. 'description' => "User's password (hashed).",
  146. ),
  147. 'mail' => array(
  148. 'type' => 'varchar',
  149. 'length' => 254,
  150. 'not null' => FALSE,
  151. 'default' => '',
  152. 'description' => "User's e-mail address.",
  153. ),
  154. 'theme' => array(
  155. 'type' => 'varchar',
  156. 'length' => 255,
  157. 'not null' => TRUE,
  158. 'default' => '',
  159. 'description' => "User's default theme.",
  160. ),
  161. 'signature' => array(
  162. 'type' => 'varchar',
  163. 'length' => 255,
  164. 'not null' => TRUE,
  165. 'default' => '',
  166. 'description' => "User's signature.",
  167. ),
  168. 'signature_format' => array(
  169. 'type' => 'varchar',
  170. 'length' => 255,
  171. 'not null' => FALSE,
  172. 'description' => 'The {filter_format}.format of the signature.',
  173. ),
  174. 'created' => array(
  175. 'type' => 'int',
  176. 'not null' => TRUE,
  177. 'default' => 0,
  178. 'description' => 'Timestamp for when user was created.',
  179. ),
  180. 'changed' => array(
  181. 'type' => 'int',
  182. 'not null' => TRUE,
  183. 'default' => 0,
  184. 'description' => 'Timestamp for when user was changed.',
  185. ),
  186. 'access' => array(
  187. 'type' => 'int',
  188. 'not null' => TRUE,
  189. 'default' => 0,
  190. 'description' => 'Timestamp for previous time user accessed the site.',
  191. ),
  192. 'login' => array(
  193. 'type' => 'int',
  194. 'not null' => TRUE,
  195. 'default' => 0,
  196. 'description' => "Timestamp for user's last login.",
  197. ),
  198. 'status' => array(
  199. 'type' => 'int',
  200. 'not null' => TRUE,
  201. 'default' => 0,
  202. 'size' => 'tiny',
  203. 'description' => 'Whether the user is active(1) or blocked(0).',
  204. ),
  205. 'timezone' => array(
  206. 'type' => 'varchar',
  207. 'length' => 32,
  208. 'not null' => FALSE,
  209. 'description' => "User's time zone.",
  210. ),
  211. 'language' => array(
  212. 'type' => 'varchar',
  213. 'length' => 12,
  214. 'not null' => TRUE,
  215. 'default' => '',
  216. 'description' => "User's default language.",
  217. ),
  218. 'picture' => array(
  219. 'type' => 'int',
  220. 'not null' => TRUE,
  221. 'default' => 0,
  222. 'description' => "Foreign key: {file_managed}.fid of user's picture.",
  223. ),
  224. 'init' => array(
  225. 'type' => 'varchar',
  226. 'length' => 254,
  227. 'not null' => FALSE,
  228. 'default' => '',
  229. 'description' => 'E-mail address used for initial account creation.',
  230. ),
  231. 'data' => array(
  232. 'type' => 'blob',
  233. 'not null' => FALSE,
  234. 'size' => 'big',
  235. 'serialize' => TRUE,
  236. 'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
  237. ),
  238. ),
  239. 'indexes' => array(
  240. 'access' => array('access'),
  241. 'created' => array('created'),
  242. 'changed' => array('changed'),
  243. 'mail' => array('mail'),
  244. 'picture' => array('picture'),
  245. ),
  246. 'unique keys' => array(
  247. 'name' => array('name'),
  248. ),
  249. 'primary key' => array('uid'),
  250. 'foreign keys' => array(
  251. 'signature_format' => array(
  252. 'table' => 'filter_format',
  253. 'columns' => array('signature_format' => 'format'),
  254. ),
  255. ),
  256. );
  257. $schema['users_roles'] = array(
  258. 'description' => 'Maps users to roles.',
  259. 'fields' => array(
  260. 'uid' => array(
  261. 'type' => 'int',
  262. 'unsigned' => TRUE,
  263. 'not null' => TRUE,
  264. 'default' => 0,
  265. 'description' => 'Primary Key: {users}.uid for user.',
  266. ),
  267. 'rid' => array(
  268. 'type' => 'int',
  269. 'unsigned' => TRUE,
  270. 'not null' => TRUE,
  271. 'default' => 0,
  272. 'description' => 'Primary Key: {role}.rid for role.',
  273. ),
  274. ),
  275. 'primary key' => array('uid', 'rid'),
  276. 'indexes' => array(
  277. 'rid' => array('rid'),
  278. ),
  279. 'foreign keys' => array(
  280. 'user' => array(
  281. 'table' => 'users',
  282. 'columns' => array('uid' => 'uid'),
  283. ),
  284. 'role' => array(
  285. 'table' => 'role',
  286. 'columns' => array('rid' => 'rid'),
  287. ),
  288. ),
  289. );
  290. return $schema;
  291. }
  292. /**
  293. * Implements hook_install().
  294. */
  295. function user_install() {
  296. // Insert a row for the anonymous user.
  297. db_insert('users')
  298. ->fields(array(
  299. 'uid' => 0,
  300. 'name' => '',
  301. 'mail' => '',
  302. ))
  303. ->execute();
  304. // We need some placeholders here as name and mail are uniques and data is
  305. // presumed to be a serialized array. This will be changed by the settings
  306. // form in the installer.
  307. db_insert('users')
  308. ->fields(array(
  309. 'uid' => 1,
  310. 'name' => 'placeholder-for-uid-1',
  311. 'mail' => 'placeholder-for-uid-1',
  312. 'created' => REQUEST_TIME,
  313. 'changed' => REQUEST_TIME,
  314. 'status' => 1,
  315. 'data' => NULL,
  316. ))
  317. ->execute();
  318. // Built-in roles.
  319. $rid_anonymous = db_insert('role')
  320. ->fields(array('name' => 'anonymous user', 'weight' => 0))
  321. ->execute();
  322. $rid_authenticated = db_insert('role')
  323. ->fields(array('name' => 'authenticated user', 'weight' => 1))
  324. ->execute();
  325. // Sanity check to ensure the anonymous and authenticated role IDs are the
  326. // same as the drupal defined constants. In certain situations, this will
  327. // not be true.
  328. if ($rid_anonymous != DRUPAL_ANONYMOUS_RID) {
  329. db_update('role')
  330. ->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
  331. ->condition('rid', $rid_anonymous)
  332. ->execute();
  333. }
  334. if ($rid_authenticated != DRUPAL_AUTHENTICATED_RID) {
  335. db_update('role')
  336. ->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
  337. ->condition('rid', $rid_authenticated)
  338. ->execute();
  339. }
  340. }
  341. /**
  342. * Implements hook_update_dependencies().
  343. */
  344. function user_update_dependencies() {
  345. // user_update_7006() updates data in the {role_permission} table, so it must
  346. // run after system_update_7007(), which populates that table.
  347. $dependencies['user'][7006] = array(
  348. 'system' => 7007,
  349. );
  350. // user_update_7010() needs to query the {filter_format} table to get a list
  351. // of existing text formats, so it must run after filter_update_7000(), which
  352. // creates that table.
  353. $dependencies['user'][7010] = array(
  354. 'filter' => 7000,
  355. );
  356. // user_update_7012() uses the file API and inserts records into the
  357. // {file_managed} table, so it therefore must run after system_update_7061(),
  358. // which inserts files with specific IDs into the table and therefore relies
  359. // on the table being empty (otherwise it would accidentally overwrite
  360. // existing records).
  361. $dependencies['user'][7012] = array(
  362. 'system' => 7061,
  363. );
  364. // user_update_7013() uses the file usage API, which relies on the
  365. // {file_usage} table, so it must run after system_update_7059(), which
  366. // creates that table.
  367. $dependencies['user'][7013] = array(
  368. 'system' => 7059,
  369. );
  370. return $dependencies;
  371. }
  372. /**
  373. * Utility function: grant a set of permissions to a role during update.
  374. *
  375. * This function is valid for a database schema version 7000.
  376. *
  377. * @param $rid
  378. * The role ID.
  379. * @param $permissions
  380. * An array of permissions names.
  381. * @param $module
  382. * The name of the module defining the permissions.
  383. * @ingroup update_api
  384. */
  385. function _update_7000_user_role_grant_permissions($rid, array $permissions, $module) {
  386. // Grant new permissions for the role.
  387. foreach ($permissions as $name) {
  388. db_merge('role_permission')
  389. ->key(array(
  390. 'rid' => $rid,
  391. 'permission' => $name,
  392. ))
  393. ->fields(array(
  394. 'module' => $module,
  395. ))
  396. ->execute();
  397. }
  398. }
  399. /**
  400. * @addtogroup updates-6.x-to-7.x
  401. * @{
  402. */
  403. /**
  404. * Increase the length of the password field to accommodate better hashes.
  405. *
  406. * Also re-hashes all current passwords to improve security. This may be a
  407. * lengthy process, and is performed batch-wise.
  408. */
  409. function user_update_7000(&$sandbox) {
  410. $sandbox['#finished'] = 0;
  411. // Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed.
  412. $hash_count_log2 = 11;
  413. // Multi-part update.
  414. if (!isset($sandbox['user_from'])) {
  415. db_change_field('users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
  416. $sandbox['user_from'] = 0;
  417. $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
  418. }
  419. else {
  420. require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  421. // Hash again all current hashed passwords.
  422. $has_rows = FALSE;
  423. // Update this many per page load.
  424. $count = 1000;
  425. $result = db_query_range("SELECT uid, pass FROM {users} WHERE uid > 0 ORDER BY uid", $sandbox['user_from'], $count);
  426. foreach ($result as $account) {
  427. $has_rows = TRUE;
  428. // If the $account->pass value is not a MD5 hash (a 32 character
  429. // hexadecimal string) then skip it.
  430. if (!preg_match('/^[0-9a-f]{32}$/', $account->pass)) {
  431. continue;
  432. }
  433. $new_hash = user_hash_password($account->pass, $hash_count_log2);
  434. if ($new_hash) {
  435. // Indicate an updated password.
  436. $new_hash = 'U' . $new_hash;
  437. db_update('users')
  438. ->fields(array('pass' => $new_hash))
  439. ->condition('uid', $account->uid)
  440. ->execute();
  441. }
  442. }
  443. $sandbox['#finished'] = $sandbox['user_from']/$sandbox['user_count'];
  444. $sandbox['user_from'] += $count;
  445. if (!$has_rows) {
  446. $sandbox['#finished'] = 1;
  447. return t('User passwords rehashed to improve security');
  448. }
  449. }
  450. }
  451. /**
  452. * Remove the 'threshold', 'mode' and 'sort' columns from the {users} table.
  453. *
  454. * These fields were previously used to store per-user comment settings.
  455. */
  456. function user_update_7001() {
  457. db_drop_field('users', 'threshold');
  458. db_drop_field('users', 'mode');
  459. db_drop_field('users', 'sort');
  460. }
  461. /**
  462. * Convert user time zones from time zone offsets to time zone names.
  463. */
  464. function user_update_7002(&$sandbox) {
  465. $sandbox['#finished'] = 0;
  466. // Multi-part update.
  467. if (!isset($sandbox['user_from'])) {
  468. db_change_field('users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE));
  469. $sandbox['user_from'] = 0;
  470. $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
  471. $sandbox['user_not_migrated'] = 0;
  472. }
  473. else {
  474. $timezones = system_time_zones();
  475. // Update this many per page load.
  476. $count = 10000;
  477. $contributed_date_module = db_field_exists('users', 'timezone_name');
  478. $contributed_event_module = db_field_exists('users', 'timezone_id');
  479. $results = db_query_range("SELECT uid FROM {users} ORDER BY uid", $sandbox['user_from'], $count);
  480. foreach ($results as $account) {
  481. $timezone = NULL;
  482. // If the contributed Date module has created a users.timezone_name
  483. // column, use this data to set each user's time zone.
  484. if ($contributed_date_module) {
  485. $date_timezone = db_query("SELECT timezone_name FROM {users} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField();
  486. if (isset($timezones[$date_timezone])) {
  487. $timezone = $date_timezone;
  488. }
  489. }
  490. // If the contributed Event module has stored user time zone information
  491. // use that information to update the user accounts.
  492. if (!$timezone && $contributed_event_module) {
  493. try {
  494. $event_timezone = db_query("SELECT t.name FROM {users} u LEFT JOIN {event_timezones} t ON u.timezone_id = t.timezone WHERE u.uid = :uid", array(':uid' => $account->uid))->fetchField();
  495. $event_timezone = str_replace(' ', '_', $event_timezone);
  496. if (isset($timezones[$event_timezone])) {
  497. $timezone = $event_timezone;
  498. }
  499. }
  500. catch (PDOException $e) {
  501. // Ignore error if event_timezones table does not exist or unexpected
  502. // schema found.
  503. }
  504. }
  505. if ($timezone) {
  506. db_update('users')
  507. ->fields(array('timezone' => $timezone))
  508. ->condition('uid', $account->uid)
  509. ->execute();
  510. }
  511. else {
  512. $sandbox['user_not_migrated']++;
  513. db_update('users')
  514. ->fields(array('timezone' => NULL))
  515. ->condition('uid', $account->uid)
  516. ->execute();
  517. }
  518. $sandbox['user_from']++;
  519. }
  520. $sandbox['#finished'] = $sandbox['user_from'] / $sandbox['user_count'];
  521. if ($sandbox['user_from'] == $sandbox['user_count']) {
  522. if ($sandbox['user_not_migrated'] > 0) {
  523. variable_set('empty_timezone_message', 1);
  524. drupal_set_message(format_string('Some user time zones have been emptied and need to be set to the correct values. Use the new <a href="@config-url">time zone options</a> to choose whether to remind users at login to set the correct time zone.', array('@config-url' => url('admin/config/regional/settings'))), 'warning');
  525. }
  526. return t('Migrated user time zones');
  527. }
  528. }
  529. }
  530. /**
  531. * Update user settings for cancelling user accounts.
  532. *
  533. * Prior to 7.x, users were not able to cancel their accounts. When
  534. * administrators deleted an account, all contents were assigned to uid 0,
  535. * which is the same as the 'user_cancel_reassign' method now.
  536. */
  537. function user_update_7003() {
  538. // Set the default account cancellation method.
  539. variable_set('user_cancel_method', 'user_cancel_reassign');
  540. // Re-assign notification setting.
  541. if ($setting = variable_get('user_mail_status_deleted_notify', FALSE)) {
  542. variable_set('user_mail_status_canceled_notify', $setting);
  543. variable_del('user_mail_status_deleted_notify');
  544. }
  545. // Re-assign "Account deleted" mail strings to "Account canceled" mail.
  546. if ($setting = variable_get('user_mail_status_deleted_subject', FALSE)) {
  547. variable_set('user_mail_status_canceled_subject', $setting);
  548. variable_del('user_mail_status_deleted_subject');
  549. }
  550. if ($setting = variable_get('user_mail_status_deleted_body', FALSE)) {
  551. variable_set('user_mail_status_canceled_body', $setting);
  552. variable_del('user_mail_status_deleted_body');
  553. }
  554. }
  555. /**
  556. * Changes the users table to allow longer e-mail addresses.
  557. */
  558. function user_update_7005(&$sandbox) {
  559. $mail_field = array(
  560. 'type' => 'varchar',
  561. 'length' => 254,
  562. 'not null' => FALSE,
  563. 'default' => '',
  564. 'description' => "User's e-mail address.",
  565. );
  566. $init_field = array(
  567. 'type' => 'varchar',
  568. 'length' => 254,
  569. 'not null' => FALSE,
  570. 'default' => '',
  571. 'description' => 'E-mail address used for initial account creation.',
  572. );
  573. db_drop_index('users', 'mail');
  574. db_change_field('users', 'mail', 'mail', $mail_field, array('indexes' => array('mail' => array('mail'))));
  575. db_change_field('users', 'init', 'init', $init_field);
  576. }
  577. /**
  578. * Add module data to {role_permission}.
  579. */
  580. function user_update_7006(&$sandbox) {
  581. $module_field = array(
  582. 'type' => 'varchar',
  583. 'length' => 255,
  584. 'not null' => TRUE,
  585. 'default' => '',
  586. 'description' => "The module declaring the permission.",
  587. );
  588. // Check that the field hasn't been updated in an aborted run of this
  589. // update.
  590. if (!db_field_exists('role_permission', 'module')) {
  591. // Add a new field for the fid.
  592. db_add_field('role_permission', 'module', $module_field);
  593. }
  594. }
  595. /**
  596. * Add a weight column to user roles.
  597. */
  598. function user_update_7007() {
  599. db_add_field('role', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  600. db_add_index('role', 'name_weight', array('name', 'weight'));
  601. }
  602. /**
  603. * If 'user_register' variable was unset in Drupal 6, set it to be the same as
  604. * the Drupal 6 default setting.
  605. */
  606. function user_update_7008() {
  607. if (!isset($GLOBALS['conf']['user_register'])) {
  608. // Set to the Drupal 6 default, "visitors can create accounts".
  609. variable_set('user_register', USER_REGISTER_VISITORS);
  610. }
  611. }
  612. /**
  613. * Converts fields that store serialized variables from text to blob.
  614. */
  615. function user_update_7009() {
  616. $spec = array(
  617. 'type' => 'blob',
  618. 'not null' => FALSE,
  619. 'size' => 'big',
  620. 'serialize' => TRUE,
  621. 'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
  622. );
  623. db_change_field('users', 'data', 'data', $spec);
  624. }
  625. /**
  626. * Update the {user}.signature_format column.
  627. */
  628. function user_update_7010() {
  629. // Update the database column to allow NULL values.
  630. db_change_field('users', 'signature_format', 'signature_format', array(
  631. 'type' => 'int',
  632. 'unsigned' => TRUE,
  633. 'not null' => FALSE,
  634. 'description' => 'The {filter_format}.format of the signature.',
  635. ));
  636. // Replace the signature format with NULL if the signature is empty and does
  637. // not already have a stored text format.
  638. //
  639. // In Drupal 6, "0" (the former FILTER_FORMAT_DEFAULT constant) could be used
  640. // to indicate this situation, but in Drupal 7, only NULL is supported. This
  641. // update therefore preserves the ability of user accounts which were never
  642. // given a signature (for example, if the site did not have user signatures
  643. // enabled, or if the user never edited their account information) to not
  644. // have a particular text format assumed for them the first time the
  645. // signature is edited.
  646. db_update('users')
  647. ->fields(array('signature_format' => NULL))
  648. ->condition('signature', '')
  649. ->condition('signature_format', 0)
  650. ->execute();
  651. // There are a number of situations in which a Drupal 6 site could store
  652. // content with a nonexistent text format. This includes text formats that
  653. // had later been deleted, or non-empty content stored with a value of "0"
  654. // (the former FILTER_FORMAT_DEFAULT constant). Drupal 6 would filter this
  655. // content using whatever the site-wide default text format was at the moment
  656. // the text was being displayed.
  657. //
  658. // In Drupal 7, this behavior is no longer supported, and all content must be
  659. // stored with an explicit text format (or it will not be displayed when it
  660. // is filtered). Therefore, to preserve the behavior of the site after the
  661. // upgrade, we must replace all instances described above with the current
  662. // value of the (old) site-wide default format at the moment of the upgrade.
  663. $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
  664. $default_format = variable_get('filter_default_format', 1);
  665. db_update('users')
  666. ->fields(array('signature_format' => $default_format))
  667. ->isNotNull('signature_format')
  668. ->condition('signature_format', $existing_formats, 'NOT IN')
  669. ->execute();
  670. }
  671. /**
  672. * Placeholder function.
  673. *
  674. * As a fix for user_update_7011() not updating email templates to use the new
  675. * tokens, user_update_7017() now targets email templates of Drupal 6 sites and
  676. * already upgraded sites.
  677. */
  678. function user_update_7011() {
  679. }
  680. /**
  681. * Add the user's pictures to the {file_managed} table and make them managed
  682. * files.
  683. */
  684. function user_update_7012(&$sandbox) {
  685. $picture_field = array(
  686. 'type' => 'int',
  687. 'not null' => TRUE,
  688. 'default' => 0,
  689. 'description' => "Foreign key: {file_managed}.fid of user's picture.",
  690. );
  691. if (!isset($sandbox['progress'])) {
  692. // Check that the field hasn't been updated in an aborted run of this
  693. // update.
  694. if (!db_field_exists('users', 'picture_fid')) {
  695. // Add a new field for the fid.
  696. db_add_field('users', 'picture_fid', $picture_field);
  697. }
  698. // Initialize batch update information.
  699. $sandbox['progress'] = 0;
  700. $sandbox['last_user_processed'] = -1;
  701. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} WHERE picture <> ''")->fetchField();
  702. }
  703. // As a batch operation move the photos into the {file_managed} table and
  704. // update the {users} records.
  705. $limit = 500;
  706. $result = db_query_range("SELECT uid, picture FROM {users} WHERE picture <> '' AND uid > :uid ORDER BY uid", 0, $limit, array(':uid' => $sandbox['last_user_processed']));
  707. foreach ($result as $user) {
  708. // Don't bother adding files that don't exist.
  709. if (file_exists($user->picture)) {
  710. // Check if the file already exists.
  711. $files = file_load_multiple(array(), array('uri' => $user->picture));
  712. if (count($files)) {
  713. $file = reset($files);
  714. }
  715. else {
  716. // Create a file object.
  717. $file = new stdClass();
  718. $file->uri = $user->picture;
  719. $file->filename = drupal_basename($file->uri);
  720. $file->filemime = file_get_mimetype($file->uri);
  721. $file->uid = $user->uid;
  722. $file->status = FILE_STATUS_PERMANENT;
  723. $file = file_save($file);
  724. }
  725. db_update('users')
  726. ->fields(array('picture_fid' => $file->fid))
  727. ->condition('uid', $user->uid)
  728. ->execute();
  729. }
  730. // Update our progress information for the batch update.
  731. $sandbox['progress']++;
  732. $sandbox['last_user_processed'] = $user->uid;
  733. }
  734. // Indicate our current progress to the batch update system. If there's no
  735. // max value then there's nothing to update and we're finished.
  736. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  737. // When we're finished, drop the old picture field and rename the new one to
  738. // replace it.
  739. if (isset($sandbox['#finished']) && $sandbox['#finished'] == 1) {
  740. db_drop_field('users', 'picture');
  741. db_change_field('users', 'picture_fid', 'picture', $picture_field);
  742. }
  743. }
  744. /**
  745. * Add user module file usage entries.
  746. */
  747. function user_update_7013(&$sandbox) {
  748. if (!isset($sandbox['progress'])) {
  749. // Initialize batch update information.
  750. $sandbox['progress'] = 0;
  751. $sandbox['last_uid_processed'] = -1;
  752. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} u WHERE u.picture <> 0")->fetchField();
  753. }
  754. // Add usage entries for the user picture files.
  755. $limit = 500;
  756. $result = db_query_range('SELECT f.*, u.uid as user_uid FROM {users} u INNER JOIN {file_managed} f ON u.picture = f.fid WHERE u.picture <> 0 AND u.uid > :uid ORDER BY u.uid', 0, $limit, array(':uid' => $sandbox['last_uid_processed']))->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
  757. foreach ($result as $row) {
  758. $uid = $row['user_uid'];
  759. $file = (object) $row;
  760. file_usage_add($file, 'user', 'user', $uid);
  761. // Update our progress information for the batch update.
  762. $sandbox['progress']++;
  763. $sandbox['last_uid_processed'] = $uid;
  764. }
  765. // Indicate our current progress to the batch update system.
  766. $sandbox['#finished'] = empty($sandbox['max']) || ($sandbox['progress'] / $sandbox['max']);
  767. }
  768. /**
  769. * Rename the 'post comments without approval' permission.
  770. *
  771. * In Drupal 7, this permission has been renamed to 'skip comment approval'.
  772. */
  773. function user_update_7014() {
  774. db_update('role_permission')
  775. ->fields(array('permission' => 'skip comment approval'))
  776. ->condition('permission', 'post comments without approval')
  777. ->execute();
  778. return t("Renamed the 'post comments without approval' permission to 'skip comment approval'.");
  779. }
  780. /**
  781. * Change {users}.signature_format into varchar.
  782. */
  783. function user_update_7015() {
  784. db_change_field('users', 'signature_format', 'signature_format', array(
  785. 'type' => 'varchar',
  786. 'length' => 255,
  787. 'not null' => FALSE,
  788. 'description' => 'The {filter_format}.format of the signature.',
  789. ));
  790. }
  791. /**
  792. * @} End of "addtogroup updates-6.x-to-7.x".
  793. */
  794. /**
  795. * @addtogroup updates-7.x-extra
  796. * @{
  797. */
  798. /**
  799. * Update the database to match the schema.
  800. */
  801. function user_update_7016() {
  802. // Add field default.
  803. db_change_field('users', 'uid', 'uid', array(
  804. 'type' => 'int',
  805. 'unsigned' => TRUE,
  806. 'not null' => TRUE,
  807. 'default' => 0,
  808. ));
  809. }
  810. /**
  811. * Update email templates to use new tokens.
  812. *
  813. * This function upgrades customized email templates from the old !token format
  814. * to the new core tokens format. Additionally, in Drupal 7 we no longer e-mail
  815. * plain text passwords to users, and there is no token for a plain text
  816. * password in the new token system. Therefore, it also modifies any saved
  817. * templates using the old '!password' token such that the token is removed, and
  818. * displays a warning to users that they may need to go and modify the wording
  819. * of their templates.
  820. */
  821. function user_update_7017() {
  822. $message = '';
  823. $tokens = array(
  824. '!site' => '[site:name]',
  825. '!username' => '[user:name]',
  826. '!mailto' => '[user:mail]',
  827. '!login_uri' => '[site:login-url]',
  828. '!uri_brief' => '[site:url-brief]',
  829. '!edit_uri' => '[user:edit-url]',
  830. '!login_url' => '[user:one-time-login-url]',
  831. '!uri' => '[site:url]',
  832. '!date' => '[date:medium]',
  833. '!password' => '',
  834. );
  835. $result = db_select('variable', 'v')
  836. ->fields('v', array('name'))
  837. ->condition('name', db_like('user_mail_') . '%', 'LIKE')
  838. ->execute();
  839. foreach ($result as $row) {
  840. // Use variable_get() to get the unserialized value for free.
  841. if ($value = variable_get($row->name, FALSE)) {
  842. if (empty($message) && (strpos($value, '!password') !== FALSE)) {
  843. $message = t('The ability to send users their passwords in plain text has been removed in Drupal 7. Your existing email templates have been modified to remove it. You should <a href="@template-url">review these templates</a> to make sure they read properly.', array('@template-url' => url('admin/config/people/accounts')));
  844. }
  845. variable_set($row->name, str_replace(array_keys($tokens), $tokens, $value));
  846. }
  847. }
  848. return $message;
  849. }
  850. /**
  851. * Ensure there is an index on {users}.picture.
  852. */
  853. function user_update_7018() {
  854. if (!db_index_exists('users', 'picture')) {
  855. db_add_index('users', 'picture', array('picture'));
  856. }
  857. }
  858. /**
  859. * Ensure there is a combined index on {authmap}.uid and {authmap}.module.
  860. */
  861. function user_update_7019() {
  862. // Check first in case it was already added manually.
  863. if (!db_index_exists('authmap', 'uid_module')) {
  864. db_add_index('authmap', 'uid_module', array('uid', 'module'));
  865. }
  866. }
  867. /**
  868. * Add changed field to users table.
  869. */
  870. function user_update_7020() {
  871. // The "changed" column was renamed to "access" in system_update_136(), and
  872. // the "changed" index on "access" column may persist on old MySQL databases.
  873. if (db_index_exists('users', 'changed')) {
  874. if (!db_index_exists('users', 'access')) {
  875. db_add_index('users', 'access', array('access'));
  876. }
  877. db_drop_index('users', 'changed');
  878. }
  879. $spec = array(
  880. 'type' => 'int',
  881. 'not null' => TRUE,
  882. 'default' => 0,
  883. 'description' => 'Timestamp for when user was changed.',
  884. );
  885. $keys = array(
  886. 'indexes' => array(
  887. 'changed' => array('changed'),
  888. ),
  889. );
  890. // In some cases sites have added the changed field themselves e.g. via a
  891. // contrib or custom module. Ensure the field uses core's new schema.
  892. if (db_field_exists('users', 'changed')) {
  893. db_change_field('users', 'changed', 'changed', $spec, $keys);
  894. }
  895. else {
  896. db_add_field('users', 'changed', $spec, $keys);
  897. // Set the initial value for existing users.
  898. db_update('users')
  899. ->expression('changed', 'created')
  900. ->execute();
  901. }
  902. }
  903. /**
  904. * @} End of "addtogroup updates-7.x-extra".
  905. */