Add DROP INDEX support to DatabaseSqlite::replaceVars method
[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
34 class ApiQueryRandom extends ApiQueryGeneratorBase {
35
36 private $pageIDs;
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'rn' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param $randstr
52 * @param $limit
53 * @param $namespace
54 * @param $resultPageSet ApiPageSet
55 * @param $redirect
56 * @return void
57 */
58 protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
59 $this->resetQueryParams();
60 $this->addTables( 'page' );
61 $this->addOption( 'LIMIT', $limit );
62 $this->addWhereFld( 'page_namespace', $namespace );
63 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
64 $this->addWhereFld( 'page_is_redirect', $redirect );
65 $this->addOption( 'USE INDEX', 'page_random' );
66 if ( is_null( $resultPageSet ) ) {
67 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
68 } else {
69 $this->addFields( $resultPageSet->getPageTableFields() );
70 }
71 }
72
73 /**
74 * @param $resultPageSet ApiPageSet
75 * @return int
76 */
77 protected function runQuery( $resultPageSet = null ) {
78 $res = $this->select( __METHOD__ );
79 $count = 0;
80 foreach ( $res as $row ) {
81 $count++;
82 if ( is_null( $resultPageSet ) ) {
83 // Prevent duplicates
84 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
85 $fit = $this->getResult()->addValue(
86 array( 'query', $this->getModuleName() ),
87 null, $this->extractRowInfo( $row ) );
88 if ( !$fit ) {
89 // We can't really query-continue a random list.
90 // Return an insanely high value so
91 // $count < $limit is false
92 return 1E9;
93 }
94 $this->pageIDs[] = $row->page_id;
95 }
96 } else {
97 $resultPageSet->processDbRow( $row );
98 }
99 }
100
101 return $count;
102 }
103
104 /**
105 * @param $resultPageSet ApiPageSet
106 * @return void
107 */
108 public function run( $resultPageSet = null ) {
109 $params = $this->extractRequestParams();
110 $result = $this->getResult();
111 $this->pageIDs = array();
112
113 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
114 $count = $this->runQuery( $resultPageSet );
115 if ( $count < $params['limit'] ) {
116 /* We got too few pages, we probably picked a high value
117 * for page_random. We'll just take the lowest ones, see
118 * also the comment in Title::getRandomTitle()
119 */
120 $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
121 $this->runQuery( $resultPageSet );
122 }
123
124 if ( is_null( $resultPageSet ) ) {
125 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
126 }
127 }
128
129 private function extractRowInfo( $row ) {
130 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
131 $vals = array();
132 $vals['id'] = intval( $row->page_id );
133 ApiQueryBase::addTitleInfo( $vals, $title );
134 return $vals;
135 }
136
137 public function getCacheMode( $params ) {
138 return 'public';
139 }
140
141 public function getAllowedParams() {
142 return array(
143 'namespace' => array(
144 ApiBase::PARAM_TYPE => 'namespace',
145 ApiBase::PARAM_ISMULTI => true
146 ),
147 'limit' => array(
148 ApiBase::PARAM_TYPE => 'limit',
149 ApiBase::PARAM_DFLT => 1,
150 ApiBase::PARAM_MIN => 1,
151 ApiBase::PARAM_MAX => 10,
152 ApiBase::PARAM_MAX2 => 20
153 ),
154 'redirect' => false,
155 );
156 }
157
158 public function getParamDescription() {
159 return array(
160 'namespace' => 'Return pages in these namespaces only',
161 'limit' => 'Limit how many random pages will be returned',
162 'redirect' => 'Load a random redirect instead of a random page'
163 );
164 }
165
166 public function getResultProperties() {
167 return array(
168 '' => array(
169 'id' => 'integer',
170 'ns' => 'namespace',
171 'title' => 'string'
172 )
173 );
174 }
175
176 public function getDescription() {
177 return array(
178 'Get a set of random pages',
179 '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 ',
180 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
181 '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'
182 );
183 }
184
185 public function getExamples() {
186 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
187 }
188
189 public function getHelpUrls() {
190 return 'https://www.mediawiki.org/wiki/API:Random';
191 }
192 }