1
tcnet
Hack for Automatic Wordbook Links in News Articles
  • 2006/8/17 5:18

  • tcnet

  • Friend of XOOPS

  • Posts: 297

  • Since: 2006/5/12


This experimental hack searches News 1.44 articles looking for Wordbook 1.16 terms. It will automatically locate, code links and add styling emphasis to any Wordbook terms found in the news article. It can be modified to create links to Wordbook definitions or referral URLs.

The code was copied from Wordbook 1.6 entry.php (line 112 thru 141). I am not a coder, so I can't really develop this much further. I offer it here in hope that someone with more skill will improve it or develop a better method.

Thanks to herve (News 1.44) and hsalasar (Wordbook).
Insert the following code into a copy of news/article.php after line 182 "$bodytext = $article->bodytext();"
 
//** TCNET HACK News module for Wordbook Glossary links **
//Warning: Experimental Hack! Use at your own risk! Make backups first! 
//News article's with hyperlink text containing glossary 
//terms will be split or completely broken by this hack.

$parts explode("|TERMSON|"$bodytext);
        
// Retrieve all terms from the Wordbook glossary.
$allterms $xoopsDB -> query"SELECT entryID, term FROM " $xoopsDB -> prefix"wbentries" ));
while ( list( 
$entryID$term ) = $xoopsDB -> fetchrow($allterms ))
{
foreach(
$parts as $key=>$part)
{
// singular
$term_q preg_quote($term'/');
$search_term "/b$term_qb/i";
$replace_term ".XOOPS_URL."/modules/wordbook/entry.php?entryID=".$entryID."' title=' View a definition for ".$term."'>".$term."";
$parts[$key] = preg_replace($search_term$replace_term,$parts[$key]);

// plural
$term $term."s";
$term_q preg_quote($term'/');
$search_term "/b$term_qb/i";
$replace_term ".XOOPS_URL."/modules/wordbook/entry.php?entryID=".$entryID."' title=' View a definition for ".$term."'>".$term."";
$parts[$key] = preg_replace($search_term$replace_term,$parts[$key]);

// plural with e
$term $term."es";
$term_q preg_quote($term'/');
$search_term "/b$term_qb/i";
$replace_term ".XOOPS_URL."/modules/wordbook/entry.php?entryID=".$entryID."' title=' View a definition for ".$term."'>".$term."";
$parts[$key] = preg_replace($search_term$replace_term,$parts[$key]);
}
}
$bodytext implode("|TERMSON|"$parts);

//**** END TCNET HACK for Wordbook module terms ****


There are two ways to apply this hack:

1) Glossary links always on.
-Insert the hack into news/article.php after line 182.
-Save and publish article.php to the news directory.

I think this option will help search engine rankings because it automatically locates and adds emphasis to specific keywords in the article. The disadvantage to this option is that it may cause high cpu load if you have long articles or many glossary terms.


2) Glossary links off with a link to display them.
-Copy news/article.php and rename as article_glossary.php
-Insert the hack after line 182.
-Publish article_glossary.php to the news module directory.
-Publish a "Glossary View" link to articles_glossary.php in news/templates/news_item.html.

See a demo by viewing any news article at http://www.technicalcrew.com and selecting "View with Glossary Links".

This option causes less server load because it only displays the links when requested. The disadvantage is that search engines may penalize you for duplicate content. For this option add rel="nofollow" to the "Glossary View" link to exclude article_glossary.php from robots.

Sample code for "View Glossary" link in the news_item.html template:
<a href="<{xoops_url}>/modules/news/article_glossary.php?storyid=<{$story.id}>" title="Click to display glossary links." rel="nofollow">View with Glossary Linksa>


>>>>>>>>>>>>>>>

Display Links to a WB Definition or URL

Using Wordbook's url field, you can modify this hack to create internal or external links instead of definition links.

-Modify db query:
replace:
$allterms $xoopsDB -> query"SELECT entryID, term FROM " $xoopsDB -> prefix"wbentries" ));
while ( list( 
$entryID$term ) = $xoopsDB -> fetchrow($allterms ))

with:
$allterms $xoopsDB -> query"SELECT url, term FROM " $xoopsDB -> prefix"wbentries" ));
while ( list( 
$term_url$term ) = $xoopsDB -> fetchrow($allterms ))


-Modify the link text:
replace:
<
a style='color: #009933; text-decoration: underline; 'href='".XOOPS_URL."/modules/wordbook/entry.php?entryID=".$entryID."' title=' View a definition for ".$term."'>".$term."a>

with:
<
a style='color: #009933; text-decoration: underline; ' href='".$term_url."' title='Visit ".$term_url."'>".$term."a>


>>>>>>>>>>>>>>>

Warning:
This hack may not be suitable if your articles have link anchor text that contains glossary terms.

-Anchor text that contains a glossary term is split into multiple working links. Example

-Anchor text displaying URLs that contain glossary terms are split into multiple links and the original href url is ruined. Example

I need help coming up with some code that will ignore text in anchor tags. Any help appreciated!

>>>>>>>>>>>>>>>
QUESTION:
The explode ">" only worked with dhtml or xoopseditor text and when I replaced the ">" with any other character, the html worked as well. What are the proper characters to use in that parameter? Could it be used as a string variable in an article's bodytext to switch the glossary links on? Example: Explode only when the string "|TERMSON|" is found in the body text.
Wordbook Code changes:
//$parts = explode(">", $definition); //Original code
$parts explode("|TERMSON|"$bodytext); //New Code
//replaced $definition with $bodytext and ">" with |TERMSON|.

