Slight fix for r47781: remove useless if($index) conditional: $index is always set...
[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
9 function __construct(){
10 SpecialPage::SpecialPage( 'Recentchangeslinked' );
11 $this->includable( true );
12 }
13
14 public function getDefaultOptions() {
15 $opts = parent::getDefaultOptions();
16 $opts->add( 'target', '' );
17 $opts->add( 'showlinkedto', false );
18 $opts->add( 'tagfilter', '' );
19 return $opts;
20 }
21
22 public function parseParameters( $par, FormOptions $opts ) {
23 $opts['target'] = $par;
24 }
25
26 public function feedSetup() {
27 global $wgRequest;
28 $opts = parent::feedSetup();
29 # Feed is cached on limit,hideminor,target; other params would randomly not work
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->mTargetTitle->getPrefixedText() ),
38 wfMsgForContent( 'recentchangeslinked' )
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">$1</div><br clear="both" />', 'allpagesbadtitle' );
56 return false;
57 }
58 $this->mTargetTitle = $title;
59
60 $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
61
62 /*
63 * Ordinary links are in the pagelinks table, while transclusions are
64 * in the templatelinks table, categorizations in categorylinks and
65 * image use in imagelinks. We need to somehow combine all these.
66 * Special:Whatlinkshere does this by firing multiple queries and
67 * merging the results, but the code we inherit from our parent class
68 * expects only one result set so we use UNION instead.
69 */
70
71 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
72 $id = $title->getArticleId();
73 $ns = $title->getNamespace();
74 $dbkey = $title->getDBkey();
75
76 $tables = array( 'recentchanges' );
77 $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
78 $join_conds = 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
87 ChangeTags::modifyDisplayQuery( $tables, $select, $conds, $join_conds, $opts['tagfilter'] );
88
89 // XXX: parent class does this, should we too?
90 // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
91
92 if( $ns == NS_CATEGORY && !$showlinkedto ) {
93 // special handling for categories
94 // XXX: should try to make this less klugy
95 $link_tables = array( 'categorylinks' );
96 $showlinkedto = true;
97 } else {
98 // for now, always join on these tables; really should be configurable as in whatlinkshere
99 $link_tables = array( 'pagelinks', 'templatelinks' );
100 // imagelinks only contains links to pages in NS_FILE
101 if( $ns == NS_FILE || !$showlinkedto ) $link_tables[] = 'imagelinks';
102 }
103
104 if( $id == 0 && !$showlinkedto )
105 return false; // nonexistent pages can't link to any pages
106
107 // field name prefixes for all the various tables we might want to join with
108 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
109
110 $subsql = array(); // SELECT statements to combine with UNION
111
112 foreach( $link_tables as $link_table ) {
113 $pfx = $prefix[$link_table];
114
115 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
116 if( $link_table == 'imagelinks' ) $link_ns = NS_FILE;
117 else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
118 else $link_ns = 0;
119
120 if( $showlinkedto ) {
121 // find changes to pages linking to this page
122 if( $link_ns ) {
123 if( $ns != $link_ns ) continue; // should never happen, but check anyway
124 $subconds = array( "{$pfx}_to" => $dbkey );
125 } else {
126 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
127 }
128 $subjoin = "rc_cur_id = {$pfx}_from";
129 } else {
130 // find changes to pages linked from this page
131 $subconds = array( "{$pfx}_from" => $id );
132 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
133 $subconds["rc_namespace"] = $link_ns;
134 $subjoin = "rc_title = {$pfx}_to";
135 } else {
136 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
137 }
138 }
139
140 $subsql[] = $dbr->selectSQLText(
141 array_merge( $tables, array( $link_table ) ),
142 $select,
143 $conds + $subconds,
144 __METHOD__,
145 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ),
146 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
147 );
148 }
149
150 if( count($subsql) == 0 )
151 return false; // should never happen
152 if( count($subsql) == 1 )
153 $sql = $subsql[0];
154 else {
155 // need to resort and relimit after union
156 $sql = "(" . implode( ") UNION (", $subsql ) . ") ORDER BY rc_timestamp DESC LIMIT {$limit}";
157 }
158
159 $res = $dbr->query( $sql, __METHOD__ );
160
161 if( $res->numRows() == 0 )
162 $this->mResultEmpty = true;
163
164 return $res;
165 }
166
167 function getExtraOptions( $opts ){
168 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
169 $extraOpts = array();
170 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
171 $extraOpts['target'] = array( wfMsg( 'recentchangeslinked-page' ),
172 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
173 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
174 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
175 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
176 if ($tagFilter)
177 $extraOpts['tagfilter'] = $tagFilter;
178 return $extraOpts;
179 }
180
181 function setTopText( OutputPage $out, FormOptions $opts ) {
182 global $wgUser;
183 $skin = $wgUser->getSkin();
184 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) )
185 $out->setSubtitle( wfMsg( 'recentchangeslinked-backlink', $skin->link( $this->mTargetTitle,
186 $this->mTargetTitle->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
187 }
188
189 function setBottomText( OutputPage $out, FormOptions $opts ){
190 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) ){
191 global $wgUser;
192 $out->setFeedAppendQuery( "target=" . urlencode( $this->mTargetTitle->getPrefixedDBkey() ) );
193 }
194 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ){
195 $out->addWikiMsg( 'recentchangeslinked-noresult' );
196 }
197 }
198 }