regenerate-pages.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $sapi_type = php_sapi_name();
  24. $script_file = basename(__FILE__);
  25. $path = __DIR__.'/';
  26. // Test if batch mode
  27. if (substr($sapi_type, 0, 3) == 'cgi') {
  28. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  29. exit(-1);
  30. }
  31. @set_time_limit(0); // No timeout for this script
  32. 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".
  33. $error = 0;
  34. $mode = empty($argv[1])?'':$argv[1];
  35. $websiteref = empty($argv[2])?'':$argv[2];
  36. $max = (!isset($argv[3]) || (empty($argv[3]) && $argv[3] !== '0'))?'10':$argv[3];
  37. if (empty($argv[2]) || !in_array($argv[1], array('test', 'confirm')) || empty($websiteref)) {
  38. print '***** '.$script_file.' *****'."\n";
  39. print "Usage: $script_file (test|confirm) website [nbmaxrecord]\n";
  40. print "\n";
  41. print "Regenerate all pages of a web site.\n";
  42. exit(-1);
  43. }
  44. require $path."../../htdocs/master.inc.php";
  45. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  46. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  47. include_once DOL_DOCUMENT_ROOT.'/core/lib/website2.lib.php';
  48. $langs->load('main');
  49. $website = new Website($db);
  50. $result = $website->fetch(0, $websiteref);
  51. if ($result <= 0) {
  52. print 'Error, web site '.$websiteref.' not found'."\n";
  53. exit(-1);
  54. }
  55. $websitepagestatic = new WebsitePage($db);
  56. $db->begin();
  57. $listofpages = $websitepagestatic->fetchAll($website->id, '', $max);
  58. global $dolibarr_main_data_root;
  59. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$websiteref;
  60. $nbgenerated = 0;
  61. foreach($listofpages as $websitepage) {
  62. $filealias = $pathofwebsite.'/'.$websitepage->pageurl.'.php';
  63. $filetpl = $pathofwebsite.'/page'.$websitepage->id.'.tpl.php';
  64. if ($mode == 'confirm') {
  65. dolSavePageAlias($filealias, $website, $websitepage);
  66. dolSavePageContent($filetpl, $website, $websitepage);
  67. }
  68. print "Generation of page done - pageid = ".$websitepage->id." - ".$websitepage->pageurl."\n";
  69. $nbgenerated++;
  70. if ($max && $nbgenerated >= $max) {
  71. print 'Nb max of record ('.$max.') reached. We stop now.'."\n";
  72. break;
  73. }
  74. }
  75. if ($mode == 'confirm') {
  76. print $nbgenerated." page(s) generated into ".$pathofwebsite."\n";
  77. } else {
  78. print $nbgenerated." page(s) found but not generated (test mode)\n";
  79. }
  80. exit($error);