user.install 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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' => 'roles',
  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' => 'roles',
  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, which relies on the {file_managed}
  346. // table, so it must run after system_update_7034(), which creates that
  347. // table.
  348. $dependencies['user'][7012] = array(
  349. 'system' => 7034,
  350. );
  351. // user_update_7013() uses the file usage API, which relies on the
  352. // {file_usage} table, so it must run after system_update_7059(), which
  353. // creates that table.
  354. $dependencies['user'][7013] = array(
  355. 'system' => 7059,
  356. );
  357. return $dependencies;
  358. }
  359. /**
  360. * Utility function: grant a set of permissions to a role during update.
  361. *
  362. * This function is valid for a database schema version 7000.
  363. *
  364. * @param $rid
  365. * The role ID.
  366. * @param $permissions
  367. * An array of permissions names.
  368. * @param $module
  369. * The name of the module defining the permissions.
  370. * @ingroup update_api
  371. */
  372. function _update_7000_user_role_grant_permissions($rid, array $permissions, $module) {
  373. // Grant new permissions for the role.
  374. foreach ($permissions as $name) {
  375. db_merge('role_permission')
  376. ->key(array(
  377. 'rid' => $rid,
  378. 'permission' => $name,
  379. ))
  380. ->fields(array(
  381. 'module' => $module,
  382. ))
  383. ->execute();
  384. }
  385. }
  386. /**
  387. * @addtogroup updates-6.x-to-7.x
  388. * @{
  389. */
  390. /**
  391. * Increase the length of the password field to accommodate better hashes.
  392. *
  393. * Also re-hashes all current passwords to improve security. This may be a
  394. * lengthy process, and is performed batch-wise.
  395. */
  396. function user_update_7000(&$sandbox) {
  397. $sandbox['#finished'] = 0;
  398. // Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed.
  399. $hash_count_log2 = 11;
  400. // Multi-part update.
  401. if (!isset($sandbox['user_from'])) {
  402. db_change_field('users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
  403. $sandbox['user_from'] = 0;
  404. $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
  405. }
  406. else {
  407. require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  408. // Hash again all current hashed passwords.
  409. $has_rows = FALSE;
  410. // Update this many per page load.
  411. $count = 1000;
  412. $result = db_query_range("SELECT uid, pass FROM {users} WHERE uid > 0 ORDER BY uid", $sandbox['user_from'], $count);
  413. foreach ($result as $account) {
  414. $has_rows = TRUE;
  415. // If the $account->pass value is not a MD5 hash (a 32 character
  416. // hexadecimal string) then skip it.
  417. if (!preg_match('/^[0-9a-f]{32}$/', $account->pass)) {
  418. continue;
  419. }
  420. $new_hash = user_hash_password($account->pass, $hash_count_log2);
  421. if ($new_hash) {
  422. // Indicate an updated password.
  423. $new_hash = 'U' . $new_hash;
  424. db_update('users')
  425. ->fields(array('pass' => $new_hash))
  426. ->condition('uid', $account->uid)
  427. ->execute();
  428. }
  429. }
  430. $sandbox['#finished'] = $sandbox['user_from']/$sandbox['user_count'];
  431. $sandbox['user_from'] += $count;
  432. if (!$has_rows) {
  433. $sandbox['#finished'] = 1;
  434. return t('User passwords rehashed to improve security');
  435. }
  436. }
  437. }
  438. /**
  439. * Remove the 'threshold', 'mode' and 'sort' columns from the {users} table.
  440. *
  441. * These fields were previously used to store per-user comment settings.
  442. */
  443. function user_update_7001() {
  444. db_drop_field('users', 'threshold');
  445. db_drop_field('users', 'mode');
  446. db_drop_field('users', 'sort');
  447. }
  448. /**
  449. * Convert user time zones from time zone offsets to time zone names.
  450. */
  451. function user_update_7002(&$sandbox) {
  452. $sandbox['#finished'] = 0;
  453. // Multi-part update.
  454. if (!isset($sandbox['user_from'])) {
  455. db_change_field('users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE));
  456. $sandbox['user_from'] = 0;
  457. $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
  458. $sandbox['user_not_migrated'] = 0;
  459. }
  460. else {
  461. $timezones = system_time_zones();
  462. // Update this many per page load.
  463. $count = 10000;
  464. $contributed_date_module = db_field_exists('users', 'timezone_name');
  465. $contributed_event_module = db_field_exists('users', 'timezone_id');
  466. $results = db_query_range("SELECT uid FROM {users} ORDER BY uid", $sandbox['user_from'], $count);
  467. foreach ($results as $account) {
  468. $timezone = NULL;
  469. // If the contributed Date module has created a users.timezone_name
  470. // column, use this data to set each user's time zone.
  471. if ($contributed_date_module) {
  472. $date_timezone = db_query("SELECT timezone_name FROM {users} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField();
  473. if (isset($timezones[$date_timezone])) {
  474. $timezone = $date_timezone;
  475. }
  476. }
  477. // If the contributed Event module has stored user time zone information
  478. // use that information to update the user accounts.
  479. if (!$timezone && $contributed_event_module) {
  480. try {
  481. $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();
  482. $event_timezone = str_replace(' ', '_', $event_timezone);
  483. if (isset($timezones[$event_timezone])) {
  484. $timezone = $event_timezone;
  485. }
  486. }
  487. catch (PDOException $e) {
  488. // Ignore error if event_timezones table does not exist or unexpected
  489. // schema found.
  490. }
  491. }
  492. if ($timezone) {
  493. db_update('users')
  494. ->fields(array('timezone' => $timezone))
  495. ->condition('uid', $account->uid)
  496. ->execute();
  497. }
  498. else {
  499. $sandbox['user_not_migrated']++;
  500. db_update('users')
  501. ->fields(array('timezone' => NULL))
  502. ->condition('uid', $account->uid)
  503. ->execute();
  504. }
  505. $sandbox['user_from']++;
  506. }
  507. $sandbox['#finished'] = $sandbox['user_from'] / $sandbox['user_count'];
  508. if ($sandbox['user_from'] == $sandbox['user_count']) {
  509. if ($sandbox['user_not_migrated'] > 0) {
  510. variable_set('empty_timezone_message', 1);
  511. drupal_set_message('Some user time zones have been emptied and need to be set to the correct values. Use the new ' . l('time zone options', 'admin/config/regional/settings') . ' to choose whether to remind users at login to set the correct time zone.', 'warning');
  512. }
  513. return t('Migrated user time zones');
  514. }
  515. }
  516. }
  517. /**
  518. * Update user settings for cancelling user accounts.
  519. *
  520. * Prior to 7.x, users were not able to cancel their accounts. When
  521. * administrators deleted an account, all contents were assigned to uid 0,
  522. * which is the same as the 'user_cancel_reassign' method now.
  523. */
  524. function user_update_7003() {
  525. // Set the default account cancellation method.
  526. variable_set('user_cancel_method', 'user_cancel_reassign');
  527. // Re-assign notification setting.
  528. if ($setting = variable_get('user_mail_status_deleted_notify', FALSE)) {
  529. variable_set('user_mail_status_canceled_notify', $setting);
  530. variable_del('user_mail_status_deleted_notify');
  531. }
  532. // Re-assign "Account deleted" mail strings to "Account canceled" mail.
  533. if ($setting = variable_get('user_mail_status_deleted_subject', FALSE)) {
  534. variable_set('user_mail_status_canceled_subject', $setting);
  535. variable_del('user_mail_status_deleted_subject');
  536. }
  537. if ($setting = variable_get('user_mail_status_deleted_body', FALSE)) {
  538. variable_set('user_mail_status_canceled_body', $setting);
  539. variable_del('user_mail_status_deleted_body');
  540. }
  541. }
  542. /**
  543. * Changes the users table to allow longer e-mail addresses.
  544. */
  545. function user_update_7005(&$sandbox) {
  546. $mail_field = array(
  547. 'type' => 'varchar',
  548. 'length' => 254,
  549. 'not null' => FALSE,
  550. 'default' => '',
  551. 'description' => "User's e-mail address.",
  552. );
  553. $init_field = array(
  554. 'type' => 'varchar',
  555. 'length' => 254,
  556. 'not null' => FALSE,
  557. 'default' => '',
  558. 'description' => 'E-mail address used for initial account creation.',
  559. );
  560. db_drop_index('users', 'mail');
  561. db_change_field('users', 'mail', 'mail', $mail_field, array('indexes' => array('mail' => array('mail'))));
  562. db_change_field('users', 'init', 'init', $init_field);
  563. }
  564. /**
  565. * Add module data to {role_permission}.
  566. */
  567. function user_update_7006(&$sandbox) {
  568. $module_field = array(
  569. 'type' => 'varchar',
  570. 'length' => 255,
  571. 'not null' => TRUE,
  572. 'default' => '',
  573. 'description' => "The module declaring the permission.",
  574. );
  575. // Check that the field hasn't been updated in an aborted run of this
  576. // update.
  577. if (!db_field_exists('role_permission', 'module')) {
  578. // Add a new field for the fid.
  579. db_add_field('role_permission', 'module', $module_field);
  580. }
  581. }
  582. /**
  583. * Add a weight column to user roles.
  584. */
  585. function user_update_7007() {
  586. db_add_field('role', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
  587. db_add_index('role', 'name_weight', array('name', 'weight'));
  588. }
  589. /**
  590. * If 'user_register' variable was unset in Drupal 6, set it to be the same as
  591. * the Drupal 6 default setting.
  592. */
  593. function user_update_7008() {
  594. if (!isset($GLOBALS['conf']['user_register'])) {
  595. // Set to the Drupal 6 default, "visitors can create accounts".
  596. variable_set('user_register', USER_REGISTER_VISITORS);
  597. }
  598. }
  599. /**
  600. * Converts fields that store serialized variables from text to blob.
  601. */
  602. function user_update_7009() {
  603. $spec = array(
  604. 'type' => 'blob',
  605. 'not null' => FALSE,
  606. 'size' => 'big',
  607. 'serialize' => TRUE,
  608. '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.',
  609. );
  610. db_change_field('users', 'data', 'data', $spec);
  611. }
  612. /**
  613. * Update the {user}.signature_format column.
  614. */
  615. function user_update_7010() {
  616. // Update the database column to allow NULL values.
  617. db_change_field('users', 'signature_format', 'signature_format', array(
  618. 'type' => 'int',
  619. 'unsigned' => TRUE,
  620. 'not null' => FALSE,
  621. 'description' => 'The {filter_format}.format of the signature.',
  622. ));
  623. // Replace the signature format with NULL if the signature is empty and does
  624. // not already have a stored text format.
  625. //
  626. // In Drupal 6, "0" (the former FILTER_FORMAT_DEFAULT constant) could be used
  627. // to indicate this situation, but in Drupal 7, only NULL is supported. This
  628. // update therefore preserves the ability of user accounts which were never
  629. // given a signature (for example, if the site did not have user signatures
  630. // enabled, or if the user never edited their account information) to not
  631. // have a particular text format assumed for them the first time the
  632. // signature is edited.
  633. db_update('users')
  634. ->fields(array('signature_format' => NULL))
  635. ->condition('signature', '')
  636. ->condition('signature_format', 0)
  637. ->execute();
  638. // There are a number of situations in which a Drupal 6 site could store
  639. // content with a nonexistent text format. This includes text formats that
  640. // had later been deleted, or non-empty content stored with a value of "0"
  641. // (the former FILTER_FORMAT_DEFAULT constant). Drupal 6 would filter this
  642. // content using whatever the site-wide default text format was at the moment
  643. // the text was being displayed.
  644. //
  645. // In Drupal 7, this behavior is no longer supported, and all content must be
  646. // stored with an explicit text format (or it will not be displayed when it
  647. // is filtered). Therefore, to preserve the behavior of the site after the
  648. // upgrade, we must replace all instances described above with the current
  649. // value of the (old) site-wide default format at the moment of the upgrade.
  650. $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
  651. $default_format = variable_get('filter_default_format', 1);
  652. db_update('users')
  653. ->fields(array('signature_format' => $default_format))
  654. ->isNotNull('signature_format')
  655. ->condition('signature_format', $existing_formats, 'NOT IN')
  656. ->execute();
  657. }
  658. /**
  659. * Placeholder function.
  660. *
  661. * As a fix for user_update_7011() not updating email templates to use the new
  662. * tokens, user_update_7017() now targets email templates of Drupal 6 sites and
  663. * already upgraded sites.
  664. */
  665. function user_update_7011() {
  666. }
  667. /**
  668. * Add the user's pictures to the {file_managed} table and make them managed
  669. * files.
  670. */
  671. function user_update_7012(&$sandbox) {
  672. $picture_field = array(
  673. 'type' => 'int',
  674. 'not null' => TRUE,
  675. 'default' => 0,
  676. 'description' => "Foreign key: {file_managed}.fid of user's picture.",
  677. );
  678. if (!isset($sandbox['progress'])) {
  679. // Check that the field hasn't been updated in an aborted run of this
  680. // update.
  681. if (!db_field_exists('users', 'picture_fid')) {
  682. // Add a new field for the fid.
  683. db_add_field('users', 'picture_fid', $picture_field);
  684. }
  685. // Initialize batch update information.
  686. $sandbox['progress'] = 0;
  687. $sandbox['last_user_processed'] = -1;
  688. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} WHERE picture <> ''")->fetchField();
  689. }
  690. // As a batch operation move the photos into the {file_managed} table and
  691. // update the {users} records.
  692. $limit = 500;
  693. $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']));
  694. foreach ($result as $user) {
  695. // Don't bother adding files that don't exist.
  696. if (file_exists($user->picture)) {
  697. // Check if the file already exists.
  698. $files = file_load_multiple(array(), array('uri' => $user->picture));
  699. if (count($files)) {
  700. $file = reset($files);
  701. }
  702. else {
  703. // Create a file object.
  704. $file = new stdClass();
  705. $file->uri = $user->picture;
  706. $file->filename = drupal_basename($file->uri);
  707. $file->filemime = file_get_mimetype($file->uri);
  708. $file->uid = $user->uid;
  709. $file->status = FILE_STATUS_PERMANENT;
  710. $file = file_save($file);
  711. }
  712. db_update('users')
  713. ->fields(array('picture_fid' => $file->fid))
  714. ->condition('uid', $user->uid)
  715. ->execute();
  716. }
  717. // Update our progress information for the batch update.
  718. $sandbox['progress']++;
  719. $sandbox['last_user_processed'] = $user->uid;
  720. }
  721. // Indicate our current progress to the batch update system. If there's no
  722. // max value then there's nothing to update and we're finished.
  723. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  724. // When we're finished, drop the old picture field and rename the new one to
  725. // replace it.
  726. if (isset($sandbox['#finished']) && $sandbox['#finished'] == 1) {
  727. db_drop_field('users', 'picture');
  728. db_change_field('users', 'picture_fid', 'picture', $picture_field);
  729. }
  730. }
  731. /**
  732. * Add user module file usage entries.
  733. */
  734. function user_update_7013(&$sandbox) {
  735. if (!isset($sandbox['progress'])) {
  736. // Initialize batch update information.
  737. $sandbox['progress'] = 0;
  738. $sandbox['last_uid_processed'] = -1;
  739. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} u WHERE u.picture <> 0")->fetchField();
  740. }
  741. // Add usage entries for the user picture files.
  742. $limit = 500;
  743. $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);
  744. foreach ($result as $row) {
  745. $uid = $row['user_uid'];
  746. $file = (object) $row;
  747. file_usage_add($file, 'user', 'user', $uid);
  748. // Update our progress information for the batch update.
  749. $sandbox['progress']++;
  750. $sandbox['last_uid_processed'] = $uid;
  751. }
  752. // Indicate our current progress to the batch update system.
  753. $sandbox['#finished'] = empty($sandbox['max']) || ($sandbox['progress'] / $sandbox['max']);
  754. }
  755. /**
  756. * Rename the 'post comments without approval' permission.
  757. *
  758. * In Drupal 7, this permission has been renamed to 'skip comment approval'.
  759. */
  760. function user_update_7014() {
  761. db_update('role_permission')
  762. ->fields(array('permission' => 'skip comment approval'))
  763. ->condition('permission', 'post comments without approval')
  764. ->execute();
  765. return t("Renamed the 'post comments without approval' permission to 'skip comment approval'.");
  766. }
  767. /**
  768. * Change {users}.signature_format into varchar.
  769. */
  770. function user_update_7015() {
  771. db_change_field('users', 'signature_format', 'signature_format', array(
  772. 'type' => 'varchar',
  773. 'length' => 255,
  774. 'not null' => FALSE,
  775. 'description' => 'The {filter_format}.format of the signature.',
  776. ));
  777. }
  778. /**
  779. * @} End of "addtogroup updates-6.x-to-7.x".
  780. */
  781. /**
  782. * @addtogroup updates-7.x-extra
  783. * @{
  784. */
  785. /**
  786. * Update the database to match the schema.
  787. */
  788. function user_update_7016() {
  789. // Add field default.
  790. db_change_field('users', 'uid', 'uid', array(
  791. 'type' => 'int',
  792. 'unsigned' => TRUE,
  793. 'not null' => TRUE,
  794. 'default' => 0,
  795. ));
  796. }
  797. /**
  798. * Update email templates to use new tokens.
  799. *
  800. * This function upgrades customized email templates from the old !token format
  801. * to the new core tokens format. Additionally, in Drupal 7 we no longer e-mail
  802. * plain text passwords to users, and there is no token for a plain text
  803. * password in the new token system. Therefore, it also modifies any saved
  804. * templates using the old '!password' token such that the token is removed, and
  805. * displays a warning to users that they may need to go and modify the wording
  806. * of their templates.
  807. */
  808. function user_update_7017() {
  809. $message = '';
  810. $tokens = array(
  811. '!site' => '[site:name]',
  812. '!username' => '[user:name]',
  813. '!mailto' => '[user:mail]',
  814. '!login_uri' => '[site:login-url]',
  815. '!uri_brief' => '[site:url-brief]',
  816. '!edit_uri' => '[user:edit-url]',
  817. '!login_url' => '[user:one-time-login-url]',
  818. '!uri' => '[site:url]',
  819. '!date' => '[date:medium]',
  820. '!password' => '',
  821. );
  822. $result = db_select('variable', 'v')
  823. ->fields('v', array('name'))
  824. ->condition('name', db_like('user_mail_') . '%', 'LIKE')
  825. ->execute();
  826. foreach ($result as $row) {
  827. // Use variable_get() to get the unserialized value for free.
  828. if ($value = variable_get($row->name, FALSE)) {
  829. if (empty($message) && (strpos($value, '!password') !== FALSE)) {
  830. $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')));
  831. }
  832. variable_set($row->name, str_replace(array_keys($tokens), $tokens, $value));
  833. }
  834. }
  835. return $message;
  836. }
  837. /**
  838. * Ensure there is an index on {users}.picture.
  839. */
  840. function user_update_7018() {
  841. if (!db_index_exists('users', 'picture')) {
  842. db_add_index('users', 'picture', array('picture'));
  843. }
  844. }
  845. /**
  846. * @} End of "addtogroup updates-7.x-extra".
  847. */