user.install 29 KB

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