SpecialRecentChangesLinked.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * Implements Special:Recentchangeslinked
  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. * @ingroup SpecialPage
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * This is to display changes made to all articles linked in an article.
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialRecentChangesLinked extends SpecialRecentChanges {
  30. /** @var bool|Title */
  31. protected $rclTargetTitle;
  32. function __construct() {
  33. parent::__construct( 'Recentchangeslinked' );
  34. }
  35. public function getDefaultOptions() {
  36. $opts = parent::getDefaultOptions();
  37. $opts->add( 'target', '' );
  38. $opts->add( 'showlinkedto', false );
  39. return $opts;
  40. }
  41. public function parseParameters( $par, FormOptions $opts ) {
  42. $opts['target'] = $par;
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. protected function doMainQuery( $tables, $select, $conds, $query_options,
  48. $join_conds, FormOptions $opts
  49. ) {
  50. $target = $opts['target'];
  51. $showlinkedto = $opts['showlinkedto'];
  52. $limit = $opts['limit'];
  53. if ( $target === '' ) {
  54. return false;
  55. }
  56. $outputPage = $this->getOutput();
  57. $title = Title::newFromText( $target );
  58. if ( !$title || $title->isExternal() ) {
  59. $outputPage->addHTML(
  60. Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
  61. );
  62. return false;
  63. }
  64. $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
  65. /*
  66. * Ordinary links are in the pagelinks table, while transclusions are
  67. * in the templatelinks table, categorizations in categorylinks and
  68. * image use in imagelinks. We need to somehow combine all these.
  69. * Special:Whatlinkshere does this by firing multiple queries and
  70. * merging the results, but the code we inherit from our parent class
  71. * expects only one result set so we use UNION instead.
  72. */
  73. $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
  74. $id = $title->getArticleID();
  75. $ns = $title->getNamespace();
  76. $dbkey = $title->getDBkey();
  77. $rcQuery = RecentChange::getQueryInfo();
  78. $tables = array_merge( $tables, $rcQuery['tables'] );
  79. $select = array_merge( $rcQuery['fields'], $select );
  80. $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
  81. // left join with watchlist table to highlight watched rows
  82. $uid = $this->getUser()->getId();
  83. if ( $uid && MediaWikiServices::getInstance()
  84. ->getPermissionManager()
  85. ->userHasRight( $this->getUser(), 'viewmywatchlist' )
  86. ) {
  87. $tables[] = 'watchlist';
  88. $select[] = 'wl_user';
  89. $join_conds['watchlist'] = [ 'LEFT JOIN', [
  90. 'wl_user' => $uid,
  91. 'wl_title=rc_title',
  92. 'wl_namespace=rc_namespace'
  93. ] ];
  94. }
  95. // JOIN on page, used for 'last revision' filter highlight
  96. $tables[] = 'page';
  97. $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
  98. $select[] = 'page_latest';
  99. $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
  100. ChangeTags::modifyDisplayQuery(
  101. $tables,
  102. $select,
  103. $conds,
  104. $join_conds,
  105. $query_options,
  106. $tagFilter
  107. );
  108. if ( $dbr->unionSupportsOrderAndLimit() ) {
  109. if ( count( $tagFilter ) > 1 ) {
  110. // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
  111. // To prevent this from causing query performance problems, we need to add
  112. // a GROUP BY, and add rc_id to the ORDER BY.
  113. $order = [
  114. 'GROUP BY' => 'rc_timestamp, rc_id',
  115. 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
  116. ];
  117. } else {
  118. $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
  119. }
  120. } else {
  121. $order = [];
  122. }
  123. if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
  124. $opts )
  125. ) {
  126. return false;
  127. }
  128. if ( $ns == NS_CATEGORY && !$showlinkedto ) {
  129. // special handling for categories
  130. // XXX: should try to make this less kludgy
  131. $link_tables = [ 'categorylinks' ];
  132. $showlinkedto = true;
  133. } else {
  134. // for now, always join on these tables; really should be configurable as in whatlinkshere
  135. $link_tables = [ 'pagelinks', 'templatelinks' ];
  136. // imagelinks only contains links to pages in NS_FILE
  137. if ( $ns == NS_FILE || !$showlinkedto ) {
  138. $link_tables[] = 'imagelinks';
  139. }
  140. }
  141. if ( $id == 0 && !$showlinkedto ) {
  142. return false; // nonexistent pages can't link to any pages
  143. }
  144. // field name prefixes for all the various tables we might want to join with
  145. $prefix = [
  146. 'pagelinks' => 'pl',
  147. 'templatelinks' => 'tl',
  148. 'categorylinks' => 'cl',
  149. 'imagelinks' => 'il'
  150. ];
  151. $subsql = []; // SELECT statements to combine with UNION
  152. foreach ( $link_tables as $link_table ) {
  153. $pfx = $prefix[$link_table];
  154. // imagelinks and categorylinks tables have no xx_namespace field,
  155. // and have xx_to instead of xx_title
  156. if ( $link_table == 'imagelinks' ) {
  157. $link_ns = NS_FILE;
  158. } elseif ( $link_table == 'categorylinks' ) {
  159. $link_ns = NS_CATEGORY;
  160. } else {
  161. $link_ns = 0;
  162. }
  163. if ( $showlinkedto ) {
  164. // find changes to pages linking to this page
  165. if ( $link_ns ) {
  166. if ( $ns != $link_ns ) {
  167. continue;
  168. } // should never happen, but check anyway
  169. $subconds = [ "{$pfx}_to" => $dbkey ];
  170. } else {
  171. $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
  172. }
  173. $subjoin = "rc_cur_id = {$pfx}_from";
  174. } else {
  175. // find changes to pages linked from this page
  176. $subconds = [ "{$pfx}_from" => $id ];
  177. if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
  178. $subconds["rc_namespace"] = $link_ns;
  179. $subjoin = "rc_title = {$pfx}_to";
  180. } else {
  181. $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
  182. }
  183. }
  184. $query = $dbr->selectSQLText(
  185. array_merge( $tables, [ $link_table ] ),
  186. $select,
  187. $conds + $subconds,
  188. __METHOD__,
  189. $order + $query_options,
  190. $join_conds + [ $link_table => [ 'JOIN', $subjoin ] ]
  191. );
  192. if ( $dbr->unionSupportsOrderAndLimit() ) {
  193. $query = $dbr->limitResult( $query, $limit );
  194. }
  195. $subsql[] = $query;
  196. }
  197. if ( count( $subsql ) == 0 ) {
  198. return false; // should never happen
  199. }
  200. if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
  201. $sql = $subsql[0];
  202. } else {
  203. // need to resort and relimit after union
  204. $sql = $dbr->unionQueries( $subsql, $dbr::UNION_DISTINCT ) .
  205. ' ORDER BY rc_timestamp DESC';
  206. $sql = $dbr->limitResult( $sql, $limit, false );
  207. }
  208. return $dbr->query( $sql, __METHOD__ );
  209. }
  210. function setTopText( FormOptions $opts ) {
  211. $target = $this->getTargetTitle();
  212. if ( $target ) {
  213. $this->getOutput()->addBacklinkSubtitle( $target );
  214. $this->getSkin()->setRelevantTitle( $target );
  215. }
  216. }
  217. /**
  218. * Get options to be displayed in a form
  219. *
  220. * @param FormOptions $opts
  221. * @return array
  222. */
  223. function getExtraOptions( $opts ) {
  224. $extraOpts = parent::getExtraOptions( $opts );
  225. $opts->consumeValues( [ 'showlinkedto', 'target' ] );
  226. $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
  227. Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
  228. Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
  229. Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
  230. $this->addHelpLink( 'Help:Related changes' );
  231. return $extraOpts;
  232. }
  233. /**
  234. * @return Title
  235. */
  236. function getTargetTitle() {
  237. if ( $this->rclTargetTitle === null ) {
  238. $opts = $this->getOptions();
  239. if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
  240. $this->rclTargetTitle = Title::newFromText( $opts['target'] );
  241. } else {
  242. $this->rclTargetTitle = false;
  243. }
  244. }
  245. return $this->rclTargetTitle;
  246. }
  247. /**
  248. * Return an array of subpages beginning with $search that this special page will accept.
  249. *
  250. * @param string $search Prefix to search for
  251. * @param int $limit Maximum number of results to return (usually 10)
  252. * @param int $offset Number of results to skip (usually 0)
  253. * @return string[] Matching subpages
  254. */
  255. public function prefixSearchSubpages( $search, $limit, $offset ) {
  256. return $this->prefixSearchString( $search, $limit, $offset );
  257. }
  258. protected function outputNoResults() {
  259. $targetTitle = $this->getTargetTitle();
  260. if ( $targetTitle === false ) {
  261. $this->getOutput()->addHTML(
  262. Html::rawElement(
  263. 'div',
  264. [ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ],
  265. $this->msg( 'recentchanges-notargetpage' )->parse()
  266. )
  267. );
  268. } elseif ( !$targetTitle || $targetTitle->isExternal() ) {
  269. $this->getOutput()->addHTML(
  270. Html::rawElement(
  271. 'div',
  272. [ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ],
  273. $this->msg( 'allpagesbadtitle' )->parse()
  274. )
  275. );
  276. } else {
  277. parent::outputNoResults();
  278. }
  279. }
  280. }