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