user.install 29 KB

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