Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/07/2007 07:04:40 AM (17 years ago)
Author:
mdawaffe
Message:

varchar(255) for slugs, trim multibyte data before inserting in db. Fixes #655

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-admin/upgrade-functions.php

    r845 r846  
    1616    $bb_upgrade += bb_upgrade_190(); // Move topic_resolved to topicmeta
    1717    $bb_upgrade += bb_upgrade_200(); // Indices
     18    $bb_upgrade += bb_upgrade_210(); // Convert text slugs to varchar slugs
    1819    require_once( BBPATH . 'bb-admin/upgrade-schema.php');
    1920    bb_make_db_current();
     
    269270}
    270271
     272function bb_upgrade_process_all_slugs() {
     273    global $bbdb;
     274    // Forums
     275
     276    $ids = (array) $bbdb->get_col("SELECT forum_id, forum_name FROM $bbdb->forums ORDER BY forum_order ASC" );
     277
     278    $names = $bbdb->get_col('', 1);
     279
     280    $slugs = array();
     281    foreach ( $ids as $r => $id ) :
     282        $slug = bb_slug_sanitize( $names[$r] );
     283        $slugs[$slug][] = $id;
     284    endforeach;
     285
     286    foreach ( $slugs as $slug => $forum_ids ) :
     287        foreach ( $forum_ids as $count => $forum_id ) :
     288            if ( $count > 0 )
     289                $slug = bb_slug_increment( $slug, "-" . ( $count - 1 ) );
     290            $bbdb->query("UPDATE $bbdb->forums SET forum_slug = '$slug' WHERE forum_id = '$forum_id';");
     291        endforeach;
     292    endforeach;
     293    unset($ids, $names, $slugs, $r, $id, $slug, $forum_ids, $forum_id, $count);
     294
     295    // Topics
     296
     297    $ids = (array) $bbdb->get_col("SELECT topic_id, topic_title FROM $bbdb->topics ORDER BY topic_start_time ASC" );
     298
     299    $names = $bbdb->get_col('', 1);
     300
     301    $slugs = array();
     302    foreach ( $ids as $r => $id ) :
     303        $slug = bb_slug_sanitize( $names[$r] );
     304        $slugs[$slug][] = $id;
     305    endforeach;
     306
     307    foreach ( $slugs as $slug => $topic_ids ) :
     308        foreach ( $topic_ids as $count => $topic_id ) :
     309            if ( $count > 0 )
     310                $slug = bb_slug_increment( $slug, "-" . ( $count - 1 ) );
     311            $bbdb->query("UPDATE $bbdb->topics SET topic_slug = '$slug' WHERE topic_id = '$topic_id';");
     312        endforeach;
     313    endforeach;
     314    unset($ids, $names, $slugs, $r, $id, $slug, $topic_ids, $topic_id, $count);
     315}
     316
    271317// Reversibly break passwords of blocked users.
    272318function bb_upgrade_160() {
     
    359405}
    360406
    361 function bb_upgrade_1000() {
    362     if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 788 )
    363         return 0;
     407// 210 converts text slugs to varchar(255) width slugs (upgrading from alpha version - fires before dbDelta)
     408// 1000 Gives new slugs (upgrading from previous release - fires after dbDelta)
     409function bb_upgrade_210() {
     410    if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 846 )
     411        return 0;
     412
     413    global $bbdb;
     414
     415    $bbdb->hide_errors();
     416    if ( !$ids = $bbdb->get_var("SELECT forum_slug FROM $bbdb->forums ORDER BY forum_order ASC LIMIT 1" ) )
     417        return; // Wait till after dbDelta
     418    $bbdb->show_errors();
     419
     420    bb_upgrade_process_all_slugs();
     421
     422    bb_update_option( 'bb_db_version', 846 );
    364423   
    365     global $bbdb;
    366    
    367     $forums = (array) $bbdb->get_results("SELECT forum_id, forum_name, forum_slug FROM $bbdb->forums ORDER BY forum_order ASC" );
    368     foreach ($forums  as $forum) {
    369         $slug = bb_slug_sanitize(trim($forum->forum_name));
    370         $forum_slugs[$slug][] = $forum->forum_id;
    371     }
    372     foreach ($forum_slugs as $slug => $forums) {
    373         foreach ($forums as $count => $forum_id) {
    374             if ($count > 0) {
    375                 $increment = '-' . ($count + 1);
    376             } else {
    377                 $increment = null;
    378             }
    379             $slug .= $increment;
    380             $bbdb->query("UPDATE $bbdb->forums SET forum_slug = '$slug' WHERE forum_id = $forum_id;");
    381         }
    382     }
    383     unset($forums,$forum,$forum_slugs,$slug,$forum_id,$increment,$count);
    384    
    385     $topics = (array) $bbdb->get_results("SELECT topic_id, topic_title, topic_slug FROM $bbdb->topics ORDER BY topic_start_time ASC" );
    386     foreach ($topics  as $topic) {
    387         $slug = bb_slug_sanitize(trim($topic->topic_title));
    388         $topic_slugs[$slug][] = $topic->topic_id;
    389     }
    390     foreach ($topic_slugs as $slug => $topics) {
    391         foreach ($topics as $count => $topic_id) {
    392             if ($count > 0) {
    393                 $increment = '-' . ($count + 1);
    394             } else {
    395                 $increment = null;
    396             }
    397             $slug .= $increment;
    398             $bbdb->query("UPDATE $bbdb->topics SET topic_slug = '$slug' WHERE topic_id = $topic_id;");
    399         }
    400     }
    401     unset($topics,$topic,$topic_slugs,$slug,$topic_id,$increment,$count);
    402    
    403     bb_update_option( 'bb_db_version', 788 );
     424    echo "Done adding slugs.<br />";
     425    return 1;
     426}
     427
     428function bb_upgrade_1000() { // Give all topics and forums slugs
     429    if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 846 )
     430        return 0;
     431
     432    bb_upgrade_process_all_slugs();
     433
     434    bb_update_option( 'bb_db_version', 846 );
    404435   
    405436    echo "Done adding slugs.<br />";
Note: See TracChangeset for help on using the changeset viewer.