Skip to:
Content

bbPress.org

Changeset 125


Ignore:
Timestamp:
06/04/2005 11:27:38 PM (21 years ago)
Author:
mdawaffe
Message:

Ability to Merge Tags. Clean up tag removal and destruction. Fixes #73.

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-admin/tag-destroy.php

    r123 r125  
    1313    die('Tag not found.');
    1414
    15 if ( $destroyed = destroy_tag( $tag_id ) )
    16     header('Location: ' . $bb->path );
    17 else
    18     die("Something odd happened when attempting to destroy that tag.  Error code: $destroyed.  <a href=\"" . $_SERVER['HTTP_REFERER'] . '">Try Again</a>');
     15if ( $destroyed = destroy_tag( $tag_id ) ) {
     16    echo 'Rows deleted from tags table: ' . $destroyed['tags'] . "<br />\n";
     17    echo 'Rows deleted from tagged table: ' . $destroyed['tagged'] . "<br />\n";
     18    echo 'Destruction Successful? ' . ($destroyed['destroyed']) ? "TRUE\n" : "FALSE<br />\n
     19        \t NOTE: destruction of a tag that is not associated with any posts will give a false negative.<br />\n";
     20    echo '<a href="'. $bb->path . '">Home</a>';
     21} else {
     22    var_dump($destroyed);
     23    die("<br />Something odd happened when attempting to destroy that tag.  See the above Error codes.<br />\n<a href=\"" . $_SERVER['HTTP_REFERER'] . '">Try Again?</a>');
     24}
    1925?>
  • trunk/bb-includes/formatting-functions.php

    r59 r125  
    117117}
    118118
     119function tag_sanitize( $tag ) {
     120    $tag    = trim         ( $tag );
     121    $tag    = strtolower   ( $tag );
     122    $tag    = preg_replace ( '/\s/', '', $tag );
     123    $tag    = user_sanitize( $tag );
     124    return $tag;
     125}
     126
    119127function show_context( $term, $text ) {
    120128    $text = strip_tags($text);
  • trunk/bb-includes/functions.php

    r122 r125  
    658658    global $bbdb;
    659659    $raw_tag = $tag;
    660     $tag     = trim         ( $tag );
    661     $tag     = strtolower   ( $tag );
    662     $tag     = preg_replace ( '/\s/', '', $tag );
    663     $tag     = user_sanitize( $tag );
     660    $tag     = tag_sanitize( $tag );
    664661    if ( empty( $tag ) )
    665662        return false;
     
    677674        return false;
    678675    $raw_tag = $tag;
    679     $tag     = trim         ( $tag );
    680     $tag     = strtolower   ( $tag );
    681     $tag     = preg_replace ( '/\s/', '', $tag );
    682     $tag     = user_sanitize( $tag );
     676    $tag     = tag_sanitize( $tag );
    683677
    684678    if ( empty( $tag ) )
     
    705699    bb_do_action('bb_tag_removed', $tagged);
    706700
    707     if ( $removed = $bbdb->query("DELETE FROM $bbdb->tagged WHERE tag_id = '$tag_id' AND user_id = '$user_id' AND topic_id = '$topic_id'") );
    708         $removed += $bbdb->query("UPDATE $bbdb->tags SET tag_count = tag_count - 1 WHERE tag_id = '$tag_id'");
    709     $removed *= 10;
     701    if ( $tags = $bbdb->query("DELETE FROM $bbdb->tagged WHERE tag_id = '$tag_id' AND user_id = '$user_id' AND topic_id = '$topic_id'") );
     702        $tagged = $bbdb->query("UPDATE $bbdb->tags SET tag_count = tag_count - 1 WHERE tag_id = '$tag_id'");
    710703    if ( !$bbdb->get_var("SELECT tag_id FROM $bbdb->tagged WHERE tag_id = '$tag_id' LIMIT 1") ) // don't trust tag_count?
    711         $removed += destroy_tag( $tag_id );
    712     return $removed; // debugging: 2_ normally, 1_ if didn't update count, 0_ if didn't delete from tagged
     704        $destroyed = destroy_tag( $tag_id );
     705    return array( 'removed' => ($tags && $tagged), 'tags' => $tags, 'tagged' => $tagged, 'destroyed' => $destroyed );
     706}
     707
     708// merge $old_id into $new_id.  MySQL 4.0 can't do IN on tuples!
     709function merge_tags( $old_id, $new_id ) {
     710    global $bbdb, $current_user;
     711    if ( $current_user->user_type < 2)
     712        return false;
     713    if ( $old_id == $new_id )
     714        return false;
     715
     716    $merged_tags = serialize( array( 'old_id' => $old_id, 'new_id' => $new_id ) );
     717
     718    bb_do_action('bb_tag_merged', $merged_tags);
     719
     720    $tagged_del = 0;
     721    if ( $old_topic_ids = $bbdb->get_col( "SELECT topic_id FROM $bbdb->tagged WHERE tag_id = '$old_id'" ) ) {
     722        $old_topic_ids = join(',', $old_topic_ids);
     723        $shared_query = "SELECT user_id, topic_id FROM $bbdb->tagged WHERE tag_id = '$new_id' AND topic_id IN ($old_topic_ids)";
     724    $shared_topics = $bbdb->get_results( $shared_query );
     725    $shared_count = count($shared_topics);
     726    foreach ( $shared_topics as $shared_topic )
     727        $tagged_del += $bbdb->query( "DELETE FROM $bbdb->tagged WHERE tag_id = '$old_id' AND user_id = '$shared_topic->user_id' AND topic_id = '$shared_topic->topic_id'" );
     728    }
     729
     730    if ( $diff_count = $bbdb->query( "UPDATE $bbdb->tagged SET tag_id = '$new_id' WHERE tag_id = '$old_id'" ) )
     731        $bbdb->query( "UPDATE $bbdb->tags SET tag_count = tag_count + $diff_count WHERE tag_id = '$new_id'" );
     732
     733    // return values and destroy the old tag
     734    return array( 'destroyed' => destroy_tag( $old_id ), 'old_count' => $diff_count + $shared_count, 'diff_count' => $diff_count );
    713735}
    714736
     
    720742    bb_do_action('bb_tag_destroyed', $tag_id);
    721743
    722     if ( $destroyed = $bbdb->query("DELETE FROM $bbdb->tags WHERE tag_id = '$tag_id'") )
    723         $destroyed += $bbdb->query("DELETE FROM $bbdb->tagged WHERE tag_id = '$tag_id'");
    724     return $destroyed; // debugging: 2 normally, 1 if didn't delete from tagged, 0 if didn't delete from tags
     744    if ( $tags = $bbdb->query("DELETE FROM $bbdb->tags WHERE tag_id = '$tag_id'") )
     745        $tagged = $bbdb->query("DELETE FROM $bbdb->tagged WHERE tag_id = '$tag_id'");
     746    return array( 'destroyed' => ($tags && $tagged), 'tags' => $tags, 'tagged' => $tagged );
    725747}
    726748
    727749function get_tag_id( $tag ) {
    728750    global $bbdb;
    729     $tag     = strtolower   ( $tag );
    730     $tag     = preg_replace ( '/\s/', '', $tag );
    731     $tag     = user_sanitize( $tag );
     751    $tag     = tag_sanitize( $tag );
    732752
    733753    return $bbdb->get_var("SELECT tag_id FROM $bbdb->tags WHERE tag = '$tag'");
     
    742762function get_tag_by_name( $tag ) {
    743763    global $bbdb;
    744     $tag     = strtolower   ( $tag );
    745     $tag     = preg_replace ( '/\s/', '', $tag );
    746     $tag     = user_sanitize( $tag );
     764    $tag     = tag_sanitize( $tag );
    747765
    748766    return $bbdb->get_row("SELECT * FROM $bbdb->tags WHERE tag = '$tag'");
  • trunk/bb-includes/template-functions.php

    r124 r125  
    549549}
    550550
     551function tag_merge_form() {
     552    global $tag, $current_user;
     553    if ( $current_user->user_type < 2 )
     554        return false;
     555    $tag_merge_form  = '<form id="tag-merge" method="post" action="' . bb_get_option('uri') . 'bb-admin/tag-merge.php">' . "\n";
     556    $tag_merge_form .= "<p>Merge this tag into the tag specified</p>\n<p>\n" . '<input type="text"   name="tag" size="10" maxlength="30" />' . "\n";
     557    $tag_merge_form .= '<input type="hidden" name="id" value="' . $tag->tag_id . '" />' . "\n";
     558    $tag_merge_form .= '<input type="submit" name="Submit" value="Merge" />' . "\n</p>\n</form>";
     559    echo $tag_merge_form;
     560}
     561
    551562function tag_destroy_form() {
    552563    global $tag, $current_user;
  • trunk/bb-templates/tag-single.php

    r120 r125  
    66
    77<?php tag_destroy_form(); ?>
     8
     9<?php tag_merge_form(); ?>
    810
    911<h2><a href="<?php option('uri'); ?>"><?php option('name'); ?></a> &raquo; <a href="<?php tag_page_link(); ?>">Tags</a> &raquo; <?php tag_name(); ?></h2>
Note: See TracChangeset for help on using the changeset viewer.