Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ## OpenCart Automatic Category/Product/Info Page Names to SEO-Friendly Keywords
- ## Version 1.6
- ## Written by David O'Neill on August 11th, 2014
- ## Updated on August 20th, 2014
- ## Changelog ##
- ## ========= ##
- ## 1.6:
- ## * Tidied the code and cut the amount of repetition by implementing multidimensional arrays and loops
- ##
- ## 1.5:
- ## * Prepared statements are now being used
- ## * Better error handling
- ## * Ordered categories, products, and information pages by ID ascending
- ## * Clearer result report shown at the end of the execution
- ## * Minor, non-crucial bug fixes
- # Put this file in your OpenCart root directory and run it in your browser.
- # Always backup your database before running this or any other database-changing script - it's just best practice.
- # This script uses the existing Category, Product, and Information page names to assign each SEO-friendly keywords.
- # Fully-automatic and safe, and if your database tables are InnoDB, it's even safer as autocommit is set to off in the script.
- # You may run this script and any SEO-friendly keywords that are already based on the respective names will not be affected.
- # Any SEO-friendly keywords that are not based exactly on the respective names will be changed, so bear that in mind.
- # This has been tested on my own live installations of OpenCart 1.5.6.1 and 1.5.6.4 with 100% success in all scenarios.
- // Include the OpenCart configuration file to get the database constants
- include('config.php');
- // Connect to the database
- $mysqli = new MySQLi(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
- if ($mysqli->connect_errno)
- die('Error: Could not make a database link (' . $mysqli->connect_errno . ')');
- // Turn off transation autocommit
- // Only applies to OpenCart installations whose database tables have been converted to InnoDB
- // This is safe to leave alone even if using MyISAM - it just won't have any effect (commits will remain automatic)
- $mysqli->autocommit(false);
- // Query the category names and IDs
- $stmt = prepareStatement("SELECT `category_id`, `name` FROM `oc_category_description` ORDER BY `category_id` ASC");
- executeStatement($stmt);
- $stmt->bind_result($id, $name);
- while ($stmt->fetch()){
- $seoinfo['categories']['ids'][] = $id;
- $seoinfo['categories']['names'][] = html_entity_decode($name);
- $seoinfo['categories']['raw_names'][] = html_entity_decode($name);
- }
- $stmt->free_result();
- $stmt->close();
- namesToSEOKeywords($seoinfo['categories']['names']);
- // Query the product names and IDs
- $stmt = prepareStatement("SELECT `product_id`, `name` FROM `oc_product_description` ORDER BY `product_id` ASC");
- executeStatement($stmt);
- $stmt->bind_result($id, $name);
- while ($stmt->fetch()){
- $seoinfo['products']['ids'][] = $id;
- $seoinfo['products']['names'][] = html_entity_decode($name);
- $seoinfo['products']['raw_names'][] = html_entity_decode($name);
- }
- $stmt->free_result();
- $stmt->close();
- namesToSEOKeywords($seoinfo['products']['names']);
- // Query the information page names and IDs
- $stmt = prepareStatement("SELECT `information_id`, `title` FROM `oc_information_description` ORDER BY `information_id` ASC");
- executeStatement($stmt);
- $stmt->bind_result($id, $title);
- while ($stmt->fetch()){
- $seoinfo['infopages']['ids'][] = $id;
- $seoinfo['infopages']['names'][] = html_entity_decode($title);
- $seoinfo['infopages']['raw_names'][] = html_entity_decode($title);
- }
- $stmt->free_result();
- $stmt->close();
- namesToSEOKeywords($seoinfo['infopages']['names']);
- // Query the existing SEO keywords
- $stmt = prepareStatement("SELECT `query` FROM `oc_url_alias`");
- executeStatement($stmt);
- $stmt->bind_result($query);
- while ($stmt->fetch()){
- $urlalias['queries'][] = $query;
- }
- $stmt->free_result();
- $stmt->close();
- $html = "<!DOCTYPE html>\n";
- $html .= "<html lang='en-US'>\n";
- $html .= "<head>\n";
- $html .= "<title>OpenCart Automatic Category/Product/Info Page Names to SEO-Fiendly Keywords</title>\n";
- $html .= "<meta charset='utf-8'>\n";
- $html .= "</head>\n";
- $html .= "<body>\n";
- $html .= "<div style='font-family:Arial,Helvetica,sans-serif;'>\n";
- $html .= "Done!\n<br><br><br>\n";
- // INSERT or UPDATE the SEO values
- foreach ($seoinfo as $key => $value){
- if ($key == "categories"){
- $querytype = "category_id=";
- $seotype = "Category";
- } elseif ($key == "products"){
- $querytype = "product_id=";
- $seotype = "Product";
- } else{
- $querytype = "information_id=";
- $seotype = "Information page";
- }
- foreach ($seoinfo[$key]['ids'] as $key2 => $value2){
- $query = $querytype . $value2;
- if (isset($urlalias['queries']) && array_search($query, $urlalias['queries']) !== false){
- $stmt = prepareStatement("SELECT `keyword` FROM `oc_url_alias` WHERE `query` = ? LIMIT 1");
- bindParams('s', $query);
- executeStatement($stmt);
- $stmt->bind_result($keyword);
- $stmt->fetch();
- $stmt->free_result();
- $stmt->close();
- if ($keyword == $seoinfo[$key]['names'][$key2]){
- $seoinfo[$key]['old_names'][] = "none";
- $seoinfo[$key]['task'][] = "none";
- } else{
- $stmt = prepareStatement("UPDATE `oc_url_alias` SET `keyword` = ? WHERE `query` = ? LIMIT 1");
- bindParams('ss', $seoinfo[$key]['names'][$key2], $query);
- executeStatement($stmt);
- $stmt->close();
- $seoinfo[$key]['old_names'][] = $keyword;
- $seoinfo[$key]['task'][] = 'update';
- }
- } else{
- $stmt = prepareStatement("INSERT INTO `oc_url_alias` (`query`, `keyword`) VALUES (?, ?)");
- bindParams('ss', $query, $seoinfo[$key]['names'][$key2]);
- executeStatement($stmt);
- $stmt->close();
- $seoinfo[$key]['old_names'][] = "none";
- $seoinfo[$key]['task'][] = "insert";
- }
- }
- // Note the SEO values added and changed
- $html .= "<table style='min-width:50%;border:0;'>\n";
- $html .= "<tr>\n<th colspan='2' style='text-align:left;'><b>" . $seotype . " SEO keywords added ("
- . $sizeof = sizeof(array_keys($seoinfo[$key]['task'], 'insert')) .
- "):</b></th>\n</tr>\n";
- if ($sizeof > 0){
- foreach ($seoinfo[$key]['names'] as $key2 => $value2)
- if ($seoinfo[$key]['task'][$key2] == 'insert')
- $html .= "<tr>\n<td style='padding-right:10px;'>"
- . $seoinfo[$key]['raw_names'][$key2] .
- "</td>\n<td>= "
- . $value2 .
- "</td>\n</tr>\n";
- } else{
- $html .= "<tr>\n<td>None</td>\n<td></td>\n</tr>\n";
- }
- $html .= "</table>\n<br>\n";
- $html .= "<table style='min-width:50%;border:0;'>\n";
- $html .= "<tr>\n<th colspan='2' style='text-align:left;'><b>" . $seotype . " SEO keywords changed ("
- . $sizeof = sizeof(array_keys($seoinfo[$key]['task'], 'update')) .
- "):</b></th>\n</tr>\n";
- if ($sizeof > 0){
- foreach ($seoinfo[$key]['names'] as $key2 => $value2)
- if ($seoinfo[$key]['task'][$key2] == 'update')
- $html .= "<tr>\n<td style='padding-right:10px;'>"
- . $seoinfo[$key]['raw_names'][$key2] .
- "</td>\n<td>= "
- . $seoinfo[$key]['old_names'][$key2] . " <b>-></b> " . $value2 .
- "</td>\n</tr>\n";
- } else{
- $html .= "<tr>\n<td>None</td>\n<td></td>\n</tr>\n";
- }
- $html .= "</table>\n<br>\n";
- }
- $html .= "</div>\n";
- $html .= "</body>\n";
- $html .= "</html>";
- // Commit the changes
- // Only applies to OpenCart installations whose database tables have been converted to InnoDB
- // This is safe to leave alone even if using MyISAM - it just won't have any effect (commits will remain automatic)
- if (!$mysqli->commit())
- die('Transaction failed - no changes have been committed. Please try again.');
- // Close the database connection
- $mysqli->close();
- // All good? You're done!
- // Let's echo out the changes
- echo $html;
- // Convert the names to lowercase, strip symbols, and put hyphens in the place of spaces
- function namesToSEOKeywords(&$names){
- foreach ($names as &$value){
- $value = strtolower($value);
- $value = preg_replace('/&/', 'and', $value);
- $value = trim(preg_replace('/[^A-Za-z0-9\']/', ' ', $value));
- $value = preg_replace('/\'/', '', $value);
- $value = preg_replace('/\s+/', '-', $value);
- }
- }
- // Prepare the MySQLi statement, return the result, and die on error
- function prepareStatement($query){
- global $mysqli;
- if (!$stmt = $mysqli->prepare($query)){
- // Die and display the error
- // OpenCart installations whose database tables have been converted to InnoDB
- // will take advantage of not having changes committed at this point
- $error = $mysqli->error;
- $mysqli->close();
- die('mysqli::prepare(): ' . $error);
- }
- return $stmt;
- }
- // Bind MySQLi paramters and die on error
- // Variadic function whose arguments should be in the order of datatypes, param1, param2, etc. - the same as mysqli_stmt::bind_param()
- function bindParams(){
- global $mysqli, $stmt;
- $args = func_get_args();
- // Convert the arguments to references from values to avoid an error with mysqli_stmt::bind_param()
- foreach ($args as $key => $value)
- $args[$key] = &$args[$key];
- if (!call_user_func_array(array($stmt, 'bind_param'), $args)){
- // Die and display the error
- // OpenCart installations whose database tables have been converted to InnoDB
- // will take advantage of not having changes committed at this point
- $stmt->close();
- $mysqli->close();
- die('mysqli_stmt::bind_param(): failed');
- }
- }
- // Execute the MySQLi statement and die on error
- function executeStatement(&$stmt){
- global $mysqli;
- if (!$stmt->execute()){
- // Die and display the error
- // OpenCart installations whose database tables have been converted to InnoDB
- // will take advantage of not having changes committed at this point
- $error = $stmt->error;
- $stmt->close();
- $mysqli->close();
- die('mysqli_stmt::execute(): ' . $error);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement