re-tweak filename: todo -- refactor this crap to use classes and the autoloader
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchangeslinked.php
1 <?php
2 /**
3 * This is to display changes made to all articles linked in an article.
4 * @file
5 * @ingroup SpecialPage
6 */
7
8 require_once( 'SpecialRecentchanges.php' );
9
10 /**
11 * Entrypoint
12 * @param string $par parent page we will look at
13 */
14 function wfSpecialRecentchangeslinked( $par = NULL ) {
15 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle, $wgScript;
16
17 $days = $wgRequest->getInt( 'days' );
18 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
19 $hideminor = $wgRequest->getBool( 'hideminor' ) ? 1 : 0;
20 $showlinkedto = $wgRequest->getBool( 'showlinkedto' ) ? 1 : 0;
21
22 $title = Title::newFromURL( $target );
23 $target = $title ? $title->getPrefixedText() : '';
24
25 $wgOut->setPagetitle( wfMsg( 'recentchangeslinked' ) );
26 $sk = $wgUser->getSkin();
27
28 $wgOut->addHTML(
29 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
30 Xml::openElement( 'fieldset' ) .
31 Xml::element( 'legend', array(), wfMsg( 'recentchangeslinked' ) ) . "\n" .
32 Xml::inputLabel( wfMsg( 'recentchangeslinked-page' ), 'target', 'recentchangeslinked-target', 40, $target ) .
33 "&nbsp;&nbsp;&nbsp;<span style='white-space: nowrap'>" .
34 Xml::check( 'showlinkedto', $showlinkedto, array('id' => 'showlinkedto') ) . ' ' .
35 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) .
36 "</span><br/>\n" .
37 Xml::hidden( 'title', $wgTitle->getPrefixedText() ). "\n" .
38 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
39 Xml::closeElement( 'fieldset' ) .
40 Xml::closeElement( 'form' ) . "\n"
41 );
42
43 if ( !$target ) {
44 return;
45 }
46 $nt = Title::newFromURL( $target );
47 if( !$nt ) {
48 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
49 return;
50 }
51 $id = $nt->getArticleId();
52
53 $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $nt->getPrefixedText() ) );
54 $wgOut->setSyndicated();
55 $wgOut->setFeedAppendQuery( "target=" . urlencode( $target ) );
56
57 if ( !$days ) {
58 $days = (int)$wgUser->getOption( 'rcdays', 7 );
59 }
60 list( $limit, /* offset */ ) = wfCheckLimits( 100, 'rclimit' );
61
62 $dbr = wfGetDB( DB_SLAVE,'recentchangeslinked' );
63 $cutoff = $dbr->timestamp( time() - ( $days * 86400 ) );
64
65 $hideminor = ($hideminor ? 1 : 0);
66 if ( $hideminor ) {
67 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchangeslinked' ),
68 wfMsg( 'show' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
69 "&days={$days}&limit={$limit}&hideminor=0&showlinkedto={$showlinkedto}" );
70 } else {
71 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Recentchangeslinked" ),
72 wfMsg( "hide" ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
73 "&days={$days}&limit={$limit}&hideminor=1&showlinkedto={$showlinkedto}" );
74 }
75 if ( $hideminor ) {
76 $cmq = 'AND rc_minor=0';
77 } else { $cmq = ''; }
78
79 list($recentchanges, $categorylinks, $pagelinks, $watchlist) =
80 $dbr->tableNamesN( 'recentchanges', 'categorylinks', 'pagelinks', "watchlist" );
81
82 $uid = $wgUser->getId();
83 // The fields we are selecting
84 $fields = "rc_cur_id,rc_namespace,rc_title,
85 rc_user,rc_comment,rc_user_text,rc_timestamp,rc_minor,rc_log_type,rc_log_action,rc_params,rc_deleted,
86 rc_new, rc_id, rc_this_oldid, rc_last_oldid, rc_bot, rc_patrolled, rc_type, rc_old_len, rc_new_len";
87 $fields .= $uid ? ",wl_user" : "";
88
89 // Check if this should be a feed
90
91 $feed = false;
92 global $wgFeedLimit;
93 $feedFormat = $wgRequest->getVal( 'feed' );
94 if( $feedFormat ) {
95 $feed = new ChangesFeed( $feedFormat, false );
96 # Sanity check
97 if( $limit > $wgFeedLimit ) {
98 $limit = $wgFeedLimit;
99 }
100 }
101
102 // If target is a Category, use categorylinks and invert from and to
103 if( $nt->getNamespace() == NS_CATEGORY ) {
104 $catkey = $dbr->addQuotes( $nt->getDBkey() );
105 # The table clauses
106 $tables = "$categorylinks, $recentchanges";
107 $tables .= $uid ? " LEFT JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "";
108
109 $sql = "SELECT /* wfSpecialRecentchangeslinked */ $fields FROM $tables
110 WHERE rc_timestamp > '{$cutoff}' {$cmq}
111 AND cl_from=rc_cur_id
112 AND cl_to=$catkey
113 GROUP BY $fields ORDER BY rc_timestamp DESC LIMIT {$limit}"; // Shitty-ass GROUP BY by for postgres apparently
114 } else {
115 if( $showlinkedto ) {
116 $ns = $dbr->addQuotes( $nt->getNamespace() );
117 $dbkey = $dbr->addQuotes( $nt->getDBkey() );
118 $joinConds = "AND pl_namespace={$ns} AND pl_title={$dbkey} AND pl_from=rc_cur_id";
119 } else {
120 $joinConds = "AND pl_namespace=rc_namespace AND pl_title=rc_title AND pl_from=$id";
121 }
122 # The table clauses
123 $tables = "$pagelinks, $recentchanges";
124 $tables .= $uid ? " LEFT JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "";
125
126 $sql = "SELECT /* wfSpecialRecentchangeslinked */ $fields FROM $tables
127 WHERE rc_timestamp > '{$cutoff}' {$cmq}
128 {$joinConds}
129 GROUP BY $fields ORDER BY rc_timestamp DESC LIMIT {$limit}"; // Shitty-ass GROUP BY by for postgres apparently
130 }
131 # Actually do the query
132 $res = $dbr->query( $sql, __METHOD__ );
133 $count = $dbr->numRows( $res );
134 $rchanges = array();
135 # Output feeds now and be done with it!
136 if( $feed ) {
137 if( $count ) {
138 $counter = 1;
139 while ( $limit ) {
140 if ( 0 == $count ) { break; }
141 $obj = $dbr->fetchObject( $res );
142 --$count;
143 $rc = RecentChange::newFromRow( $obj );
144 $rc->counter = $counter++;
145 --$limit;
146 $rchanges[] = $obj;
147 }
148 }
149 $wgOut->disable();
150
151 $feedObj = $feed->getFeedObject(
152 wfMsgForContent( 'recentchangeslinked-title', $nt->getPrefixedText() ),
153 wfMsgForContent( 'recentchangeslinked' )
154 );
155 ChangesFeed::generateFeed( $rchanges, $feedObj );
156 return;
157 }
158
159 # Otherwise, carry on with regular output...
160 $wgOut->addHTML("&lt; ".$sk->makeLinkObj($nt, "", "redirect=no" )."<br />\n");
161 $note = wfMsgExt( "rcnote", array ( 'parseinline' ), $limit, $days, $wgLang->timeAndDate( wfTimestampNow(), true ) );
162 $wgOut->addHTML( "<hr />\n{$note}\n<br />" );
163
164 $note = rcDayLimitlinks( $days, $limit, "Recentchangeslinked",
165 "target=" . $nt->getPrefixedURL() . "&hideminor={$hideminor}&showlinkedto={$showlinkedto}",
166 false, $mlink );
167
168 $wgOut->addHTML( $note."\n" );
169
170 $list = ChangesList::newFromUser( $wgUser );
171 $s = $list->beginRecentChangesList();
172
173 if ( $count ) {
174 $counter = 1;
175 while ( $limit ) {
176 if ( 0 == $count ) { break; }
177 $obj = $dbr->fetchObject( $res );
178 --$count;
179 $rc = RecentChange::newFromRow( $obj );
180 $rc->counter = $counter++;
181 $s .= $list->recentChangesLine( $rc , !empty( $obj->wl_user) );
182 --$limit;
183 }
184 } else {
185 $wgOut->addWikiMsg('recentchangeslinked-noresult');
186 }
187 $s .= $list->endRecentChangesList();
188
189 $dbr->freeResult( $res );
190 $wgOut->addHTML( $s );
191 }