How do I... List the most popular pages?

Table of contents
  1. 1. Variation for 9.02
    1. 1.1. Code

There is currently no built-in function for retrieving the most popular pages.  However, there is an Deki API function which can be invoked from DekiScript!

For this sample, we use uri.build to build the URI from the site.api URi.  Then, we use web.xml to fetch the document from it (see what the XML looks like).  Now, we iterate over all <page> elements in the document and print out foreach the link, title, and view count using xml.text.

DekiScript retrieves this information as the anonymous user (it does not pass on the context of the current user), so if your MindTouch is private, than this code snippet will return no pages.

<ul block="var pages = web.xml(uri.build(site.api, ['pages', 'popular'], {}))['page']">
    <li foreach="var p in pages" where="__count &lt; 10">{{ web.link(uri.build(site.uri, "index.php", { title: xml.text(p, 'path') ?? "" }), xml.text(p, 'title')) }} ({{ num.format(xml.text(p, 'metrics/metric.views'), '#,#00') }} views)</li>
</ul>

Sample output

Variation for 9.02

This variation is designed to take advantage of the DekiScript innovations found in the 9.02 release.  Create a DekiScript section in WYSIWYG mode and copy-paste the following code into it.  No need to toggle to source mode!

Code

var pages = wiki.api(site.api & 'pages/popular')['page'];
<ul>
    foreach(var p in pages where __count < 10) {
        <li>
            web.link(site.uri & "index.php" & { title: xml.text(p, 'path') ?? "" }, xml.text(p, 'title')); 
            " ("; 
            num.format(xml.text(p, 'metrics/metric.views'), '#,#00'); 
            " views)";
        </li>
    }
</ul>

 

Tag page
Viewing 10 of 10 comments: view all
Hi,
It's a good function but it does not work in my wiki. I pasted it but nothing occurs.
any ideas why this happens?
Thanks! edited 00:04, 6 Feb 2009
Posted 00:02, 6 Feb 2009
Did you paste it in source mode? Make sure to NOT paste it in WYSIWYG mode, because then it won't work.
Posted 01:38, 6 Feb 2009
Can this script be placed in the Custom HTML sections of the Fiesta skin? If so, do I need to use the <eval:> tags?
Posted 14:11, 9 Apr 2009
No, custom areas only allow plain HTML. Instead, you can use a Global Template to generate output for the custom HTML areas. It's a bit more involved: create Global Template, config Deki to execute Global Template page, target HTML areas, whitelist targetable HTML areas. edited 16:20, 9 Apr 2009
Posted 16:20, 9 Apr 2009
Yes SteveB, I pasted in source mode but nothing occurs. is there any other config I need?
I have kilen woods v.8.08.2
regards! edited 15:23, 16 Apr 2009
Posted 21:16, 13 Apr 2009
Consider using the wiki.api function instead for private wikis.
Posted 03:51, 22 Apr 2009
Hi StevevB, do you mean use wiki.api instead of site.api and site.uri? I did it but still nothing occurs.
I also used wiki.api(site.api) and it threw this:
content/body/ul, line 1, column 49: ")" expected

perhaps misunderstood, :S what do I change?
thanks!!
Posted 23:01, 22 Apr 2009
Oh, sorry, I meant use "wiki.api" instead of "web.xml".
Posted 09:33, 30 May 2009
Still don't get it.
Posted 12:59, 2 Jun 2009
I added a variation written for 9.02 that uses wiki.api. Hopefully that will clarify it for you.
Posted 05:53, 3 Jun 2009
Viewing 10 of 10 comments: view all
You must login to post a comment.