function update_attachments_table() { // NOTE: It should be harmless to run this function multiple times // Get the existing field names $db =& JFactory::getDBO(); $query = "explain #__attachments"; $db->setQuery($query); $rows = $db->loadObjectList(); $fields = array(); foreach ($rows as $row) { $fields[] = $row->Field; } // Define the columns/fields that may need to be added $new_fields = array(); $new_fields['display_filename'] = "ALTER TABLE #__attachments ADD `display_filename` VARCHAR(80) NOT NULL DEFAULT ''"; $new_fields['user_field_1'] = "ALTER TABLE #__attachments ADD `user_field_1` VARCHAR(100) NOT NULL DEFAULT ''"; $new_fields['user_field_2'] = "ALTER TABLE #__attachments ADD `user_field_2` VARCHAR(100) NOT NULL DEFAULT ''"; $new_fields['user_field_3'] = "ALTER TABLE #__attachments ADD `user_field_3` VARCHAR(100) NOT NULL DEFAULT ''"; $new_fields['create_date'] = "ALTER TABLE #__attachments ADD `create_date` DATETIME DEFAULT NULL"; $new_fields['modification_date'] = "ALTER TABLE #__attachments ADD `modification_date` DATETIME DEFAULT NULL"; $new_fields['download_count'] = "ALTER TABLE #__attachments ADD `download_count` INT(11) UNSIGNED DEFAULT '0'"; // See if we need to add specific fields $num_fields_added = 0; $dates_added = false; foreach ($new_fields as $field => $query) { if (!in_array($field, $fields)) { echo " Adding missing column '{$field}'<br>"; $db->setQuery($query); if (!$db->query()) { JError::raiseError(500, "Error installing field " . $field . ": " . $db->stderr()); return false; } if ($field == 'create_date') { $dates_added = true; } $num_fields_added++; } } if ($num_fields_added != 0) { echo ""; } if ($dates_added) { echo " Adding missing create/modification dates (using date each attachment file was created). <br>"; require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_attachments' . DS . 'update.php'; $num = AttachmentsUpdate::update_null_dates(); if ($num == 0) { echo " No attachments needed updating<br>"; } elseif ($num == 1) { echo " 1 attachment updated<br>"; } else { echo " {$num} attachments updated<br>"; } } // Get the existing indexed column names $query = "show index from #__attachments"; $db->setQuery($query); $rows = $db->loadObjectList(); $indexes = array(); foreach ($rows as $row) { $indexes[] = $row->Key_name; } // Define the indexes that may need to get added $new_indexes = array(); $new_indexes['attachment_article_id_index'] = "CREATE INDEX attachment_article_id_index ON `#__attachments` (`article_id`)"; /* These indeces do not add any benefit See: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html $new_indexes['attachment_filename_index'] = "CREATE INDEX attachment_filename_index ON `#__attachments` (`filename`)"; $new_indexes['attachment_file_size_index'] = "CREATE INDEX attachment_file_size_index ON `#__attachments` (`file_size`)"; $new_indexes['attachment_create_date_index'] = "CREATE INDEX attachment_create_date_index ON `#__attachments` (`create_date`)"; $new_indexes['attachment_modification_date_index'] = "CREATE INDEX attachment_modification_date_index ON `#__attachments` (`modification_date`)"; */ // Add any missing indexes echo "<br>"; $num_indexes_added = 0; foreach ($new_indexes as $index => $query) { if (!in_array($index, $indexes)) { echo " Adding index '{$index}'<br>"; $db->setQuery($query); if (!$db->query()) { JError::raiseError(500, "Error installing index " . $index . ": " . $db->stderr()); return false; } $num_indexes_added++; } } echo "<h3>The attachments table is now up to date. <h3>\n"; return true; }
/** * Update any null dates in any attachments * (See AttachmentsUpdate::update_null_dates() in update.php for details ) */ public function update_null_dates() { // Access check. if (!JFactory::getUser()->authorise('core.admin', 'com_attachments')) { return JError::raiseError(404, JText::_('JERROR_ALERTNOAUTHOR') . ' (ERR 152)'); } require_once JPATH_ADMINISTRATOR . '/components/com_attachments/update.php'; $numUpdated = AttachmentsUpdate::update_null_dates(); $msg = JText::sprintf('ATTACH_UPDATED_N_ATTACHMENTS', $numUpdated); $this->setRedirect('index.php?option=' . $this->option, $msg); }
function update_null_dates($redirect = true) { global $option; require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_attachments' . DS . 'update.php'; $numUpdated = AttachmentsUpdate::update_null_dates(); $msg = JText::sprintf('UPDATED N ATTACHMENTS', $numUpdated); $this->setRedirect('index.php?option=' . $option, $msg); }