Using the net API to fetch "Special:Random" doesn't work because MediaWiki
doesn't pass on the option for action=raw to the target random page. (This
makes some sense in that title=Special%3ARandom&action=edit could be
abused, but otherwise it might be worthwhile reporting/patching the bug in
MediaWiki.)
Alternatively, mwlib could be patched to handle 'Special:Random'
specially, and fetch the raw text only after first retrieving a random
title (by not following the redirect from Special:Random).
If you have a wiki stored locally in CDB format, the following is my hack
to patch the mwlib.cdb.Cdb class with a method to get a random entry from
the index:
class Cdb:
...
def randitem(self):
# FIXME: currenlty only gets first for given hash key...
import random
u = random.getrandbits(32)
buf = self.read(8, u << 3 & 2047)
hslots = uint32_unpack(buf[4:])
if not hslots:
raise KeyError
hpos = uint32_unpack(buf[:4])
u >>= 8
u %= hslots
u <<= 3
kpos = hpos + u
for loop in xrange(hslots):
buf = self.read(8, kpos)
pos = uint32_unpack(buf[4:])
kpos += 8
if kpos == hpos + (hslots << 3):
kpos = hpos
if not pos:
continue
u = uint32_unpack(buf[:4])
klen, vlen = struct.unpack("<LL", self.map[pos:pos+8])
pos += 8
key = self.map[pos:pos+klen]
pos += klen
val = self.map[pos:pos+vlen]
pos += vlen
return key, val
raise KeyError
- Joel
On Thu, 28 May 2009 06:39:46 +1000, Deej <[email protected]> wrote:
>
> Is there a way to fetch a random wikipedia page?
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"mwlib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/mwlib?hl=en
-~----------~----~----~----~------~----~------~--~---