4 * @addtogroup SpecialPage
9 * @param string $par An article name ??
11 function wfSpecialWhatlinkshere($par = NULL) {
13 $page = new WhatLinksHerePage( $wgRequest, $par );
17 class WhatLinksHerePage
{
19 var $limit, $from, $dir, $target;
20 var $selfTitle, $skin;
22 function WhatLinksHerePage( &$request, $par = null ) {
24 $this->request
=& $request;
25 $this->skin
= $wgUser->getSkin();
32 $this->limit
= min( $this->request
->getInt( 'limit', 50 ), 5000 );
33 if ( $this->limit
<= 0 ) {
36 $this->from
= $this->request
->getInt( 'from' );
37 $this->dir
= $this->request
->getText( 'dir', 'next' );
38 if ( $this->dir
!= 'prev' ) {
42 $targetString = isset($this->par
) ?
$this->par
: $this->request
->getVal( 'target' );
44 if (is_null($targetString)) {
45 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
49 $this->target
= Title
::newFromURL( $targetString );
50 if( !$this->target
) {
51 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
54 $this->selfTitle
= Title
::makeTitleSafe( NS_SPECIAL
,
55 'Whatlinkshere/' . $this->target
->getPrefixedDBkey() );
56 $wgOut->setPagetitle( $this->target
->getPrefixedText() );
57 $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
59 $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' ' .$this->skin
->makeLinkObj($this->target
, '', 'redirect=no' )."<br />\n");
61 $this->showIndirectLinks( 0, $this->target
, $this->limit
, $this->from
, $this->dir
);
65 * @param int $level Recursion level
66 * @param Title $target Target title
67 * @param int $limit Number of entries to display
68 * @param Title $from Display from this article ID
69 * @param string $dir 'next' or 'prev', whether $fromTitle is the start or end of the list
72 function showIndirectLinks( $level, $target, $limit, $from = 0, $dir = 'next' ) {
74 $fname = 'WhatLinksHerePage::showIndirectLinks';
76 $dbr = wfGetDB( DB_READ
);
78 // Some extra validation
79 $from = intval( $from );
80 if ( !$from && $dir == 'prev' ) {
81 // Before start? No make sense
88 'pl_namespace' => $target->getNamespace(),
89 'pl_title' => $target->getDBkey(),
94 'tl_namespace' => $target->getNamespace(),
95 'tl_title' => $target->getDBkey(),
99 if ( 'prev' == $dir ) {
100 $offsetCond = "page_id < $from";
101 $options = array( 'ORDER BY page_id DESC' );
103 $offsetCond = "page_id >= $from";
104 $options = array( 'ORDER BY page_id' );
108 $options = array( 'ORDER BY page_id,is_template DESC' );
110 // Read an extra row as an at-end check
111 $queryLimit = $limit +
1;
112 $options['LIMIT'] = $queryLimit;
114 $tlConds[] = $offsetCond;
115 $plConds[] = $offsetCond;
117 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
119 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
120 $plConds, $fname, $options );
121 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
122 $tlConds, $fname, $options );
124 if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
126 $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target
->getPrefixedText() ) );
131 // Read the rows into an array and remove duplicates
132 // templatelinks comes second so that the templatelinks row overwrites the
133 // pagelinks row, so we get (inclusion) rather than nothing
134 while ( $row = $dbr->fetchObject( $plRes ) ) {
135 $row->is_template
= 0;
136 $rows[$row->page_id
] = $row;
138 $dbr->freeResult( $plRes );
139 while ( $row = $dbr->fetchObject( $tlRes ) ) {
140 $row->is_template
= 1;
141 $rows[$row->page_id
] = $row;
143 $dbr->freeResult( $tlRes );
145 // Sort by key and then change the keys to 0-based indices
147 $rows = array_values( $rows );
149 $numRows = count( $rows );
151 // Work out the start and end IDs, for prev/next links
152 if ( $dir == 'prev' ) {
154 if ( $numRows > $limit ) {
155 // More rows available before these ones
156 // Get the ID from the next row past the end of the displayed set
157 $prevId = $rows[$limit]->page_id
;
158 // Remove undisplayed rows
159 $rows = array_slice( $rows, 0, $limit );
161 // No more rows available before
164 // Assume that the ID specified in $from exists, so there must be another page
167 // Reverse order ready for display
168 $rows = array_reverse( $rows );
171 if ( $numRows > $limit ) {
172 // More rows available after these ones
173 // Get the ID from the last row in the result set
174 $nextId = $rows[$limit]->page_id
;
175 // Remove undisplayed rows
176 $rows = array_slice( $rows, 0, $limit );
178 // No more rows after
185 $wgOut->addWikiText( wfMsg( 'linkshere', $this->target
->getPrefixedText() ) );
187 $isredir = wfMsg( 'isredirect' );
188 $istemplate = wfMsg( 'istemplate' );
191 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId );
192 $wgOut->addHTML( $prevnext );
195 $wgOut->addHTML( '<ul>' );
196 foreach ( $rows as $row ) {
197 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
199 if ( $row->page_is_redirect
) {
200 $extra = 'redirect=no';
205 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
206 $wgOut->addHTML( '<li>'.$link );
208 // Display properties (redirect or template)
210 if ( $row->page_is_redirect
) {
213 if ( $row->is_template
) {
214 $props[] = $istemplate;
216 if ( count( $props ) ) {
217 // FIXME? Cultural assumption, hard-coded punctuation
218 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
221 if ( $row->page_is_redirect
) {
223 $this->showIndirectLinks( $level +
1, $nt, 500 );
226 $wgOut->addHTML( "</li>\n" );
228 $wgOut->addHTML( "</ul>\n" );
231 $wgOut->addHTML( $prevnext );
235 function makeSelfLink( $text, $query ) {
236 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
239 function getPrevNext( $limit, $prevId, $nextId ) {
241 $fmtLimit = $wgLang->formatNum( $limit );
242 $prev = wfMsg( 'prevn', $fmtLimit );
243 $next = wfMsg( 'nextn', $fmtLimit );
245 if ( 0 != $prevId ) {
246 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$prevId}&dir=prev" );
250 if ( 0 != $nextId ) {
251 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}" );
255 $nums = $this->numLink( 20, $prevId ) . ' | ' .
256 $this->numLink( 50, $prevId ) . ' | ' .
257 $this->numLink( 100, $prevId ) . ' | ' .
258 $this->numLink( 250, $prevId ) . ' | ' .
259 $this->numLink( 500, $prevId );
261 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
264 function numLink( $limit, $from ) {
266 $query = "limit={$limit}&from={$from}";
267 $fmtLimit = $wgLang->formatNum( $limit );
268 return $this->makeSelfLink( $fmtLimit, $query );