Join our DNN Community    (Newsletter, Tips, Tricks and Forums for DNN Skins & Modules)

 


 
Microsoft Gold Certified Partner - DNN Benefactor

DotNetNuke Powered! 


Friday, November 21, 2008 Register · Login · Contact · Search:  
Company Solutions Portfolio Contact
Forums
Tips and Tricks
Discussion of the popular 'Tips and Tricks' from the Newsletter. Questions or comments regarding the provided tips, or other general tricks of the trade.
Subject: Find and Replace Procedure for Html Module
You are not authorized to post a reply.
Page 1 of 212 > >>
 
Author Messages
kevinmschreiner
Please Wait...
Posts:749

09/30/2005 10:45 AM Alert 

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

Kevin M Schreiner

Business Intelligence Force, Inc. (bi4ce)
jabull
Flyweight
Posts:2

01/18/2006 4:59 PM Alert 

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.

kevinmschreiner
Please Wait...
Posts:749

01/24/2006 9:19 AM Alert 
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.

Kevin M Schreiner

Business Intelligence Force, Inc. (bi4ce)
RegGFX
Featherweight
Posts:10

02/14/2006 2:38 PM Alert 
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.

kevinmschreiner
Please Wait...
Posts:749

02/14/2006 9:49 PM Alert 
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.

Kevin M Schreiner

Business Intelligence Force, Inc. (bi4ce)
RegGFX
Featherweight
Posts:10

10/11/2006 12:53 PM Alert 
This is working great for DNN 2.X however Will this work for DNN 3.x or 4.x

Please advise...

Thanks
kevinmschreiner
Please Wait...
Posts:749

10/19/2006 3:08 PM Alert 
Yes, it works for all current versions of DNN

Kevin M Schreiner

Business Intelligence Force, Inc. (bi4ce)
rtonerii
Superweight
Posts:305

05/21/2007 1:05 PM Alert 
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
dnncreative
Flyweight
Posts:3

05/24/2007 5:04 AM Alert 
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
dnncreative
Flyweight
Posts:3

05/24/2007 5:09 AM Alert 
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

dnncreative
Flyweight
Posts:3

05/25/2007 4:39 AM Alert 
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!
DavidWSnow
Cruiserweight
Posts:127

07/06/2007 10:03 PM Alert 
Great Job Kevin! Very Useful.

/DaveS

DavidWSnow
Cruiserweight
Posts:127

07/07/2007 1:08 PM Alert 
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
DavidWSnow
Cruiserweight
Posts:127

07/27/2007 11:46 AM Alert 
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
contactdp
Superweight
Posts:475

07/27/2007 12:41 PM Alert 
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
R2integrated
AlexeiXX3
Flyweight
Posts:1

08/02/2007 1:08 PM Alert 
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
contactdp
Superweight
Posts:475

08/02/2007 3:22 PM Alert 
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
R2integrated
klb5770
Flyweight
Posts:1

08/14/2007 1:24 PM Alert 
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?
stefanp
Flyweight
Posts:2

09/05/2007 3:24 PM Alert 
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.
DavidWSnow
Cruiserweight
Posts:127

09/05/2007 3:30 PM Alert 
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 > >>
Forums > Dotnetnuke > Tips and Tricks > Find and Replace Procedure for Html Module



ActiveForums 3.6
Latest Post
 
At R2integrated (formerly Bi4ce), we take support seriously.  That's why we support our customers and DNN community with daily monitoring from our experienced engineering team.  We ask that the first step taken is to read the relevant documentation and support forums prior to submitting any questions that may already be available or have been answered.  We ask that you review the documentation that we provide for our products before posting a question.

The Forums are for our customers to chat, exchange ideas and strategies, and submit feedback.  Please be sure to perform keyword searches for previous related forum responses.

To be helpful when submitting a new item, please include the following: 
  1. DNN Version
  2. Module Version
  3. Admin Log Viewer Information
  4. Environment detail: Operating system, .NET framework version, database and version, IIS version, Browser version (if appropriate)
We always try to respond quickly and monitor the forums daily during business hours (EST).  Occasionally, requests for a specific project requirement may not apply for the free support offered. For project specific support please submit via our Information Request form.

Thank you for using our Forums.

Click here to register for the Forums
 
© 2008 by R2integrated (formerly Bi4ce) | DNN® is a registered trademark of DotNetNuke Corporation