ApiQueryInfo.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Sep 25, 2006
  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. use MediaWiki\MediaWikiServices;
  27. use MediaWiki\Linker\LinkTarget;
  28. /**
  29. * A query module to show basic page information.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryInfo extends ApiQueryBase {
  34. private $fld_protection = false, $fld_talkid = false,
  35. $fld_subjectid = false, $fld_url = false,
  36. $fld_readable = false, $fld_watched = false,
  37. $fld_watchers = false, $fld_visitingwatchers = false,
  38. $fld_notificationtimestamp = false,
  39. $fld_preload = false, $fld_displaytitle = false;
  40. private $params;
  41. /** @var Title[] */
  42. private $titles;
  43. /** @var Title[] */
  44. private $missing;
  45. /** @var Title[] */
  46. private $everything;
  47. private $pageRestrictions, $pageIsRedir, $pageIsNew, $pageTouched,
  48. $pageLatest, $pageLength;
  49. private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
  50. $notificationtimestamps, $talkids, $subjectids, $displaytitles;
  51. private $showZeroWatchers = false;
  52. private $tokenFunctions;
  53. private $countTestedActions = 0;
  54. public function __construct( ApiQuery $query, $moduleName ) {
  55. parent::__construct( $query, $moduleName, 'in' );
  56. }
  57. /**
  58. * @param ApiPageSet $pageSet
  59. * @return void
  60. */
  61. public function requestExtraData( $pageSet ) {
  62. $pageSet->requestField( 'page_restrictions' );
  63. // If the pageset is resolving redirects we won't get page_is_redirect.
  64. // But we can't know for sure until the pageset is executed (revids may
  65. // turn it off), so request it unconditionally.
  66. $pageSet->requestField( 'page_is_redirect' );
  67. $pageSet->requestField( 'page_is_new' );
  68. $config = $this->getConfig();
  69. $pageSet->requestField( 'page_touched' );
  70. $pageSet->requestField( 'page_latest' );
  71. $pageSet->requestField( 'page_len' );
  72. if ( $config->get( 'ContentHandlerUseDB' ) ) {
  73. $pageSet->requestField( 'page_content_model' );
  74. }
  75. if ( $config->get( 'PageLanguageUseDB' ) ) {
  76. $pageSet->requestField( 'page_lang' );
  77. }
  78. }
  79. /**
  80. * Get an array mapping token names to their handler functions.
  81. * The prototype for a token function is func($pageid, $title)
  82. * it should return a token or false (permission denied)
  83. * @deprecated since 1.24
  84. * @return array [ tokenname => function ]
  85. */
  86. protected function getTokenFunctions() {
  87. // Don't call the hooks twice
  88. if ( isset( $this->tokenFunctions ) ) {
  89. return $this->tokenFunctions;
  90. }
  91. // If we're in a mode that breaks the same-origin policy, no tokens can
  92. // be obtained
  93. if ( $this->lacksSameOriginSecurity() ) {
  94. return [];
  95. }
  96. $this->tokenFunctions = [
  97. 'edit' => [ 'ApiQueryInfo', 'getEditToken' ],
  98. 'delete' => [ 'ApiQueryInfo', 'getDeleteToken' ],
  99. 'protect' => [ 'ApiQueryInfo', 'getProtectToken' ],
  100. 'move' => [ 'ApiQueryInfo', 'getMoveToken' ],
  101. 'block' => [ 'ApiQueryInfo', 'getBlockToken' ],
  102. 'unblock' => [ 'ApiQueryInfo', 'getUnblockToken' ],
  103. 'email' => [ 'ApiQueryInfo', 'getEmailToken' ],
  104. 'import' => [ 'ApiQueryInfo', 'getImportToken' ],
  105. 'watch' => [ 'ApiQueryInfo', 'getWatchToken' ],
  106. ];
  107. Hooks::run( 'APIQueryInfoTokens', [ &$this->tokenFunctions ] );
  108. return $this->tokenFunctions;
  109. }
  110. static protected $cachedTokens = [];
  111. /**
  112. * @deprecated since 1.24
  113. */
  114. public static function resetTokenCache() {
  115. self::$cachedTokens = [];
  116. }
  117. /**
  118. * @deprecated since 1.24
  119. */
  120. public static function getEditToken( $pageid, $title ) {
  121. // We could check for $title->userCan('edit') here,
  122. // but that's too expensive for this purpose
  123. // and would break caching
  124. global $wgUser;
  125. if ( !$wgUser->isAllowed( 'edit' ) ) {
  126. return false;
  127. }
  128. // The token is always the same, let's exploit that
  129. if ( !isset( self::$cachedTokens['edit'] ) ) {
  130. self::$cachedTokens['edit'] = $wgUser->getEditToken();
  131. }
  132. return self::$cachedTokens['edit'];
  133. }
  134. /**
  135. * @deprecated since 1.24
  136. */
  137. public static function getDeleteToken( $pageid, $title ) {
  138. global $wgUser;
  139. if ( !$wgUser->isAllowed( 'delete' ) ) {
  140. return false;
  141. }
  142. // The token is always the same, let's exploit that
  143. if ( !isset( self::$cachedTokens['delete'] ) ) {
  144. self::$cachedTokens['delete'] = $wgUser->getEditToken();
  145. }
  146. return self::$cachedTokens['delete'];
  147. }
  148. /**
  149. * @deprecated since 1.24
  150. */
  151. public static function getProtectToken( $pageid, $title ) {
  152. global $wgUser;
  153. if ( !$wgUser->isAllowed( 'protect' ) ) {
  154. return false;
  155. }
  156. // The token is always the same, let's exploit that
  157. if ( !isset( self::$cachedTokens['protect'] ) ) {
  158. self::$cachedTokens['protect'] = $wgUser->getEditToken();
  159. }
  160. return self::$cachedTokens['protect'];
  161. }
  162. /**
  163. * @deprecated since 1.24
  164. */
  165. public static function getMoveToken( $pageid, $title ) {
  166. global $wgUser;
  167. if ( !$wgUser->isAllowed( 'move' ) ) {
  168. return false;
  169. }
  170. // The token is always the same, let's exploit that
  171. if ( !isset( self::$cachedTokens['move'] ) ) {
  172. self::$cachedTokens['move'] = $wgUser->getEditToken();
  173. }
  174. return self::$cachedTokens['move'];
  175. }
  176. /**
  177. * @deprecated since 1.24
  178. */
  179. public static function getBlockToken( $pageid, $title ) {
  180. global $wgUser;
  181. if ( !$wgUser->isAllowed( 'block' ) ) {
  182. return false;
  183. }
  184. // The token is always the same, let's exploit that
  185. if ( !isset( self::$cachedTokens['block'] ) ) {
  186. self::$cachedTokens['block'] = $wgUser->getEditToken();
  187. }
  188. return self::$cachedTokens['block'];
  189. }
  190. /**
  191. * @deprecated since 1.24
  192. */
  193. public static function getUnblockToken( $pageid, $title ) {
  194. // Currently, this is exactly the same as the block token
  195. return self::getBlockToken( $pageid, $title );
  196. }
  197. /**
  198. * @deprecated since 1.24
  199. */
  200. public static function getEmailToken( $pageid, $title ) {
  201. global $wgUser;
  202. if ( !$wgUser->canSendEmail() || $wgUser->isBlockedFromEmailuser() ) {
  203. return false;
  204. }
  205. // The token is always the same, let's exploit that
  206. if ( !isset( self::$cachedTokens['email'] ) ) {
  207. self::$cachedTokens['email'] = $wgUser->getEditToken();
  208. }
  209. return self::$cachedTokens['email'];
  210. }
  211. /**
  212. * @deprecated since 1.24
  213. */
  214. public static function getImportToken( $pageid, $title ) {
  215. global $wgUser;
  216. if ( !$wgUser->isAllowedAny( 'import', 'importupload' ) ) {
  217. return false;
  218. }
  219. // The token is always the same, let's exploit that
  220. if ( !isset( self::$cachedTokens['import'] ) ) {
  221. self::$cachedTokens['import'] = $wgUser->getEditToken();
  222. }
  223. return self::$cachedTokens['import'];
  224. }
  225. /**
  226. * @deprecated since 1.24
  227. */
  228. public static function getWatchToken( $pageid, $title ) {
  229. global $wgUser;
  230. if ( !$wgUser->isLoggedIn() ) {
  231. return false;
  232. }
  233. // The token is always the same, let's exploit that
  234. if ( !isset( self::$cachedTokens['watch'] ) ) {
  235. self::$cachedTokens['watch'] = $wgUser->getEditToken( 'watch' );
  236. }
  237. return self::$cachedTokens['watch'];
  238. }
  239. /**
  240. * @deprecated since 1.24
  241. */
  242. public static function getOptionsToken( $pageid, $title ) {
  243. global $wgUser;
  244. if ( !$wgUser->isLoggedIn() ) {
  245. return false;
  246. }
  247. // The token is always the same, let's exploit that
  248. if ( !isset( self::$cachedTokens['options'] ) ) {
  249. self::$cachedTokens['options'] = $wgUser->getEditToken();
  250. }
  251. return self::$cachedTokens['options'];
  252. }
  253. public function execute() {
  254. $this->params = $this->extractRequestParams();
  255. if ( !is_null( $this->params['prop'] ) ) {
  256. $prop = array_flip( $this->params['prop'] );
  257. $this->fld_protection = isset( $prop['protection'] );
  258. $this->fld_watched = isset( $prop['watched'] );
  259. $this->fld_watchers = isset( $prop['watchers'] );
  260. $this->fld_visitingwatchers = isset( $prop['visitingwatchers'] );
  261. $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
  262. $this->fld_talkid = isset( $prop['talkid'] );
  263. $this->fld_subjectid = isset( $prop['subjectid'] );
  264. $this->fld_url = isset( $prop['url'] );
  265. $this->fld_readable = isset( $prop['readable'] );
  266. $this->fld_preload = isset( $prop['preload'] );
  267. $this->fld_displaytitle = isset( $prop['displaytitle'] );
  268. }
  269. $pageSet = $this->getPageSet();
  270. $this->titles = $pageSet->getGoodTitles();
  271. $this->missing = $pageSet->getMissingTitles();
  272. $this->everything = $this->titles + $this->missing;
  273. $result = $this->getResult();
  274. uasort( $this->everything, [ 'Title', 'compare' ] );
  275. if ( !is_null( $this->params['continue'] ) ) {
  276. // Throw away any titles we're gonna skip so they don't
  277. // clutter queries
  278. $cont = explode( '|', $this->params['continue'] );
  279. $this->dieContinueUsageIf( count( $cont ) != 2 );
  280. $conttitle = Title::makeTitleSafe( $cont[0], $cont[1] );
  281. foreach ( $this->everything as $pageid => $title ) {
  282. if ( Title::compare( $title, $conttitle ) >= 0 ) {
  283. break;
  284. }
  285. unset( $this->titles[$pageid] );
  286. unset( $this->missing[$pageid] );
  287. unset( $this->everything[$pageid] );
  288. }
  289. }
  290. $this->pageRestrictions = $pageSet->getCustomField( 'page_restrictions' );
  291. // when resolving redirects, no page will have this field
  292. $this->pageIsRedir = !$pageSet->isResolvingRedirects()
  293. ? $pageSet->getCustomField( 'page_is_redirect' )
  294. : [];
  295. $this->pageIsNew = $pageSet->getCustomField( 'page_is_new' );
  296. $this->pageTouched = $pageSet->getCustomField( 'page_touched' );
  297. $this->pageLatest = $pageSet->getCustomField( 'page_latest' );
  298. $this->pageLength = $pageSet->getCustomField( 'page_len' );
  299. // Get protection info if requested
  300. if ( $this->fld_protection ) {
  301. $this->getProtectionInfo();
  302. }
  303. if ( $this->fld_watched || $this->fld_notificationtimestamp ) {
  304. $this->getWatchedInfo();
  305. }
  306. if ( $this->fld_watchers ) {
  307. $this->getWatcherInfo();
  308. }
  309. if ( $this->fld_visitingwatchers ) {
  310. $this->getVisitingWatcherInfo();
  311. }
  312. // Run the talkid/subjectid query if requested
  313. if ( $this->fld_talkid || $this->fld_subjectid ) {
  314. $this->getTSIDs();
  315. }
  316. if ( $this->fld_displaytitle ) {
  317. $this->getDisplayTitle();
  318. }
  319. /** @var Title $title */
  320. foreach ( $this->everything as $pageid => $title ) {
  321. $pageInfo = $this->extractPageInfo( $pageid, $title );
  322. $fit = $pageInfo !== null && $result->addValue( [
  323. 'query',
  324. 'pages'
  325. ], $pageid, $pageInfo );
  326. if ( !$fit ) {
  327. $this->setContinueEnumParameter( 'continue',
  328. $title->getNamespace() . '|' .
  329. $title->getText() );
  330. break;
  331. }
  332. }
  333. }
  334. /**
  335. * Get a result array with information about a title
  336. * @param int $pageid Page ID (negative for missing titles)
  337. * @param Title $title
  338. * @return array|null
  339. */
  340. private function extractPageInfo( $pageid, $title ) {
  341. $pageInfo = [];
  342. // $title->exists() needs pageid, which is not set for all title objects
  343. $titleExists = $pageid > 0;
  344. $ns = $title->getNamespace();
  345. $dbkey = $title->getDBkey();
  346. $pageInfo['contentmodel'] = $title->getContentModel();
  347. $pageLanguage = $title->getPageLanguage();
  348. $pageInfo['pagelanguage'] = $pageLanguage->getCode();
  349. $pageInfo['pagelanguagehtmlcode'] = $pageLanguage->getHtmlCode();
  350. $pageInfo['pagelanguagedir'] = $pageLanguage->getDir();
  351. if ( $titleExists ) {
  352. $pageInfo['touched'] = wfTimestamp( TS_ISO_8601, $this->pageTouched[$pageid] );
  353. $pageInfo['lastrevid'] = intval( $this->pageLatest[$pageid] );
  354. $pageInfo['length'] = intval( $this->pageLength[$pageid] );
  355. if ( isset( $this->pageIsRedir[$pageid] ) && $this->pageIsRedir[$pageid] ) {
  356. $pageInfo['redirect'] = true;
  357. }
  358. if ( $this->pageIsNew[$pageid] ) {
  359. $pageInfo['new'] = true;
  360. }
  361. }
  362. if ( !is_null( $this->params['token'] ) ) {
  363. $tokenFunctions = $this->getTokenFunctions();
  364. $pageInfo['starttimestamp'] = wfTimestamp( TS_ISO_8601, time() );
  365. foreach ( $this->params['token'] as $t ) {
  366. $val = call_user_func( $tokenFunctions[$t], $pageid, $title );
  367. if ( $val === false ) {
  368. $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
  369. } else {
  370. $pageInfo[$t . 'token'] = $val;
  371. }
  372. }
  373. }
  374. if ( $this->fld_protection ) {
  375. $pageInfo['protection'] = [];
  376. if ( isset( $this->protections[$ns][$dbkey] ) ) {
  377. $pageInfo['protection'] =
  378. $this->protections[$ns][$dbkey];
  379. }
  380. ApiResult::setIndexedTagName( $pageInfo['protection'], 'pr' );
  381. $pageInfo['restrictiontypes'] = [];
  382. if ( isset( $this->restrictionTypes[$ns][$dbkey] ) ) {
  383. $pageInfo['restrictiontypes'] =
  384. $this->restrictionTypes[$ns][$dbkey];
  385. }
  386. ApiResult::setIndexedTagName( $pageInfo['restrictiontypes'], 'rt' );
  387. }
  388. if ( $this->fld_watched && $this->watched !== null ) {
  389. $pageInfo['watched'] = $this->watched[$ns][$dbkey];
  390. }
  391. if ( $this->fld_watchers ) {
  392. if ( $this->watchers !== null && $this->watchers[$ns][$dbkey] !== 0 ) {
  393. $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
  394. } elseif ( $this->showZeroWatchers ) {
  395. $pageInfo['watchers'] = 0;
  396. }
  397. }
  398. if ( $this->fld_visitingwatchers ) {
  399. if ( $this->visitingwatchers !== null && $this->visitingwatchers[$ns][$dbkey] !== 0 ) {
  400. $pageInfo['visitingwatchers'] = $this->visitingwatchers[$ns][$dbkey];
  401. } elseif ( $this->showZeroWatchers ) {
  402. $pageInfo['visitingwatchers'] = 0;
  403. }
  404. }
  405. if ( $this->fld_notificationtimestamp ) {
  406. $pageInfo['notificationtimestamp'] = '';
  407. if ( $this->notificationtimestamps[$ns][$dbkey] ) {
  408. $pageInfo['notificationtimestamp'] =
  409. wfTimestamp( TS_ISO_8601, $this->notificationtimestamps[$ns][$dbkey] );
  410. }
  411. }
  412. if ( $this->fld_talkid && isset( $this->talkids[$ns][$dbkey] ) ) {
  413. $pageInfo['talkid'] = $this->talkids[$ns][$dbkey];
  414. }
  415. if ( $this->fld_subjectid && isset( $this->subjectids[$ns][$dbkey] ) ) {
  416. $pageInfo['subjectid'] = $this->subjectids[$ns][$dbkey];
  417. }
  418. if ( $this->fld_url ) {
  419. $pageInfo['fullurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  420. $pageInfo['editurl'] = wfExpandUrl( $title->getFullURL( 'action=edit' ), PROTO_CURRENT );
  421. $pageInfo['canonicalurl'] = wfExpandUrl( $title->getFullURL(), PROTO_CANONICAL );
  422. }
  423. if ( $this->fld_readable ) {
  424. $pageInfo['readable'] = $title->userCan( 'read', $this->getUser() );
  425. }
  426. if ( $this->fld_preload ) {
  427. if ( $titleExists ) {
  428. $pageInfo['preload'] = '';
  429. } else {
  430. $text = null;
  431. Hooks::run( 'EditFormPreloadText', [ &$text, &$title ] );
  432. $pageInfo['preload'] = $text;
  433. }
  434. }
  435. if ( $this->fld_displaytitle ) {
  436. if ( isset( $this->displaytitles[$pageid] ) ) {
  437. $pageInfo['displaytitle'] = $this->displaytitles[$pageid];
  438. } else {
  439. $pageInfo['displaytitle'] = $title->getPrefixedText();
  440. }
  441. }
  442. if ( $this->params['testactions'] ) {
  443. $limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_SML1 : self::LIMIT_SML2;
  444. if ( $this->countTestedActions >= $limit ) {
  445. return null; // force a continuation
  446. }
  447. $user = $this->getUser();
  448. $pageInfo['actions'] = [];
  449. foreach ( $this->params['testactions'] as $action ) {
  450. $this->countTestedActions++;
  451. $pageInfo['actions'][$action] = $title->userCan( $action, $user );
  452. }
  453. }
  454. return $pageInfo;
  455. }
  456. /**
  457. * Get information about protections and put it in $protections
  458. */
  459. private function getProtectionInfo() {
  460. $this->protections = [];
  461. $db = $this->getDB();
  462. // Get normal protections for existing titles
  463. if ( count( $this->titles ) ) {
  464. $this->resetQueryParams();
  465. $this->addTables( 'page_restrictions' );
  466. $this->addFields( [ 'pr_page', 'pr_type', 'pr_level',
  467. 'pr_expiry', 'pr_cascade' ] );
  468. $this->addWhereFld( 'pr_page', array_keys( $this->titles ) );
  469. $res = $this->select( __METHOD__ );
  470. foreach ( $res as $row ) {
  471. /** @var Title $title */
  472. $title = $this->titles[$row->pr_page];
  473. $a = [
  474. 'type' => $row->pr_type,
  475. 'level' => $row->pr_level,
  476. 'expiry' => ApiResult::formatExpiry( $row->pr_expiry )
  477. ];
  478. if ( $row->pr_cascade ) {
  479. $a['cascade'] = true;
  480. }
  481. $this->protections[$title->getNamespace()][$title->getDBkey()][] = $a;
  482. }
  483. // Also check old restrictions
  484. foreach ( $this->titles as $pageId => $title ) {
  485. if ( $this->pageRestrictions[$pageId] ) {
  486. $namespace = $title->getNamespace();
  487. $dbKey = $title->getDBkey();
  488. $restrictions = explode( ':', trim( $this->pageRestrictions[$pageId] ) );
  489. foreach ( $restrictions as $restrict ) {
  490. $temp = explode( '=', trim( $restrict ) );
  491. if ( count( $temp ) == 1 ) {
  492. // old old format should be treated as edit/move restriction
  493. $restriction = trim( $temp[0] );
  494. if ( $restriction == '' ) {
  495. continue;
  496. }
  497. $this->protections[$namespace][$dbKey][] = [
  498. 'type' => 'edit',
  499. 'level' => $restriction,
  500. 'expiry' => 'infinity',
  501. ];
  502. $this->protections[$namespace][$dbKey][] = [
  503. 'type' => 'move',
  504. 'level' => $restriction,
  505. 'expiry' => 'infinity',
  506. ];
  507. } else {
  508. $restriction = trim( $temp[1] );
  509. if ( $restriction == '' ) {
  510. continue;
  511. }
  512. $this->protections[$namespace][$dbKey][] = [
  513. 'type' => $temp[0],
  514. 'level' => $restriction,
  515. 'expiry' => 'infinity',
  516. ];
  517. }
  518. }
  519. }
  520. }
  521. }
  522. // Get protections for missing titles
  523. if ( count( $this->missing ) ) {
  524. $this->resetQueryParams();
  525. $lb = new LinkBatch( $this->missing );
  526. $this->addTables( 'protected_titles' );
  527. $this->addFields( [ 'pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry' ] );
  528. $this->addWhere( $lb->constructSet( 'pt', $db ) );
  529. $res = $this->select( __METHOD__ );
  530. foreach ( $res as $row ) {
  531. $this->protections[$row->pt_namespace][$row->pt_title][] = [
  532. 'type' => 'create',
  533. 'level' => $row->pt_create_perm,
  534. 'expiry' => ApiResult::formatExpiry( $row->pt_expiry )
  535. ];
  536. }
  537. }
  538. // Separate good and missing titles into files and other pages
  539. // and populate $this->restrictionTypes
  540. $images = $others = [];
  541. foreach ( $this->everything as $title ) {
  542. if ( $title->getNamespace() == NS_FILE ) {
  543. $images[] = $title->getDBkey();
  544. } else {
  545. $others[] = $title;
  546. }
  547. // Applicable protection types
  548. $this->restrictionTypes[$title->getNamespace()][$title->getDBkey()] =
  549. array_values( $title->getRestrictionTypes() );
  550. }
  551. if ( count( $others ) ) {
  552. // Non-images: check templatelinks
  553. $lb = new LinkBatch( $others );
  554. $this->resetQueryParams();
  555. $this->addTables( [ 'page_restrictions', 'page', 'templatelinks' ] );
  556. $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
  557. 'page_title', 'page_namespace',
  558. 'tl_title', 'tl_namespace' ] );
  559. $this->addWhere( $lb->constructSet( 'tl', $db ) );
  560. $this->addWhere( 'pr_page = page_id' );
  561. $this->addWhere( 'pr_page = tl_from' );
  562. $this->addWhereFld( 'pr_cascade', 1 );
  563. $res = $this->select( __METHOD__ );
  564. foreach ( $res as $row ) {
  565. $source = Title::makeTitle( $row->page_namespace, $row->page_title );
  566. $this->protections[$row->tl_namespace][$row->tl_title][] = [
  567. 'type' => $row->pr_type,
  568. 'level' => $row->pr_level,
  569. 'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
  570. 'source' => $source->getPrefixedText()
  571. ];
  572. }
  573. }
  574. if ( count( $images ) ) {
  575. // Images: check imagelinks
  576. $this->resetQueryParams();
  577. $this->addTables( [ 'page_restrictions', 'page', 'imagelinks' ] );
  578. $this->addFields( [ 'pr_type', 'pr_level', 'pr_expiry',
  579. 'page_title', 'page_namespace', 'il_to' ] );
  580. $this->addWhere( 'pr_page = page_id' );
  581. $this->addWhere( 'pr_page = il_from' );
  582. $this->addWhereFld( 'pr_cascade', 1 );
  583. $this->addWhereFld( 'il_to', $images );
  584. $res = $this->select( __METHOD__ );
  585. foreach ( $res as $row ) {
  586. $source = Title::makeTitle( $row->page_namespace, $row->page_title );
  587. $this->protections[NS_FILE][$row->il_to][] = [
  588. 'type' => $row->pr_type,
  589. 'level' => $row->pr_level,
  590. 'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
  591. 'source' => $source->getPrefixedText()
  592. ];
  593. }
  594. }
  595. }
  596. /**
  597. * Get talk page IDs (if requested) and subject page IDs (if requested)
  598. * and put them in $talkids and $subjectids
  599. */
  600. private function getTSIDs() {
  601. $getTitles = $this->talkids = $this->subjectids = [];
  602. /** @var Title $t */
  603. foreach ( $this->everything as $t ) {
  604. if ( MWNamespace::isTalk( $t->getNamespace() ) ) {
  605. if ( $this->fld_subjectid ) {
  606. $getTitles[] = $t->getSubjectPage();
  607. }
  608. } elseif ( $this->fld_talkid ) {
  609. $getTitles[] = $t->getTalkPage();
  610. }
  611. }
  612. if ( !count( $getTitles ) ) {
  613. return;
  614. }
  615. $db = $this->getDB();
  616. // Construct a custom WHERE clause that matches
  617. // all titles in $getTitles
  618. $lb = new LinkBatch( $getTitles );
  619. $this->resetQueryParams();
  620. $this->addTables( 'page' );
  621. $this->addFields( [ 'page_title', 'page_namespace', 'page_id' ] );
  622. $this->addWhere( $lb->constructSet( 'page', $db ) );
  623. $res = $this->select( __METHOD__ );
  624. foreach ( $res as $row ) {
  625. if ( MWNamespace::isTalk( $row->page_namespace ) ) {
  626. $this->talkids[MWNamespace::getSubject( $row->page_namespace )][$row->page_title] =
  627. intval( $row->page_id );
  628. } else {
  629. $this->subjectids[MWNamespace::getTalk( $row->page_namespace )][$row->page_title] =
  630. intval( $row->page_id );
  631. }
  632. }
  633. }
  634. private function getDisplayTitle() {
  635. $this->displaytitles = [];
  636. $pageIds = array_keys( $this->titles );
  637. if ( !count( $pageIds ) ) {
  638. return;
  639. }
  640. $this->resetQueryParams();
  641. $this->addTables( 'page_props' );
  642. $this->addFields( [ 'pp_page', 'pp_value' ] );
  643. $this->addWhereFld( 'pp_page', $pageIds );
  644. $this->addWhereFld( 'pp_propname', 'displaytitle' );
  645. $res = $this->select( __METHOD__ );
  646. foreach ( $res as $row ) {
  647. $this->displaytitles[$row->pp_page] = $row->pp_value;
  648. }
  649. }
  650. /**
  651. * Get information about watched status and put it in $this->watched
  652. * and $this->notificationtimestamps
  653. */
  654. private function getWatchedInfo() {
  655. $user = $this->getUser();
  656. if ( $user->isAnon() || count( $this->everything ) == 0
  657. || !$user->isAllowed( 'viewmywatchlist' )
  658. ) {
  659. return;
  660. }
  661. $this->watched = [];
  662. $this->notificationtimestamps = [];
  663. $store = MediaWikiServices::getInstance()->getWatchedItemStore();
  664. $timestamps = $store->getNotificationTimestampsBatch( $user, $this->everything );
  665. if ( $this->fld_watched ) {
  666. foreach ( $timestamps as $namespaceId => $dbKeys ) {
  667. $this->watched[$namespaceId] = array_map(
  668. function ( $x ) {
  669. return $x !== false;
  670. },
  671. $dbKeys
  672. );
  673. }
  674. }
  675. if ( $this->fld_notificationtimestamp ) {
  676. $this->notificationtimestamps = $timestamps;
  677. }
  678. }
  679. /**
  680. * Get the count of watchers and put it in $this->watchers
  681. */
  682. private function getWatcherInfo() {
  683. if ( count( $this->everything ) == 0 ) {
  684. return;
  685. }
  686. $user = $this->getUser();
  687. $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
  688. $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
  689. if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
  690. return;
  691. }
  692. $this->showZeroWatchers = $canUnwatchedpages;
  693. $countOptions = [];
  694. if ( !$canUnwatchedpages ) {
  695. $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
  696. }
  697. $this->watchers = MediaWikiServices::getInstance()->getWatchedItemStore()->countWatchersMultiple(
  698. $this->everything,
  699. $countOptions
  700. );
  701. }
  702. /**
  703. * Get the count of watchers who have visited recent edits and put it in
  704. * $this->visitingwatchers
  705. *
  706. * Based on InfoAction::pageCounts
  707. */
  708. private function getVisitingWatcherInfo() {
  709. $config = $this->getConfig();
  710. $user = $this->getUser();
  711. $db = $this->getDB();
  712. $canUnwatchedpages = $user->isAllowed( 'unwatchedpages' );
  713. $unwatchedPageThreshold = $this->getConfig()->get( 'UnwatchedPageThreshold' );
  714. if ( !$canUnwatchedpages && !is_int( $unwatchedPageThreshold ) ) {
  715. return;
  716. }
  717. $this->showZeroWatchers = $canUnwatchedpages;
  718. $titlesWithThresholds = [];
  719. if ( $this->titles ) {
  720. $lb = new LinkBatch( $this->titles );
  721. // Fetch last edit timestamps for pages
  722. $this->resetQueryParams();
  723. $this->addTables( [ 'page', 'revision' ] );
  724. $this->addFields( [ 'page_namespace', 'page_title', 'rev_timestamp' ] );
  725. $this->addWhere( [
  726. 'page_latest = rev_id',
  727. $lb->constructSet( 'page', $db ),
  728. ] );
  729. $this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
  730. $timestampRes = $this->select( __METHOD__ );
  731. $age = $config->get( 'WatchersMaxAge' );
  732. $timestamps = [];
  733. foreach ( $timestampRes as $row ) {
  734. $revTimestamp = wfTimestamp( TS_UNIX, (int)$row->rev_timestamp );
  735. $timestamps[$row->page_namespace][$row->page_title] = $revTimestamp - $age;
  736. }
  737. $titlesWithThresholds = array_map(
  738. function ( LinkTarget $target ) use ( $timestamps ) {
  739. return [
  740. $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
  741. ];
  742. },
  743. $this->titles
  744. );
  745. }
  746. if ( $this->missing ) {
  747. $titlesWithThresholds = array_merge(
  748. $titlesWithThresholds,
  749. array_map(
  750. function ( LinkTarget $target ) {
  751. return [ $target, null ];
  752. },
  753. $this->missing
  754. )
  755. );
  756. }
  757. $store = MediaWikiServices::getInstance()->getWatchedItemStore();
  758. $this->visitingwatchers = $store->countVisitingWatchersMultiple(
  759. $titlesWithThresholds,
  760. !$canUnwatchedpages ? $unwatchedPageThreshold : null
  761. );
  762. }
  763. public function getCacheMode( $params ) {
  764. // Other props depend on something about the current user
  765. $publicProps = [
  766. 'protection',
  767. 'talkid',
  768. 'subjectid',
  769. 'url',
  770. 'preload',
  771. 'displaytitle',
  772. ];
  773. if ( array_diff( (array)$params['prop'], $publicProps ) ) {
  774. return 'private';
  775. }
  776. // testactions also depends on the current user
  777. if ( $params['testactions'] ) {
  778. return 'private';
  779. }
  780. if ( !is_null( $params['token'] ) ) {
  781. return 'private';
  782. }
  783. return 'public';
  784. }
  785. public function getAllowedParams() {
  786. return [
  787. 'prop' => [
  788. ApiBase::PARAM_ISMULTI => true,
  789. ApiBase::PARAM_TYPE => [
  790. 'protection',
  791. 'talkid',
  792. 'watched', # private
  793. 'watchers', # private
  794. 'visitingwatchers', # private
  795. 'notificationtimestamp', # private
  796. 'subjectid',
  797. 'url',
  798. 'readable', # private
  799. 'preload',
  800. 'displaytitle',
  801. // If you add more properties here, please consider whether they
  802. // need to be added to getCacheMode()
  803. ],
  804. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  805. ],
  806. 'testactions' => [
  807. ApiBase::PARAM_TYPE => 'string',
  808. ApiBase::PARAM_ISMULTI => true,
  809. ],
  810. 'token' => [
  811. ApiBase::PARAM_DEPRECATED => true,
  812. ApiBase::PARAM_ISMULTI => true,
  813. ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() )
  814. ],
  815. 'continue' => [
  816. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  817. ],
  818. ];
  819. }
  820. protected function getExamplesMessages() {
  821. return [
  822. 'action=query&prop=info&titles=Main%20Page'
  823. => 'apihelp-query+info-example-simple',
  824. 'action=query&prop=info&inprop=protection&titles=Main%20Page'
  825. => 'apihelp-query+info-example-protection',
  826. ];
  827. }
  828. public function getHelpUrls() {
  829. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Info';
  830. }
  831. }