FANDOM


  • I've been trying every obvious method I know of, but I can't seem to find any way to get the most recent post under the "thread" namespace. Could anybody help me?

      Loading editor
    • Wait- should this go under API Development?

      I'm new here, sorry-

        Loading editor
    • Yeah, I suppose it should go under API Discussion.

      The most logical way of getting the latest thread I can think of right now is going through all thread pages the MediaWiki API returns, finding the one that isn't a sub-subpage (as in, isn't a reply to a thread) and whose page ID is the highest (meaning, it was posted last) and returning that one. It might not be very fast, though. Here's example code that works for me in my JavaScript console:

      function getLastThread(api, last, offset) {
          api.get({
              action: 'query',
              list: 'allpages',
              apfilterredir: 'nonredirects',
              apfrom: offset,
              aplimit: 'max',
              // This is "Board Thread", change to 1201 for "Thread"
              apnamespace: 2001
          }).then(function(data) {
              if (data.error) {
                  // This will be unneeded on UCP
                  console.error('MediaWiki API error:', data.error);
              } else {
                  data.query.allpages.forEach(function(page) {
                      var firstSlashPosition = page.title.indexOf('/'),
                          lastSlashPosition = page.title.lastIndexOf('/');
                      if (
                          // If there is no maximum ID or we reached a new maximum
                          (!last || page.pageid > last) &&
                          // and there is exactly one slash in the page name
                          firstSlashPosition !== -1 &&
                          firstSlashPosition === lastSlashPosition
                      ) {
                          last = page.pageid;
                      }
                  });
                  // This will probably need to be changed to rawcontinue on UCP
                  if (data['query-continue']) {
                      // Continue searching for the last thread
                      setTimeout(getLastThread.bind(null, api, last, data['query-continue'].allpages.apfrom), 0);
                  } else {
                      console.info('Latest thread:', new mw.Uri(mw.util.getUrl('Thread:' + last)).toString());
                  }
              }
          }).fail(function(error) {
              console.error('AJAX error:', error);
          });
      }
       
      function load() {
          var api = new mw.Api();
          getLastThread(api);
      }
       
      mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(load);
        Loading editor
    • A FANDOM user
        Loading editor
Give Kudos to this message
You've given this message Kudos!
See who gave Kudos to this message
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+