ApiQueryBacklinksprop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * API module to handle links table back-queries
  4. *
  5. * Copyright © 2014 Wikimedia Foundation and contributors
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @since 1.24
  24. */
  25. /**
  26. * This implements prop=redirects, prop=linkshere, prop=catmembers,
  27. * prop=transcludedin, and prop=fileusage
  28. *
  29. * @ingroup API
  30. * @since 1.24
  31. */
  32. class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
  33. // Data for the various modules implemented by this class
  34. private static $settings = [
  35. 'redirects' => [
  36. 'code' => 'rd',
  37. 'prefix' => 'rd',
  38. 'linktable' => 'redirect',
  39. 'props' => [
  40. 'fragment',
  41. ],
  42. 'showredirects' => false,
  43. 'show' => [
  44. 'fragment',
  45. '!fragment',
  46. ],
  47. ],
  48. 'linkshere' => [
  49. 'code' => 'lh',
  50. 'prefix' => 'pl',
  51. 'linktable' => 'pagelinks',
  52. 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
  53. 'from_namespace' => true,
  54. 'showredirects' => true,
  55. ],
  56. 'transcludedin' => [
  57. 'code' => 'ti',
  58. 'prefix' => 'tl',
  59. 'linktable' => 'templatelinks',
  60. 'indexes' => [ 'tl_namespace', 'tl_backlinks_namespace' ],
  61. 'from_namespace' => true,
  62. 'showredirects' => true,
  63. ],
  64. 'fileusage' => [
  65. 'code' => 'fu',
  66. 'prefix' => 'il',
  67. 'linktable' => 'imagelinks',
  68. 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
  69. 'from_namespace' => true,
  70. 'to_namespace' => NS_FILE,
  71. 'exampletitle' => 'File:Example.jpg',
  72. 'showredirects' => true,
  73. ],
  74. ];
  75. public function __construct( ApiQuery $query, $moduleName ) {
  76. parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
  77. }
  78. public function execute() {
  79. $this->run();
  80. }
  81. public function executeGenerator( $resultPageSet ) {
  82. $this->run( $resultPageSet );
  83. }
  84. /**
  85. * @param ApiPageSet|null $resultPageSet
  86. */
  87. private function run( ApiPageSet $resultPageSet = null ) {
  88. $settings = self::$settings[$this->getModuleName()];
  89. $db = $this->getDB();
  90. $params = $this->extractRequestParams();
  91. $prop = array_flip( $params['prop'] );
  92. $emptyString = $db->addQuotes( '' );
  93. $pageSet = $this->getPageSet();
  94. $titles = $pageSet->getGoodAndMissingTitles();
  95. $map = $pageSet->getGoodAndMissingTitlesByNamespace();
  96. // Add in special pages, they can theoretically have backlinks too.
  97. // (although currently they only do for prop=redirects)
  98. foreach ( $pageSet->getSpecialTitles() as $id => $title ) {
  99. $titles[] = $title;
  100. $map[$title->getNamespace()][$title->getDBkey()] = $id;
  101. }
  102. // Determine our fields to query on
  103. $p = $settings['prefix'];
  104. $hasNS = !isset( $settings['to_namespace'] );
  105. if ( $hasNS ) {
  106. $bl_namespace = "{$p}_namespace";
  107. $bl_title = "{$p}_title";
  108. } else {
  109. $bl_namespace = $settings['to_namespace'];
  110. $bl_title = "{$p}_to";
  111. $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
  112. return $t->getNamespace() === $bl_namespace;
  113. } );
  114. $map = array_intersect_key( $map, [ $bl_namespace => true ] );
  115. }
  116. $bl_from = "{$p}_from";
  117. if ( !$titles ) {
  118. return; // nothing to do
  119. }
  120. if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
  121. return; // nothing to do
  122. }
  123. // Figure out what we're sorting by, and add associated WHERE clauses.
  124. // MySQL's query planner screws up if we include a field in ORDER BY
  125. // when it's constant in WHERE, so we have to test that for each field.
  126. $sortby = [];
  127. if ( $hasNS && count( $map ) > 1 ) {
  128. $sortby[$bl_namespace] = 'ns';
  129. }
  130. $theTitle = null;
  131. foreach ( $map as $nsTitles ) {
  132. reset( $nsTitles );
  133. $key = key( $nsTitles );
  134. if ( $theTitle === null ) {
  135. $theTitle = $key;
  136. }
  137. if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
  138. $sortby[$bl_title] = 'title';
  139. break;
  140. }
  141. }
  142. $miser_ns = null;
  143. if ( $params['namespace'] !== null ) {
  144. if ( empty( $settings['from_namespace'] ) ) {
  145. if ( $this->getConfig()->get( 'MiserMode' ) ) {
  146. $miser_ns = $params['namespace'];
  147. } else {
  148. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  149. }
  150. } else {
  151. $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
  152. if ( !empty( $settings['from_namespace'] )
  153. && $params['namespace'] !== null && count( $params['namespace'] ) > 1
  154. ) {
  155. $sortby["{$p}_from_namespace"] = 'int';
  156. }
  157. }
  158. }
  159. $sortby[$bl_from] = 'int';
  160. // Now use the $sortby to figure out the continuation
  161. if ( !is_null( $params['continue'] ) ) {
  162. $cont = explode( '|', $params['continue'] );
  163. $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
  164. $where = '';
  165. $i = count( $sortby ) - 1;
  166. foreach ( array_reverse( $sortby, true ) as $field => $type ) {
  167. $v = $cont[$i];
  168. switch ( $type ) {
  169. case 'ns':
  170. case 'int':
  171. $v = (int)$v;
  172. $this->dieContinueUsageIf( $v != $cont[$i] );
  173. break;
  174. default:
  175. $v = $db->addQuotes( $v );
  176. break;
  177. }
  178. if ( $where === '' ) {
  179. $where = "$field >= $v";
  180. } else {
  181. $where = "$field > $v OR ($field = $v AND ($where))";
  182. }
  183. $i--;
  184. }
  185. $this->addWhere( $where );
  186. }
  187. // Populate the rest of the query
  188. $this->addTables( [ $settings['linktable'], 'page' ] );
  189. $this->addWhere( "$bl_from = page_id" );
  190. if ( $this->getModuleName() === 'redirects' ) {
  191. $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
  192. }
  193. $this->addFields( array_keys( $sortby ) );
  194. $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
  195. if ( is_null( $resultPageSet ) ) {
  196. $fld_pageid = isset( $prop['pageid'] );
  197. $fld_title = isset( $prop['title'] );
  198. $fld_redirect = isset( $prop['redirect'] );
  199. $this->addFieldsIf( 'page_id', $fld_pageid );
  200. $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
  201. $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
  202. // prop=redirects
  203. $fld_fragment = isset( $prop['fragment'] );
  204. $this->addFieldsIf( 'rd_fragment', $fld_fragment );
  205. } else {
  206. $this->addFields( $resultPageSet->getPageTableFields() );
  207. }
  208. $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
  209. if ( $hasNS ) {
  210. // Can't use LinkBatch because it throws away Special titles.
  211. // And we already have the needed data structure anyway.
  212. $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
  213. } else {
  214. $where = [];
  215. foreach ( $titles as $t ) {
  216. if ( $t->getNamespace() == $bl_namespace ) {
  217. $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
  218. }
  219. }
  220. $this->addWhere( $db->makeList( $where, LIST_OR ) );
  221. }
  222. if ( $params['show'] !== null ) {
  223. // prop=redirects only
  224. $show = array_flip( $params['show'] );
  225. if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
  226. isset( $show['redirect'] ) && isset( $show['!redirect'] )
  227. ) {
  228. $this->dieWithError( 'apierror-show' );
  229. }
  230. $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
  231. $this->addWhereIf(
  232. "rd_fragment = $emptyString OR rd_fragment IS NULL",
  233. isset( $show['!fragment'] )
  234. );
  235. $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
  236. $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
  237. }
  238. // Override any ORDER BY from above with what we calculated earlier.
  239. $this->addOption( 'ORDER BY', array_keys( $sortby ) );
  240. // MySQL's optimizer chokes if we have too many values in "$bl_title IN
  241. // (...)" and chooses the wrong index, so specify the correct index to
  242. // use for the query. See T139056 for details.
  243. if ( !empty( $settings['indexes'] ) ) {
  244. list( $idxNoFromNS, $idxWithFromNS ) = $settings['indexes'];
  245. if ( $params['namespace'] !== null && !empty( $settings['from_namespace'] ) ) {
  246. $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
  247. } else {
  248. $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
  249. }
  250. }
  251. // MySQL (or at least 5.5.5-10.0.23-MariaDB) chooses a really bad query
  252. // plan if it thinks there will be more matching rows in the linktable
  253. // than are in page. Use STRAIGHT_JOIN here to force it to use the
  254. // intended, fast plan. See T145079 for details.
  255. $this->addOption( 'STRAIGHT_JOIN' );
  256. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  257. $res = $this->select( __METHOD__ );
  258. if ( is_null( $resultPageSet ) ) {
  259. if ( $fld_title ) {
  260. $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
  261. }
  262. $count = 0;
  263. foreach ( $res as $row ) {
  264. if ( ++$count > $params['limit'] ) {
  265. // We've reached the one extra which shows that
  266. // there are additional pages to be had. Stop here...
  267. $this->setContinue( $row, $sortby );
  268. break;
  269. }
  270. if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
  271. // Miser mode namespace check
  272. continue;
  273. }
  274. // Get the ID of the current page
  275. $id = $map[$row->bl_namespace][$row->bl_title];
  276. $vals = [];
  277. if ( $fld_pageid ) {
  278. $vals['pageid'] = (int)$row->page_id;
  279. }
  280. if ( $fld_title ) {
  281. ApiQueryBase::addTitleInfo( $vals,
  282. Title::makeTitle( $row->page_namespace, $row->page_title )
  283. );
  284. }
  285. if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
  286. $vals['fragment'] = $row->rd_fragment;
  287. }
  288. if ( $fld_redirect ) {
  289. $vals['redirect'] = (bool)$row->page_is_redirect;
  290. }
  291. $fit = $this->addPageSubItem( $id, $vals );
  292. if ( !$fit ) {
  293. $this->setContinue( $row, $sortby );
  294. break;
  295. }
  296. }
  297. } else {
  298. $titles = [];
  299. $count = 0;
  300. foreach ( $res as $row ) {
  301. if ( ++$count > $params['limit'] ) {
  302. // We've reached the one extra which shows that
  303. // there are additional pages to be had. Stop here...
  304. $this->setContinue( $row, $sortby );
  305. break;
  306. }
  307. if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
  308. // Miser mode namespace check
  309. continue;
  310. }
  311. $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
  312. }
  313. $resultPageSet->populateFromTitles( $titles );
  314. }
  315. }
  316. private function setContinue( $row, $sortby ) {
  317. $cont = [];
  318. foreach ( $sortby as $field => $v ) {
  319. $cont[] = $row->$field;
  320. }
  321. $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
  322. }
  323. public function getCacheMode( $params ) {
  324. return 'public';
  325. }
  326. public function getAllowedParams() {
  327. $settings = self::$settings[$this->getModuleName()];
  328. $ret = [
  329. 'prop' => [
  330. ApiBase::PARAM_TYPE => [
  331. 'pageid',
  332. 'title',
  333. ],
  334. ApiBase::PARAM_ISMULTI => true,
  335. ApiBase::PARAM_DFLT => 'pageid|title',
  336. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  337. ],
  338. 'namespace' => [
  339. ApiBase::PARAM_ISMULTI => true,
  340. ApiBase::PARAM_TYPE => 'namespace',
  341. ],
  342. 'show' => null, // Will be filled/removed below
  343. 'limit' => [
  344. ApiBase::PARAM_DFLT => 10,
  345. ApiBase::PARAM_TYPE => 'limit',
  346. ApiBase::PARAM_MIN => 1,
  347. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  348. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  349. ],
  350. 'continue' => [
  351. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  352. ],
  353. ];
  354. if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
  355. $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
  356. 'api-help-param-limited-in-miser-mode',
  357. ];
  358. }
  359. if ( !empty( $settings['showredirects'] ) ) {
  360. $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
  361. $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
  362. }
  363. if ( isset( $settings['props'] ) ) {
  364. $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
  365. $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
  366. );
  367. }
  368. $show = [];
  369. if ( !empty( $settings['showredirects'] ) ) {
  370. $show[] = 'redirect';
  371. $show[] = '!redirect';
  372. }
  373. if ( isset( $settings['show'] ) ) {
  374. $show = array_merge( $show, $settings['show'] );
  375. }
  376. if ( $show ) {
  377. $ret['show'] = [
  378. ApiBase::PARAM_TYPE => $show,
  379. ApiBase::PARAM_ISMULTI => true,
  380. ];
  381. } else {
  382. unset( $ret['show'] );
  383. }
  384. return $ret;
  385. }
  386. protected function getExamplesMessages() {
  387. $settings = self::$settings[$this->getModuleName()];
  388. $name = $this->getModuleName();
  389. $path = $this->getModulePath();
  390. $title = $settings['exampletitle'] ?? 'Main Page';
  391. $etitle = rawurlencode( $title );
  392. return [
  393. "action=query&prop={$name}&titles={$etitle}"
  394. => "apihelp-$path-example-simple",
  395. "action=query&generator={$name}&titles={$etitle}&prop=info"
  396. => "apihelp-$path-example-generator",
  397. ];
  398. }
  399. public function getHelpUrls() {
  400. $name = ucfirst( $this->getModuleName() );
  401. return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
  402. }
  403. }