R2i DotNetNuke® Forum

R2i wants you to have the opportunity to ask questions, post reviews, help others or just rant and rave about DotNetNuke® or any of the R2i Modules and Skins. Our team spends hour upon hour, day after day, working on custom DotNetNuke® modules and services; please feel free to ask us anything.
 
Find and Replace Procedure for Html Module
Last Post 19 Nov 2010 11:44 PM by lprager. 28 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Page 1 of 212 > >>
Author Messages Informative
kevinmschreinerUser is Offline
Advanced Member
Advanced Member
Posts:729

--
30 Sep 2005 03:45 PM  

Broken images and links within DotNetNuke are very common for instances of the application that have been migrated from one server to another, or from a server to a development machine. There are usually a few ways to combat the situation, but some ways cause more problems than they correct. This is because most solutions involve a quick Search and Replace within the data table by converting the value to a varchar, executing the REPLACE function and then updating the table with the new value. Often this causes trouble since varchar has a length maximum of 8000 characters, and many rows within your table may be longer than that, and will consequently end up truncated.

The problem is represented generally by images broken due to the URL within the text having a value which pushes the URL outside of the application domain. Meaning - you may have a server instance with image sources like: /Portals/0/thisimage.gif. On a server with the domain thisServer.com, while the local installation is contained within a virtual directory like “localhost/DNN_thisServer”. The images work on the server, but not within your local environment.

The reason for the failure is that the URL is reading the slash in the front of the URL and jumping to the parent directory, only since you are on a Virtual Directory on localhost - it is jumping outside of the application directory - therefore returning a broken link.

This is a very common problem, and the best solution is to replace the URL's on the fly with a corrected value. Since again we are restricted due to the data type of the column, we cannot simply exercise the REPLACE function within SQL to correct the values - instead we need to utilize the inherent text functions which are provided within SQL Server which takes a little more work, but does the trick.

Provided is a procedure which can be called to replace any value you want with whatever the replacement would be with the DesktopHtml column of the HtmlText table. To correct this issue - you can simply create this procedure on your database and execute it directly. For this above example you would execute:

Replace_DesktopHtml '/Portals/0/',''

Execution of the above would remove any instance of the /Portals/0/ value therefore correcting the issue. Verification has been added to the procedure to check that the anticipated length of the outgoing data matches the original length minus the count of values replaced and the difference between the value we are looking for and the value we are replacing it with. Any records which fail this verification are not updated and are instead returned by the procedure.

With no further ado - here is the full procedure:

CREATE Procedure Replace_DesktopHtml(
   
@For as varchar(100),
   
@With as varchar(100))
AS
--* REPLACES TEXT IN THE DESKTOP HTML MATCHING THE FOR VARIABLE
--
* SAMPLE: Replace_DesktopHtml '/Portals/0/', ''

--* MAIN DECLARATION
DECLARE
   
@pointer binary(16),
   
@index INT,
   
@lenFor INT,
   
@lenWith INT,
   
@diff int,
   
@id INT,
   
@count INT

--* STANDARD RUNTIME VARIABLES
SET @lenFor = LEN(@For)
SET @diff = @lenFor - LEN(@With)

--* CREATE THE TEMPORARY TABLE
CREATE TABLE #Temporary
    (
       
[id] int,     --MAPS TO YOUR SOURCE TABLE PKID
        [oldtext] text, --ORIGINAL SOURCE TEXT (NOT REQUIRED)
        [oldlength] int,--ORIGINAL SOURCE LENGTH
        [text] text,     --NEW TEXT VALUE
        [length] int,     --NEW LENGTH
        [cLength] int    --CHECKSUM LENGTH
    )

--* LOOP THROUGH THE SOURCE TABLE
--
* IDENTIFY ALL RECORDS WHICH MATCH YOUR PATTERN
DECLARE irows CURSOR
    LOCAL FORWARD_ONLY STATIC READ_ONLY
FOR
   
SELECT moduleid
       
FROM HtmlText
       
WHERE PATINDEX('%'+@For+'%', DesktopHtml)>0
OPEN irows

FETCH NEXT FROM irows INTO @id

WHILE (@@FETCH_STATUS = 0)
BEGIN
--* INSERT MATCHING RECORDS INTO THE TEMPORARY TABLE
    INSERT INTO #Temporary(id, oldtext, oldlength, text)
   
SELECT
        ModuleID,
        DesktopHtml,
       
datalength(DesktopHtml),
        DesktopHtml
from HtmlText
   
where ModuleID = @id

--* GRAB THE POINTER OF THE OBJECT TO BE USED FOR UPDATETEXT
    SELECT
       
@pointer = TEXTPTR(text)
   
FROM #Temporary
   
WHERE id=@id
--* GET THE FIRST INDEX OF OUR PATTERN
    SELECT
       
@index = PATINDEX('%'+@For+'%', text)
   
FROM #Temporary
   
WHERE id=@id

--* IF WE FOUND ANY ENTRIES, LOOP UNTIL WE REPLACE ALL OF THEM
    IF @index > 0
   
BEGIN
       
select @count = 0
       
