17. 7. 2012

Batch update URL aliases in Joomla 2.5

Jak nastavit spravne URL aliasy pro existujici zaznamy v databazi.

Zjistil jsem, ze Joomla neumi odstranit diakritiku z nazvu tak aby friendly URL bylo spravne a č nebylo smazano. Nenasel jsem zadny nastroj, ktery by to dokazal opravit nad existujicimi zaznamy, tak jsem si napsal malinkej PHP script.

Prepiste si ho tak aby jste mohli parametry zadavat treba v konzoli, tohle byla jednorazovka.


mb_internal_encoding('UTF-8');


$conn = mysql_connect('localhost','xxxxx','xxxxxxx');
mysql_select_db('xxxxxx');
mysql_query('set names utf8');


$result = mysql_query('select * from j25_content order by id desc');
while ( $record = mysql_fetch_assoc($result) ) {
    $found = mysql_query('select count(*) matched from j25_content where `alias`="'.addslashes(seoUrl($record['title'])).'" and id<>'.$record['id']);
    $foundM = mysql_fetch_assoc($found);
    $alias = seoUrl($record['title']);
    if ($foundM['matched']>1)
    {
        $duplicates++;
        $alias.="-".(intval($found['matched'])+1);
        echo ('[!!] ' . $record['alias']. 'would be duplicit, changed to: '.$alias."\n");
    }
    if ($record['alias']!=$alias) {
        echo ('[++] ' . $record['alias']. ' changed to '.$alias.' from title "'.$record['title'].'"'."\n");
        mysql_query('update j25_content set alias="'.$alias.'" where id='.$record['id']);
        $modified++;
    } else {
        $correct++;
    }
       
}
$result = mysql_query('select * from j25_menu order by id');
while ( $record = mysql_fetch_assoc($result) ) {
    $found = mysql_query('select count(*) matched from j25_menu where `link`="'.addslashes(seoUrl($record['title'])).'" and id<>'.$record['id']);
    $foundM = mysql_fetch_assoc($found);
    $alias = seoUrl($record['title']);
    if ($foundM['matched']>1)
    {
        $duplicates++;
        $alias.="-".(intval($foundM['matched'])+1);
        echo ('[!!] ' . $record['alias']. 'would be duplicit, changed to: '.$alias."\n");
    }
    if ($record['link']!=$alias) {
        echo ('[++] ' . $record['link']. ' changed to '.$alias.' from title "'.$record['title'].'"'."\n");
        mysql_query('update j25_menu set alias="'.$alias.'",  path="'.$alias.'" where id='.$record['id']);
        $modified++;
    } else {
        $correct++;
    }
       
}

echo mysql_num_rows($result)." analyzed, $correct was ok, $modified has been modified, $duplicates was duplicate...\n";


/**
  * Return URL-Friendly string slug
  * @param string $string
  * @return string
  */

function seoUrl($string) {
        //Unwanted:  {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
        $string = mb_strtolower($string);
    $string = iconv('UTF-8','ASCII//TRANSLIT',$string);
        //Strip any unwanted characters
        $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
        //Clean multiple dashes or whitespaces
        $string = preg_replace("/[\s-]+/", " ", $string);
        //Convert whitespaces and underscore to dash
        $string = preg_replace("/[\s_]/", "-", $string);
        return $string;
}

Žádné komentáře: