Use $wgContLang here, this should not vary with the user language.
[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 }
12
13 public function getDefaultOptions() {
14 $opts = parent::getDefaultOptions();
15 $opts->add( 'target', '' );
16 $opts->add( 'showlinkedto', false );
17 return $opts;
18 }
19
20 public function parseParameters( $par, FormOptions $opts ) {
21 $opts['target'] = $par;
22 }
23
24 public function feedSetup(){
25 global $wgRequest;
26 $opts = parent::feedSetup();
27 $opts['target'] = $wgRequest->getVal( 'target' );
28 return $opts;
29 }
30
31 public function getFeedObject( $feedFormat ){
32 $feed = new ChangesFeed( $feedFormat, false );
33 $feedObj = $feed->getFeedObject(
34 wfMsgForContent( 'recentchangeslinked-title', $this->mTargetTitle->getPrefixedText() ),
35 wfMsgForContent( 'recentchangeslinked' )
36 );
37 return array( $feed, $feedObj );
38 }
39
40 public function doMainQuery( $conds, $opts ) {
41 global $wgUser, $wgOut;
42
43 $target = $opts['target'];
44 $showlinkedto = $opts['showlinkedto'];
45 $limit = $opts['limit'];
46
47 if ( $target === '' ) {
48 return false;
49 }
50 $title = Title::newFromURL( $target );
51 if( !$title || $title->getInterwiki() != '' ){
52 $wgOut->wrapWikiMsg( '<div class="errorbox">$1</div><br clear="both" />', 'allpagesbadtitle' );
53 return false;
54 }
55 $this->mTargetTitle = $title;
56
57 $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
58
59 /*
60 * Ordinary links are in the pagelinks table, while transclusions are
61 * in the templatelinks table, categorizations in categorylinks and
62 * image use in imagelinks. We need to somehow combine all these.
63 * Special:Whatlinkshere does this by firing multiple queries and
64 * merging the results, but the code we inherit from our parent class
65 * expects only one result set so we use UNION instead.
66 */
67
68 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
69 $id = $title->getArticleId();
70 $ns = $title->getNamespace();
71 $dbkey = $title->getDBkey();
72
73 $tables = array( 'recentchanges' );
74 $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
75 $join_conds = array();
76
77 // left join with watchlist table to highlight watched rows
78 if( $uid = $wgUser->getId() ) {
79 $tables[] = 'watchlist';
80 $select[] = 'wl_user';
81 $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
82 }
83
84 // XXX: parent class does this, should we too?
85 // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
86
87 if( $ns == NS_CATEGORY && !$showlinkedto ) {
88 // special handling for categories
89 // XXX: should try to make this less klugy
90 $link_tables = array( 'categorylinks' );
91 $showlinkedto = true;
92 } else {
93 // for now, always join on these tables; really should be configurable as in whatlinkshere
94 $link_tables = array( 'pagelinks', 'templatelinks' );
95 // imagelinks only contains links to pages in NS_IMAGE
96 if( $ns == NS_IMAGE || !$showlinkedto ) $link_tables[] = 'imagelinks';
97 }
98
99 if( $id == 0 && !$showlinkedto )
100 return false; // nonexistent pages can't link to any pages
101
102 // field name prefixes for all the various tables we might want to join with
103 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
104
105 $subsql = array(); // SELECT statements to combine with UNION
106
107 foreach( $link_tables as $link_table ) {
108 $pfx = $prefix[$link_table];
109
110 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
111 if( $link_table == 'imagelinks' ) $link_ns = NS_IMAGE;
112 else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
113 else $link_ns = 0;
114
115 if( $showlinkedto ) {
116 // find changes to pages linking to this page
117 if( $link_ns ) {
118 if( $ns != $link_ns ) continue; // should never happen, but check anyway
119 $subconds = array( "{$pfx}_to" => $dbkey );
120 } else {
121 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
122 }
123 $subjoin = "rc_cur_id = {$pfx}_from";
124 } else {
125 // find changes to pages linked from this page
126 $subconds = array( "{$pfx}_from" => $id );
127 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
128 $subconds["rc_namespace"] = $link_ns;
129 $subjoin = "rc_title = {$pfx}_to";
130 } else {
131 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
132 }
133 }
134
135 $subsql[] = $dbr->selectSQLText( array_merge( $tables, array( $link_table ) ), $select, $conds + $subconds,
136 __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ),
137 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) ) );
138 }
139
140 if( count($subsql) == 0 )
141 return false; // should never happen
142 if( count($subsql) == 1 )
143 $sql = $subsql[0];
144 else {
145 // need to resort and relimit after union
146 $sql = "(" . implode( ") UNION (", $subsql ) . ") ORDER BY rc_timestamp DESC LIMIT {$limit}";
147 }
148
149 $res = $dbr->query( $sql, __METHOD__ );
150
151 if( $res->numRows() == 0 )
152 $this->mResultEmpty = true;
153
154 return $res;
155 }
156
157 function getExtraOptions( $opts ){
158 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
159 $extraOpts = array();
160 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
161 $extraOpts['target'] = array( wfMsg( 'recentchangeslinked-page' ),
162 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
163 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
164 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
165 return $extraOpts;
166 }
167
168 function setTopText( OutputPage $out, FormOptions $opts ) {
169 global $wgUser;
170 $skin = $wgUser->getSkin();
171 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) )
172 $out->setSubtitle( wfMsg( 'recentchangeslinked-backlink', $skin->link( $this->mTargetTitle,
173 $this->mTargetTitle->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
174 }
175
176 function setBottomText( OutputPage $out, FormOptions $opts ){
177 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) ){
178 global $wgUser;
179 $out->setFeedAppendQuery( "target=" . urlencode( $this->mTargetTitle->getPrefixedDBkey() ) );
180 }
181 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ){
182 $out->addWikiMsg( 'recentchangeslinked-noresult' );
183 }
184 }
185 }