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