ApiQueryBacklinks.php 17 KB

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