update-search-views.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is addons.mozilla.org site.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * The Mozilla Foundation.
  19. * Portions created by the Initial Developer are Copyright (C) 2006
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Chris Pollett (cpollett@gmail.com)
  24. * Mike Morgan <morgamic@mozilla.com>
  25. * Justin Scott <fligtar@gmail.com>
  26. *
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either the GNU General Public License Version 2 or later (the "GPL"), or
  30. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. /**
  42. * The script should be run as a cron job to periodically update the text_search_view
  43. * and versions_most_recent view in the amo database.
  44. *
  45. * This script should not ever be accessed over HTTP, and instead run via cron.
  46. * Only sysadmins should be responsible for operating this script.
  47. *
  48. * @package amo
  49. * @subpackage bin
  50. */
  51. // Before doing anything, test to see if we are calling this from the command
  52. // line. If this is being called from the web, HTTP environment variables will
  53. // be automatically set by Apache. If these are found, exit immediately.
  54. if (isset($_SERVER['HTTP_HOST'])) {
  55. exit;
  56. }
  57. // If we get here, we're on the command line, which means we can continue.
  58. // Include config file
  59. require_once('../site/app/config/config.php');
  60. require_once('../site/app/config/constants.php');
  61. global $valid_status;
  62. /*
  63. First, we set up an array of sql commands we will execute to update our two views that are used by search
  64. In testing it seemed faster to delete the table completely and rebuild it rather than incrementally maintain it
  65. */
  66. $sql_commands = array();
  67. $sql_commands[] = "BEGIN";
  68. $sql_commands[] = "DELETE FROM `text_search_summary`";
  69. $sql_commands[] = "INSERT INTO `text_search_summary`
  70. SELECT a.id AS id,
  71. `tr_name`.locale AS locale,
  72. a.addontype_id AS addontype,
  73. a.status AS status,
  74. a.inactive AS inactive,
  75. a.averagerating AS averagerating,
  76. a.weeklydownloads AS weeklydownloads,
  77. `tr_name`.localized_string AS name,
  78. `tr_summary`.localized_string AS summary,
  79. `tr_description`.localized_string AS description
  80. FROM addons AS a
  81. LEFT JOIN translations AS `tr_name` ON (`tr_name`.id = a.`name`)
  82. LEFT JOIN translations AS `tr_summary` ON (`tr_summary`.id = a.`summary` AND `tr_name`.locale = `tr_summary`.locale)
  83. LEFT JOIN translations AS `tr_description`
  84. ON (`tr_description`.id = a.`description` AND `tr_name`.locale = `tr_description`.locale)
  85. WHERE `tr_name`.locale IS NOT NULL AND (
  86. `tr_name`.localized_string IS NOT NULL
  87. OR `tr_summary`.localized_string IS NOT NULL
  88. OR `tr_description`.localized_string IS NOT NULL
  89. )
  90. ORDER BY a.id ASC, locale DESC;";
  91. //the purpose of the temporary table is to get the most recently created version of an addon (avoiding sub-selects which are mysql 4 bad)
  92. $sql_commands[] = "DROP TABLE IF EXISTS `most_recent_version`"; //I am being paranoid to make sure temp table does not exist (it shouldn't by below)
  93. $sql_commands[] = "CREATE TEMPORARY TABLE `most_recent_version` (
  94. `addon_id` int(11) NOT NULL,
  95. `created` DATETIME NOT NULL
  96. ) DEFAULT CHARSET=utf8";
  97. $sql_commands[] = "DELETE FROM `versions_summary`";
  98. $sql_commands[] = "INSERT INTO `most_recent_version`
  99. SELECT DISTINCT v.addon_id, MAX(v.created)
  100. FROM versions AS v
  101. INNER JOIN files AS f ON (f.version_id = v.id AND f.status IN (".implode(',',$valid_status)."))
  102. GROUP BY v.addon_id";
  103. $sql_commands[] = "INSERT INTO `versions_summary`
  104. SELECT DISTINCT v.addon_id, v.id, av.application_id, v.created, v.modified, av.min, av.max
  105. FROM (most_recent_version AS mrv NATURAL JOIN versions AS v) LEFT JOIN applications_versions AS av
  106. ON (av.version_id = v.id )";
  107. $sql_commands[] = "DROP TABLE `most_recent_version`";
  108. $sql_commands[] = "COMMIT";
  109. // Connect to our database and execute the command list above.
  110. $write = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ' . mysql_error());
  111. mysql_select_db(DB_NAME, $write) or die('Could not select database '.DB_NAME);
  112. foreach($sql_commands as $sql_command) {
  113. if(!mysql_query($sql_command)) {
  114. mysql_query("ROLLBACK");
  115. die("The update '$sql_command' failed: ".mysql_error());
  116. }
  117. }
  118. mysql_close();
  119. exit;
  120. ?>