WHILE (
           
SELECT
               
PATINDEX('%'+@For+'%', text)
               
FROM #Temporary
           
WHERE id=@id
              )
> 0
       
BEGIN
--* KEEP A RUNNING TOTAL OF IDENTIFIED ENTRIES FOR LENGTH CHECKSUM
            select @count = @count + 1
           
SELECT
           
@index = PATINDEX('%'+@For+'%', text)-1
           
FROM #Temporary
           
WHERE id=@id

--* UPDATE THE TEMPORARY VALUE        
            UPDATETEXT #Temporary.text @pointer @index @lenFor @With
       
END
--* UPDATE THE TEMPORARY RECORD, SET LENGTH AND CLENGTH VALUES FOR CHECKSUM
        UPDATE #Temporary set
            length
=datalength(text),
            cLength
=datalength(oldtext) - @count * @diff 
       
WHERE id=@id
   
END
   
FETCH NEXT FROM irows INTO @id
END

CLOSE irows
DEALLOCATE irows
--* UPDATE THE DATABASE FOR ALL RECORDS IN THE TEMPORARY
--
* WHERE THE CHECKSUM LENGTH MATCHED THE RESULT LENGTH
UPDATE target set
    target.DesktopHtml
= t.[text]
FROM
    HtmlText target
JOIN #Temporary t
   
ON
        target.ModuleId
= t.id AND
        t.length
= t.cLength

--* DELETE ALL RECORDS FROM TEMPORARY WHICH WERE SUCCESSFUL
DELETE from #Temporary where length=clength

--* RETURN ALL ROWS WHICH FAILED LENGTH COMPARISON
--
* THIS SHOULD ALWAYS BE EMPTY
select * from #Temporary

--* DROP THE TEMP TABLE AND EXIT
DROP TABLE #Temporary
jabullUser is Offline
New Member
New Member
Posts:2

--
18 Jan 2006 09:59 PM  

Kevin, this is a fantastic tip.  I keep running into this problem and have been fixing it the hard way.

thanks for the great procedure.

kevinmschreinerUser is Offline
Advanced Member
Advanced Member
Posts:729

--
24 Jan 2006 02:19 PM  
Thanks. Yes - it is a procedure we use all the time, and really helps out because we do in-house development on local virtual directories and then send them to target ISP's which use root directories.

Anyway - there were two typos in the procedure above, which happened when I pasted the code in through the code formatter. It has been corrected.
RegGFXUser is Offline
New Member
New Member
Posts:10

--
14 Feb 2006 07:38 PM  
HEY THIS IS GREAT... its what i've been looking for...


But i have a question on how to make your script work in my situation

I have to change the name of a company that is scatterd throughout some 40 or so pages located in various TEXT/HTML modules as content.

So for the sake of illustration and helping understand just what to do,
and using your script, how would i and mass replace  
"Joe's Cafe" with "Buckwheat's Cafe"

In other words how do i modify the execution of
Replace_DesktopHtml

to swap "Joe's Cafe" with "Buckwheat's Cafe" throughout the site?

PLEASE... advise...

Thank you in advance for your response to this post.... otherwise i'm going to be up all night.

kevinmschreinerUser is Offline
Advanced Member
Advanced Member
Posts:729

--
15 Feb 2006 02:49 AM  
Since you are just attempting to replace the offending text within the Text/HTML modules, You should be able to execute the procedure as we have provided - with the following statement:

Replace_DesktopHtml 'Joe''s Cafe', 'Buckwheat''s Cafe'

Thats it.
RegGFXUser is Offline
New Member
New Member
Posts:10

--
11 Oct 2006 05:53 PM  
This is working great for DNN 2.X however Will this work for DNN 3.x or 4.x

Please advise...

Thanks
kevinmschreinerUser is Offline
Advanced Member
Advanced Member
Posts:729

--
19 Oct 2006 08:08 PM  
Yes, it works for all current versions of DNN
rtoneriiUser is Offline
Basic Member
Basic Member
Posts:308

--
21 May 2007 06:05 PM  
Hi Kevin,
Do you happen to just have a stored procedure that will show the results of what tab / module that have a certain value? I need to tell a user where some text is on a site and I can not find it but when I do a find and replace and make the replace val equal the find value I am finding 4.

Thanks
Rick
dnncreativeUser is Offline
New Member
New Member
Posts:3

--
24 May 2007 10:04 AM  
Hi Kevin,

This is an excellent tip, if I can get it to work for stripping out HTML code this is going to save me weeks of work.

I'm currently cleaning up a site for a client which needs a lot of HTML stripping out, what's the best way to strip out these examples?



I've tried:

Replace_DesktopHtml '',''

but it doesn't work, or:

Replace_DesktopHtml '
','
'

Do I have to write the code in a certain way in order for it to pick these up? - I've got it working ok for replacing text and /Portals/0

Any tips you can give are appreciated, thanks,

Lee
dnncreativeUser is Offline
New Member
New Member
Posts:3

--
24 May 2007 10:09 AM  
the forum has stripped it out, so here's the code in a text file:

http://www.dnncreative.net/Portals/0/html_to_remove.txt

