Merge "QA: updates to edit test ported from qa/browsertests"
[mediawiki.git] / includes / specials / SpecialRecentchangeslinked.php
blobe73cabce79964a7a3a3bf2279bdc1f0d1a6411ce
1 <?php
2 /**
3 * Implements Special:Recentchangeslinked
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * This is to display changes made to all articles linked in an article.
27 * @ingroup SpecialPage
29 class SpecialRecentChangesLinked extends SpecialRecentChanges {
30 /** @var bool|Title */
31 protected $rclTargetTitle;
33 function __construct() {
34 parent::__construct( 'Recentchangeslinked' );
37 public function getDefaultOptions() {
38 $opts = parent::getDefaultOptions();
39 $opts->add( 'target', '' );
40 $opts->add( 'showlinkedto', false );
42 return $opts;
45 public function parseParameters( $par, FormOptions $opts ) {
46 $opts['target'] = $par;
49 public function doMainQuery( $conds, $opts ) {
50 $target = $opts['target'];
51 $showlinkedto = $opts['showlinkedto'];
52 $limit = $opts['limit'];
54 if ( $target === '' ) {
55 return false;
57 $outputPage = $this->getOutput();
58 $title = Title::newFromURL( $target );
59 if ( !$title || $title->isExternal() ) {
60 $outputPage->addHtml( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )
61 ->parse() . '</div>' );
63 return false;
66 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
69 * Ordinary links are in the pagelinks table, while transclusions are
70 * in the templatelinks table, categorizations in categorylinks and
71 * image use in imagelinks. We need to somehow combine all these.
72 * Special:Whatlinkshere does this by firing multiple queries and
73 * merging the results, but the code we inherit from our parent class
74 * expects only one result set so we use UNION instead.
77 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
78 $id = $title->getArticleID();
79 $ns = $title->getNamespace();
80 $dbkey = $title->getDBkey();
82 $tables = array( 'recentchanges' );
83 $select = RecentChange::selectFields();
84 $join_conds = array();
85 $query_options = array();
87 // left join with watchlist table to highlight watched rows
88 $uid = $this->getUser()->getId();
89 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
90 $tables[] = 'watchlist';
91 $select[] = 'wl_user';
92 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
93 'wl_user' => $uid,
94 'wl_title=rc_title',
95 'wl_namespace=rc_namespace'
96 ) );
98 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
99 $tables[] = 'page';
100 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
101 $select[] = 'page_latest';
103 ChangeTags::modifyDisplayQuery(
104 $tables,
105 $select,
106 $conds,
107 $join_conds,
108 $query_options,
109 $opts['tagfilter']
112 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
113 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ),
114 '1.23' )
116 return false;
119 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
120 // special handling for categories
121 // XXX: should try to make this less kludgy
122 $link_tables = array( 'categorylinks' );
123 $showlinkedto = true;
124 } else {
125 // for now, always join on these tables; really should be configurable as in whatlinkshere
126 $link_tables = array( 'pagelinks', 'templatelinks' );
127 // imagelinks only contains links to pages in NS_FILE
128 if ( $ns == NS_FILE || !$showlinkedto ) {
129 $link_tables[] = 'imagelinks';
133 if ( $id == 0 && !$showlinkedto ) {
134 return false; // nonexistent pages can't link to any pages
137 // field name prefixes for all the various tables we might want to join with
138 $prefix = array(
139 'pagelinks' => 'pl',
140 'templatelinks' => 'tl',
141 'categorylinks' => 'cl',
142 'imagelinks' => 'il'
145 $subsql = array(); // SELECT statements to combine with UNION
147 foreach ( $link_tables as $link_table ) {
148 $pfx = $prefix[$link_table];
150 // imagelinks and categorylinks tables have no xx_namespace field,
151 // and have xx_to instead of xx_title
152 if ( $link_table == 'imagelinks' ) {
153 $link_ns = NS_FILE;
154 } elseif ( $link_table == 'categorylinks' ) {
155 $link_ns = NS_CATEGORY;
156 } else {
157 $link_ns = 0;
160 if ( $showlinkedto ) {
161 // find changes to pages linking to this page
162 if ( $link_ns ) {
163 if ( $ns != $link_ns ) {
164 continue;
165 } // should never happen, but check anyway
166 $subconds = array( "{$pfx}_to" => $dbkey );
167 } else {
168 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
170 $subjoin = "rc_cur_id = {$pfx}_from";
171 } else {
172 // find changes to pages linked from this page
173 $subconds = array( "{$pfx}_from" => $id );
174 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
175 $subconds["rc_namespace"] = $link_ns;
176 $subjoin = "rc_title = {$pfx}_to";
177 } else {
178 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
182 if ( $dbr->unionSupportsOrderAndLimit() ) {
183 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
184 } else {
185 $order = array();
188 $query = $dbr->selectSQLText(
189 array_merge( $tables, array( $link_table ) ),
190 $select,
191 $conds + $subconds,
192 __METHOD__,
193 $order + $query_options,
194 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
197 if ( $dbr->unionSupportsOrderAndLimit() ) {
198 $query = $dbr->limitResult( $query, $limit );
201 $subsql[] = $query;
204 if ( count( $subsql ) == 0 ) {
205 return false; // should never happen
207 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
208 $sql = $subsql[0];
209 } else {
210 // need to resort and relimit after union
211 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
212 $sql = $dbr->limitResult( $sql, $limit, false );
215 $res = $dbr->query( $sql, __METHOD__ );
217 if ( $res->numRows() == 0 ) {
218 $this->mResultEmpty = true;
221 return $res;
224 function setTopText( FormOptions $opts ) {
225 $target = $this->getTargetTitle();
226 if ( $target ) {
227 $this->getOutput()->addBacklinkSubtitle( $target );
232 * Get options to be displayed in a form
234 * @param FormOptions $opts
235 * @return array
237 function getExtraOptions( $opts ) {
238 $extraOpts = parent::getExtraOptions( $opts );
240 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
242 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
243 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
244 Xml::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
245 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
247 return $extraOpts;
251 * @return Title
253 function getTargetTitle() {
254 if ( $this->rclTargetTitle === null ) {
255 $opts = $this->getOptions();
256 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
257 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
258 } else {
259 $this->rclTargetTitle = false;
263 return $this->rclTargetTitle;