]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryDeletedrevs.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2
3 /*
4  * Created on Jul 2, 2007
5  *
6  * API for MediaWiki 1.8+
7  *
8  * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  * http://www.gnu.org/copyleft/gpl.html
24  */
25
26 if (!defined('MEDIAWIKI')) {
27         // Eclipse helper - will be ignored in production
28         require_once ('ApiQueryBase.php');
29 }
30
31 /**
32  * Query module to enumerate all available pages.
33  *
34  * @ingroup API
35  */
36 class ApiQueryDeletedrevs extends ApiQueryBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'dr');
40         }
41
42         public function execute() {
43
44                 global $wgUser;
45                 // Before doing anything at all, let's check permissions
46                 if(!$wgUser->isAllowed('deletedhistory'))
47                         $this->dieUsage('You don\'t have permission to view deleted revision information', 'permissiondenied');
48
49                 $db = $this->getDB();
50                 $params = $this->extractRequestParams(false);
51                 $prop = array_flip($params['prop']);
52                 $fld_revid = isset($prop['revid']);
53                 $fld_user = isset($prop['user']);
54                 $fld_comment = isset($prop['comment']);
55                 $fld_minor = isset($prop['minor']);
56                 $fld_len = isset($prop['len']);
57                 $fld_content = isset($prop['content']);
58                 $fld_token = isset($prop['token']);
59
60                 $result = $this->getResult();
61                 $pageSet = $this->getPageSet();
62                 $titles = $pageSet->getTitles();
63                 $data = array();
64
65                 $this->addTables('archive');
66                 $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
67                 if($fld_revid)
68                         $this->addFields('ar_rev_id');
69                 if($fld_user)
70                         $this->addFields('ar_user_text');
71                 if($fld_comment)
72                         $this->addFields('ar_comment');
73                 if($fld_minor)
74                         $this->addFields('ar_minor_edit');
75                 if($fld_len)
76                         $this->addFields('ar_len');
77                 if($fld_content)
78                 {
79                         $this->addTables('text');
80                         $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
81                         $this->addWhere('ar_text_id = old_id');
82
83                         // This also means stricter restrictions
84                         if(!$wgUser->isAllowed('undelete'))
85                                 $this->dieUsage('You don\'t have permission to view deleted revision content', 'permissiondenied');
86                 }
87                 // Check limits
88                 $userMax = $fld_content ? ApiBase :: LIMIT_SML1 : ApiBase :: LIMIT_BIG1;
89                 $botMax  = $fld_content ? ApiBase :: LIMIT_SML2 : ApiBase :: LIMIT_BIG2;
90
91                 $limit = $params['limit'];
92
93                 if( $limit == 'max' ) {
94                         $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
95                         $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
96                 }
97
98                 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
99
100                 if($fld_token)
101                         // Undelete tokens are identical for all pages, so we cache one here
102                         $token = $wgUser->editToken();
103
104                 // We need a custom WHERE clause that matches all titles.
105                 if(count($titles) > 0)
106                 {
107                         $lb = new LinkBatch($titles);
108                         $where = $lb->constructSet('ar', $db);
109                         $this->addWhere($where);
110                 } else {
111                         $this->dieUsage('You have to specify a page title or titles');
112                 }
113
114                 $this->addOption('LIMIT', $limit + 1);
115                 $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
116                 $res = $this->select(__METHOD__);
117                 $pages = array();
118                 $count = 0;
119                 // First populate the $pages array
120                 while($row = $db->fetchObject($res))
121                 {
122                         if(++$count > $limit)
123                         {
124                                 // We've had enough
125                                 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
126                                 break;
127                         }
128
129                         $rev = array();
130                         $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
131                         if($fld_revid)
132                                 $rev['revid'] = $row->ar_rev_id;
133                         if($fld_user)
134                                 $rev['user'] = $row->ar_user_text;
135                         if($fld_comment)
136                                 $rev['comment'] = $row->ar_comment;
137                         if($fld_minor)
138                                 if($row->ar_minor_edit == 1)
139                                         $rev['minor'] = '';
140                         if($fld_len)
141                                 $rev['len'] = $row->ar_len;
142                         if($fld_content)
143                                 ApiResult::setContent($rev, Revision::getRevisionText($row));
144
145                         $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
146                         if(!isset($pages[$t->getPrefixedText()]))
147                         {
148                                 $pages[$t->getPrefixedText()] = array(
149                                         'title' => $t->getPrefixedText(),
150                                         'ns' => intval($row->ar_namespace),
151                                         'revisions' => array($rev)
152                                 );
153                                 if($fld_token)
154                                         $pages[$t->getPrefixedText()]['token'] = $token;
155                         }
156                         else
157                                 $pages[$t->getPrefixedText()]['revisions'][] = $rev;
158                 }
159                 $db->freeResult($res);
160
161                 // We don't want entire pagenames as keys, so let's make this array indexed
162                 foreach($pages as $page)
163                 {
164                         $result->setIndexedTagName($page['revisions'], 'rev');
165                         $data[] = $page;
166                 }
167                 $result->setIndexedTagName($data, 'page');
168                 $result->addValue('query', $this->getModuleName(), $data);
169                 }
170
171         public function getAllowedParams() {
172                 return array (
173                         'start' => array(
174                                 ApiBase :: PARAM_TYPE => 'timestamp'
175                         ),
176                         'end' => array(
177                                 ApiBase :: PARAM_TYPE => 'timestamp',
178                         ),
179                         'dir' => array(
180                                 ApiBase :: PARAM_TYPE => array(
181                                         'newer',
182                                         'older'
183                                 ),
184                                 ApiBase :: PARAM_DFLT => 'older'
185                         ),
186                         'limit' => array(
187                                 ApiBase :: PARAM_DFLT => 10,
188                                 ApiBase :: PARAM_TYPE => 'limit',
189                                 ApiBase :: PARAM_MIN => 1,
190                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
191                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
192                         ),
193                         'prop' => array(
194                                 ApiBase :: PARAM_DFLT => 'user|comment',
195                                 ApiBase :: PARAM_TYPE => array(
196                                         'revid',
197                                         'user',
198                                         'comment',
199                                         'minor',
200                                         'len',
201                                         'content',
202                                         'token'
203                                 ),
204                                 ApiBase :: PARAM_ISMULTI => true
205                         )
206                 );
207         }
208
209         public function getParamDescription() {
210                 return array (
211                         'start' => 'The timestamp to start enumerating from',
212                         'end' => 'The timestamp to stop enumerating at',
213                         'dir' => 'The direction in which to enumerate',
214                         'limit' => 'The maximum amount of revisions to list',
215                         'prop' => 'Which properties to get'
216                 );
217         }
218
219         public function getDescription() {
220                 return 'List deleted revisions.';
221         }
222
223         protected function getExamples() {
224                 return array (
225                         'List the first 50 deleted revisions',
226                         '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
227                         'List the last deleted revisions of Main Page and Talk:Main Page, with content:',
228                         '  api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content'
229                 );
230         }
231
232         public function getVersion() {
233                 return __CLASS__ . ': $Id: ApiQueryDeletedrevs.php 40798 2008-09-13 20:41:58Z aaron $';
234         }
235 }