PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / api / ApiQueryAllRevisions.php
blobda895e2e42243725096ca7c2b7fe67920dbb5091
1 <?php
2 /**
3 * Created on Sep 27, 2015
5 * Copyright © 2015 Brad Jorsch "bjorsch@wikimedia.org"
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
25 /**
26 * Query module to enumerate all revisions.
28 * @ingroup API
29 * @since 1.27
31 class ApiQueryAllRevisions extends ApiQueryRevisionsBase {
33 public function __construct( ApiQuery $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'arv' );
37 /**
38 * @param ApiPageSet $resultPageSet
39 * @return void
41 protected function run( ApiPageSet $resultPageSet = null ) {
42 $db = $this->getDB();
43 $params = $this->extractRequestParams( false );
45 $result = $this->getResult();
47 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
49 // Namespace check is likely to be desired, but can't be done
50 // efficiently in SQL.
51 $miser_ns = null;
52 $needPageTable = false;
53 if ( $params['namespace'] !== null ) {
54 $params['namespace'] = array_unique( $params['namespace'] );
55 sort( $params['namespace'] );
56 if ( $params['namespace'] != MWNamespace::getValidNamespaces() ) {
57 $needPageTable = true;
58 if ( $this->getConfig()->get( 'MiserMode' ) ) {
59 $miser_ns = $params['namespace'];
60 } else {
61 $this->addWhere( array( 'page_namespace' => $params['namespace'] ) );
66 $this->addTables( 'revision' );
67 if ( $resultPageSet === null ) {
68 $this->parseParameters( $params );
69 $this->addTables( 'page' );
70 $this->addJoinConds(
71 array( 'page' => array( 'INNER JOIN', array( 'rev_page = page_id' ) ) )
73 $this->addFields( Revision::selectFields() );
74 $this->addFields( Revision::selectPageFields() );
76 // Review this depeneding on the outcome of T113901
77 $this->addOption( 'STRAIGHT_JOIN' );
78 } else {
79 $this->limit = $this->getParameter( 'limit' ) ?: 10;
80 $this->addFields( array( 'rev_timestamp', 'rev_id' ) );
81 if ( $params['generatetitles'] ) {
82 $this->addFields( array( 'rev_page' ) );
85 if ( $needPageTable ) {
86 $this->addTables( 'page' );
87 $this->addJoinConds(
88 array( 'page' => array( 'INNER JOIN', array( 'rev_page = page_id' ) ) )
90 $this->addFieldsIf( array( 'page_namespace' ), (bool)$miser_ns );
92 // Review this depeneding on the outcome of T113901
93 $this->addOption( 'STRAIGHT_JOIN' );
97 $dir = $params['dir'];
98 $this->addTimestampWhereRange( 'rev_timestamp', $dir, $params['start'], $params['end'] );
100 if ( $this->fld_tags ) {
101 $this->addTables( 'tag_summary' );
102 $this->addJoinConds(
103 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
105 $this->addFields( 'ts_tags' );
108 if ( $this->fetchContent ) {
109 $this->addTables( 'text' );
110 $this->addJoinConds(
111 array( 'text' => array( 'INNER JOIN', array( 'rev_text_id=old_id' ) ) )
113 $this->addFields( 'old_id' );
114 $this->addFields( Revision::selectTextFields() );
117 if ( $params['user'] !== null ) {
118 $id = User::idFromName( $params['user'] );
119 if ( $id ) {
120 $this->addWhereFld( 'rev_user', $id );
121 } else {
122 $this->addWhereFld( 'rev_user_text', $params['user'] );
124 } elseif ( $params['excludeuser'] !== null ) {
125 $id = User::idFromName( $params['excludeuser'] );
126 if ( $id ) {
127 $this->addWhere( 'rev_user != ' . $id );
128 } else {
129 $this->addWhere( 'rev_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
133 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
134 // Paranoia: avoid brute force searches (bug 17342)
135 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
136 $bitmask = Revision::DELETED_USER;
137 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
138 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
139 } else {
140 $bitmask = 0;
142 if ( $bitmask ) {
143 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
147 if ( $params['continue'] !== null ) {
148 $op = ( $dir == 'newer' ? '>' : '<' );
149 $cont = explode( '|', $params['continue'] );
150 $this->dieContinueUsageIf( count( $cont ) != 2 );
151 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
152 $rev_id = (int)$cont[1];
153 $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
154 $this->addWhere( "rev_timestamp $op $ts OR " .
155 "(rev_timestamp = $ts AND " .
156 "rev_id $op= $rev_id)" );
159 $this->addOption( 'LIMIT', $this->limit + 1 );
161 $sort = ( $dir == 'newer' ? '' : ' DESC' );
162 $orderby = array();
163 // Targeting index rev_timestamp, user_timestamp, or usertext_timestamp
164 // But 'user' is always constant for the latter two, so it doesn't matter here.
165 $orderby[] = "rev_timestamp $sort";
166 $orderby[] = "rev_id $sort";
167 $this->addOption( 'ORDER BY', $orderby );
169 $res = $this->select( __METHOD__ );
170 $pageMap = array(); // Maps rev_page to array index
171 $count = 0;
172 $nextIndex = 0;
173 $generated = array();
174 foreach ( $res as $row ) {
175 if ( ++$count > $this->limit ) {
176 // We've had enough
177 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
178 break;
181 // Miser mode namespace check
182 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
183 continue;
186 if ( $resultPageSet !== null ) {
187 if ( $params['generatetitles'] ) {
188 $generated[$row->rev_page] = $row->rev_page;
189 } else {
190 $generated[] = $row->rev_id;
192 } else {
193 $revision = Revision::newFromRow( $row );
194 $rev = $this->extractRevisionInfo( $revision, $row );
196 if ( !isset( $pageMap[$row->rev_page] ) ) {
197 $index = $nextIndex++;
198 $pageMap[$row->rev_page] = $index;
199 $title = $revision->getTitle();
200 $a = array(
201 'pageid' => $title->getArticleID(),
202 'revisions' => array( $rev ),
204 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
205 ApiQueryBase::addTitleInfo( $a, $title );
206 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $index, $a );
207 } else {
208 $index = $pageMap[$row->rev_page];
209 $fit = $result->addValue(
210 array( 'query', $this->getModuleName(), $index, 'revisions' ),
211 null, $rev );
213 if ( !$fit ) {
214 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
215 break;
220 if ( $resultPageSet !== null ) {
221 if ( $params['generatetitles'] ) {
222 $resultPageSet->populateFromPageIDs( $generated );
223 } else {
224 $resultPageSet->populateFromRevisionIDs( $generated );
226 } else {
227 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'page' );
231 public function getAllowedParams() {
232 $ret = parent::getAllowedParams() + array(
233 'user' => array(
234 ApiBase::PARAM_TYPE => 'user',
236 'namespace' => array(
237 ApiBase::PARAM_ISMULTI => true,
238 ApiBase::PARAM_TYPE => 'namespace',
239 ApiBase::PARAM_DFLT => null,
241 'start' => array(
242 ApiBase::PARAM_TYPE => 'timestamp',
244 'end' => array(
245 ApiBase::PARAM_TYPE => 'timestamp',
247 'dir' => array(
248 ApiBase::PARAM_TYPE => array(
249 'newer',
250 'older'
252 ApiBase::PARAM_DFLT => 'older',
253 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
255 'excludeuser' => array(
256 ApiBase::PARAM_TYPE => 'user',
258 'continue' => array(
259 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
261 'generatetitles' => array(
262 ApiBase::PARAM_DFLT => false,
266 if ( $this->getConfig()->get( 'MiserMode' ) ) {
267 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
268 'api-help-param-limited-in-miser-mode',
272 return $ret;
275 protected function getExamplesMessages() {
276 return array(
277 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
278 => 'apihelp-query+allrevisions-example-user',
279 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
280 => 'apihelp-query+allrevisions-example-ns-main',
284 public function getHelpUrls() {
285 return 'https://www.mediawiki.org/wiki/API:Allrevisions';