SpecialRecentchangeslinked.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * This is to display changes made to all articles linked in an article.
  4. * @ingroup SpecialPage
  5. */
  6. class SpecialRecentchangeslinked extends SpecialRecentchanges {
  7. function __construct(){
  8. SpecialPage::SpecialPage( 'Recentchangeslinked' );
  9. $this->includable( true );
  10. }
  11. public function getDefaultOptions() {
  12. $opts = parent::getDefaultOptions();
  13. $opts->add( 'target', '' );
  14. $opts->add( 'showlinkedto', false );
  15. $opts->add( 'tagfilter', '' );
  16. return $opts;
  17. }
  18. public function parseParameters( $par, FormOptions $opts ) {
  19. $opts['target'] = $par;
  20. }
  21. public function feedSetup() {
  22. global $wgRequest;
  23. $opts = parent::feedSetup();
  24. # Feed is cached on limit,hideminor,target; other params would randomly not work
  25. $opts['target'] = $wgRequest->getVal( 'target' );
  26. return $opts;
  27. }
  28. public function getFeedObject( $feedFormat ){
  29. $feed = new ChangesFeed( $feedFormat, false );
  30. $feedObj = $feed->getFeedObject(
  31. wfMsgForContent( 'recentchangeslinked-title', $this->mTargetTitle->getPrefixedText() ),
  32. wfMsgForContent( 'recentchangeslinked' )
  33. );
  34. return array( $feed, $feedObj );
  35. }
  36. public function doMainQuery( $conds, $opts ) {
  37. global $wgUser, $wgOut;
  38. $target = $opts['target'];
  39. $showlinkedto = $opts['showlinkedto'];
  40. $limit = $opts['limit'];
  41. if ( $target === '' ) {
  42. return false;
  43. }
  44. $title = Title::newFromURL( $target );
  45. if( !$title || $title->getInterwiki() != '' ){
  46. $wgOut->wrapWikiMsg( '<div class="errorbox">$1</div><br clear="both" />', 'allpagesbadtitle' );
  47. return false;
  48. }
  49. $this->mTargetTitle = $title;
  50. $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
  51. /*
  52. * Ordinary links are in the pagelinks table, while transclusions are
  53. * in the templatelinks table, categorizations in categorylinks and
  54. * image use in imagelinks. We need to somehow combine all these.
  55. * Special:Whatlinkshere does this by firing multiple queries and
  56. * merging the results, but the code we inherit from our parent class
  57. * expects only one result set so we use UNION instead.
  58. */
  59. $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
  60. $id = $title->getArticleId();
  61. $ns = $title->getNamespace();
  62. $dbkey = $title->getDBkey();
  63. $tables = array( 'recentchanges' );
  64. $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
  65. $join_conds = array();
  66. $query_options = array();
  67. // left join with watchlist table to highlight watched rows
  68. if( $uid = $wgUser->getId() ) {
  69. $tables[] = 'watchlist';
  70. $select[] = 'wl_user';
  71. $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
  72. }
  73. ChangeTags::modifyDisplayQuery( $tables, $select, $conds, $join_conds,
  74. $query_options, $opts['tagfilter'] );
  75. // XXX: parent class does this, should we too?
  76. // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
  77. if( $ns == NS_CATEGORY && !$showlinkedto ) {
  78. // special handling for categories
  79. // XXX: should try to make this less klugy
  80. $link_tables = array( 'categorylinks' );
  81. $showlinkedto = true;
  82. } else {
  83. // for now, always join on these tables; really should be configurable as in whatlinkshere
  84. $link_tables = array( 'pagelinks', 'templatelinks' );
  85. // imagelinks only contains links to pages in NS_FILE
  86. if( $ns == NS_FILE || !$showlinkedto ) $link_tables[] = 'imagelinks';
  87. }
  88. if( $id == 0 && !$showlinkedto )
  89. return false; // nonexistent pages can't link to any pages
  90. // field name prefixes for all the various tables we might want to join with
  91. $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
  92. $subsql = array(); // SELECT statements to combine with UNION
  93. foreach( $link_tables as $link_table ) {
  94. $pfx = $prefix[$link_table];
  95. // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
  96. if( $link_table == 'imagelinks' ) $link_ns = NS_FILE;
  97. else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
  98. else $link_ns = 0;
  99. if( $showlinkedto ) {
  100. // find changes to pages linking to this page
  101. if( $link_ns ) {
  102. if( $ns != $link_ns ) continue; // should never happen, but check anyway
  103. $subconds = array( "{$pfx}_to" => $dbkey );
  104. } else {
  105. $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
  106. }
  107. $subjoin = "rc_cur_id = {$pfx}_from";
  108. } else {
  109. // find changes to pages linked from this page
  110. $subconds = array( "{$pfx}_from" => $id );
  111. if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
  112. $subconds["rc_namespace"] = $link_ns;
  113. $subjoin = "rc_title = {$pfx}_to";
  114. } else {
  115. $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
  116. }
  117. }
  118. $subsql[] = $dbr->selectSQLText(
  119. array_merge( $tables, array( $link_table ) ),
  120. $select,
  121. $conds + $subconds,
  122. __METHOD__,
  123. array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options,
  124. $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
  125. );
  126. }
  127. if( count($subsql) == 0 )
  128. return false; // should never happen
  129. if( count($subsql) == 1 )
  130. $sql = $subsql[0];
  131. else {
  132. // need to resort and relimit after union
  133. $sql = "(" . implode( ") UNION (", $subsql ) . ") ORDER BY rc_timestamp DESC LIMIT {$limit}";
  134. }
  135. $res = $dbr->query( $sql, __METHOD__ );
  136. if( $res->numRows() == 0 )
  137. $this->mResultEmpty = true;
  138. return $res;
  139. }
  140. function getExtraOptions( $opts ){
  141. $opts->consumeValues( array( 'showlinkedto', 'target' ) );
  142. $extraOpts = array();
  143. $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
  144. $extraOpts['target'] = array( wfMsg( 'recentchangeslinked-page' ),
  145. Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
  146. Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
  147. Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
  148. $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
  149. if ($tagFilter)
  150. $extraOpts['tagfilter'] = $tagFilter;
  151. return $extraOpts;
  152. }
  153. function setTopText( OutputPage $out, FormOptions $opts ) {
  154. global $wgUser;
  155. $skin = $wgUser->getSkin();
  156. if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) )
  157. $out->setSubtitle( wfMsg( 'recentchangeslinked-backlink', $skin->link( $this->mTargetTitle,
  158. $this->mTargetTitle->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
  159. }
  160. function setBottomText( OutputPage $out, FormOptions $opts ){
  161. if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) ){
  162. global $wgUser;
  163. $out->setFeedAppendQuery( "target=" . urlencode( $this->mTargetTitle->getPrefixedDBkey() ) );
  164. }
  165. if( isset( $this->mResultEmpty ) && $this->mResultEmpty ){
  166. $out->addWikiMsg( 'recentchangeslinked-noresult' );
  167. }
  168. }
  169. }