ApiQueryRevisionsBase.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Logger\LoggerFactory;
  23. use MediaWiki\Revision\RevisionAccessException;
  24. use MediaWiki\Revision\RevisionRecord;
  25. use MediaWiki\Revision\SlotRecord;
  26. use MediaWiki\MediaWikiServices;
  27. /**
  28. * A base class for functions common to producing a list of revisions.
  29. *
  30. * @ingroup API
  31. */
  32. abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase {
  33. /**
  34. * @name Constants for internal use. Don't use externally.
  35. * @{
  36. */
  37. // Bits to indicate the results of the revdel permission check on a revision,
  38. // see self::checkRevDel()
  39. const IS_DELETED = 1; // Whether the field is revision-deleted
  40. const CANNOT_VIEW = 2; // Whether the user cannot view the field due to revdel
  41. /** @} */
  42. protected $limit, $diffto, $difftotext, $difftotextpst, $expandTemplates, $generateXML,
  43. $section, $parseContent, $fetchContent, $contentFormat, $setParsedLimit = true,
  44. $slotRoles = null, $needSlots;
  45. protected $fld_ids = false, $fld_flags = false, $fld_timestamp = false,
  46. $fld_size = false, $fld_slotsize = false, $fld_sha1 = false, $fld_slotsha1 = false,
  47. $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
  48. $fld_content = false, $fld_tags = false, $fld_contentmodel = false, $fld_roles = false,
  49. $fld_parsetree = false;
  50. public function execute() {
  51. $this->run();
  52. }
  53. public function executeGenerator( $resultPageSet ) {
  54. $this->run( $resultPageSet );
  55. }
  56. /**
  57. * @param ApiPageSet|null $resultPageSet
  58. * @return void
  59. */
  60. abstract protected function run( ApiPageSet $resultPageSet = null );
  61. /**
  62. * Parse the parameters into the various instance fields.
  63. *
  64. * @param array $params
  65. */
  66. protected function parseParameters( $params ) {
  67. $prop = array_flip( $params['prop'] );
  68. $this->fld_ids = isset( $prop['ids'] );
  69. $this->fld_flags = isset( $prop['flags'] );
  70. $this->fld_timestamp = isset( $prop['timestamp'] );
  71. $this->fld_comment = isset( $prop['comment'] );
  72. $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
  73. $this->fld_size = isset( $prop['size'] );
  74. $this->fld_slotsize = isset( $prop['slotsize'] );
  75. $this->fld_sha1 = isset( $prop['sha1'] );
  76. $this->fld_slotsha1 = isset( $prop['slotsha1'] );
  77. $this->fld_content = isset( $prop['content'] );
  78. $this->fld_contentmodel = isset( $prop['contentmodel'] );
  79. $this->fld_userid = isset( $prop['userid'] );
  80. $this->fld_user = isset( $prop['user'] );
  81. $this->fld_tags = isset( $prop['tags'] );
  82. $this->fld_roles = isset( $prop['roles'] );
  83. $this->fld_parsetree = isset( $prop['parsetree'] );
  84. $this->slotRoles = $params['slots'];
  85. if ( $this->slotRoles !== null ) {
  86. if ( $this->fld_parsetree ) {
  87. $this->dieWithError( [
  88. 'apierror-invalidparammix-cannotusewith',
  89. $this->encodeParamName( 'prop=parsetree' ),
  90. $this->encodeParamName( 'slots' ),
  91. ], 'invalidparammix' );
  92. }
  93. foreach ( [
  94. 'expandtemplates', 'generatexml', 'parse', 'diffto', 'difftotext', 'difftotextpst',
  95. 'contentformat'
  96. ] as $p ) {
  97. if ( $params[$p] !== null && $params[$p] !== false ) {
  98. $this->dieWithError( [
  99. 'apierror-invalidparammix-cannotusewith',
  100. $this->encodeParamName( $p ),
  101. $this->encodeParamName( 'slots' ),
  102. ], 'invalidparammix' );
  103. }
  104. }
  105. }
  106. if ( !empty( $params['contentformat'] ) ) {
  107. $this->contentFormat = $params['contentformat'];
  108. }
  109. $this->limit = $params['limit'];
  110. if ( !is_null( $params['difftotext'] ) ) {
  111. $this->difftotext = $params['difftotext'];
  112. $this->difftotextpst = $params['difftotextpst'];
  113. } elseif ( !is_null( $params['diffto'] ) ) {
  114. if ( $params['diffto'] == 'cur' ) {
  115. $params['diffto'] = 0;
  116. }
  117. if ( ( !ctype_digit( $params['diffto'] ) || $params['diffto'] < 0 )
  118. && $params['diffto'] != 'prev' && $params['diffto'] != 'next'
  119. ) {
  120. $p = $this->getModulePrefix();
  121. $this->dieWithError( [ 'apierror-baddiffto', $p ], 'diffto' );
  122. }
  123. // Check whether the revision exists and is readable,
  124. // DifferenceEngine returns a rather ambiguous empty
  125. // string if that's not the case
  126. if ( $params['diffto'] != 0 ) {
  127. $difftoRev = MediaWikiServices::getInstance()->getRevisionStore()
  128. ->getRevisionById( $params['diffto'] );
  129. if ( !$difftoRev ) {
  130. $this->dieWithError( [ 'apierror-nosuchrevid', $params['diffto'] ] );
  131. }
  132. $revDel = $this->checkRevDel( $difftoRev, RevisionRecord::DELETED_TEXT );
  133. if ( $revDel & self::CANNOT_VIEW ) {
  134. $this->addWarning( [ 'apiwarn-difftohidden', $difftoRev->getId() ] );
  135. $params['diffto'] = null;
  136. }
  137. }
  138. $this->diffto = $params['diffto'];
  139. }
  140. $this->fetchContent = $this->fld_content || !is_null( $this->diffto )
  141. || !is_null( $this->difftotext ) || $this->fld_parsetree;
  142. $smallLimit = false;
  143. if ( $this->fetchContent ) {
  144. $smallLimit = true;
  145. $this->expandTemplates = $params['expandtemplates'];
  146. $this->generateXML = $params['generatexml'];
  147. $this->parseContent = $params['parse'];
  148. if ( $this->parseContent ) {
  149. // Must manually initialize unset limit
  150. if ( is_null( $this->limit ) ) {
  151. $this->limit = 1;
  152. }
  153. }
  154. $this->section = $params['section'] ?? false;
  155. }
  156. $userMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1 );
  157. $botMax = $this->parseContent ? 1 : ( $smallLimit ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2 );
  158. if ( $this->limit == 'max' ) {
  159. $this->limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  160. if ( $this->setParsedLimit ) {
  161. $this->getResult()->addParsedLimit( $this->getModuleName(), $this->limit );
  162. }
  163. }
  164. if ( is_null( $this->limit ) ) {
  165. $this->limit = 10;
  166. }
  167. $this->validateLimit( 'limit', $this->limit, 1, $userMax, $botMax );
  168. $this->needSlots = $this->fetchContent || $this->fld_contentmodel ||
  169. $this->fld_slotsize || $this->fld_slotsha1;
  170. if ( $this->needSlots && $this->slotRoles === null ) {
  171. $encParam = $this->encodeParamName( 'slots' );
  172. $name = $this->getModuleName();
  173. $parent = $this->getParent();
  174. $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
  175. $this->addDeprecation(
  176. [ 'apiwarn-deprecation-missingparam', $encParam ],
  177. "action=query&{$parentParam}={$name}&!{$encParam}"
  178. );
  179. }
  180. }
  181. /**
  182. * Test revision deletion status
  183. * @param RevisionRecord $revision Revision to check
  184. * @param int $field One of the RevisionRecord::DELETED_* constants
  185. * @return int Revision deletion status flags. Bitwise OR of
  186. * self::IS_DELETED and self::CANNOT_VIEW, as appropriate.
  187. */
  188. private function checkRevDel( RevisionRecord $revision, $field ) {
  189. $ret = $revision->isDeleted( $field ) ? self::IS_DELETED : 0;
  190. if ( $ret ) {
  191. $canSee = $revision->audienceCan( $field, RevisionRecord::FOR_THIS_USER, $this->getUser() );
  192. $ret = $ret | ( $canSee ? 0 : self::CANNOT_VIEW );
  193. }
  194. return $ret;
  195. }
  196. /**
  197. * Extract information from the RevisionRecord
  198. *
  199. * @since 1.32, takes a RevisionRecord instead of a Revision
  200. * @param RevisionRecord $revision Revision
  201. * @param object $row Should have a field 'ts_tags' if $this->fld_tags is set
  202. * @return array
  203. */
  204. protected function extractRevisionInfo( RevisionRecord $revision, $row ) {
  205. $vals = [];
  206. $anyHidden = false;
  207. if ( $this->fld_ids ) {
  208. $vals['revid'] = (int)$revision->getId();
  209. if ( !is_null( $revision->getParentId() ) ) {
  210. $vals['parentid'] = (int)$revision->getParentId();
  211. }
  212. }
  213. if ( $this->fld_flags ) {
  214. $vals['minor'] = $revision->isMinor();
  215. }
  216. if ( $this->fld_user || $this->fld_userid ) {
  217. $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
  218. if ( ( $revDel & self::IS_DELETED ) ) {
  219. $vals['userhidden'] = true;
  220. $anyHidden = true;
  221. }
  222. if ( !( $revDel & self::CANNOT_VIEW ) ) {
  223. $u = $revision->getUser( RevisionRecord::RAW );
  224. if ( $this->fld_user ) {
  225. $vals['user'] = $u->getName();
  226. }
  227. $userid = $u->getId();
  228. if ( !$userid ) {
  229. $vals['anon'] = true;
  230. }
  231. if ( $this->fld_userid ) {
  232. $vals['userid'] = $userid;
  233. }
  234. }
  235. }
  236. if ( $this->fld_timestamp ) {
  237. $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $revision->getTimestamp() );
  238. }
  239. if ( $this->fld_size ) {
  240. try {
  241. $vals['size'] = (int)$revision->getSize();
  242. } catch ( RevisionAccessException $e ) {
  243. // Back compat: If there's no size, return 0.
  244. // @todo: Gergő says to mention T198099 as a "todo" here.
  245. $vals['size'] = 0;
  246. }
  247. }
  248. if ( $this->fld_sha1 ) {
  249. $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
  250. if ( ( $revDel & self::IS_DELETED ) ) {
  251. $vals['sha1hidden'] = true;
  252. $anyHidden = true;
  253. }
  254. if ( !( $revDel & self::CANNOT_VIEW ) ) {
  255. try {
  256. $vals['sha1'] = Wikimedia\base_convert( $revision->getSha1(), 36, 16, 40 );
  257. } catch ( RevisionAccessException $e ) {
  258. // Back compat: If there's no sha1, return emtpy string.
  259. // @todo: Gergő says to mention T198099 as a "todo" here.
  260. $vals['sha1'] = '';
  261. }
  262. }
  263. }
  264. try {
  265. if ( $this->fld_roles ) {
  266. $vals['roles'] = $revision->getSlotRoles();
  267. }
  268. if ( $this->needSlots ) {
  269. $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
  270. if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
  271. $anyHidden = true;
  272. }
  273. $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
  274. }
  275. } catch ( RevisionAccessException $ex ) {
  276. // This is here so T212428 doesn't spam the log.
  277. // TODO: find out why T212428 happens in the first place!
  278. $vals['slotsmissing'] = true;
  279. LoggerFactory::getInstance( 'api-warning' )->error(
  280. 'Failed to access revision slots',
  281. [ 'revision' => $revision->getId(), 'exception' => $ex, ]
  282. );
  283. }
  284. if ( $this->fld_comment || $this->fld_parsedcomment ) {
  285. $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
  286. if ( ( $revDel & self::IS_DELETED ) ) {
  287. $vals['commenthidden'] = true;
  288. $anyHidden = true;
  289. }
  290. if ( !( $revDel & self::CANNOT_VIEW ) ) {
  291. $comment = $revision->getComment( RevisionRecord::RAW );
  292. $comment = $comment ? $comment->text : '';
  293. if ( $this->fld_comment ) {
  294. $vals['comment'] = $comment;
  295. }
  296. if ( $this->fld_parsedcomment ) {
  297. $vals['parsedcomment'] = Linker::formatComment(
  298. $comment, Title::newFromLinkTarget( $revision->getPageAsLinkTarget() )
  299. );
  300. }
  301. }
  302. }
  303. if ( $this->fld_tags ) {
  304. if ( $row->ts_tags ) {
  305. $tags = explode( ',', $row->ts_tags );
  306. ApiResult::setIndexedTagName( $tags, 'tag' );
  307. $vals['tags'] = $tags;
  308. } else {
  309. $vals['tags'] = [];
  310. }
  311. }
  312. if ( $anyHidden && $revision->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
  313. $vals['suppressed'] = true;
  314. }
  315. return $vals;
  316. }
  317. /**
  318. * Extracts information about all relevant slots.
  319. *
  320. * @param RevisionRecord $revision
  321. * @param int $revDel
  322. *
  323. * @return array
  324. * @throws ApiUsageException
  325. */
  326. private function extractAllSlotInfo( RevisionRecord $revision, $revDel ): array {
  327. $vals = [];
  328. if ( $this->slotRoles === null ) {
  329. try {
  330. $slot = $revision->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
  331. } catch ( RevisionAccessException $e ) {
  332. // Back compat: If there's no slot, there's no content, so set 'textmissing'
  333. // @todo: Gergő says to mention T198099 as a "todo" here.
  334. $vals['textmissing'] = true;
  335. $slot = null;
  336. }
  337. if ( $slot ) {
  338. $content = null;
  339. $vals += $this->extractSlotInfo( $slot, $revDel, $content );
  340. if ( !empty( $vals['nosuchsection'] ) ) {
  341. $this->dieWithError(
  342. [
  343. 'apierror-nosuchsection-what',
  344. wfEscapeWikiText( $this->section ),
  345. $this->msg( 'revid', $revision->getId() )
  346. ],
  347. 'nosuchsection'
  348. );
  349. }
  350. if ( $content ) {
  351. $vals += $this->extractDeprecatedContent( $content, $revision );
  352. }
  353. }
  354. } else {
  355. $roles = array_intersect( $this->slotRoles, $revision->getSlotRoles() );
  356. $vals['slots'] = [
  357. ApiResult::META_KVP_MERGE => true,
  358. ];
  359. foreach ( $roles as $role ) {
  360. try {
  361. $slot = $revision->getSlot( $role, RevisionRecord::RAW );
  362. } catch ( RevisionAccessException $e ) {
  363. // Don't error out here so the client can still process other slots/revisions.
  364. // @todo: Gergő says to mention T198099 as a "todo" here.
  365. $vals['slots'][$role]['missing'] = true;
  366. continue;
  367. }
  368. $content = null;
  369. $vals['slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
  370. // @todo Move this into extractSlotInfo() (and remove its $content parameter)
  371. // when extractDeprecatedContent() is no more.
  372. if ( $content ) {
  373. $vals['slots'][$role]['contentmodel'] = $content->getModel();
  374. $vals['slots'][$role]['contentformat'] = $content->getDefaultFormat();
  375. ApiResult::setContentValue(
  376. $vals['slots'][$role],
  377. 'content',
  378. $content->serialize()
  379. );
  380. }
  381. }
  382. ApiResult::setArrayType( $vals['slots'], 'kvp', 'role' );
  383. ApiResult::setIndexedTagName( $vals['slots'], 'slot' );
  384. }
  385. return $vals;
  386. }
  387. /**
  388. * Extract information from the SlotRecord
  389. *
  390. * @param SlotRecord $slot
  391. * @param int $revDel Revdel status flags, from self::checkRevDel()
  392. * @param Content|null &$content Set to the slot's content, if available
  393. * and $this->fetchContent is true
  394. * @return array
  395. */
  396. private function extractSlotInfo( SlotRecord $slot, $revDel, &$content = null ) {
  397. $vals = [];
  398. ApiResult::setArrayType( $vals, 'assoc' );
  399. if ( $this->fld_slotsize ) {
  400. $vals['size'] = (int)$slot->getSize();
  401. }
  402. if ( $this->fld_slotsha1 ) {
  403. if ( ( $revDel & self::IS_DELETED ) ) {
  404. $vals['sha1hidden'] = true;
  405. }
  406. if ( !( $revDel & self::CANNOT_VIEW ) ) {
  407. if ( $slot->getSha1() != '' ) {
  408. $vals['sha1'] = Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
  409. } else {
  410. $vals['sha1'] = '';
  411. }
  412. }
  413. }
  414. if ( $this->fld_contentmodel ) {
  415. $vals['contentmodel'] = $slot->getModel();
  416. }
  417. $content = null;
  418. if ( $this->fetchContent ) {
  419. if ( ( $revDel & self::IS_DELETED ) ) {
  420. $vals['texthidden'] = true;
  421. }
  422. if ( !( $revDel & self::CANNOT_VIEW ) ) {
  423. try {
  424. $content = $slot->getContent();
  425. } catch ( RevisionAccessException $e ) {
  426. // @todo: Gergő says to mention T198099 as a "todo" here.
  427. $vals['textmissing'] = true;
  428. }
  429. // Expand templates after getting section content because
  430. // template-added sections don't count and Parser::preprocess()
  431. // will have less input
  432. if ( $content && $this->section !== false ) {
  433. $content = $content->getSection( $this->section, false );
  434. if ( !$content ) {
  435. $vals['nosuchsection'] = true;
  436. }
  437. }
  438. }
  439. }
  440. return $vals;
  441. }
  442. /**
  443. * Format a Content using deprecated options
  444. * @param Content $content Content to format
  445. * @param RevisionRecord $revision Revision being processed
  446. * @return array
  447. */
  448. private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
  449. $vals = [];
  450. $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
  451. if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
  452. if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
  453. /** @var WikitextContent $content */
  454. '@phan-var WikitextContent $content';
  455. $t = $content->getText(); # note: don't set $text
  456. $parser = MediaWikiServices::getInstance()->getParser();
  457. $parser->startExternalParse(
  458. $title,
  459. ParserOptions::newFromContext( $this->getContext() ),
  460. Parser::OT_PREPROCESS
  461. );
  462. $dom = $parser->preprocessToDom( $t );
  463. // @phan-suppress-next-line PhanUndeclaredMethodInCallable
  464. if ( is_callable( [ $dom, 'saveXML' ] ) ) {
  465. // @phan-suppress-next-line PhanUndeclaredMethod
  466. $xml = $dom->saveXML();
  467. } else {
  468. // @phan-suppress-next-line PhanUndeclaredMethod
  469. $xml = $dom->__toString();
  470. }
  471. $vals['parsetree'] = $xml;
  472. } else {
  473. $vals['badcontentformatforparsetree'] = true;
  474. $this->addWarning(
  475. [
  476. 'apierror-parsetree-notwikitext-title',
  477. wfEscapeWikiText( $title->getPrefixedText() ),
  478. $content->getModel()
  479. ],
  480. 'parsetree-notwikitext'
  481. );
  482. }
  483. }
  484. if ( $this->fld_content ) {
  485. $text = null;
  486. if ( $this->expandTemplates && !$this->parseContent ) {
  487. if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
  488. /** @var WikitextContent $content */
  489. '@phan-var WikitextContent $content';
  490. $text = $content->getText();
  491. $text = MediaWikiServices::getInstance()->getParser()->preprocess(
  492. $text,
  493. $title,
  494. ParserOptions::newFromContext( $this->getContext() )
  495. );
  496. } else {
  497. $this->addWarning( [
  498. 'apierror-templateexpansion-notwikitext',
  499. wfEscapeWikiText( $title->getPrefixedText() ),
  500. $content->getModel()
  501. ] );
  502. $vals['badcontentformat'] = true;
  503. $text = false;
  504. }
  505. }
  506. if ( $this->parseContent ) {
  507. $po = $content->getParserOutput(
  508. $title,
  509. $revision->getId(),
  510. ParserOptions::newFromContext( $this->getContext() )
  511. );
  512. $text = $po->getText();
  513. }
  514. if ( $text === null ) {
  515. $format = $this->contentFormat ?: $content->getDefaultFormat();
  516. $model = $content->getModel();
  517. if ( !$content->isSupportedFormat( $format ) ) {
  518. $name = wfEscapeWikiText( $title->getPrefixedText() );
  519. $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
  520. $vals['badcontentformat'] = true;
  521. $text = false;
  522. } else {
  523. $text = $content->serialize( $format );
  524. // always include format and model.
  525. // Format is needed to deserialize, model is needed to interpret.
  526. $vals['contentformat'] = $format;
  527. $vals['contentmodel'] = $model;
  528. }
  529. }
  530. if ( $text !== false ) {
  531. ApiResult::setContentValue( $vals, 'content', $text );
  532. }
  533. }
  534. if ( $content && ( !is_null( $this->diffto ) || !is_null( $this->difftotext ) ) ) {
  535. static $n = 0; // Number of uncached diffs we've had
  536. if ( $n < $this->getConfig()->get( 'APIMaxUncachedDiffs' ) ) {
  537. $vals['diff'] = [];
  538. $context = new DerivativeContext( $this->getContext() );
  539. $context->setTitle( $title );
  540. $handler = $content->getContentHandler();
  541. if ( !is_null( $this->difftotext ) ) {
  542. $model = $title->getContentModel();
  543. if ( $this->contentFormat
  544. && !ContentHandler::getForModelID( $model )->isSupportedFormat( $this->contentFormat )
  545. ) {
  546. $name = wfEscapeWikiText( $title->getPrefixedText() );
  547. $this->addWarning( [ 'apierror-badformat', $this->contentFormat, $model, $name ] );
  548. $vals['diff']['badcontentformat'] = true;
  549. $engine = null;
  550. } else {
  551. $difftocontent = ContentHandler::makeContent(
  552. $this->difftotext,
  553. $title,
  554. $model,
  555. $this->contentFormat
  556. );
  557. if ( $this->difftotextpst ) {
  558. $popts = ParserOptions::newFromContext( $this->getContext() );
  559. $difftocontent = $difftocontent->preSaveTransform( $title, $this->getUser(), $popts );
  560. }
  561. $engine = $handler->createDifferenceEngine( $context );
  562. $engine->setContent( $content, $difftocontent );
  563. }
  564. } else {
  565. $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
  566. $vals['diff']['from'] = $engine->getOldid();
  567. $vals['diff']['to'] = $engine->getNewid();
  568. }
  569. if ( $engine ) {
  570. $difftext = $engine->getDiffBody();
  571. ApiResult::setContentValue( $vals['diff'], 'body', $difftext );
  572. if ( !$engine->wasCacheHit() ) {
  573. $n++;
  574. }
  575. }
  576. } else {
  577. $vals['diff']['notcached'] = true;
  578. }
  579. }
  580. return $vals;
  581. }
  582. public function getCacheMode( $params ) {
  583. if ( $this->userCanSeeRevDel() ) {
  584. return 'private';
  585. }
  586. return 'public';
  587. }
  588. public function getAllowedParams() {
  589. $slotRoles = MediaWikiServices::getInstance()->getSlotRoleRegistry()->getKnownRoles();
  590. sort( $slotRoles, SORT_STRING );
  591. return [
  592. 'prop' => [
  593. ApiBase::PARAM_ISMULTI => true,
  594. ApiBase::PARAM_DFLT => 'ids|timestamp|flags|comment|user',
  595. ApiBase::PARAM_TYPE => [
  596. 'ids',
  597. 'flags',
  598. 'timestamp',
  599. 'user',
  600. 'userid',
  601. 'size',
  602. 'slotsize',
  603. 'sha1',
  604. 'slotsha1',
  605. 'contentmodel',
  606. 'comment',
  607. 'parsedcomment',
  608. 'content',
  609. 'tags',
  610. 'roles',
  611. 'parsetree',
  612. ],
  613. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop',
  614. ApiBase::PARAM_HELP_MSG_PER_VALUE => [
  615. 'ids' => 'apihelp-query+revisions+base-paramvalue-prop-ids',
  616. 'flags' => 'apihelp-query+revisions+base-paramvalue-prop-flags',
  617. 'timestamp' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp',
  618. 'user' => 'apihelp-query+revisions+base-paramvalue-prop-user',
  619. 'userid' => 'apihelp-query+revisions+base-paramvalue-prop-userid',
  620. 'size' => 'apihelp-query+revisions+base-paramvalue-prop-size',
  621. 'slotsize' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize',
  622. 'sha1' => 'apihelp-query+revisions+base-paramvalue-prop-sha1',
  623. 'slotsha1' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1',
  624. 'contentmodel' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel',
  625. 'comment' => 'apihelp-query+revisions+base-paramvalue-prop-comment',
  626. 'parsedcomment' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment',
  627. 'content' => 'apihelp-query+revisions+base-paramvalue-prop-content',
  628. 'tags' => 'apihelp-query+revisions+base-paramvalue-prop-tags',
  629. 'roles' => 'apihelp-query+revisions+base-paramvalue-prop-roles',
  630. 'parsetree' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree',
  631. CONTENT_MODEL_WIKITEXT ],
  632. ],
  633. ApiBase::PARAM_DEPRECATED_VALUES => [
  634. 'parsetree' => true,
  635. ],
  636. ],
  637. 'slots' => [
  638. ApiBase::PARAM_TYPE => $slotRoles,
  639. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots',
  640. ApiBase::PARAM_ISMULTI => true,
  641. ApiBase::PARAM_ALL => true,
  642. ],
  643. 'limit' => [
  644. ApiBase::PARAM_TYPE => 'limit',
  645. ApiBase::PARAM_MIN => 1,
  646. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  647. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
  648. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-limit',
  649. ],
  650. 'expandtemplates' => [
  651. ApiBase::PARAM_DFLT => false,
  652. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates',
  653. ApiBase::PARAM_DEPRECATED => true,
  654. ],
  655. 'generatexml' => [
  656. ApiBase::PARAM_DFLT => false,
  657. ApiBase::PARAM_DEPRECATED => true,
  658. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml',
  659. ],
  660. 'parse' => [
  661. ApiBase::PARAM_DFLT => false,
  662. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-parse',
  663. ApiBase::PARAM_DEPRECATED => true,
  664. ],
  665. 'section' => [
  666. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section',
  667. ],
  668. 'diffto' => [
  669. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-diffto',
  670. ApiBase::PARAM_DEPRECATED => true,
  671. ],
  672. 'difftotext' => [
  673. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotext',
  674. ApiBase::PARAM_DEPRECATED => true,
  675. ],
  676. 'difftotextpst' => [
  677. ApiBase::PARAM_DFLT => false,
  678. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst',
  679. ApiBase::PARAM_DEPRECATED => true,
  680. ],
  681. 'contentformat' => [
  682. ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
  683. ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat',
  684. ApiBase::PARAM_DEPRECATED => true,
  685. ],
  686. ];
  687. }
  688. }