49cef5b53aa0e9ef707ca28ad189050abab8b31a
[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-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 $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 $query_options = array();
80
81 // left join with watchlist table to highlight watched rows
82 if( $uid = $wgUser->getId() ) {
83 $tables[] = 'watchlist';
84 $select[] = 'wl_user';
85 $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
86 }
87 if ( $wgUser->isAllowed( 'rollback' ) ) {
88 $tables[] = 'page';
89 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
90 $select[] = 'page_latest';
91 }
92
93 ChangeTags::modifyDisplayQuery( $tables, $select, $conds, $join_conds,
94 $query_options, $opts['tagfilter'] );
95
96 // XXX: parent class does this, should we too?
97 // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
98
99 if( $ns == NS_CATEGORY && !$showlinkedto ) {
100 // special handling for categories
101 // XXX: should try to make this less klugy
102 $link_tables = array( 'categorylinks' );
103 $showlinkedto = true;
104 } else {
105 // for now, always join on these tables; really should be configurable as in whatlinkshere
106 $link_tables = array( 'pagelinks', 'templatelinks' );
107 // imagelinks only contains links to pages in NS_FILE
108 if( $ns == NS_FILE || !$showlinkedto ) $link_tables[] = 'imagelinks';
109 }
110
111 if( $id == 0 && !$showlinkedto )
112 return false; // nonexistent pages can't link to any pages
113
114 // field name prefixes for all the various tables we might want to join with
115 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
116
117 $subsql = array(); // SELECT statements to combine with UNION
118
119 foreach( $link_tables as $link_table ) {
120 $pfx = $prefix[$link_table];
121
122 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
123 if( $link_table == 'imagelinks' ) $link_ns = NS_FILE;
124 else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
125 else $link_ns = 0;
126
127 if( $showlinkedto ) {
128 // find changes to pages linking to this page
129 if( $link_ns ) {
130 if( $ns != $link_ns ) continue; // should never happen, but check anyway
131 $subconds = array( "{$pfx}_to" => $dbkey );
132 } else {
133 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
134 }
135 $subjoin = "rc_cur_id = {$pfx}_from";
136 } else {
137 // find changes to pages linked from this page
138 $subconds = array( "{$pfx}_from" => $id );
139 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
140 $subconds["rc_namespace"] = $link_ns;
141 $subjoin = "rc_title = {$pfx}_to";
142 } else {
143 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
144 }
145 }
146
147 if( $dbr->unionSupportsOrderAndLimit())
148 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
149 else
150 $order = array();
151
152
153 $query = $dbr->selectSQLText(
154 array_merge( $tables, array( $link_table ) ),
155 $select,
156 $conds + $subconds,
157 __METHOD__,
158 $order + $query_options,
159 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
160 );
161
162 if( $dbr->unionSupportsOrderAndLimit())
163 $query = $dbr->limitResult( $query, $limit );
164
165 $subsql[] = $query;
166 }
167
168 if( count($subsql) == 0 )
169 return false; // should never happen
170 if( count($subsql) == 1 && $dbr->unionSupportsOrderAndLimit() )
171 $sql = $subsql[0];
172 else {
173 // need to resort and relimit after union
174 $sql = $dbr->unionQueries($subsql, false).' ORDER BY rc_timestamp DESC';
175 $sql = $dbr->limitResult($sql, $limit, false);
176 }
177
178 $res = $dbr->query( $sql, __METHOD__ );
179
180 if( $res->numRows() == 0 )
181 $this->mResultEmpty = true;
182
183 return $res;
184 }
185
186 function getExtraOptions( $opts ){
187 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
188 $extraOpts = array();
189 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
190 $extraOpts['target'] = array( wfMsgHtml( 'recentchangeslinked-page' ),
191 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
192 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
193 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
194 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
195 if ($tagFilter)
196 $extraOpts['tagfilter'] = $tagFilter;
197 return $extraOpts;
198 }
199
200 function setTopText( OutputPage $out, FormOptions $opts ) {
201 global $wgUser;
202 $skin = $wgUser->getSkin();
203 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) )
204 $out->setSubtitle( wfMsg( 'recentchangeslinked-backlink', $skin->link( $this->mTargetTitle,
205 $this->mTargetTitle->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
206 }
207
208 function setBottomText( OutputPage $out, FormOptions $opts ){
209 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) ){
210 $out->setFeedAppendQuery( "target=" . urlencode( $this->mTargetTitle->getPrefixedDBkey() ) );
211 }
212 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ){
213 $out->addWikiMsg( 'recentchangeslinked-noresult' );
214 }
215 }
216 }