tmgmt.install 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. /**
  3. * @file
  4. * Installation hooks for the Translation Management module.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function tmgmt_uninstall() {
  10. // Remove all variables that we added.
  11. variable_del('tmgmt_quick_checkout');
  12. variable_del('tmgmt_purge_finished');
  13. }
  14. /**
  15. * Implements hook_schema().
  16. */
  17. function tmgmt_schema() {
  18. $schema['tmgmt_job'] = array(
  19. 'description' => 'A translation job represents a translation order that can be assigned to a translator.',
  20. 'fields' => array(
  21. 'tjid' => array(
  22. 'description' => 'The identifier of the translation job.',
  23. 'type' => 'serial',
  24. 'not null' => TRUE,
  25. ),
  26. 'source_language' => array(
  27. 'description' => 'The source language of the data.',
  28. 'type' => 'varchar',
  29. 'length' => 12,
  30. 'not null' => TRUE,
  31. ),
  32. 'target_language' => array(
  33. 'description' => 'The language the data should be translated to.',
  34. 'type' => 'varchar',
  35. 'length' => 12,
  36. 'not null' => TRUE,
  37. ),
  38. 'state' => array(
  39. 'description' => 'The state of the translation job.',
  40. 'type' => 'int',
  41. 'not null' => TRUE,
  42. ),
  43. 'created' => array(
  44. 'description' => 'Created timestamp.',
  45. 'type' => 'int',
  46. 'unsigned' => TRUE,
  47. 'not null' => TRUE,
  48. ),
  49. 'changed' => array(
  50. 'description' => 'Changed timestamp.',
  51. 'type' => 'int',
  52. 'unsigned' => TRUE,
  53. 'not null' => TRUE,
  54. ),
  55. 'translator' => array(
  56. 'description' => 'Machine name of the translator.',
  57. 'type' => 'varchar',
  58. 'length' => 128,
  59. ),
  60. 'settings' => array(
  61. 'description' => 'Translator specific configuration and context for this job.',
  62. 'type' => 'text',
  63. 'size' => 'big',
  64. 'serialize' => TRUE,
  65. ),
  66. 'reference' => array(
  67. 'description' => 'Remote identifier of this translation job.',
  68. 'type' => 'varchar',
  69. 'length' => 255,
  70. ),
  71. 'uid' => array(
  72. 'description' => 'uid of the job creator',
  73. 'type' => 'int',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. ),
  77. 'label' => array(
  78. 'description' => 'Optional user provided label of the job.',
  79. 'type' => 'varchar',
  80. 'length' => 255,
  81. ),
  82. ),
  83. 'primary key' => array('tjid'),
  84. 'indexes' => array(
  85. 'state' => array('state'),
  86. 'reference' => array('reference'),
  87. ),
  88. );
  89. $schema['tmgmt_remote'] = array(
  90. 'description' => 'TMGMT job remote mapping.',
  91. 'fields' => array(
  92. 'trid' => array(
  93. 'description' => 'The primary key.',
  94. 'type' => 'serial',
  95. 'not null' => TRUE,
  96. ),
  97. 'tjid' => array(
  98. 'type' => 'int',
  99. 'not null' => TRUE,
  100. 'default' => 0,
  101. 'description' => '{tmgmt_job}.tjid foreign key',
  102. ),
  103. 'tjiid' => array(
  104. 'type' => 'int',
  105. 'not null' => TRUE,
  106. 'default' => 0,
  107. 'description' => '{tmgmt_job_item}.tjjid foreign key',
  108. ),
  109. 'data_item_key' => array(
  110. 'type' => 'varchar',
  111. 'length' => 255,
  112. 'not null' => TRUE,
  113. 'default' => '',
  114. 'description' => 'Translation job data item key.',
  115. ),
  116. 'remote_identifier_1' => array(
  117. 'type' => 'varchar',
  118. 'length' => 127,
  119. 'not null' => TRUE,
  120. 'default' => '',
  121. 'description' => 'Custom remote identifier data.',
  122. ),
  123. 'remote_identifier_2' => array(
  124. 'type' => 'varchar',
  125. 'length' => 127,
  126. 'not null' => TRUE,
  127. 'default' => '',
  128. 'description' => 'Custom remote identifier data.',
  129. ),
  130. 'remote_identifier_3' => array(
  131. 'type' => 'varchar',
  132. 'length' => 255,
  133. 'not null' => TRUE,
  134. 'default' => '',
  135. 'description' => 'Custom remote identifier data.',
  136. ),
  137. 'remote_url' => array(
  138. 'type' => 'varchar',
  139. 'length' => 255,
  140. 'not null' => TRUE,
  141. 'default' => '',
  142. 'description' => 'Remote job url.',
  143. ),
  144. 'word_count' => array(
  145. 'type' => 'int',
  146. 'not null' => TRUE,
  147. 'default' => 0,
  148. 'description' => 'Word count info provided by the remote service.',
  149. ),
  150. 'amount' => array(
  151. 'type' => 'int',
  152. 'size' => 'big',
  153. 'not null' => TRUE,
  154. 'default' => 0,
  155. 'description' => 'Amount charged for the remote translation job.',
  156. ),
  157. 'currency' => array(
  158. 'type' => 'char',
  159. 'length' => 3,
  160. 'not null' => TRUE,
  161. 'default' => '',
  162. 'description' => 'Amount charged currency.',
  163. ),
  164. 'remote_data' => array(
  165. 'description' => 'Custom remote data.',
  166. 'type' => 'text',
  167. 'size' => 'big',
  168. 'serialize' => TRUE,
  169. ),
  170. ),
  171. 'primary key' => array('trid'),
  172. 'indexes' => array(
  173. 'tjid' => array('tjid'),
  174. 'tjiid' => array('tjiid'),
  175. 'remote_identifiers' => array('remote_identifier_1', 'remote_identifier_2'),
  176. ),
  177. );
  178. $schema['tmgmt_job_item'] = array(
  179. 'description' => 'A job item connects a source to a translation job.',
  180. 'fields' => array(
  181. 'tjiid' => array(
  182. 'description' => 'The identifier of the translation job item.',
  183. 'type' => 'serial',
  184. 'not null' => TRUE,
  185. ),
  186. 'tjid' => array(
  187. 'description' => 'The identifier of the translation job.',
  188. 'type' => 'int',
  189. 'not null' => TRUE,
  190. 'unsigned' => TRUE,
  191. ),
  192. 'plugin' => array(
  193. 'description' => 'Indicates the plugin which provides this item.',
  194. 'type' => 'varchar',
  195. 'length' => 128,
  196. 'not null' => TRUE,
  197. ),
  198. 'item_type' => array(
  199. 'description' => 'The type of the item, e.g. the entity type.',
  200. 'type' => 'varchar',
  201. 'length' => 128,
  202. ),
  203. 'item_id' => array(
  204. 'description' => 'The unique id (within the given item type) of the item.',
  205. 'type' => 'varchar',
  206. 'length' => 128,
  207. 'not null' => TRUE,
  208. ),
  209. 'state' => array(
  210. 'description' => 'The state of the translation job item.',
  211. 'type' => 'int',
  212. 'not null' => TRUE,
  213. ),
  214. 'data' => array(
  215. 'description' => 'Can be used by the source plugin to store the data if it can not be retrieved anymore later on.',
  216. 'type' => 'text',
  217. 'not null' => TRUE,
  218. 'size' => 'big',
  219. 'serialize' => TRUE,
  220. ),
  221. 'changed' => array(
  222. 'description' => 'Changed timestamp.',
  223. 'type' => 'int',
  224. 'unsigned' => TRUE,
  225. 'not null' => TRUE,
  226. ),
  227. 'count_pending' => array(
  228. 'description' => 'Counter for all pending data items.',
  229. 'type' => 'int',
  230. 'not null' => TRUE,
  231. 'default' => 0,
  232. ),
  233. 'count_translated' => array(
  234. 'description' => 'Counter for all translated data items.',
  235. 'type' => 'int',
  236. 'not null' => TRUE,
  237. 'default' => 0,
  238. ),
  239. 'count_accepted' => array(
  240. 'description' => 'Counter for all accepted data items.',
  241. 'type' => 'int',
  242. 'not null' => TRUE,
  243. 'default' => 0,
  244. ),
  245. 'count_reviewed' => array(
  246. 'description' => 'Counter for all reviewed data items.',
  247. 'type' => 'int',
  248. 'not null' => TRUE,
  249. 'default' => 0,
  250. ),
  251. 'word_count' => array(
  252. 'description' => 'Word count of all texts contained in this job item.',
  253. 'type' => 'int',
  254. 'not null' => TRUE,
  255. 'default' => 0,
  256. ),
  257. ),
  258. 'primary key' => array('tjiid'),
  259. 'indexes' => array(
  260. 'job_id' => array('tjid'),
  261. ),
  262. 'foreign keys' => array(
  263. 'job' => array(
  264. 'table' => 'tmgmt_job',
  265. 'columns' => array('tjid'),
  266. ),
  267. ),
  268. );
  269. $schema['tmgmt_translator'] = array(
  270. 'description' => 'A translator is a combination of a translator type and type specific configuration.',
  271. 'fields' => array(
  272. 'tid' => array(
  273. 'description' => 'The identifier of the translator.',
  274. 'type' => 'serial',
  275. 'not null' => TRUE,
  276. ),
  277. 'name' => array(
  278. 'description' => 'Machine name identifier of the translator.',
  279. 'type' => 'varchar',
  280. 'length' => 128,
  281. 'not null' => TRUE,
  282. ),
  283. 'label' => array(
  284. 'description' => 'Label of the translator.',
  285. 'type' => 'varchar',
  286. 'length' => 255,
  287. 'not null' => TRUE,
  288. ),
  289. 'description' => array(
  290. 'description' => 'Description of the translator.',
  291. 'type' => 'text',
  292. 'size' => 'medium',
  293. ),
  294. 'plugin' => array(
  295. 'description' => 'Name of the translator service plugin.',
  296. 'type' => 'varchar',
  297. 'length' => 128,
  298. 'not null' => TRUE,
  299. ),
  300. 'settings' => array(
  301. 'description' => 'Translator specific settings.',
  302. 'type' => 'text',
  303. 'size' => 'big',
  304. 'serialize' => TRUE,
  305. ),
  306. 'weight' => array(
  307. 'description' => 'The weight of the translator.',
  308. 'type' => 'int',
  309. 'not null' => TRUE,
  310. 'default' => 0,
  311. ),
  312. 'status' => array(
  313. 'description' => 'The exportable status of the entity.',
  314. 'type' => 'int',
  315. 'not null' => TRUE,
  316. // Set the default to ENTITY_CUSTOM without using the constant as it is
  317. // not safe to use it at this point.
  318. 'default' => 0x01,
  319. 'size' => 'tiny',
  320. ),
  321. 'module' => array(
  322. 'description' => 'The name of the providing module if the entity has been defined in code.',
  323. 'type' => 'varchar',
  324. 'length' => 255,
  325. 'not null' => FALSE,
  326. ),
  327. ),
  328. 'primary key' => array('tid'),
  329. 'unique keys' => array(
  330. 'name' => array('name'),
  331. ),
  332. );
  333. $schema['tmgmt_message'] = array(
  334. 'description' => 'A log message can be used to store events that affect a job.',
  335. 'fields' => array(
  336. 'mid' => array(
  337. 'description' => 'The identifier of the message.',
  338. 'type' => 'serial',
  339. 'not null' => TRUE,
  340. ),
  341. 'tjid' => array(
  342. 'description' => 'The identifier of the translation job that the message belongs to.',
  343. 'type' => 'int',
  344. 'unsigned' => TRUE,
  345. 'not null' => TRUE,
  346. ),
  347. 'tjiid' => array(
  348. 'description' => 'The identifier of the translation job item that the message belongs to.',
  349. 'type' => 'int',
  350. 'unsigned' => TRUE,
  351. ),
  352. 'uid' => array(
  353. 'description' => 'The identifier of the user who performed the action.',
  354. 'type' => 'int',
  355. 'unsigned' => TRUE,
  356. 'default' => 0,
  357. ),
  358. 'message' => array(
  359. 'description' => 'The language into the data should be translated.',
  360. 'type' => 'text',
  361. 'size' => 'big',
  362. ),
  363. 'variables' => array(
  364. 'description' => 'The variables of the message as expected by t().',
  365. 'type' => 'text',
  366. 'size' => 'big',
  367. 'serialize' => TRUE,
  368. ),
  369. 'created' => array(
  370. 'description' => 'Created timestamp.',
  371. 'type' => 'int',
  372. 'unsigned' => TRUE,
  373. 'not null' => TRUE,
  374. ),
  375. 'type' => array(
  376. 'description' => 'Type of the message (debug, notice, warning or error)',
  377. 'type' => 'varchar',
  378. 'length' => 128,
  379. 'not null' => TRUE,
  380. ),
  381. ),
  382. 'primary key' => array('mid'),
  383. 'indexes' => array(
  384. 'tjid' => array('tjid'),
  385. 'tjiid' => array('tjiid'),
  386. ),
  387. );
  388. // Clone the schema for our cache table from Drupal core.
  389. $schema['cache_tmgmt'] = drupal_get_schema_unprocessed('system', 'cache');
  390. // Clone the schema for the entity cache module if it is enabled.
  391. if (module_exists('entitycache')) {
  392. $schema['cache_entity_tmgmt_translator'] = drupal_get_schema_unprocessed('system', 'cache');
  393. }
  394. return $schema;
  395. }
  396. /**
  397. * Merge the content of the 'translation' field into the 'data' field.
  398. */
  399. function tmgmt_update_7000(&$sandbox) {
  400. if (!isset($sandbox['progress'])) {
  401. $sandbox['progress'] = 0;
  402. $sandbox['max'] = db_query('SELECT COUNT(*) FROM {tmgmt_job_item}')->fetchField();
  403. }
  404. $results = db_select('tmgmt_job_item', 'tji')
  405. ->fields('tji', array('tjiid', 'data', 'translation'))
  406. ->range($sandbox['progress'], 50)
  407. ->orderBy('tjiid', 'ASC')
  408. ->execute();
  409. foreach ($results as $item) {
  410. $data = unserialize($item->data);
  411. if (!empty($item->translation)) {
  412. foreach (tmgmt_flatten_data(unserialize($item->translation)) as $key => $translation) {
  413. if (!empty($item->data)) {
  414. $key = explode('][', $key);
  415. drupal_array_set_nested_value($data, array_merge($key, array('#translation')), $translation);
  416. }
  417. }
  418. db_update('tmgmt_job_item')
  419. ->condition('tjiid', $item->tjiid)
  420. ->fields(array('data' => serialize($data)))
  421. ->execute();
  422. }
  423. $sandbox['progress']++;
  424. }
  425. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  426. }
  427. /**
  428. * Remove the 'translation' field from the job item entity.
  429. */
  430. function tmgmt_update_7001() {
  431. db_drop_field('tmgmt_job_item', 'translation');
  432. }
  433. /**
  434. * Add counter columns to {tmgmt_job_item}.
  435. */
  436. function tmgmt_update_7002() {
  437. // Defining schema of additional fields.
  438. $schema = array(
  439. 'count_pending' => array(
  440. 'description' => 'Counter for all pending data items.',
  441. 'type' => 'int',
  442. 'not null' => TRUE,
  443. 'default' => 0,
  444. ),
  445. 'count_translated' => array(
  446. 'description' => 'Counter for all translated data items.',
  447. 'type' => 'int',
  448. 'not null' => TRUE,
  449. 'default' => 0,
  450. ),
  451. 'count_approved' => array(
  452. 'description' => 'Counter for all approved data items.',
  453. 'type' => 'int',
  454. 'not null' => TRUE,
  455. 'default' => 0,
  456. ),
  457. );
  458. // Add aditional fields to db.
  459. foreach ($schema as $field => $spec) {
  460. db_add_field('tmgmt_job_item', $field, $spec);
  461. }
  462. }
  463. /**
  464. * Data parsing helper function for tmgmt_update_7003().
  465. *
  466. * Copied from TMGMTJobItemController::count().
  467. *
  468. * @param $item
  469. * The current data item.
  470. * @param $entity
  471. * The job item the count should be calculated.
  472. */
  473. function _tmgmt_data_count_7003(&$item, $entity) {
  474. if (!empty($item['#text'])) {
  475. if (_tmgmt_filter_data($item)) {
  476. // Set default states if no state is set.
  477. if (!isset($item['#status'])) {
  478. // Translation is present.
  479. if (!empty($item['#translation'])) {
  480. $item['#status'] = TMGMT_DATA_ITEM_STATE_TRANSLATED;
  481. }
  482. // No translation present.
  483. else {
  484. $item['#status'] = TMGMT_DATA_ITEM_STATE_PENDING;
  485. }
  486. }
  487. switch ($item['#status']) {
  488. case 1:
  489. $entity->count_approved++;
  490. break;
  491. case 2:
  492. $entity->count_translated++;
  493. break;
  494. default:
  495. $entity->count_pending++;
  496. break;
  497. }
  498. }
  499. }
  500. else {
  501. foreach (element_children($item) as $key) {
  502. _tmgmt_data_count_7003($item[$key], $entity);
  503. }
  504. }
  505. }
  506. /**
  507. * Set counters for existing job items.
  508. */
  509. function tmgmt_update_7003(&$sandbox) {
  510. if (!isset($sandbox['progress'])) {
  511. $sandbox['progress'] = 0;
  512. $sandbox['last_tjiid'] = 0;
  513. $sandbox['max'] = db_query('SELECT COUNT(tjiid) FROM {tmgmt_job_item}')->fetchField();
  514. }
  515. $result = db_query('SELECT tjiid, data FROM {tmgmt_job_item} WHERE tjiid > :last_tjiid ORDER BY tjiid LIMIT 10', array( ':last_tjiid' => $sandbox['last_tjiid']));
  516. foreach ($result as $row) {
  517. // Unseralize data, count it and then save the counters.
  518. if (!empty($row->data)) {
  519. $fake_job_item = (object)array(
  520. 'count_approved' => 0,
  521. 'count_translated' => 0,
  522. 'count_pending' => 0,
  523. );
  524. $data = unserialize($row->data);
  525. _tmgmt_data_count_7003($data, $fake_job_item);
  526. $fields = (array)$fake_job_item + array(
  527. 'data' => serialize($data),
  528. );
  529. db_update('tmgmt_job_item')
  530. ->condition('tjiid', $row->tjiid)
  531. ->fields($fields)
  532. ->execute();
  533. }
  534. $sandbox['progress']++;
  535. }
  536. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  537. if ($row) {
  538. $sandbox['last_tjiid'] = $row->tjiid;
  539. }
  540. }
  541. /**
  542. * Replace the [#translation][#finished] attribute with [#status].
  543. */
  544. function tmgmt_update_7004(&$sandbox) {
  545. if (!isset($sandbox['progress'])) {
  546. $sandbox['progress'] = 0;
  547. $sandbox['last_tjiid'] = 0;
  548. $sandbox['max'] = db_query('SELECT COUNT(tjiid) FROM {tmgmt_job_item}')->fetchField();
  549. }
  550. $result = db_query('SELECT tjiid, data FROM {tmgmt_job_item} WHERE tjiid > :last_tjiid ORDER BY tjiid LIMIT 10', array(':last_tjiid' => $sandbox['last_tjiid']));
  551. foreach ($result as $row) {
  552. if (!empty($row->data)) {
  553. $data = unserialize($row->data);
  554. $flattened_data = array_filter(tmgmt_flatten_data($data), '_tmgmt_filter_data');
  555. // Loop over data, find finished translations, remove the flag and set
  556. // the status instead.
  557. foreach ($flattened_data as $key => $values) {
  558. if (!empty($values['#translation']['#finished'])) {
  559. $finished = $values['#translation']['#finished'];
  560. unset($values['#translation']['#finished']);
  561. drupal_array_set_nested_value($data, array_merge($key, array('#translation')), $values['#translation']);
  562. if ($finished && (empty($values['#status']) || $values['#status'] == TMGMT_DATA_ITEM_STATE_PENDING)) {
  563. drupal_array_set_nested_value($data, array_merge($key, array('#status')), TMGMT_DATA_ITEM_STATE_TRANSLATED);
  564. }
  565. // Save the updated data structure.
  566. db_update('tmgmt_job_item')
  567. ->condition('tjiid', $row->tjiid)
  568. ->fields(array('data' => serialize($data)))
  569. ->execute();
  570. }
  571. }
  572. }
  573. $sandbox['progress']++;
  574. }
  575. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  576. if ($row) {
  577. $sandbox['last_tjiid'] = $row->tjiid;
  578. }
  579. }
  580. /**
  581. * Add word count column to {tmgmt_job_item}.
  582. */
  583. function tmgmt_update_7005() {
  584. if (!db_field_exists('tmgmt_job_item', 'word_count')) {
  585. $spec = array(
  586. 'description' => 'Word count of all texts contained in this job item.',
  587. 'type' => 'int',
  588. 'not null' => TRUE,
  589. 'default' => 0,
  590. );
  591. db_add_field('tmgmt_job_item', 'word_count', $spec);
  592. }
  593. }
  594. /**
  595. * Data parsing helper function for tmgmt_update_7006().
  596. *
  597. * Copied from TMGMTJobItemController::count().
  598. *
  599. * @param $item
  600. * The current data item.
  601. * @param $entity
  602. * The job item the count should be calculated.
  603. */
  604. function _tmgmt_data_count_7006($item, $entity) {
  605. if (!empty($item['#text'])) {
  606. if (_tmgmt_filter_data($item)) {
  607. module_load_include('module', 'tmgmt');
  608. // Count words of the data item.
  609. $entity->word_count += tmgmt_word_count($item['#text']);
  610. }
  611. }
  612. else {
  613. foreach (element_children($item) as $key) {
  614. _tmgmt_data_count_7006($item[$key], $entity);
  615. }
  616. }
  617. }
  618. /**
  619. * Set word count for existing job items.
  620. */
  621. function tmgmt_update_7006(&$sandbox) {
  622. if (!isset($sandbox['progress'])) {
  623. $sandbox['progress'] = 0;
  624. $sandbox['last_tjiid'] = 0;
  625. $sandbox['max'] = db_query('SELECT COUNT(tjiid) FROM {tmgmt_job_item}')->fetchField();
  626. }
  627. $result = db_query('SELECT tjiid, data FROM {tmgmt_job_item} WHERE tjiid > :last_tjiid ORDER BY tjiid LIMIT 10', array(':last_tjiid' => $sandbox['last_tjiid']));
  628. foreach ($result as $row) {
  629. // Unseralize data, count it and then save the counters.
  630. if (!empty($row->data)) {
  631. $fake_job_item = (object)array(
  632. 'word_count' => 0,
  633. );
  634. _tmgmt_data_count_7006(unserialize($row->data), $fake_job_item);
  635. db_update('tmgmt_job_item')
  636. ->condition('tjiid', $row->tjiid)
  637. ->fields((array)$fake_job_item)
  638. ->execute();
  639. }
  640. $sandbox['progress']++;
  641. }
  642. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  643. if ($row) {
  644. $sandbox['last_tjiid'] = $row->tjiid;
  645. }
  646. }
  647. /**
  648. * Removing the tmgmt_auto_accept variable.
  649. */
  650. function tmgmt_update_7007() {
  651. variable_del('tmgmt_auto_accept');
  652. }
  653. /**
  654. * Introduce the reviewed counter and rename approved.
  655. */
  656. function tmgmt_update_7008() {
  657. // Rename approved to reviewed to simplify the upgrade path.
  658. db_change_field('tmgmt_job_item', 'count_approved', 'count_reviewed', array(
  659. 'description' => 'Counter for all reviewed data items.',
  660. 'type' => 'int',
  661. 'not null' => TRUE,
  662. 'default' => 0,
  663. ));
  664. // Add the accepted counter.
  665. db_add_field('tmgmt_job_item', 'count_accepted', array(
  666. 'description' => 'Counter for all accepted data items.',
  667. 'type' => 'int',
  668. 'not null' => TRUE,
  669. 'default' => 0,
  670. ));
  671. }
  672. /**
  673. * Update data item status of accepted job items to accepted.
  674. */
  675. function tmgmt_update_7009() {
  676. // First set count_accepted to the value of count_reviewed.
  677. db_update('tmgmt_job_item')
  678. ->expression('count_accepted', 'count_reviewed + count_pending + count_translated')
  679. ->condition('state', TMGMT_JOB_ITEM_STATE_ACCEPTED)
  680. ->execute();
  681. // Then set count_reviewed to 0.
  682. db_update('tmgmt_job_item')
  683. ->fields(array(
  684. 'count_reviewed' => 0,
  685. 'count_translated' => 0,
  686. 'count_pending' => 0,
  687. ))
  688. ->condition('state', TMGMT_JOB_ITEM_STATE_ACCEPTED)
  689. ->execute();
  690. }
  691. /**
  692. * Create tmgmt_remote table to provide generic functionality for remote job
  693. * mappings.
  694. */
  695. function tmgmt_update_7010() {
  696. db_create_table('tmgmt_remote', array(
  697. 'description' => 'TMGMT job remote mapping.',
  698. 'fields' => array(
  699. 'trid' => array(
  700. 'description' => 'The primary key.',
  701. 'type' => 'serial',
  702. 'not null' => TRUE,
  703. ),
  704. 'tjid' => array(
  705. 'type' => 'int',
  706. 'not null' => TRUE,
  707. 'default' => 0,
  708. 'description' => '{tmgmt_job}.tjid foreign key',
  709. ),
  710. 'tjiid' => array(
  711. 'type' => 'int',
  712. 'not null' => TRUE,
  713. 'default' => 0,
  714. 'description' => '{tmgmt_job_item}.tjjid foreign key',
  715. ),
  716. 'data_item_key' => array(
  717. 'type' => 'varchar',
  718. 'length' => 255,
  719. 'not null' => TRUE,
  720. 'default' => '',
  721. 'description' => 'Translation job data item key.',
  722. ),
  723. 'remote_identifier_1' => array(
  724. 'type' => 'varchar',
  725. 'length' => 255,
  726. 'not null' => TRUE,
  727. 'default' => '',
  728. 'description' => 'Custom remote identifier data.',
  729. ),
  730. 'remote_identifier_2' => array(
  731. 'type' => 'varchar',
  732. 'length' => 255,
  733. 'not null' => TRUE,
  734. 'default' => '',
  735. 'description' => 'Custom remote identifier data.',
  736. ),
  737. 'remote_identifier_3' => array(
  738. 'type' => 'varchar',
  739. 'length' => 255,
  740. 'not null' => TRUE,
  741. 'default' => '',
  742. 'description' => 'Custom remote identifier data.',
  743. ),
  744. 'remote_url' => array(
  745. 'type' => 'varchar',
  746. 'length' => 255,
  747. 'not null' => TRUE,
  748. 'default' => '',
  749. 'description' => 'Remote job url.',
  750. ),
  751. 'word_count' => array(
  752. 'type' => 'int',
  753. 'not null' => TRUE,
  754. 'default' => 0,
  755. 'description' => 'Word count info provided by the remote service.',
  756. ),
  757. 'remote_data' => array(
  758. 'description' => 'Custom remote data.',
  759. 'type' => 'text',
  760. 'size' => 'big',
  761. 'serialize' => TRUE,
  762. ),
  763. ),
  764. 'primary key' => array('trid'),
  765. 'indexes' => array(
  766. 'tjid' => array('tjid'),
  767. 'tjiid' => array('tjiid'),
  768. 'remote_identifiers' => array('remote_identifier_1', 'remote_identifier_2', 'remote_identifier_3'),
  769. ),
  770. ));
  771. }
  772. /**
  773. * Add uid field for {tmgmt_message} table.
  774. */
  775. function tmgmt_update_7011() {
  776. db_add_field('tmgmt_message', 'uid', array(
  777. 'description' => 'The identifier of the user who performed the action.',
  778. 'type' => 'int',
  779. 'not null' => TRUE,
  780. 'default' => 0,
  781. ));
  782. }
  783. /**
  784. * Shortens remote_identifier_1/2 fields and recreates index for remote
  785. * identifier fields to include only the first two.
  786. */
  787. function tmgmt_update_7012() {
  788. db_drop_index('tmgmt_remote', 'remote_identifiers');
  789. db_change_field('tmgmt_remote', 'remote_identifier_1', 'remote_identifier_1', array(
  790. 'type' => 'varchar',
  791. 'length' => 127,
  792. 'not null' => TRUE,
  793. 'default' => '',
  794. 'description' => 'Custom remote identifier data.',
  795. ));
  796. db_change_field('tmgmt_remote', 'remote_identifier_2', 'remote_identifier_2', array(
  797. 'type' => 'varchar',
  798. 'length' => 127,
  799. 'not null' => TRUE,
  800. 'default' => '',
  801. 'description' => 'Custom remote identifier data.',
  802. ));
  803. db_add_index('tmgmt_remote', 'remote_identifiers', array('remote_identifier_1', 'remote_identifier_2'));
  804. }
  805. /**
  806. * Shortens reference and label fields in {tmgmt_job} to length 255.
  807. *
  808. * Avoids issues with InnoDB and UTF-8 key limits.
  809. */
  810. function tmgmt_update_7013() {
  811. db_drop_index('tmgmt_job', 'reference');
  812. db_change_field('tmgmt_job', 'reference', 'reference', array(
  813. 'description' => 'Remote identifier of this translation job.',
  814. 'type' => 'varchar',
  815. 'length' => 255,
  816. ));
  817. db_change_field('tmgmt_job', 'label', 'label', array(
  818. 'description' => 'Optional user provided label of the job.',
  819. 'type' => 'varchar',
  820. 'length' => 255,
  821. ));
  822. db_add_index('tmgmt_job', 'reference', array('reference'));
  823. }
  824. /**
  825. * Add amount and currency fields for {tmgmt_remote} table.
  826. */
  827. function tmgmt_update_7014() {
  828. db_add_field('tmgmt_remote', 'amount', array(
  829. 'type' => 'int',
  830. 'not null' => TRUE,
  831. 'default' => 0,
  832. 'description' => 'Amount charged for the remote translation job.',
  833. ));
  834. db_add_field('tmgmt_remote', 'currency', array(
  835. 'type' => 'char',
  836. 'length' => 3,
  837. 'not null' => TRUE,
  838. 'default' => '',
  839. 'description' => 'Amount charged currency.',
  840. ));
  841. }