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