ApiQueryBacklinks.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. /**
  23. * This is a three-in-one module to query:
  24. * * backlinks - links pointing to the given page,
  25. * * embeddedin - what pages transclude the given page within themselves,
  26. * * imageusage - what pages use the given image
  27. *
  28. * @ingroup API
  29. */
  30. class ApiQueryBacklinks extends ApiQueryGeneratorBase {
  31. /**
  32. * @var Title
  33. */
  34. private $rootTitle;
  35. private $params;
  36. /** @var array */
  37. private $cont;
  38. private $redirect;
  39. private $bl_ns, $bl_from, $bl_from_ns, $bl_table, $bl_code, $bl_title, $bl_fields, $hasNS;
  40. /** @var string */
  41. private $helpUrl;
  42. /**
  43. * Maps ns and title to pageid
  44. *
  45. * @var array
  46. */
  47. private $pageMap = [];
  48. private $resultArr;
  49. private $redirTitles = [];
  50. private $continueStr = null;
  51. // output element name, database column field prefix, database table
  52. private $backlinksSettings = [
  53. 'backlinks' => [
  54. 'code' => 'bl',
  55. 'prefix' => 'pl',
  56. 'linktbl' => 'pagelinks',
  57. 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Backlinks',
  58. ],
  59. 'embeddedin' => [
  60. 'code' => 'ei',
  61. 'prefix' => 'tl',
  62. 'linktbl' => 'templatelinks',
  63. 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Embeddedin',
  64. ],
  65. 'imageusage' => [
  66. 'code' => 'iu',
  67. 'prefix' => 'il',
  68. 'linktbl' => 'imagelinks',
  69. 'helpurl' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Imageusage',
  70. ]
  71. ];
  72. public function __construct( ApiQuery $query, $moduleName ) {
  73. $settings = $this->backlinksSettings[$moduleName];
  74. $prefix = $settings['prefix'];
  75. $code = $settings['code'];
  76. $this->resultArr = [];
  77. parent::__construct( $query, $moduleName, $code );
  78. $this->bl_ns = $prefix . '_namespace';
  79. $this->bl_from = $prefix . '_from';
  80. $this->bl_from_ns = $prefix . '_from_namespace';
  81. $this->bl_table = $settings['linktbl'];
  82. $this->bl_code = $code;
  83. $this->helpUrl = $settings['helpurl'];
  84. $this->hasNS = $moduleName !== 'imageusage';
  85. if ( $this->hasNS ) {
  86. $this->bl_title = $prefix . '_title';
  87. $this->bl_fields = [
  88. $this->bl_ns,
  89. $this->bl_title
  90. ];
  91. } else {
  92. $this->bl_title = $prefix . '_to';
  93. $this->bl_fields = [
  94. $this->bl_title
  95. ];
  96. }
  97. }
  98. public function execute() {
  99. $this->run();
  100. }
  101. public function getCacheMode( $params ) {
  102. return 'public';
  103. }
  104. public function executeGenerator( $resultPageSet ) {
  105. $this->run( $resultPageSet );
  106. }
  107. /**
  108. * @param ApiPageSet $resultPageSet
  109. * @return void
  110. */
  111. private function runFirstQuery( $resultPageSet = null ) {
  112. $this->addTables( [ $this->bl_table, 'page' ] );
  113. $this->addWhere( "{$this->bl_from}=page_id" );
  114. if ( is_null( $resultPageSet ) ) {
  115. $this->addFields( [ 'page_id', 'page_title', 'page_namespace' ] );
  116. } else {
  117. $this->addFields( $resultPageSet->getPageTableFields() );
  118. }
  119. $this->addFields( [ 'page_is_redirect', 'from_ns' => 'page_namespace' ] );
  120. $this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
  121. if ( $this->hasNS ) {
  122. $this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
  123. }
  124. $this->addWhereFld( $this->bl_from_ns, $this->params['namespace'] );
  125. if ( count( $this->cont ) >= 2 ) {
  126. $op = $this->params['dir'] == 'descending' ? '<' : '>';
  127. if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
  128. $this->addWhere(
  129. "{$this->bl_from_ns} $op {$this->cont[0]} OR " .
  130. "({$this->bl_from_ns} = {$this->cont[0]} AND " .
  131. "{$this->bl_from} $op= {$this->cont[1]})"
  132. );
  133. } else {
  134. $this->addWhere( "{$this->bl_from} $op= {$this->cont[1]}" );
  135. }
  136. }
  137. if ( $this->params['filterredir'] == 'redirects' ) {
  138. $this->addWhereFld( 'page_is_redirect', 1 );
  139. } elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) {
  140. // T24245 - Check for !redirect, as filtering nonredirects, when
  141. // getting what links to them is contradictory
  142. $this->addWhereFld( 'page_is_redirect', 0 );
  143. }
  144. $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
  145. $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
  146. $orderBy = [];
  147. if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
  148. $orderBy[] = $this->bl_from_ns . $sort;
  149. }
  150. $orderBy[] = $this->bl_from . $sort;
  151. $this->addOption( 'ORDER BY', $orderBy );
  152. $this->addOption( 'STRAIGHT_JOIN' );
  153. $res = $this->select( __METHOD__ );
  154. $count = 0;
  155. foreach ( $res as $row ) {
  156. if ( ++$count > $this->params['limit'] ) {
  157. // We've reached the one extra which shows that there are
  158. // additional pages to be had. Stop here...
  159. // Continue string may be overridden at a later step
  160. $this->continueStr = "{$row->from_ns}|{$row->page_id}";
  161. break;
  162. }
  163. // Fill in continuation fields for later steps
  164. if ( count( $this->cont ) < 2 ) {
  165. $this->cont[] = $row->from_ns;
  166. $this->cont[] = $row->page_id;
  167. }
  168. $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
  169. $t = Title::makeTitle( $row->page_namespace, $row->page_title );
  170. if ( $row->page_is_redirect ) {
  171. $this->redirTitles[] = $t;
  172. }
  173. if ( is_null( $resultPageSet ) ) {
  174. $a = [ 'pageid' => (int)$row->page_id ];
  175. ApiQueryBase::addTitleInfo( $a, $t );
  176. if ( $row->page_is_redirect ) {
  177. $a['redirect'] = true;
  178. }
  179. // Put all the results in an array first
  180. $this->resultArr[$a['pageid']] = $a;
  181. } else {
  182. $resultPageSet->processDbRow( $row );
  183. }
  184. }
  185. }
  186. /**
  187. * @param ApiPageSet $resultPageSet
  188. * @return void
  189. */
  190. private function runSecondQuery( $resultPageSet = null ) {
  191. $db = $this->getDB();
  192. $this->addTables( [ 'page', $this->bl_table ] );
  193. $this->addWhere( "{$this->bl_from}=page_id" );
  194. if ( is_null( $resultPageSet ) ) {
  195. $this->addFields( [ 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ] );
  196. } else {
  197. $this->addFields( $resultPageSet->getPageTableFields() );
  198. }
  199. $this->addFields( [ $this->bl_title, 'from_ns' => 'page_namespace' ] );
  200. if ( $this->hasNS ) {
  201. $this->addFields( $this->bl_ns );
  202. }
  203. // We can't use LinkBatch here because $this->hasNS may be false
  204. $titleWhere = [];
  205. $allRedirNs = [];
  206. $allRedirDBkey = [];
  207. /** @var Title $t */
  208. foreach ( $this->redirTitles as $t ) {
  209. $redirNs = $t->getNamespace();
  210. $redirDBkey = $t->getDBkey();
  211. $titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
  212. ( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' );
  213. $allRedirNs[$redirNs] = true;
  214. $allRedirDBkey[$redirDBkey] = true;
  215. }
  216. $this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
  217. $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
  218. if ( count( $this->cont ) >= 6 ) {
  219. $op = $this->params['dir'] == 'descending' ? '<' : '>';
  220. $where = "{$this->bl_from} $op= {$this->cont[5]}";
  221. // Don't bother with namespace, title, or from_namespace if it's
  222. // otherwise constant in the where clause.
  223. if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
  224. $where = "{$this->bl_from_ns} $op {$this->cont[4]} OR " .
  225. "({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
  226. }
  227. if ( count( $allRedirDBkey ) > 1 ) {
  228. $title = $db->addQuotes( $this->cont[3] );
  229. $where = "{$this->bl_title} $op $title OR " .
  230. "({$this->bl_title} = $title AND ($where))";
  231. }
  232. if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
  233. $where = "{$this->bl_ns} $op {$this->cont[2]} OR " .
  234. "({$this->bl_ns} = {$this->cont[2]} AND ($where))";
  235. }
  236. $this->addWhere( $where );
  237. }
  238. if ( $this->params['filterredir'] == 'redirects' ) {
  239. $this->addWhereFld( 'page_is_redirect', 1 );
  240. } elseif ( $this->params['filterredir'] == 'nonredirects' ) {
  241. $this->addWhereFld( 'page_is_redirect', 0 );
  242. }
  243. $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
  244. $orderBy = [];
  245. $sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
  246. // Don't order by namespace/title/from_namespace if it's constant in the WHERE clause
  247. if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
  248. $orderBy[] = $this->bl_ns . $sort;
  249. }
  250. if ( count( $allRedirDBkey ) > 1 ) {
  251. $orderBy[] = $this->bl_title . $sort;
  252. }
  253. if ( $this->params['namespace'] !== null && count( $this->params['namespace'] ) > 1 ) {
  254. $orderBy[] = $this->bl_from_ns . $sort;
  255. }
  256. $orderBy[] = $this->bl_from . $sort;
  257. $this->addOption( 'ORDER BY', $orderBy );
  258. $this->addOption( 'USE INDEX', [ 'page' => 'PRIMARY' ] );
  259. $res = $this->select( __METHOD__ );
  260. $count = 0;
  261. foreach ( $res as $row ) {
  262. $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
  263. if ( ++$count > $this->params['limit'] ) {
  264. // We've reached the one extra which shows that there are
  265. // additional pages to be had. Stop here...
  266. // Note we must keep the parameters for the first query constant
  267. // This may be overridden at a later step
  268. $title = $row->{$this->bl_title};
  269. $this->continueStr = implode( '|', array_slice( $this->cont, 0, 2 ) ) .
  270. "|$ns|$title|{$row->from_ns}|{$row->page_id}";
  271. break;
  272. }
  273. // Fill in continuation fields for later steps
  274. if ( count( $this->cont ) < 6 ) {
  275. $this->cont[] = $ns;
  276. $this->cont[] = $row->{$this->bl_title};
  277. $this->cont[] = $row->from_ns;
  278. $this->cont[] = $row->page_id;
  279. }
  280. if ( is_null( $resultPageSet ) ) {
  281. $a = [ 'pageid' => (int)$row->page_id ];
  282. ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
  283. if ( $row->page_is_redirect ) {
  284. $a['redirect'] = true;
  285. }
  286. $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
  287. // Put all the results in an array first
  288. $this->resultArr[$parentID]['redirlinks'][$row->page_id] = $a;
  289. } else {
  290. $resultPageSet->processDbRow( $row );
  291. }
  292. }
  293. }
  294. /**
  295. * @param ApiPageSet $resultPageSet
  296. * @return void
  297. */
  298. private function run( $resultPageSet = null ) {
  299. $this->params = $this->extractRequestParams( false );
  300. $this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
  301. $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
  302. $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
  303. $result = $this->getResult();
  304. if ( $this->params['limit'] == 'max' ) {
  305. $this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  306. $result->addParsedLimit( $this->getModuleName(), $this->params['limit'] );
  307. } else {
  308. $this->params['limit'] = (int)$this->params['limit'];
  309. $this->validateLimit( 'limit', $this->params['limit'], 1, $userMax, $botMax );
  310. }
  311. $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params );
  312. // only image titles are allowed for the root in imageinfo mode
  313. if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) {
  314. $this->dieWithError(
  315. [ 'apierror-imageusage-badtitle', $this->getModuleName() ],
  316. 'bad_image_title'
  317. );
  318. }
  319. // Parse and validate continuation parameter
  320. $this->cont = [];
  321. if ( $this->params['continue'] !== null ) {
  322. $cont = explode( '|', $this->params['continue'] );
  323. switch ( count( $cont ) ) {
  324. case 8:
  325. // redirect page ID for result adding
  326. $this->cont[7] = (int)$cont[7];
  327. $this->dieContinueUsageIf( $cont[7] !== (string)$this->cont[7] );
  328. /* Fall through */
  329. case 7:
  330. // top-level page ID for result adding
  331. $this->cont[6] = (int)$cont[6];
  332. $this->dieContinueUsageIf( $cont[6] !== (string)$this->cont[6] );
  333. /* Fall through */
  334. case 6:
  335. // ns for 2nd query (even for imageusage)
  336. $this->cont[2] = (int)$cont[2];
  337. $this->dieContinueUsageIf( $cont[2] !== (string)$this->cont[2] );
  338. // title for 2nd query
  339. $this->cont[3] = $cont[3];
  340. // from_ns for 2nd query
  341. $this->cont[4] = (int)$cont[4];
  342. $this->dieContinueUsageIf( $cont[4] !== (string)$this->cont[4] );
  343. // from_id for 1st query
  344. $this->cont[5] = (int)$cont[5];
  345. $this->dieContinueUsageIf( $cont[5] !== (string)$this->cont[5] );
  346. /* Fall through */
  347. case 2:
  348. // from_ns for 1st query
  349. $this->cont[0] = (int)$cont[0];
  350. $this->dieContinueUsageIf( $cont[0] !== (string)$this->cont[0] );
  351. // from_id for 1st query
  352. $this->cont[1] = (int)$cont[1];
  353. $this->dieContinueUsageIf( $cont[1] !== (string)$this->cont[1] );
  354. break;
  355. default:
  356. $this->dieContinueUsageIf( true );
  357. }
  358. ksort( $this->cont );
  359. }
  360. $this->runFirstQuery( $resultPageSet );
  361. if ( $this->redirect && count( $this->redirTitles ) ) {
  362. $this->resetQueryParams();
  363. $this->runSecondQuery( $resultPageSet );
  364. }
  365. // Fill in any missing fields in case it's needed below
  366. $this->cont += [ 0, 0, 0, '', 0, 0, 0 ];
  367. if ( is_null( $resultPageSet ) ) {
  368. // Try to add the result data in one go and pray that it fits
  369. $code = $this->bl_code;
  370. $data = array_map( function ( $arr ) use ( $code ) {
  371. if ( isset( $arr['redirlinks'] ) ) {
  372. $arr['redirlinks'] = array_values( $arr['redirlinks'] );
  373. ApiResult::setIndexedTagName( $arr['redirlinks'], $code );
  374. }
  375. return $arr;
  376. }, array_values( $this->resultArr ) );
  377. $fit = $result->addValue( 'query', $this->getModuleName(), $data );
  378. if ( !$fit ) {
  379. // It didn't fit. Add elements one by one until the
  380. // result is full.
  381. ksort( $this->resultArr );
  382. if ( count( $this->cont ) >= 7 ) {
  383. $startAt = $this->cont[6];
  384. } else {
  385. reset( $this->resultArr );
  386. $startAt = key( $this->resultArr );
  387. }
  388. $idx = 0;
  389. foreach ( $this->resultArr as $pageID => $arr ) {
  390. if ( $pageID < $startAt ) {
  391. continue;
  392. }
  393. // Add the basic entry without redirlinks first
  394. $fit = $result->addValue(
  395. [ 'query', $this->getModuleName() ],
  396. $idx, array_diff_key( $arr, [ 'redirlinks' => '' ] ) );
  397. if ( !$fit ) {
  398. $this->continueStr = implode( '|', array_slice( $this->cont, 0, 6 ) ) .
  399. "|$pageID";
  400. break;
  401. }
  402. $hasRedirs = false;
  403. $redirLinks = isset( $arr['redirlinks'] ) ? (array)$arr['redirlinks'] : [];
  404. ksort( $redirLinks );
  405. if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
  406. $redirStartAt = $this->cont[7];
  407. } else {
  408. reset( $redirLinks );
  409. $redirStartAt = key( $redirLinks );
  410. }
  411. foreach ( $redirLinks as $key => $redir ) {
  412. if ( $key < $redirStartAt ) {
  413. continue;
  414. }
  415. $fit = $result->addValue(
  416. [ 'query', $this->getModuleName(), $idx, 'redirlinks' ],
  417. null, $redir );
  418. if ( !$fit ) {
  419. $this->continueStr = implode( '|', array_slice( $this->cont, 0, 6 ) ) .
  420. "|$pageID|$key";
  421. break;
  422. }
  423. $hasRedirs = true;
  424. }
  425. if ( $hasRedirs ) {
  426. $result->addIndexedTagName(
  427. [ 'query', $this->getModuleName(), $idx, 'redirlinks' ],
  428. $this->bl_code );
  429. }
  430. if ( !$fit ) {
  431. break;
  432. }
  433. $idx++;
  434. }
  435. }
  436. $result->addIndexedTagName(
  437. [ 'query', $this->getModuleName() ],
  438. $this->bl_code
  439. );
  440. }
  441. if ( !is_null( $this->continueStr ) ) {
  442. $this->setContinueEnumParameter( 'continue', $this->continueStr );
  443. }
  444. }
  445. public function getAllowedParams() {
  446. $retval = [
  447. 'title' => [
  448. ApiBase::PARAM_TYPE => 'string',
  449. ],
  450. 'pageid' => [
  451. ApiBase::PARAM_TYPE => 'integer',
  452. ],
  453. 'continue' => [
  454. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  455. ],
  456. 'namespace' => [
  457. ApiBase::PARAM_ISMULTI => true,
  458. ApiBase::PARAM_TYPE => 'namespace'
  459. ],
  460. 'dir' => [
  461. ApiBase::PARAM_DFLT => 'ascending',
  462. ApiBase::PARAM_TYPE => [
  463. 'ascending',
  464. 'descending'
  465. ]
  466. ],
  467. 'filterredir' => [
  468. ApiBase::PARAM_DFLT => 'all',
  469. ApiBase::PARAM_TYPE => [
  470. 'all',
  471. 'redirects',
  472. 'nonredirects'
  473. ]
  474. ],
  475. 'limit' => [
  476. ApiBase::PARAM_DFLT => 10,
  477. ApiBase::PARAM_TYPE => 'limit',
  478. ApiBase::PARAM_MIN => 1,
  479. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  480. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  481. ]
  482. ];
  483. if ( $this->getModuleName() == 'embeddedin' ) {
  484. return $retval;
  485. }
  486. $retval['redirect'] = false;
  487. return $retval;
  488. }
  489. protected function getExamplesMessages() {
  490. static $examples = [
  491. 'backlinks' => [
  492. 'action=query&list=backlinks&bltitle=Main%20Page'
  493. => 'apihelp-query+backlinks-example-simple',
  494. 'action=query&generator=backlinks&gbltitle=Main%20Page&prop=info'
  495. => 'apihelp-query+backlinks-example-generator',
  496. ],
  497. 'embeddedin' => [
  498. 'action=query&list=embeddedin&eititle=Template:Stub'
  499. => 'apihelp-query+embeddedin-example-simple',
  500. 'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
  501. => 'apihelp-query+embeddedin-example-generator',
  502. ],
  503. 'imageusage' => [
  504. 'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
  505. => 'apihelp-query+imageusage-example-simple',
  506. 'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
  507. => 'apihelp-query+imageusage-example-generator',
  508. ]
  509. ];
  510. return $examples[$this->getModuleName()];
  511. }
  512. public function getHelpUrls() {
  513. return $this->helpUrl;
  514. }
  515. }