Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
general-topic
Link formatting when upgrading 4.5 to 5.1.3
Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Sep 17, 2012 - 9:26 am

Ooooh. So you did. And I missed that too!

andy-signature.png
YELLOW
SWORDFISH
Avatar
tracychilders
Member
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 1:01 pm

Hi Guys,

We're also having this issue when checking old posts. Is it ok if we can use @peter script to automate this? Old links from 4.5 version are broken and its a lot.

Thanks!

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 1:28 pm

Actually, we would appreciate it if you could try it out. Please note, while Peter has contributed good code in the past, we have not had a chance to test it out ourselves. There is no reason to believe there will be any issue but would recommend a db backup before starting.

Any feedback would be great. We do intend to test and include Peters code in our upgrade script this week for our 5.1.4 release next week.

Avatar
tracychilders
Member
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 3:13 pm

Ok gotcha. btw may I know what class or php file handles the rewrite of content that has a links? I've check one post to our database and it's seems the link is correct.

database post content

<a href="http://longlinkhere.com" target="_blank">http://longlinkhere.....xxxxx.zip</a>

but when viewing the post the link becomes 

<p>

<a href="http://longlinkhere.com" target="_blank"></a>

<a href="http://longlinkhere.com" rel="nofollow" target="_blank">http://longlinkhere</a>…..xxxxx.zip

</p>

Thanks!

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
Sep 17, 2012 - 6:16 pm

The problem is actually in the WP core routine make_clickable(). We have searched and tried numerous third party algorithms that purposrt to do the same thing as make_clickable() and they all seem to have the same problem whenever there is a full point in the link.

we are hoping, of course, that the WP guys will fix the bug so it just goes away...

By the way - if you do try Peters code note the SQL error he points out later in the thread.

andy-signature.png
YELLOW
SWORDFISH
Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 10:26 pm

Hey Peter, thanks for the code!!  The regex works nicely...  have added it to the upgrade script for 5.1.4 (hopefully released next weekend)...

I had to tweak a few other things to use our api and get it working... for the reference, here is our function:

function sp_fix_shortened_links() {
    $postCount = spdb_count(SFPOSTS);
    $limit = 1000;  
    for ($offset = 0; $offset < $postCount; $offset+= $limit) {
        $posts = spdb_select('set', "SELECT post_id, post_content FROM ".SFPOSTS." ORDER BY post_id ASC LIMIT $offset, $limit");
        foreach ($posts as $post) {
            /*
                Matches would be:
                0 = The entire string
                1 = The link in the <a> tag
                2 = The rest of the parameters in the <a> tag (nofollow, target, etc.)
                3 = The part after the 5 periods
    
                So then we want to replace 0 with 1 for each result
    
                We are assuming that the links have 5 consecutive periods
            */
    
            $postContent = stripslashes($post->post_content);
            preg_match_all("/<a href=\"(.*?)\"(.*?)\.\.\.\.\.(.*?)<\/a>/is", $postContent, $linkMatches);   
            if (!empty($linkMatches[0])) {
                foreach ($linkMatches[0] as $index => $stringMatch) {
                    $postContent = str_replace($stringMatch, $linkMatches[1][$index], $postContent);
                }
                $postContent = esc_sql($postContent);
                spdb_query("UPDATE ".SFPOSTS." SET post_content = '$postContent' WHERE post_id = $post->post_id");
            }
        }
    }
}

might be worth a quick check to see I didnt screw anything up! ;)

1000 seemed to work faster than 100 on a larger db...  had to move the update query outside the foreach for when multiple shortened links in the content...

again, THANKS!

Avatar
Peter
Member
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 10:39 pm

Great catch for both the different offset and having multiple shortened links.

How necessary is the stripslashes and addslashes? Is that only necessary on certain setups or are they required because of how spdb_query works? I prefer to use $wpdb->prepare and similar functions for sanitizing queries (if necessary), as typically stripslashes/addslashes introduce problems on setups that don't need them.

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 10:43 pm

well wpdb prepare does the same thing... its just hidden from you...  could have done an esc_sql() vs addslashes()...

when you pull it from db, you need to remove any escapes (slashes)... and before you put it back in, you have to escape (slashes) it again...  not worried about security here since it came from the db, but the escaping handles any single quotes (and few other chars) that might be in the post content...

but the main thing to note is that wpdb prepare will do that too (actually via walker escape by ref)...

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 10:45 pm

btw, I did change ours to esc_sql() vs addslashes()... pretty much the same thing, but use mysql real escape...  updated code above too....

Avatar
Peter
Member
Free Members
sp_UserOfflineSmall Offline
Sep 17, 2012 - 11:37 pm

You're right on the insertion; sorry for confusing the issue there. But on grabbing the data from the database, I don't understand why you would need to strip any slashes, unless they were double encoded. The slashes that are added for escaping purposes should only be for escaping purposes and wouldn't have been stored in the database... does the forum application itself have a need for the slashes to be stored in the database?

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 619
Members: 17362
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10127
Posts: 79625