* Added templatelinks table. The table currently represents a literal list of templat...
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Entry point
10 * @param string $par An article name ??
11 */
12 function wfSpecialWhatlinkshere($par = NULL) {
13 global $wgRequest;
14 $page = new WhatLinksHerePage( $wgRequest, $par );
15 $page->execute();
16 }
17
18 class WhatLinksHerePage {
19 var $request, $par;
20 var $limit, $from, $dir, $target;
21 var $selfTitle, $skin;
22
23 function WhatLinksHerePage( &$request, $par = null ) {
24 global $wgUser;
25 $this->request =& $request;
26 $this->skin =& $wgUser->getSkin();
27 $this->par = $par;
28 }
29
30 function execute() {
31 global $wgUser, $wgOut;
32
33 $this->limit = min( $this->request->getInt( 'limit', 50 ), 5000 );
34 if ( $this->limit <= 0 ) {
35 $this->limit = 50;
36 }
37 $this->from = $this->request->getInt( 'from' );
38 $this->dir = $this->request->getText( 'dir', 'next' );
39 if ( $this->dir != 'prev' ) {
40 $this->dir = 'next';
41 }
42
43 $targetString = isset($this->par) ? $this->par : $this->request->getVal( 'target' );
44
45 if (is_null($targetString)) {
46 $wgOut->errorpage( 'notargettitle', 'notargettext' );
47 return;
48 }
49
50 $this->target = Title::newFromURL( $targetString );
51 if( !$this->target ) {
52 $wgOut->errorpage( 'notargettitle', 'notargettext' );
53 return;
54 }
55 $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
56 'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
57 $wgOut->setPagetitle( $this->target->getPrefixedText() );
58 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
59
60 $isredir = ' (' . wfMsg( 'isredirect' ) . ")\n";
61
62 $wgOut->addHTML('&lt; '.$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
63
64 $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->dir );
65 }
66
67 /**
68 * @param int $level Recursion level
69 * @param Title $target Target title
70 * @param int $limit Number of entries to display
71 * @param Title $from Display from this article ID
72 * @param string $dir 'next' or 'prev', whether $fromTitle is the start or end of the list
73 * @access private
74 */
75 function showIndirectLinks( $level, $target, $limit, $from = 0, $dir = 'next' ) {
76 global $wgOut, $wgUser;
77 $fname = 'WhatLinksHerePage::showIndirectLinks';
78
79 $dbr =& wfGetDB( DB_READ );
80
81 extract( $dbr->tableNames( 'pagelinks', 'templatelinks', 'page' ) );
82
83 // Some extra validation
84 $from = intval( $from );
85 if ( !$from && $dir == 'prev' ) {
86 // Before start? No make sense
87 $dir = 'next';
88 }
89
90 // Make the query
91 if ( $from ) {
92 if ( 'prev' == $dir ) {
93 $offsetCond = "AND page_id < $from";
94 $options = 'ORDER BY page_id DESC,is_template DESC';
95 } else {
96 $offsetCond = "AND page_id >= $from";
97 $options = 'ORDER BY page_id, is_template DESC';
98 }
99 } else {
100 $offsetCond = '';
101 $options = 'ORDER BY page_id,is_template DESC';
102 }
103 // Read an extra row as an at-end check
104 $queryLimit = $limit + 1;
105 $options .= ' LIMIT ' . $queryLimit;
106
107 $ns = $dbr->addQuotes( $target->getNamespace() );
108 $dbk = $dbr->addQuotes( $target->getDBkey() );
109 $plCond = "page_id=pl_from AND pl_namespace=$ns AND pl_title=$dbk";
110 $tlCond = "page_id=tl_from AND tl_namespace=$ns AND tl_title=$dbk";
111
112 // Make a union query which will read both templatelinks and pagelinks,
113 // with an is_template field in the output indicating which one the link
114 // came from
115 $sql = "(SELECT page_id,page_namespace, page_title, page_is_redirect, 1 as is_template " .
116 "FROM page, templatelinks WHERE $tlCond $offsetCond) " .
117 "UNION (SELECT page_id,page_namespace, page_title, page_is_redirect, 0 as is_template " .
118 "FROM page, pagelinks WHERE $plCond $offsetCond) $options";
119 $res = $dbr->query( $sql, $fname );
120 $numRows = $dbr->numRows( $res );
121
122 if ( 0 == $numRows ) {
123 if ( 0 == $level ) {
124 $wgOut->addWikiText( wfMsg( 'nolinkshere' ) );
125 }
126 return;
127 }
128
129 // Read the rows into an array
130 $rows = array();
131 while ( $row = $dbr->fetchObject( $res ) ) {
132 $rows[] = $row;
133 }
134 $lastRow = end( $rows );
135 // Work out the start and end IDs, for prev/next links
136 if ( $dir == 'prev' ) {
137 // Descending order
138 if ( $numRows == $queryLimit ) {
139 // More rows available before these ones
140 // Get the ID from the last row in the result set
141 $prevId = $lastRow->page_id;
142 // Remove undisplayed row
143 unset( $rows[$queryLimit - 1] );
144 } else {
145 // No more rows available before
146 $prevId = 0;
147 }
148 // Assume that the ID specified in $from exists, so there must be another page
149 $nextId = $from;
150
151 // Reverse order
152 $rows = array_reverse( $rows );
153 } else {
154 // Ascending
155 if ( $numRows == $queryLimit ) {
156 // More rows available after these ones
157 // Get the ID from the last row in the result set
158 $nextId = $lastRow->page_id;
159 // Remove undisplayed row
160 unset( $rows[$queryLimit - 1] );
161 } else {
162 // No more rows after
163 $nextId = false;
164 }
165 $prevId = $from;
166 }
167
168 if ( 0 == $level ) {
169 $wgOut->addWikiText( wfMsg( 'linkshere' ) );
170 }
171 $isredir = wfMsg( 'isredirect' );
172 $istemplate = wfMsg( 'istemplate' );
173
174 if( $level == 0 ) {
175 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId );
176 $wgOut->addHTML( $prevnext );
177 }
178
179 $wgOut->addHTML( '<ul>' );
180 $linksShown = 0;
181 $lastNs = false;
182 $lastDbk = false;
183 foreach ( $rows as $row ) {
184 if ( $lastNs === $row->page_namespace && $lastDbk === $row->page_title ) {
185 // Skip duplicates
186 continue;
187 } else {
188 $lastNs = $row->page_namespace;
189 $lastDbk = $row->page_title;
190 }
191
192 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
193
194 if ( $row->page_is_redirect ) {
195 $extra = 'redirect=no';
196 } else {
197 $extra = '';
198 }
199
200 $link = $this->skin->makeKnownLinkObj( $nt, '', $extra );
201 $wgOut->addHTML( '<li>'.$link );
202
203 // Display properties (redirect or template)
204 $props = array();
205 if ( $row->page_is_redirect ) {
206 $props[] = $isredir;
207 }
208 if ( $row->is_template ) {
209 $props[] = $istemplate;
210 }
211 if ( count( $props ) ) {
212 // FIXME? Cultural assumption, hard-coded punctuation
213 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
214 }
215
216 if ( $row->page_is_redirect ) {
217 if ( $level < 2 ) {
218 $this->showIndirectLinks( $level + 1, $nt, 500 );
219 }
220 }
221 $wgOut->addHTML( "</li>\n" );
222 }
223 $wgOut->addHTML( "</ul>\n" );
224
225 if( $level == 0 ) {
226 $wgOut->addHTML( $prevnext );
227 }
228 }
229
230 function makeSelfLink( $text, $query ) {
231 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
232 }
233
234 function getPrevNext( $limit, $prevId, $nextId ) {
235 global $wgLang;
236 $fmtLimit = $wgLang->formatNum( $limit );
237 $prev = wfMsg( 'prevn', $fmtLimit );
238 $next = wfMsg( 'nextn', $fmtLimit );
239
240 if ( 0 != $prevId ) {
241 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$prevId}&dir=prev" );
242 } else {
243 $prevLink = $prev;
244 }
245 if ( 0 != $nextId ) {
246 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}" );
247 } else {
248 $nextLink = $next;
249 }
250 $nums = $this->numLink( 20, $prevId ) . ' | ' .
251 $this->numLink( 50, $prevId ) . ' | ' .
252 $this->numLink( 100, $prevId ) . ' | ' .
253 $this->numLink( 250, $prevId ) . ' | ' .
254 $this->numLink( 500, $prevId );
255
256 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
257 }
258
259 function numLink( $limit, $from ) {
260 global $wgLang;
261 $query = "limit={$limit}&from={$from}";
262 $fmtLimit = $wgLang->formatNum( $limit );
263 return $this->makeSelfLink( $fmtLimit, $query );
264 }
265 }
266
267 ?>