5 * @subpackage SpecialPage
10 * @param string $par An article name ??
12 function wfSpecialWhatlinkshere($par = NULL) {
14 $page = new WhatLinksHerePage( $wgRequest, $par );
18 class WhatLinksHerePage
{
20 var $limit, $from, $dir, $target;
21 var $selfTitle, $skin;
23 function WhatLinksHerePage( &$request, $par = null ) {
25 $this->request
=& $request;
26 $this->skin
=& $wgUser->getSkin();
33 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
34 if ( $this->limit
<= 0 ) {
37 $this->from
= $this->request
->getInt( 'from' );
38 $this->dir
= $this->request
->getText( 'dir', 'next' );
39 if ( $this->dir
!= 'prev' ) {
43 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
45 if (is_null($targetString)) {
46 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
50 $this->target
= Title
::newFromURL( $targetString );
51 if( !$this->target
) {
52 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
55 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
56 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
57 $wgOut->setPagetitle( $this->target
->getPrefixedText() );
58 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
60 $isredir = ' (' . wfMsg( 'isredirect' ) . ")\n";
62 $wgOut->addHTML('< '.$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
64 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->dir
);
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
75 function showIndirectLinks( $level, $target, $limit, $from = 0, $dir = 'next' ) {
77 $fname = 'WhatLinksHerePage::showIndirectLinks';
79 $dbr =& wfGetDB( DB_READ
);
81 extract( $dbr->tableNames( 'pagelinks', 'templatelinks', 'page' ) );
83 // Some extra validation
84 $from = intval( $from );
85 if ( !$from && $dir == 'prev' ) {
86 // Before start? No make sense
93 'pl_namespace' => $target->getNamespace(),
94 'pl_title' => $target->getDBkey(),
99 'tl_namespace' => $target->getNamespace(),
100 'tl_title' => $target->getDBkey(),
104 if ( 'prev' == $dir ) {
105 $offsetCond = "page_id < $from";
106 $options = array( 'ORDER BY page_id DESC' );
108 $offsetCond = "page_id >= $from";
109 $options = array( 'ORDER BY page_id' );
113 $options = array( 'ORDER BY page_id,is_template DESC' );
115 // Read an extra row as an at-end check
116 $queryLimit = $limit +
1;
117 $options['LIMIT'] = $queryLimit;
119 $tlConds[] = $offsetCond;
120 $plConds[] = $offsetCond;
122 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
124 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
125 $plConds, $fname, $options );
126 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
127 $tlConds, $fname, $options );
129 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
131 $wgOut->addWikiText( wfMsg( 'nolinkshere' ) );
136 // Read the rows into an array and remove duplicates
137 // templatelinks comes second so that the templatelinks row overwrites the
138 // pagelinks row, so we get (inclusion) rather than nothing
139 while ( $row = $dbr->fetchObject( $plRes ) ) {
140 $row->is_template
= 0;
141 $rows[$row->page_id
] = $row;
143 $dbr->freeResult( $plRes );
144 while ( $row = $dbr->fetchObject( $tlRes ) ) {
145 $row->is_template
= 1;
146 $rows[$row->page_id
] = $row;
148 $dbr->freeResult( $tlRes );
150 // Sort by key and then change the keys to 0-based indices
152 $rows = array_values( $rows );
154 $numRows = count( $rows );
156 // Work out the start and end IDs, for prev/next links
157 if ( $dir == 'prev' ) {
159 if ( $numRows > $limit ) {
160 // More rows available before these ones
161 // Get the ID from the next row past the end of the displayed set
162 $prevId = $rows[$limit]->page_id
;
163 // Remove undisplayed rows
164 $rows = array_slice( $rows, 0, $limit );
166 // No more rows available before
169 // Assume that the ID specified in $from exists, so there must be another page
172 // Reverse order ready for display
173 $rows = array_reverse( $rows );
176 if ( $numRows > $limit ) {
177 // More rows available after these ones
178 // Get the ID from the last row in the result set
179 $nextId = $rows[$limit]->page_id
;
180 // Remove undisplayed rows
181 $rows = array_slice( $rows, 0, $limit );
183 // No more rows after
190 $wgOut->addWikiText( wfMsg( 'linkshere' ) );
192 $isredir = wfMsg( 'isredirect' );
193 $istemplate = wfMsg( 'istemplate' );
196 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId );
197 $wgOut->addHTML( $prevnext );
200 $wgOut->addHTML( '<ul>' );
201 foreach ( $rows as $row ) {
202 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
204 if ( $row->page_is_redirect
) {
205 $extra = 'redirect=no';
210 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
211 $wgOut->addHTML( '<li>'.$link );
213 // Display properties (redirect or template)
215 if ( $row->page_is_redirect
) {
218 if ( $row->is_template
) {
219 $props[] = $istemplate;
221 if ( count( $props ) ) {
222 // FIXME? Cultural assumption, hard-coded punctuation
223 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
226 if ( $row->page_is_redirect
) {
228 $this->showIndirectLinks( $level +
1, $nt, 500 );
231 $wgOut->addHTML( "</li>\n" );
233 $wgOut->addHTML( "</ul>\n" );
236 $wgOut->addHTML( $prevnext );
240 function makeSelfLink( $text, $query ) {
241 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
244 function getPrevNext( $limit, $prevId, $nextId ) {
246 $fmtLimit = $wgLang->formatNum( $limit );
247 $prev = wfMsg( 'prevn', $fmtLimit );
248 $next = wfMsg( 'nextn', $fmtLimit );
250 if ( 0 != $prevId ) {
251 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$prevId}&dir=prev" );
255 if ( 0 != $nextId ) {
256 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}" );
260 $nums = $this->numLink( 20, $prevId ) . ' | ' .
261 $this->numLink( 50, $prevId ) . ' | ' .
262 $this->numLink( 100, $prevId ) . ' | ' .
263 $this->numLink( 250, $prevId ) . ' | ' .
264 $this->numLink( 500, $prevId );
266 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
269 function numLink( $limit, $from ) {
271 $query = "limit={$limit}&from={$from}";
272 $fmtLimit = $wgLang->formatNum( $limit );
273 return $this->makeSelfLink( $fmtLimit, $query );