a8fc1476263892ab586409264981946d1ebbccdc
4 * Created on Monday, January 28, 2008
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2008 Brent Garber
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * Query module to get list of random pages
37 class ApiQueryRandom
extends ApiQueryGeneratorBase
{
39 public function __construct($query, $moduleName) {
40 parent
:: __construct($query, $moduleName, 'rn');
43 public function execute() {
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
51 protected function prepareQuery($randstr, $limit, $namespace, &$resultPageSet, $redirect) {
52 $this->resetQueryParams();
53 $this->addTables('page');
54 $this->addOption('LIMIT', $limit);
55 $this->addWhereFld('page_namespace', $namespace);
56 $this->addWhereRange('page_random', 'newer', $randstr, null);
57 $this->addWhereFld('page_is_redirect', $redirect);
58 $this->addOption('USE INDEX', 'page_random');
59 if(is_null($resultPageSet))
60 $this->addFields(array('page_id', 'page_title', 'page_namespace'));
62 $this->addFields($resultPageSet->getPageTableFields());
65 protected function runQuery(&$resultPageSet) {
67 $res = $this->select(__METHOD__
);
69 while($row = $db->fetchObject($res)) {
71 if(is_null($resultPageSet))
74 if(!in_array($row->page_id
, $this->pageIDs
))
76 $fit = $this->getResult()->addValue(
77 array('query', $this->getModuleName()),
78 null, $this->extractRowInfo($row));
80 # We can't really query-continue a random list.
81 # Return an insanely high value so
82 # $count < $limit is false
84 $this->pageIDs
[] = $row->page_id
;
88 $resultPageSet->processDbRow($row);
90 $db->freeResult($res);
94 public function run($resultPageSet = null) {
95 $params = $this->extractRequestParams();
96 $result = $this->getResult();
97 $this->pageIDs
= array();
99 $this->prepareQuery(wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect']);
100 $count = $this->runQuery($resultPageSet);
101 if($count < $params['limit'])
103 /* We got too few pages, we probably picked a high value
104 * for page_random. We'll just take the lowest ones, see
105 * also the comment in Title::getRandomTitle()
107 $this->prepareQuery(0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect']);
108 $this->runQuery($resultPageSet);
111 if(is_null($resultPageSet)) {
112 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
116 private function extractRowInfo($row) {
117 $title = Title
::makeTitle($row->page_namespace
, $row->page_title
);
119 $vals['title'] = $title->getPrefixedText();
120 $vals['ns'] = $row->page_namespace
;
121 $vals['id'] = $row->page_id
;
125 public function getAllowedParams() {
127 'namespace' => array(
128 ApiBase
:: PARAM_TYPE
=> 'namespace',
129 ApiBase
:: PARAM_ISMULTI
=> true
132 ApiBase
:: PARAM_TYPE
=> 'limit',
133 ApiBase
:: PARAM_DFLT
=> 1,
134 ApiBase
:: PARAM_MIN
=> 1,
135 ApiBase
:: PARAM_MAX
=> 10,
136 ApiBase
:: PARAM_MAX2
=> 20
142 public function getParamDescription() {
144 'namespace' => 'Return pages in these namespaces only',
145 'limit' => 'Limit how many random pages will be returned',
146 'redirect' => 'Load a random redirect instead of a random page'
150 public function getDescription() {
151 return array( 'Get a set of random pages',
152 'NOTE: Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, "Main Page" is the first ',
153 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc.',
154 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will get fewer pages. You will not get the same page twice.'
158 protected function getExamples() {
159 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
162 public function getVersion() {
163 return __CLASS__
. ': $Id: ApiQueryRandom.php overlordq$';