regenerate-pages.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file scripts/website/regenerate-pages.php
  20. * \ingroup scripts
  21. * \brief Regenerate all pages of a web site
  22. */
  23. if (!defined('NOSESSION')) {
  24. define('NOSESSION', '1');
  25. }
  26. $sapi_type = php_sapi_name();
  27. $script_file = basename(__FILE__);
  28. $path = __DIR__.'/';
  29. // Test if batch mode
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  32. exit(-1);
  33. }
  34. @set_time_limit(0); // No timeout for this script
  35. define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only".
  36. $error = 0;
  37. $mode = empty($argv[1]) ? '' : $argv[1];
  38. $websiteref = empty($argv[2]) ? '' : $argv[2];
  39. $max = (!isset($argv[3]) || (empty($argv[3]) && $argv[3] !== '0')) ? '10' : $argv[3];
  40. if (empty($argv[2]) || !in_array($argv[1], array('test', 'confirm')) || empty($websiteref)) {
  41. print '***** '.$script_file.' *****'."\n";
  42. print "Usage: $script_file (test|confirm) website [nbmaxrecord]\n";
  43. print "\n";
  44. print "Regenerate all pages of a web site.\n";
  45. exit(-1);
  46. }
  47. require $path."../../htdocs/master.inc.php";
  48. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  49. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  50. include_once DOL_DOCUMENT_ROOT.'/core/lib/website2.lib.php';
  51. /*
  52. * Main
  53. */
  54. $langs->load('main');
  55. if (!empty($dolibarr_main_db_readonly)) {
  56. print "Error: instance in read-onyl mode\n";
  57. exit(-1);
  58. }
  59. $website = new Website($db);
  60. $result = $website->fetch(0, $websiteref);
  61. if ($result <= 0) {
  62. print 'Error, web site '.$websiteref.' not found'."\n";
  63. exit(-1);
  64. }
  65. $websitepagestatic = new WebsitePage($db);
  66. $db->begin();
  67. $listofpages = $websitepagestatic->fetchAll($website->id, '', '', $max);
  68. global $dolibarr_main_data_root;
  69. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$websiteref;
  70. $nbgenerated = 0;
  71. foreach ($listofpages as $websitepage) {
  72. $filealias = $pathofwebsite.'/'.$websitepage->pageurl.'.php';
  73. $filetpl = $pathofwebsite.'/page'.$websitepage->id.'.tpl.php';
  74. if ($mode == 'confirm') {
  75. dolSavePageAlias($filealias, $website, $websitepage);
  76. dolSavePageContent($filetpl, $website, $websitepage);
  77. }
  78. print "Generation of page done - pageid = ".$websitepage->id." - ".$websitepage->pageurl."\n";
  79. $nbgenerated++;
  80. if ($max && $nbgenerated >= $max) {
  81. print 'Nb max of record ('.$max.') reached. We stop now.'."\n";
  82. break;
  83. }
  84. }
  85. if ($mode == 'confirm') {
  86. print $nbgenerated." page(s) generated into ".$pathofwebsite."\n";
  87. } else {
  88. print $nbgenerated." page(s) found but not generated (test mode)\n";
  89. }
  90. exit($error);