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 public function run($resultPageSet = null) {
52 $params = $this->extractRequestParams();
53 $result = $this->getResult();
54 $randstr = wfRandom();
57 $this->addTables('page');
58 $this->addOption('LIMIT', $params['limit']);
59 $this->addWhereFld('page_namespace', $params['namespace']);
60 $this->addWhereRange('page_random', 'newer', $randstr, null);
61 $this->addWhere(array('page_is_redirect' => 0));
62 $this->addOption('USE INDEX', 'page_random');
63 if(is_null($resultPageSet))
64 $this->addFields(array('page_id', 'page_title', 'page_namespace'));
66 $this->addFields($resultPageSet->getPageTableFields());
69 $res = $this->select(__METHOD__
);
70 while($row = $db->fetchObject($res)) {
71 if(is_null($resultPageSet))
72 $data[] = $this->extractRowInfo($row);
74 $resultPageSet->processDbRow($row);
76 $db->freeResult($res);
78 if(is_null($resultPageSet)) {
79 $result->setIndexedTagName($data, 'page');
80 $result->addValue('query', $this->getModuleName(), $data);
84 private function extractRowInfo($row) {
85 $title = Title
::makeTitle($row->page_namespace
, $row->page_title
);
87 $vals['title'] = $title->getPrefixedText();
88 $vals['ns'] = $row->page_namespace
;
89 $vals['id'] = $row->page_id
;
93 public function getAllowedParams() {
96 ApiBase
:: PARAM_TYPE
=> 'namespace',
97 ApiBase
:: PARAM_ISMULTI
=> true
100 ApiBase
:: PARAM_TYPE
=> 'limit',
101 ApiBase
:: PARAM_DFLT
=> 1,
102 ApiBase
:: PARAM_MIN
=> 1,
103 ApiBase
:: PARAM_MAX
=> 10,
104 ApiBase
:: PARAM_MAX2
=> 20
109 public function getParamDescription() {
111 'namespace' => 'Return pages in these namespaces only',
112 'limit' => 'Limit how many random pages will be returned'
116 public function getDescription() {
117 return array( 'Get a set of random pages',
118 'NOTE: When using a namespace filter, this module may return an empty result. In that case, retry the request'
122 protected function getExamples() {
123 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
126 public function getVersion() {
127 return __CLASS__
. ': $Id: ApiQueryRandom.php overlordq$';