pathauto.pathauto.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * @file
  4. * Pathauto integration for core modules.
  5. *
  6. * @ingroup pathauto
  7. */
  8. /**
  9. * Implements hook_path_alias_types().
  10. *
  11. * Used primarily by the bulk delete form.
  12. */
  13. function pathauto_path_alias_types() {
  14. $objects['user/'] = t('Users');
  15. $objects['node/'] = t('Content');
  16. if (module_exists('blog')) {
  17. $objects['blog/'] = t('User blogs');
  18. }
  19. if (module_exists('taxonomy')) {
  20. $objects['taxonomy/term/'] = t('Taxonomy terms');
  21. }
  22. if (module_exists('forum')) {
  23. $objects['forum/'] = t('Forums');
  24. }
  25. return $objects;
  26. }
  27. /**
  28. * Implements hook_pathauto().
  29. *
  30. * This function is empty so that the other core module implementations can be
  31. * defined in this file. This is because in pathauto_module_implements_alter()
  32. * we add pathauto to be included first. The module system then peforms a
  33. * check on any subsequent run if this function still exists. If this does not
  34. * exist, than this file will not get included and the core implementations
  35. * will never get run.
  36. *
  37. * @see pathauto_module_implements_alter().
  38. */
  39. function pathauto_pathauto() {
  40. // Empty hook; see the above comment.
  41. }
  42. /**
  43. * Implements hook_pathauto().
  44. */
  45. function node_pathauto($op) {
  46. switch ($op) {
  47. case 'settings':
  48. $settings = array();
  49. $settings['module'] = 'node';
  50. $settings['token_type'] = 'node';
  51. $settings['groupheader'] = t('Content paths');
  52. $settings['patterndescr'] = t('Default path pattern (applies to all content types with blank patterns below)');
  53. $settings['patterndefault'] = 'content/[node:title]';
  54. $settings['batch_update_callback'] = 'node_pathauto_bulk_update_batch_process';
  55. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  56. $languages = array();
  57. if (module_exists('locale')) {
  58. $languages = array(LANGUAGE_NONE => t('language neutral')) + locale_language_list('name');
  59. }
  60. foreach (node_type_get_names() as $node_type => $node_name) {
  61. if (count($languages) && variable_get('language_content_type_' . $node_type, 0)) {
  62. $settings['patternitems'][$node_type] = t('Default path pattern for @node_type (applies to all @node_type content types with blank patterns below)', array('@node_type' => $node_name));
  63. foreach ($languages as $lang_code => $lang_name) {
  64. $settings['patternitems'][$node_type . '_' . $lang_code] = t('Pattern for all @language @node_type paths', array('@node_type' => $node_name, '@language' => $lang_name));
  65. }
  66. }
  67. else {
  68. $settings['patternitems'][$node_type] = t('Pattern for all @node_type paths', array('@node_type' => $node_name));
  69. }
  70. }
  71. return (object) $settings;
  72. default:
  73. break;
  74. }
  75. }
  76. /**
  77. * Batch processing callback; Generate aliases for nodes.
  78. */
  79. function node_pathauto_bulk_update_batch_process(&$context) {
  80. if (!isset($context['sandbox']['current'])) {
  81. $context['sandbox']['count'] = 0;
  82. $context['sandbox']['current'] = 0;
  83. }
  84. $query = db_select('node', 'n');
  85. $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source");
  86. $query->addField('n', 'nid');
  87. $query->isNull('ua.source');
  88. $query->condition('n.nid', $context['sandbox']['current'], '>');
  89. $query->orderBy('n.nid');
  90. $query->addTag('pathauto_bulk_update');
  91. $query->addMetaData('entity', 'node');
  92. // Get the total amount of items to process.
  93. if (!isset($context['sandbox']['total'])) {
  94. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  95. // If there are no nodes to update, the stop immediately.
  96. if (!$context['sandbox']['total']) {
  97. $context['finished'] = 1;
  98. return;
  99. }
  100. }
  101. $query->range(0, 25);
  102. $nids = $query->execute()->fetchCol();
  103. pathauto_node_update_alias_multiple($nids, 'bulkupdate');
  104. $context['sandbox']['count'] += count($nids);
  105. $context['sandbox']['current'] = max($nids);
  106. $context['message'] = t('Updated alias for node @nid.', array('@nid' => end($nids)));
  107. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  108. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  109. }
  110. }
  111. /**
  112. * Implements hook_pathauto().
  113. */
  114. function taxonomy_pathauto($op) {
  115. switch ($op) {
  116. case 'settings':
  117. $settings = array();
  118. $settings['module'] = 'taxonomy_term';
  119. $settings['token_type'] = 'term';
  120. $settings['groupheader'] = t('Taxonomy term paths');
  121. $settings['patterndescr'] = t('Default path pattern (applies to all vocabularies with blank patterns below)');
  122. $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
  123. $settings['batch_update_callback'] = 'taxonomy_pathauto_bulk_update_batch_process';
  124. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  125. $vocabularies = taxonomy_get_vocabularies();
  126. if (count($vocabularies)) {
  127. $settings['patternitems'] = array();
  128. foreach ($vocabularies as $vid => $vocabulary) {
  129. if ($vid == variable_get('forum_nav_vocabulary', '')) {
  130. // Skip the forum vocabulary.
  131. continue;
  132. }
  133. $settings['patternitems'][$vocabulary->machine_name] = t('Pattern for all %vocab-name paths', array('%vocab-name' => $vocabulary->name));
  134. }
  135. }
  136. return (object) $settings;
  137. default:
  138. break;
  139. }
  140. }
  141. /**
  142. * Batch processing callback; Generate aliases for taxonomy terms.
  143. */
  144. function taxonomy_pathauto_bulk_update_batch_process(&$context) {
  145. if (!isset($context['sandbox']['current'])) {
  146. $context['sandbox']['count'] = 0;
  147. $context['sandbox']['current'] = 0;
  148. }
  149. $query = db_select('taxonomy_term_data', 'td');
  150. $query->leftJoin('url_alias', 'ua', "CONCAT('taxonomy/term/', td.tid) = ua.source");
  151. $query->addField('td', 'tid');
  152. $query->isNull('ua.source');
  153. $query->condition('td.tid', $context['sandbox']['current'], '>');
  154. // Exclude the forums terms.
  155. if ($forum_vid = variable_get('forum_nav_vocabulary', '')) {
  156. $query->condition('td.vid', $forum_vid, '<>');
  157. }
  158. $query->orderBy('td.tid');
  159. $query->addTag('pathauto_bulk_update');
  160. $query->addMetaData('entity', 'taxonomy_term');
  161. // Get the total amount of items to process.
  162. if (!isset($context['sandbox']['total'])) {
  163. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  164. // If there are no nodes to update, the stop immediately.
  165. if (!$context['sandbox']['total']) {
  166. $context['finished'] = 1;
  167. return;
  168. }
  169. }
  170. $query->range(0, 25);
  171. $tids = $query->execute()->fetchCol();
  172. pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
  173. $context['sandbox']['count'] += count($tids);
  174. $context['sandbox']['current'] = max($tids);
  175. $context['message'] = t('Updated alias for term @tid.', array('@tid' => end($tids)));
  176. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  177. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  178. }
  179. }
  180. /**
  181. * Implements hook_pathauto() for forum module.
  182. */
  183. function forum_pathauto($op) {
  184. switch ($op) {
  185. case 'settings':
  186. $settings = array();
  187. $settings['module'] = 'forum';
  188. $settings['token_type'] = 'term';
  189. $settings['groupheader'] = t('Forum paths');
  190. $settings['patterndescr'] = t('Pattern for forums and forum containers');
  191. $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
  192. $settings['batch_update_callback'] = 'forum_pathauto_bulk_update_batch_process';
  193. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  194. return (object) $settings;
  195. default:
  196. break;
  197. }
  198. }
  199. /**
  200. * Batch processing callback; Generate aliases for forums.
  201. */
  202. function forum_pathauto_bulk_update_batch_process(&$context) {
  203. if (!isset($context['sandbox']['current'])) {
  204. $context['sandbox']['count'] = 0;
  205. $context['sandbox']['current'] = 0;
  206. }
  207. $query = db_select('taxonomy_term_data', 'td');
  208. $query->leftJoin('url_alias', 'ua', "CONCAT('forum/', td.tid) = ua.source");
  209. $query->addField('td', 'tid');
  210. $query->isNull('ua.source');
  211. $query->condition('td.tid', $context['sandbox']['current'], '>');
  212. $query->condition('td.vid', variable_get('forum_nav_vocabulary', ''));
  213. $query->orderBy('td.tid');
  214. $query->addTag('pathauto_bulk_update');
  215. $query->addMetaData('entity', 'taxonomy_term');
  216. // Get the total amount of items to process.
  217. if (!isset($context['sandbox']['total'])) {
  218. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  219. // If there are no nodes to update, the stop immediately.
  220. if (!$context['sandbox']['total']) {
  221. $context['finished'] = 1;
  222. return;
  223. }
  224. }
  225. $query->range(0, 25);
  226. $tids = $query->execute()->fetchCol();
  227. pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
  228. $context['sandbox']['count'] += count($tids);
  229. $context['sandbox']['current'] = max($tids);
  230. $context['message'] = t('Updated alias for forum @tid.', array('@tid' => end($tids)));
  231. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  232. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  233. }
  234. }
  235. /**
  236. * Implements hook_pathauto().
  237. */
  238. function user_pathauto($op) {
  239. switch ($op) {
  240. case 'settings':
  241. $settings = array();
  242. $settings['module'] = 'user';
  243. $settings['token_type'] = 'user';
  244. $settings['groupheader'] = t('User paths');
  245. $settings['patterndescr'] = t('Pattern for user account page paths');
  246. $settings['patterndefault'] = 'users/[user:name]';
  247. $settings['batch_update_callback'] = 'user_pathauto_bulk_update_batch_process';
  248. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  249. return (object) $settings;
  250. default:
  251. break;
  252. }
  253. }
  254. /**
  255. * Batch processing callback; Generate aliases for users.
  256. */
  257. function user_pathauto_bulk_update_batch_process(&$context) {
  258. if (!isset($context['sandbox']['current'])) {
  259. $context['sandbox']['count'] = 0;
  260. $context['sandbox']['current'] = 0;
  261. }
  262. $query = db_select('users', 'u');
  263. $query->leftJoin('url_alias', 'ua', "CONCAT('user/', u.uid) = ua.source");
  264. $query->addField('u', 'uid');
  265. $query->isNull('ua.source');
  266. $query->condition('u.uid', $context['sandbox']['current'], '>');
  267. $query->orderBy('u.uid');
  268. $query->addTag('pathauto_bulk_update');
  269. $query->addMetaData('entity', 'user');
  270. // Get the total amount of items to process.
  271. if (!isset($context['sandbox']['total'])) {
  272. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  273. // If there are no nodes to update, the stop immediately.
  274. if (!$context['sandbox']['total']) {
  275. $context['finished'] = 1;
  276. return;
  277. }
  278. }
  279. $query->range(0, 25);
  280. $uids = $query->execute()->fetchCol();
  281. pathauto_user_update_alias_multiple($uids, 'bulkupdate', array('alias blog' => FALSE));
  282. $context['sandbox']['count'] += count($uids);
  283. $context['sandbox']['current'] = max($uids);
  284. $context['message'] = t('Updated alias for user @uid.', array('@uid' => end($uids)));
  285. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  286. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  287. }
  288. }
  289. /**
  290. * Implements hook_pathauto().
  291. */
  292. function blog_pathauto($op) {
  293. switch ($op) {
  294. case 'settings':
  295. $settings = array();
  296. $settings['module'] = 'blog';
  297. $settings['token_type'] = 'user';
  298. $settings['groupheader'] = t('Blog paths');
  299. $settings['patterndescr'] = t('Pattern for blog page paths');
  300. $settings['patterndefault'] = 'blogs/[user:name]';
  301. $settings['batch_update_callback'] = 'blog_pathauto_bulk_update_batch_process';
  302. $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
  303. return (object) $settings;
  304. default:
  305. break;
  306. }
  307. }
  308. /**
  309. * Batch processing callback; Generate aliases for blogs.
  310. */
  311. function blog_pathauto_bulk_update_batch_process(&$context) {
  312. if (!isset($context['sandbox']['current'])) {
  313. $context['sandbox']['count'] = 0;
  314. $context['sandbox']['current'] = 0;
  315. }
  316. $query = db_select('users', 'u');
  317. $query->leftJoin('url_alias', 'ua', "CONCAT('blog/', u.uid) = ua.source");
  318. $query->addField('u', 'uid');
  319. $query->isNull('ua.source');
  320. $query->condition('u.uid', $context['sandbox']['current'], '>');
  321. $query->orderBy('u.uid');
  322. $query->addTag('pathauto_bulk_update');
  323. $query->addMetaData('entity', 'user');
  324. // Get the total amount of items to process.
  325. if (!isset($context['sandbox']['total'])) {
  326. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  327. // If there are no nodes to update, the stop immediately.
  328. if (!$context['sandbox']['total']) {
  329. $context['finished'] = 1;
  330. return;
  331. }
  332. }
  333. $query->range(0, 25);
  334. $uids = $query->execute()->fetchCol();
  335. $accounts = user_load_multiple($uids);
  336. foreach ($accounts as $account) {
  337. pathauto_blog_update_alias($account, 'bulkupdate');
  338. }
  339. $context['sandbox']['count'] += count($uids);
  340. $context['sandbox']['current'] = max($uids);
  341. $context['message'] = t('Updated alias for blog user @uid.', array('@uid' => end($uids)));
  342. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  343. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  344. }
  345. }