6 * Created on Monday, January 28, 2008
8 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
29 * Query module to get list of random pages
34 class ApiQueryRandom
extends ApiQueryGeneratorBase
{
36 public function __construct( $query, $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'rn' );
40 public function execute() {
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
52 * @param $resultPageSet ApiPageSet
56 protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
57 $this->resetQueryParams();
58 $this->addTables( 'page' );
59 $this->addOption( 'LIMIT', $limit );
60 $this->addWhereFld( 'page_namespace', $namespace );
61 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
62 $this->addWhereFld( 'page_is_redirect', $redirect );
63 $this->addOption( 'USE INDEX', 'page_random' );
64 if ( is_null( $resultPageSet ) ) {
65 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
67 $this->addFields( $resultPageSet->getPageTableFields() );
72 * @param $resultPageSet ApiPageSet
75 protected function runQuery( $resultPageSet = null ) {
76 $res = $this->select( __METHOD__
);
78 foreach ( $res as $row ) {
80 if ( is_null( $resultPageSet ) ) {
82 if ( !in_array( $row->page_id
, $this->pageIDs
) ) {
83 $fit = $this->getResult()->addValue(
84 array( 'query', $this->getModuleName() ),
85 null, $this->extractRowInfo( $row ) );
87 // We can't really query-continue a random list.
88 // Return an insanely high value so
89 // $count < $limit is false
92 $this->pageIDs
[] = $row->page_id
;
95 $resultPageSet->processDbRow( $row );
103 * @param $resultPageSet ApiPageSet
106 public function run( $resultPageSet = null ) {
107 $params = $this->extractRequestParams();
108 $result = $this->getResult();
109 $this->pageIDs
= array();
111 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
112 $count = $this->runQuery( $resultPageSet );
113 if ( $count < $params['limit'] ) {
114 /* We got too few pages, we probably picked a high value
115 * for page_random. We'll just take the lowest ones, see
116 * also the comment in Title::getRandomTitle()
118 $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
119 $this->runQuery( $resultPageSet );
122 if ( is_null( $resultPageSet ) ) {
123 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
127 private function extractRowInfo( $row ) {
128 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
130 $vals['id'] = intval( $row->page_id
);
131 ApiQueryBase
::addTitleInfo( $vals, $title );
135 public function getCacheMode( $params ) {
139 public function getAllowedParams() {
141 'namespace' => array(
142 ApiBase
::PARAM_TYPE
=> 'namespace',
143 ApiBase
::PARAM_ISMULTI
=> true
146 ApiBase
::PARAM_TYPE
=> 'limit',
147 ApiBase
::PARAM_DFLT
=> 1,
148 ApiBase
::PARAM_MIN
=> 1,
149 ApiBase
::PARAM_MAX
=> 10,
150 ApiBase
::PARAM_MAX2
=> 20
156 public function getParamDescription() {
158 'namespace' => 'Return pages in these namespaces only',
159 'limit' => 'Limit how many random pages will be returned',
160 'redirect' => 'Load a random redirect instead of a random page'
164 public function getDescription() {
166 'Get a set of random pages',
167 '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 ',
168 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
169 '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'
173 public function getExamples() {
174 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
177 public function getVersion() {
178 return __CLASS__
. ': $Id: ApiQueryRandom.php overlordq$';