Merge "Sort 'MediaWiki' resources definitions alphabetically"
[lhc/web/wiklou.git] / includes / api / ApiQueryRandom.php
1 <?php
2
3 /**
4 *
5 *
6 * Created on Monday, January 28, 2008
7 *
8 * Copyright © 2008 Brent Garber
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 */
27
28 /**
29 * Query module to get list of random pages
30 *
31 * @ingroup API
32 */
33 class ApiQueryRandom extends ApiQueryGeneratorBase {
34 private $pageIDs;
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'rn' );
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
46 }
47
48 /**
49 * @param $randstr
50 * @param $limit
51 * @param $namespace
52 * @param $resultPageSet ApiPageSet
53 * @param $redirect
54 * @return void
55 */
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' ) );
66 } else {
67 $this->addFields( $resultPageSet->getPageTableFields() );
68 }
69 }
70
71 /**
72 * @param $resultPageSet ApiPageSet
73 * @return int
74 */
75 protected function runQuery( $resultPageSet = null ) {
76 $res = $this->select( __METHOD__ );
77 $count = 0;
78 foreach ( $res as $row ) {
79 $count++;
80 if ( is_null( $resultPageSet ) ) {
81 // Prevent duplicates
82 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
83 $fit = $this->getResult()->addValue(
84 array( 'query', $this->getModuleName() ),
85 null, $this->extractRowInfo( $row ) );
86 if ( !$fit ) {
87 // We can't really query-continue a random list.
88 // Return an insanely high value so
89 // $count < $limit is false
90 return 1E9;
91 }
92 $this->pageIDs[] = $row->page_id;
93 }
94 } else {
95 $resultPageSet->processDbRow( $row );
96 }
97 }
98
99 return $count;
100 }
101
102 /**
103 * @param $resultPageSet ApiPageSet
104 * @return void
105 */
106 public function run( $resultPageSet = null ) {
107 $params = $this->extractRequestParams();
108 $result = $this->getResult();
109 $this->pageIDs = array();
110
111 $this->prepareQuery(
112 wfRandom(),
113 $params['limit'],
114 $params['namespace'],
115 $resultPageSet,
116 $params['redirect']
117 );
118 $count = $this->runQuery( $resultPageSet );
119 if ( $count < $params['limit'] ) {
120 /* We got too few pages, we probably picked a high value
121 * for page_random. We'll just take the lowest ones, see
122 * also the comment in Title::getRandomTitle()
123 */
124 $this->prepareQuery(
125 0,
126 $params['limit'] - $count,
127 $params['namespace'],
128 $resultPageSet,
129 $params['redirect']
130 );
131 $this->runQuery( $resultPageSet );
132 }
133
134 if ( is_null( $resultPageSet ) ) {
135 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
136 }
137 }
138
139 private function extractRowInfo( $row ) {
140 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
141 $vals = array();
142 $vals['id'] = intval( $row->page_id );
143 ApiQueryBase::addTitleInfo( $vals, $title );
144
145 return $vals;
146 }
147
148 public function getCacheMode( $params ) {
149 return 'public';
150 }
151
152 public function getAllowedParams() {
153 return array(
154 'namespace' => array(
155 ApiBase::PARAM_TYPE => 'namespace',
156 ApiBase::PARAM_ISMULTI => true
157 ),
158 'limit' => array(
159 ApiBase::PARAM_TYPE => 'limit',
160 ApiBase::PARAM_DFLT => 1,
161 ApiBase::PARAM_MIN => 1,
162 ApiBase::PARAM_MAX => 10,
163 ApiBase::PARAM_MAX2 => 20
164 ),
165 'redirect' => false,
166 );
167 }
168
169 public function getParamDescription() {
170 return array(
171 'namespace' => 'Return pages in these namespaces only',
172 'limit' => 'Limit how many random pages will be returned',
173 'redirect' => 'Load a random redirect instead of a random page'
174 );
175 }
176
177 public function getResultProperties() {
178 return array(
179 '' => array(
180 'id' => 'integer',
181 'ns' => 'namespace',
182 'title' => 'string'
183 )
184 );
185 }
186
187 public function getDescription() {
188 return array(
189 'Get a set of random pages',
190 'NOTE: Pages are listed in a fixed sequence, only the starting point is random.',
191 ' This means that if, for example, "Main Page" is the first random page on',
192 ' your list, "List of fictional monkeys" will *always* be second, "List of',
193 ' people on stamps of Vanuatu" third, etc',
194 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will',
195 ' get fewer pages. You will not get the same page twice'
196 );
197 }
198
199 public function getExamples() {
200 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
201 }
202
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Random';
205 }
206 }