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