Upfront, I have nearly no php coding skills, so please excuse if I am asking very basic questions.
I want to create a php file I can execute via cron job which creates friendly_URLs in a non-modx-database. I have a table "myuri" which has 3 columns "id" "title" "uri" (uri is default NULL)
Here is what the script should do
- connect to the db-server
- select the db
- Update all "uri"-fields that are NULL with a friendly URL created
from each title in the corresponding "title"-field
With my very basic understanding of what I am doing, I only managed to glue some code snipped mixture of php and xPDO together that is doing something, but not exaxtly what I need.
What I have sofar is a mix of modx xPDO and SQL
// [PHP/SQL] db connect
mysql_connect("hostname", "user", "password");
mysql_select_db("database");
// [xPDO] path
define('MODX_CORE_PATH', '/website/domain.tld/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
// [xPDO] Criteria for foreign Database
$host = 'hostname';
$username = 'user';
$password = 'password';
$dbname = 'database';
$port = 3306;
$charset = 'utf-8';
$dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
$xpdo = new xPDO($dsn, $username, $password);
// [PHP/SQL] Function that removes all unfriendly signs from the title example: "Hello! World" to "hello-world"
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
// [xPDO] Issue queries against the foreign database, for testing limited to a few ids instead of "WHERE id is null"
$output = '';
$sql = "SELECT * FROM myuri WHERE id BETWEEN 564780000 AND 564781000";
foreach ($xpdo->query($sql) as $row) {
$output .= $row['title'];
}
// [PHP/SQL] take the xPDO query results and run "function Slug($string)" on it (i guess this needs to go into the "foreach" somehow)
$user = $output;
$item = "test-parent-folder/".Slug($user).".html";
// [PHP/SQL] Update the column "uri", for testing limited to a few ids instead of "WHERE id is null" (this needs to go into the "foreach" too)
mysql_query("UPDATE myuri SET uri = '$item' id BETWEEN 564780000 AND 564781000");
So basically I need to do two major things.
1.) Replace all [xPDO] components with [PHP/SQL] components to be able to run it as a cron job
2.) Include the things into the for each function, here is were my php skill hits its limits
3.)(optional) From what I have read in some postings it might be clever to run this with an "implode" for make it run faster since the table has 1.000.000 rows
Would be great if anybody could help me with that.