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