>>>>>>>>>>

Possible Future Improvements:
-Modify the hack to work with other glossary modules. WB is inactive and outdated.
-Modify the hack to work with other popular content modules
-Automatically add found terms to the article's keyword meta tag.
-Open the definition in a popup window.
-Rollover link and definition in tool tip box.


Thanks for your constructive feedback. This is my first published hack, so please be gentle.*/

2
Shine
Re: Hack for Automatic Wordbook Links in News Articles
  • 2006/8/17 6:50

  • Shine

  • Just can't stay away

  • Posts: 822

  • Since: 2002/7/22


No coder either, but thanks for sharing this wonderfull hack.
Indeed I hope somebody can help out in solving the Anchor text displaying URLs problem, so this hack will be even more functional.
Also one question: Since you can turn this feature on within a newsarticle, would it be possible to turn this feature also off within a/the same newsarticle? This would make this hack even more complete.

Grtz., Shine

3
hervet
Re: Hack for Automatic Wordbook Links in News Articles
  • 2006/8/17 7:13

  • hervet

  • Friend of XOOPS

  • Posts: 2267

  • Since: 2003/11/4


This is excellent !
Congratulations

4
tcnet
Re: Hack for Automatic Wordbook Links in News Articles
  • 2006/8/24 17:10

  • tcnet

  • Friend of XOOPS

  • Posts: 297

  • Since: 2006/5/12


Thanks to Hervet and Shine for the encouragement. I was inspired to improve the code and add functions resulting in a completely new version.

NEW! Hack for Automatic Wordbook Links in News Articles Version 2

This version finds Wordbook terms and creates links that will popup the term's definition when clicked. This version fixes a problem with glossary terms found in anchor text containing URLs. This version displays either a "Glossary View On" or "Glossary View Off" link depending on page view. It has been beta tested in XOOPS 2.2.4 with DHTML and WYSIWYG encoded text.

Copy news/article.php and rename as article_glossary.php.
Insert section1 after line 120 ( include_once "../../mainfile.php"; )
//*** TCNET HACK for Wordbook links in News Articles **
//Copy news/article.php and rename as article_glossary.php
//Section1: Insert in news/article_glossary.php 
// after line 120 ( include_once "../../mainfile.php"; )
//If a glossary link is clicked this catches the request,
// and then fetches and displays the definition.

$op '';
if ( isset( 
$_GET['op'] ) ) $op $_GET['op'];

switch ( 
$op )
{    
case 
"def":
include_once 
XOOPS_ROOT_PATH "/class/module.textsanitizer.php"
$myts =& MyTextSanitizer::getInstance();
$term '';
$definition '';
$entryID = isset($_GET['entryID']) ? intval($_GET['entryID']) : 0;
$result $xoopsDB -> query"SELECT term, definition FROM " $xoopsDB -> prefix"wbentries" ) . " WHERE $entryID = entryID"1);
list(
$term$definition ) = $xoopsDB->fetchRow($result);
$term ucfirst($myts -> makeTboxData4Show$term ));
$definition $myts -> displayTarea$definition1011);
echo 
'Definition for '</span><span style="color: #007700">.</span><span style="color: #0000BB">$term</span><span style="color: #007700">.</span><span style="color: #DD0000">'

'.$term.'

'.$definition.'
'
;
exit();
}
//**  END TCNET HACK Section 1**


Insert section2 after old line 183 (now near line 222)
//** TCNET HACK News module for Wordbook Links Section2  **
// Section2: Insert after line 183($bodytext = $article->bodytext();)
//This section searches the news article for glossary
//terms and creates links to the matches.

$glossary_switch "1";
$xoopsTpl->assign('glossary_switch'$glossary_switch);
$parts explode("|TERMSON|"$bodytext);
    
// Retrieve all terms from the glossary.
$allterms $xoopsDB -> query"SELECT entryID, term FROM " $xoopsDB -> prefix"wbentries" ));
while ( list( 
$entryID$term ) = $xoopsDB -> fetchrow($allterms ))
{
foreach(
$parts as $key=>$part)
{

//Open the definition in a popup
$replace_term '.$term.'" style="color: #009933; text-decoration: none;" href='javascript:openWithSelfMain("'.XOOPS_URL.'/modules/news/article_glossary.php?op=def&entryID='.$entryID.'","Definition",600,200);'>'.$term.'';

$parts[$key] = preg_replace("/b($term)(?=[^>]*<)b/i"$replace_term$parts[$key]);
}
}
$bodytext implode("|TERMSON|"$parts);

//** END TCNET HACK Section2


Place a hyperlink in news/templates/news_item.html that will open articles_glossary.php when selected. The following code now provides "Glossary View" on or off links depending on page view. Thanks to Shine for the suggestion!

Insert the following in templates/news_item.html after line 10.

<br/>
<{if 
$glossary_switch == ""}>
<
a href="<{$xoops_url}>/modules/news/article_glossary.php?storyid=<{$story.id}>" title="Click to display glossary links." rel="nofollow">View Glossary Linksa>                  
<{else}>
<
a href="<{$xoops_url}>/modules/news/article.php?storyid=<{$story.id}>" title="Click to remove glossary links.">Remove Glossary Linksa>                                   
<{/if}>


View a demo at http://www.technicalcrew.com/modules/news.

Thanks for reviewing my hack. Constructive comments and suggestions are appreciated.*/

Login

Who's Online

185 user(s) are online (112 user(s) are browsing Support Forums)


Members: 0


Guests: 185


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits