Version 1, changed by ScottMcmullan. 11/03/2005. Show version history
The basic approach is to loop over each tag listed in the a page's main/tags property and then do a search for other pages containing that tag in its main/tags property. If found, pages that share that tag are listed.
Here's the simple TagForm that enables editing a page's tags:
!!${page/name}'s tags:
<jot:declareForm>
<jot:field name="main/tags" type="stringList" size="50" />
<span style="color: #666">Enter words separated by commas to help identify this page, e.g. ~rss, faq, wonderful, whatever~</span>
</jot:declareForm>
Here's all the code on one page: RelatedTagsPluginV1.
Here's a snapshot of the code that lists other pages that share tags with the current page. This is actually theh very first version of the plugin, which simply displayed a link to each page.
(Note: The full code tries to determine the type of each page so it can use a friendlier name (e.g. instead of ForumTopic278) and give a hint as to what kind of page it is.)
....
<jot:nodeinfo name="p" href="wiki:/${page/name}" />
<div id="related-tags" style="border: 1px solid #8cacbb; background-color: #eee; padding: 8px; margin-top: 8px; margin-bottom: 8px">
<jot:if test="${util:isDefined(page/main/tags)}">
<jot:then>
Page's tags (<a href="wiki:/${page/name}?edit=1&form=/TagForm">edit</a>): ${page/main/tags}<br /><br />
Related pages by tag:<br /><br />
<jot:loop over="${p/main/tags}">
<jot:var key="tag" value="${it}" />
<jot:search forAll="1" filter="${util:listContains(it/main/tags, tag)}" set="taggedPages" />
<jot:if test="${taggedPages/totalSize > 1}">
<jot:then>
» ${tag}:
<ul>
<jot:loop over="taggedPages" set="tp">
<jot:if test="${not(tp/name = page/name)}">
<jot:then>
<li><a href="wiki:/${tp/name}">${tp/name}</a></li>
</jot:then>
</jot:if>
</jot:loop>
</ul>
</jot:then>
<jot:else>
No others found.
</jot:else>
</jot:if>
</jot:loop>
....
Here's a snapshot of the code that walks the page's revision history to display all authors who have contributed or edited the page's tags:
<jot:script>
<![CDATA[
//returns a link to the user's profile, as appropriate for this wiki
function getUserProfileLink(user) {
return("<a href='/WikiHome/MemberProfiles/"+user+"-profile' class='tagger'>"+user+"</a>");
}
var pg = jot.pages[page.path];
//List users who have tagged this page
if (typeof pg.main.tags != "undefined") {
try { var tot = pg.revision; } catch (e) {}
// Write each change to an array
var taggerArray = new Array();
var alreadyCounted = new Array();
for (i=1; i<=tot; i++) {
if (i>1) {
var currRev = pg.otherRevision(i);
var prevRev = currRev.prevRevision;
//make sure main/tags property is defined for all pages before accessing it
if (typeof currRev.main.tags != "undefined" && typeof prevRev.main.tags != "undefined") {
if (currRev.main.tags != prevRev.main.tags && typeof alreadyCounted[currRev.user] == "undefined") {
//taggerArray.push("<a href='"+currRev.user+"-profile' class='tagger'>" + currRev.user + "</a>");
taggerArray.push(getUserProfileLink(currRev.user));
alreadyCounted[currRev.user] = "yep";
}
}
}
}
//write out list of taggers
if (taggerArray.length>0) {
jot.out.write("<br/><span class='tagger-log'>Taggers:</span><br/>\r\n");
for (j=0; j<taggerArray.length; j++) {
jot.out.write(taggerArray[j] + "<br/>\r\n");
}
}
}
]]>
</jot:script>