dnncreativeUser is Offline
New Member
New Member
Posts:3

--
25 May 2007 09:39 AM  
Hello,

I worked it out by looking at the content of the HTMLText table, you need to write the full code version of the HTML symbols in order for it to pick them up.

Thanks again, this is going to save me a load of work!
DavidWSnowUser is Offline
Basic Member
Basic Member
Posts:129

--
07 Jul 2007 03:03 AM  
Great Job Kevin! Very Useful.

/DaveS

DavidWSnowUser is Offline
Basic Member
Basic Member
Posts:129

--
07 Jul 2007 06:08 PM  
After realizing that the image link problem with the Text / Html module that this tool really helps with doesn't exist with the image references in my ListX applications, an idea arrived.

Kevin why doesn't bi4ce make an HTML module with some of the same tokens that ListX has such as [PORTALPATH] that can be fixed-up at run time.

/DaveS
DavidWSnowUser is Offline
Basic Member
Basic Member
Posts:129

--
27 Jul 2007 04:46 PM  
I just had occastion to update my localhost/asi3 development site from my production www.AgingSafely.com. I though that I would use this procedure to 'fix' all of the image references in the Text/HTML modules.

I stored the procedure in the database and executed:
Replace_DesktopHtml '/Portals/0/', '/asi3/Portals/0/'

It ran and ran for minutes and had 0 rows effected before I aborted it. Since I have a small site with only 20-30 Text/HTML modules it shouldn't have taken but a few seconds.

I checked and did have several /Portals/0/ references in the modules.

This was DNN 4.5.3 with SQLExpress.

Any idea's?

/DaveS
contactdpUser is Offline
Basic Member
Basic Member
Posts:475

--
27 Jul 2007 05:41 PM  
your query should have been

Replace_DesktopHtml '/asi3/Portals/0/', '/Portals/0/'


what is happening in your case is it is replacing '/Portals/0/' getting replaved with /asi3/Portals/0/ then agin '/Portals/0/' of /asi3<b> /Portals/0/</b> then again and again and again....


Thanks,
DP
Durga Prasad(DP) | Senior Web Engineer<br>R2integrated
AlexeiXX3User is Offline
New Member
New Member
Posts:1

--
02 Aug 2007 06:08 PM  
I just tryed your SP, its so easy!!!
Im having a little problem though
It replaced all of the references from 'dnn' with 'cobaes', but something strage happens, links only work fine when im signed as admin or host, and not when im not logged, when im not logged, i see the links in the bottom of the window status with the old reference (dnn), dows anyone know what is happening?
I erased temporary internet files, cookies, I dont think it has much to do, since it is not working unless i log in as admin

Thanks for your time
contactdpUser is Offline
Basic Member
Basic Member
Posts:475

--
02 Aug 2007 08:22 PM  
it should be server side cacheting. make sure you restart the application, clear the server / web cache.

Thanks,
DP
Durga Prasad(DP) | Senior Web Engineer<br>R2integrated
klb5770User is Offline
New Member
New Member
Posts:1

--
14 Aug 2007 06:24 PM  
We are building and will be maintaining a dnn installation with 5 portals. The project requires keeping 3 environments, development, staging/testing and production.

The sp listed above will be very useful in maintaining image links, but I am wondering how to approach/plan for synching tabIDs and moduleIDs across the different environments.

Do you have any references/advice for this problem?
stefanpUser is Offline
New Member
New Member
Posts:2

--
05 Sep 2007 08:24 PM  
I tried a similar search to DavidWSnow:
Replace_DesktopHtml '/Portals/0', '/wwwroot/Portals/0'

and I ran into the endless loop problem.

Would it be too much to ask to modify your code to exit when it has processed all the rows in the DesktopHtml table? In other words, after the search and replace has reached the last row in the table, exit the proc?

Thanks,

Stefan
CSC Inc.
DavidWSnowUser is Offline
Basic Member
Basic Member
Posts:129

--
05 Sep 2007 08:30 PM  
I tried and tried the procedure with no success. I usually move my database the other direction that you did in your example. I develop content online in hidden pages, and then make them public.

I clone my production database to localhost while developing new skins or ListX applications then install them on the production server.

You can't use this procedure in a single step to go from production to localhost!

Images in my production database begin with '/Portals/0/' while on my localhost they have '/asi3/Portals/0/'

You can't just do Replace_DestopHTML '/Portals/0/', '/asi3/Portals/0/'

because the procedure just loops on the same entry endlessly replacing the /Portals/0/

I found that I had to do Replace_DestopHTML '/Portals/0/', '/asi3/Portals/a/'
followed by
Replace_DestopHTML '/Portals/a/', '/Portals/0/'

I suppose that the next time i'll actually tweak this an miss-spell Portals rather than using the portal number, so that I don't have to do it one time per portal.


/DaveS
You are not authorized to post a reply.
Page 1 of 212 > >>


Active Forums 4.1
 

New York, NY • Baltimore, MD • Vienna, VA • St. Louis, MO • Seatle, WA • 410.327.0007 • info@R2Integrated.com

Bookmark & Share Bookmark and Share