Subject: Script to Remove unused autotags from Articles

Posted on: 14/09/2013 03:48pm
By: Laugh

I removed a plugin recently that I was using which had a number of it's autotags saved in a couple hundred stories. I needed an automated way to remove them so I quickly made this script up.

This script will scan your Geeklog database for stories that have a specific autotag you want to remove and then remove them from the stories.

Place this script into a text file in your public_html folder of your site. Name the text file replaceautotags.php. Update the line

$moduletag = 'type_tag_here'

with the autotag you want to remove. Save the file and then run the script.

Once you are finished remember to delete the file.

Please BACKUP your database before you run the script.

PHP Formatted Code

<?php

require_once 'lib-common.php';

// php script to replace [story: ...] autotag with nothing
$moduletag = 'type_tag_here'; // for example 'story'

echo "Start<br>";

$autotag_prefix = '[' . $moduletag . ':';

$sql = "SELECT * FROM {$_TABLES['stories']} WHERE introtext like '%[tag:%'";
//$sql .= " AND sid = ''";

$result = DB_query($sql);
$num = DB_numRows($result);
for($i = 0; $i < $num; $i++) {
    $A = DB_fetchArray($result);
    $content = $A['introtext'];
    $contentlen = MBYTE_strlen($content);
    $content_lower = MBYTE_strtolower($content);
       
   
    $offset = 0;
    $prev_offset = 0;
    while ($offset < $contentlen) {
        $start_pos = MBYTE_strpos($content_lower, $autotag_prefix,
                                  $offset);
        if ($start_pos === false) {
            break;
        } else {
            $end_pos  = MBYTE_strpos($content_lower, ']', $start_pos);
            $next_tag = MBYTE_strpos($content_lower, '[', $start_pos + 1);
            if (($end_pos > $start_pos) AND
                    (($next_tag === false) OR ($end_pos < $next_tag))) {
                $taglength = $end_pos - $start_pos + 1;
                $tag = MBYTE_substr($content, $start_pos, $taglength);
                $parms = explode(' ', $tag);
   
                // Extra test to see if autotag was entered with a space
                // after the module name
                if (MBYTE_substr($parms[0], -1) == ':') {
                    $startpos = MBYTE_strlen($parms[0]) + MBYTE_strlen($parms[1]) + 2;
                    $label = str_replace(']', '', MBYTE_substr($tag, $startpos));
                    $tagid = $parms[1];
                } else {
                    $label = str_replace(']', '', MBYTE_substr($tag,
                                            MBYTE_strlen($parms[0]) + 1));
                    $parms = explode(':', $parms[0]);
                    if (count($parms) > 2) {
                        // whoops, there was a ':' in the tag id ...
                        array_shift($parms);
                        $tagid = implode(':', $parms);
                    } else {
                        $tagid = $parms[1];
                    }
                }
   
                $newtag = array(
                    'module'    => $module,
                    'tag'       => $moduletag,
                    'tagstr'    => $tag,
                    'startpos'  => $start_pos,
                    'length'    => $taglength,
                    'parm1'     => str_replace(']', '', $tagid),
                    'parm2'     => $label
                );
                $tags[] = $newtag;
            } else {
                // Error: tags do not match - return with no changes
                return $content . $LANG32[32];
            }
            $prev_offset = $offset;
            $offset = $end_pos;
        }
    }

    // If we have found 1 or more AutoLink tag
    if (count($tags) > 0) {       // Found the [tag] - Now process them all
       
        //com_errorlog('Story Id = ' . $A['sid'] . ' - Before Autotag removal: ' . LB . LB . $content);
       
        foreach ($tags as $autotag) {
            // remove it
            $content = str_replace($autotag['tagstr'], '', $content);
        }
        $content = DB_escapeString($content);
       
        //com_errorlog('After Autotag removal: ' . LB . LB . $content);
        //com_errorlog("Autotag removed from Story: " . $A['sid']);
       
        $sql = "UPDATE {$_TABLES['stories']} SET introtext = '$content' WHERE sid = '{$A['sid']}'";
        DB_query($sql);    
    }
}

echo "Done";
       
?>
 

Geeklog - Forum
https://www.geeklog.net/forum/viewtopic.php?showtopic=95232