ApiQueryRevisionsBase.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Oct 3, 2014 as a split from ApiQueryRevisions
  6. *
  7. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. /**
  27. * A base class for functions common to producing a list of revisions.
  28. *
  29. * @ingroup API
  30. */
  31. abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
  32. protected $limit, $diffto, $difftotext, $difftotextpst, $expandTemplates, $generateXML,
  33. $section, $parseContent, $fetchContent, $contentFormat, $setParsedLimit = true;
  34. protected $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
  35. $fld_size = false, $fld_sha1 = false, $fld_comment = false,
  36. $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
  37. $fld_content = false, $fld_tags = false, $fld_contentmodel = false, $fld_parsetree = false;
  38. public function execute() {
  39. $this->run();
  40. }
  41. public function executeGenerator( $resultPageSet ) {
  42. $this->run( $resultPageSet );
  43. }
  44. /**
  45. * @param ApiPageSet $resultPageSet
  46. * @return void
  47. */
  48. abstract protected function run( ApiPageSet $resultPageSet = null );
  49. /**
  50. * Parse the parameters into the various instance fields.
  51. *
  52. * @param array $params
  53. */
  54. protected function parseParameters( $params ) {
  55. if ( !is_null( $params['difftotext'] ) ) {
  56. $this->difftotext = $params['difftotext'];
  57. $this->difftotextpst = $params['difftotextpst'];
  58. } elseif ( !is_null( $params['diffto'] ) ) {
  59. if ( $params['diffto'] == 'cur' ) {
  60. $params['diffto'] = 0;
  61. }
  62. if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
  63. && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
  64. ) {
  65. $p = $this->getModulePrefix();
  66. $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
  67. }
  68. // Check whether the revision exists and is readable,
  69. // DifferenceEngine returns a rather ambiguous empty
  70. // string if that's not the case
  71. if ( $params['diffto'] != 0 ) {
  72. $difftoRev = Revision::newFromId( $params['diffto'] );
  73. if ( !$difftoRev ) {
  74. $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
  75. }
  76. if ( !$difftoRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
  77. $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
  78. $params['diffto'] = null;
  79. }
  80. }
  81. $this->diffto = $params['diffto'];
  82. }
  83. $prop = array_flip( $params['prop'] );
  84. $this->fld_ids = isset( $prop['ids'] );
  85. $this->fld_flags = isset( $prop['flags'] );
  86. $this->fld_timestamp = isset( $prop['timestamp'] );
  87. $this->fld_comment = isset( $prop['comment'] );
  88. $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
  89. $this->fld_size = isset( $prop['size'] );
  90. $this->fld_sha1 = isset( $prop['sha1'] );
  91. $this->fld_content = isset( $prop['content'] );
  92. $this->fld_contentmodel = isset( $prop['contentmodel'] );
  93. $this->fld_userid = isset( $prop['userid'] );
  94. $this->fld_user = isset( $prop['user'] );
  95. $this->fld_tags = isset( $prop['tags'] );
  96. $this->fld_parsetree = isset( $prop['parsetree'] );
  97. if ( $this->fld_parsetree ) {
  98. $encParam = $this->encodeParamName( 'prop' );
  99. $name = $this->getModuleName();
  100. $parent = $this->getParent();
  101. $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
  102. $this->addDeprecation(
  103. [ 'apiwarn-deprecation-parameter', "{$encParam}=parsetree" ],
  104. "action=query&{$parentParam}={$name}&{$encParam}=parsetree"
  105. );
  106. }
  107. if ( !empty( $params['contentformat'] ) ) {
  108. $this->contentFormat = $params['contentformat'];
  109. }
  110. $this->limit = $params['limit'];
  111. $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
  112. || !is_null( $this->difftotext ) || $this->fld_parsetree;
  113. $smallLimit = false;
  114. if ( $this->fetchContent ) {
  115. $smallLimit = true;
  116. $this->expandTemplates = $params['expandtemplates'];
  117. $this->generateXML = $params['generatexml'];
  118. $this->parseContent = $params['parse'];
  119. if ( $this->parseContent ) {
  120. // Must manually initialize unset limit
  121. if ( is_null( $this->limit ) ) {
  122. $this->limit = 1;
  123. }
  124. }
  125. if ( isset( $params['section'] ) ) {
  126. $this->section = $params['section'];
  127. } else {
  128. $this->section = false;
  129. }
  130. }
  131. $userMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
  132. $botMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
  133. if ( $this->limit == 'max' ) {
  134. $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  135. if ( $this->setParsedLimit ) {
  136. $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
  137. }
  138. }
  139. if ( is_null( $this->limit ) ) {
  140. $this->limit = 10;
  141. }
  142. $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
  143. }
  144. /**
  145. * Extract information from the Revision
  146. *
  147. * @param Revision $revision
  148. * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
  149. * @return array
  150. */
  151. protected function extractRevisionInfo( Revision $revision, $row ) {
  152. $title = $revision->getTitle();
  153. $user = $this->getUser();
  154. $vals = [];
  155. $anyHidden = false;
  156. if ( $this->fld_ids ) {
  157. $vals['revid'] = intval( $revision->getId() );
  158. if ( !is_null( $revision->getParentId() ) ) {
  159. $vals['parentid'] = intval( $revision->getParentId() );
  160. }
  161. }
  162. if ( $this->fld_flags ) {
  163. $vals['minor'] = $revision->isMinor();
  164. }
  165. if ( $this->fld_user || $this->fld_userid ) {
  166. if ( $revision->isDeleted( Revision::DELETED_USER ) ) {
  167. $vals['userhidden'] = true;
  168. $anyHidden = true;
  169. }
  170. if ( $revision->userCan( Revision::DELETED_USER, $user ) ) {
  171. if ( $this->fld_user ) {
  172. $vals['user'] = $revision->getUserText( Revision::RAW );
  173. }
  174. $userid = $revision->getUser( Revision::RAW );
  175. if ( !$userid ) {
  176. $vals['anon'] = true;
  177. }
  178. if ( $this->fld_userid ) {
  179. $vals['userid'] = $userid;
  180. }
  181. }
  182. }
  183. if ( $this->fld_timestamp ) {
  184. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
  185. }
  186. if ( $this->fld_size ) {
  187. if ( !is_null( $revision->getSize() ) ) {
  188. $vals['size'] = intval( $revision->getSize() );
  189. } else {
  190. $vals['size'] = 0;
  191. }
  192. }
  193. if ( $this->fld_sha1 ) {
  194. if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
  195. $vals['sha1hidden'] = true;
  196. $anyHidden = true;
  197. }
  198. if ( $revision->userCan( Revision::DELETED_TEXT, $user ) ) {
  199. if ( $revision->getSha1() != '' ) {
  200. $vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
  201. } else {
  202. $vals['sha1'] = '';
  203. }
  204. }
  205. }
  206. if ( $this->fld_contentmodel ) {
  207. $vals['contentmodel'] = $revision->getContentModel();
  208. }
  209. if ( $this->fld_comment || $this->fld_parsedcomment ) {
  210. if ( $revision->isDeleted( Revision::DELETED_COMMENT ) ) {
  211. $vals['commenthidden'] = true;
  212. $anyHidden = true;
  213. }
  214. if ( $revision->userCan( Revision::DELETED_COMMENT, $user ) ) {
  215. $comment = $revision->getComment( Revision::RAW );
  216. if ( $this->fld_comment ) {
  217. $vals['comment'] = $comment;
  218. }
  219. if ( $this->fld_parsedcomment ) {
  220. $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
  221. }
  222. }
  223. }
  224. if ( $this->fld_tags ) {
  225. if ( $row->ts_tags ) {
  226. $tags = explode( ',', $row->ts_tags );
  227. ApiResult::setIndexedTagName( $tags, 'tag' );
  228. $vals['tags'] = $tags;
  229. } else {
  230. $vals['tags'] = [];
  231. }
  232. }
  233. $content = null;
  234. global $wgParser;
  235. if ( $this->fetchContent ) {
  236. $content = $revision->getContent( Revision::FOR_THIS_USER, $this->getUser() );
  237. // Expand templates after getting section content because
  238. // template-added sections don't count and Parser::preprocess()
  239. // will have less input
  240. if ( $content && $this->section !== false ) {
  241. $content = $content->getSection( $this->section, false );
  242. if ( !$content ) {
  243. $this->dieWithError(
  244. [
  245. 'apierror-nosuchsection-what',
  246. wfEscapeWikiText( $this->section ),
  247. $this->msg( 'revid', $revision->getId() )
  248. ],
  249. 'nosuchsection'
  250. );
  251. }
  252. }
  253. if ( $revision->isDeleted( Revision::DELETED_TEXT ) ) {
  254. $vals['texthidden'] = true;
  255. $anyHidden = true;
  256. } elseif ( !$content ) {
  257. $vals['textmissing'] = true;
  258. }
  259. }
  260. if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
  261. if ( $content ) {
  262. if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
  263. $t = $content->getNativeData(); # note: don't set $text
  264. $wgParser->startExternalParse(
  265. $title,
  266. ParserOptions::newFromContext( $this->getContext() ),
  267. Parser::OT_PREPROCESS
  268. );
  269. $dom = $wgParser->preprocessToDom( $t );
  270. if ( is_callable( [ $dom, 'saveXML' ] ) ) {
  271. $xml = $dom->saveXML();
  272. } else {
  273. $xml = $dom->__toString();
  274. }
  275. $vals['parsetree'] = $xml;
  276. } else {
  277. $vals['badcontentformatforparsetree'] = true;
  278. $this->addWarning(
  279. [
  280. 'apierror-parsetree-notwikitext-title',
  281. wfEscapeWikiText( $title->getPrefixedText() ),
  282. $content->getModel()
  283. ],
  284. 'parsetree-notwikitext'
  285. );
  286. }
  287. }
  288. }
  289. if ( $this->fld_content && $content ) {
  290. $text = null;
  291. if ( $this->expandTemplates && !$this->parseContent ) {
  292. # XXX: implement template expansion for all content types in ContentHandler?
  293. if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
  294. $text = $content->getNativeData();
  295. $text = $wgParser->preprocess(
  296. $text,
  297. $title,
  298. ParserOptions::newFromContext( $this->getContext() )
  299. );
  300. } else {
  301. $this->addWarning( [
  302. 'apierror-templateexpansion-notwikitext',
  303. wfEscapeWikiText( $title->getPrefixedText() ),
  304. $content->getModel()
  305. ] );
  306. $vals['badcontentformat'] = true;
  307. $text = false;
  308. }
  309. }
  310. if ( $this->parseContent ) {
  311. $po = $content->getParserOutput(
  312. $title,
  313. $revision->getId(),
  314. ParserOptions::newFromContext( $this->getContext() )
  315. );
  316. $text = $po->getText();
  317. }
  318. if ( $text === null ) {
  319. $format = $this->contentFormat ?: $content->getDefaultFormat();
  320. $model = $content->getModel();
  321. if ( !$content->isSupportedFormat( $format ) ) {
  322. $name = wfEscapeWikiText( $title->getPrefixedText() );
  323. $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
  324. $vals['badcontentformat'] = true;
  325. $text = false;
  326. } else {
  327. $text = $content->serialize( $format );
  328. // always include format and model.
  329. // Format is needed to deserialize, model is needed to interpret.
  330. $vals['contentformat'] = $format;
  331. $vals['contentmodel'] = $model;
  332. }
  333. }
  334. if ( $text !== false ) {
  335. ApiResult::setContentValue( $vals, 'content', $text );
  336. }
  337. }
  338. if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
  339. static $n = 0; // Number of uncached diffs we've had
  340. if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
  341. $vals['diff'] = [];
  342. $context = new DerivativeContext( $this->getContext() );
  343. $context->setTitle( $title );
  344. $handler = $revision->getContentHandler();
  345. if ( !is_null( $this->difftotext ) ) {
  346. $model = $title->getContentModel();
  347. if ( $this->contentFormat
  348. && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
  349. ) {
  350. $name = wfEscapeWikiText( $title->getPrefixedText() );
  351. $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
  352. $vals['diff']['badcontentformat'] = true;
  353. $engine = null;
  354. } else {
  355. $difftocontent = ContentHandler::makeContent(
  356. $this->difftotext,
  357. $title,
  358. $model,
  359. $this->contentFormat
  360. );
  361. if ( $this->difftotextpst ) {
  362. $popts = ParserOptions::newFromContext( $this->getContext() );
  363. $difftocontent = $difftocontent->preSaveTransform( $title, $user, $popts );
  364. }
  365. $engine = $handler->createDifferenceEngine( $context );
  366. $engine->setContent( $content, $difftocontent );
  367. }
  368. } else {
  369. $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
  370. $vals['diff']['from'] = $engine->getOldid();
  371. $vals['diff']['to'] = $engine->getNewid();
  372. }
  373. if ( $engine ) {
  374. $difftext = $engine->getDiffBody();
  375. ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
  376. if ( !$engine->wasCacheHit() ) {
  377. $n++;
  378. }
  379. }
  380. } else {
  381. $vals['diff']['notcached'] = true;
  382. }
  383. }
  384. if ( $anyHidden && $revision->isDeleted( Revision::DELETED_RESTRICTED ) ) {
  385. $vals['suppressed'] = true;
  386. }
  387. return $vals;
  388. }
  389. public function getCacheMode( $params ) {
  390. if ( $this->userCanSeeRevDel() ) {
  391. return 'private';
  392. }
  393. return 'public';
  394. }
  395. public function getAllowedParams() {
  396. return [
  397. 'prop' => [
  398. ApiBase::PARAM_ISMULTI => true,
  399. ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
  400. ApiBase::PARAM_TYPE => [
  401. 'ids',
  402. 'flags',
  403. 'timestamp',
  404. 'user',
  405. 'userid',
  406. 'size',
  407. 'sha1',
  408. 'contentmodel',
  409. 'comment',
  410. 'parsedcomment',
  411. 'content',
  412. 'tags',
  413. 'parsetree',
  414. ],
  415. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
  416. ApiBase::PARAM_HELP_MSG_PER_VALUE => [
  417. 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
  418. 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
  419. 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
  420. 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
  421. 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
  422. 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
  423. 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
  424. 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
  425. 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
  426. 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
  427. 'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
  428. 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
  429. 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
  430. CONTENT_MODEL_WIKITEXT ],
  431. ],
  432. ],
  433. 'limit' => [
  434. ApiBase::PARAM_TYPE => 'limit',
  435. ApiBase::PARAM_MIN => 1,
  436. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  437. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
  438. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-limit',
  439. ],
  440. 'expandtemplates' => [
  441. ApiBase::PARAM_DFLT => false,
  442. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
  443. ApiBase::PARAM_DEPRECATED => true,
  444. ],
  445. 'generatexml' => [
  446. ApiBase::PARAM_DFLT => false,
  447. ApiBase::PARAM_DEPRECATED => true,
  448. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
  449. ],
  450. 'parse' => [
  451. ApiBase::PARAM_DFLT => false,
  452. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
  453. ApiBase::PARAM_DEPRECATED => true,
  454. ],
  455. 'section' => [
  456. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
  457. ],
  458. 'diffto' => [
  459. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
  460. ApiBase::PARAM_DEPRECATED => true,
  461. ],
  462. 'difftotext' => [
  463. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
  464. ApiBase::PARAM_DEPRECATED => true,
  465. ],
  466. 'difftotextpst' => [
  467. ApiBase::PARAM_DFLT => false,
  468. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
  469. ApiBase::PARAM_DEPRECATED => true,
  470. ],
  471. 'contentformat' => [
  472. ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
  473. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
  474. ],
  475. ];
  476. }
  477. }