4738ab857d7109987e2593bbd25cc2b8d4e59036
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();
31 global $wgUser, $wgOut;
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->errorpage( 'notargettitle', 'notargettext' );
50 $this->target
= Title
::newFromURL( $targetString );
51 if( !$this->target
) {
52 $wgOut->errorpage( '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' ) {
76 global $wgOut, $wgUser;
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
92 if ( 'prev' == $dir ) {
93 $offsetCond = "AND page_id < $from";
94 $options = 'ORDER BY page_id DESC,is_template DESC';
96 $offsetCond = "AND page_id >= $from";
97 $options = 'ORDER BY page_id, is_template DESC';
101 $options = 'ORDER BY page_id,is_template DESC';
103 // Read an extra row as an at-end check
104 $queryLimit = $limit +
1;
105 $options .= ' LIMIT ' . $queryLimit;
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";
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
115 $sql = "(SELECT page_id,page_namespace, page_title, page_is_redirect, 1 as is_template " .
116 "FROM page, templatelinks WHERE $tlCond $offsetCond $options) " .
117 "UNION (SELECT page_id,page_namespace, page_title, page_is_redirect, 0 as is_template " .
118 "FROM page, pagelinks WHERE $plCond $offsetCond $options) $options";
119 $res = $dbr->query( $sql, $fname );
120 $numRows = $dbr->numRows( $res );
122 if ( 0 == $numRows ) {
124 $wgOut->addWikiText( wfMsg( 'nolinkshere' ) );
129 // Read the rows into an array
131 while ( $row = $dbr->fetchObject( $res ) ) {
134 $lastRow = end( $rows );
135 // Work out the start and end IDs, for prev/next links
136 if ( $dir == 'prev' ) {
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] );
145 // No more rows available before
148 // Assume that the ID specified in $from exists, so there must be another page
152 $rows = array_reverse( $rows );
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] );
162 // No more rows after
169 $wgOut->addWikiText( wfMsg( 'linkshere' ) );
171 $isredir = wfMsg( 'isredirect' );
172 $istemplate = wfMsg( 'istemplate' );
175 $prevnext = $this->getPrevNext( $limit, $prevId, $nextId );
176 $wgOut->addHTML( $prevnext );
179 $wgOut->addHTML( '<ul>' );
183 foreach ( $rows as $row ) {
184 if ( $lastNs === $row->page_namespace
&& $lastDbk === $row->page_title
) {
188 $lastNs = $row->page_namespace
;
189 $lastDbk = $row->page_title
;
192 $nt = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
194 if ( $row->page_is_redirect
) {
195 $extra = 'redirect=no';
200 $link = $this->skin
->makeKnownLinkObj( $nt, '', $extra );
201 $wgOut->addHTML( '<li>'.$link );
203 // Display properties (redirect or template)
205 if ( $row->page_is_redirect
) {
208 if ( $row->is_template
) {
209 $props[] = $istemplate;
211 if ( count( $props ) ) {
212 // FIXME? Cultural assumption, hard-coded punctuation
213 $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
216 if ( $row->page_is_redirect
) {
218 $this->showIndirectLinks( $level +
1, $nt, 500 );
221 $wgOut->addHTML( "</li>\n" );
223 $wgOut->addHTML( "</ul>\n" );
226 $wgOut->addHTML( $prevnext );
230 function makeSelfLink( $text, $query ) {
231 return $this->skin
->makeKnownLinkObj( $this->selfTitle
, $text, $query );
234 function getPrevNext( $limit, $prevId, $nextId ) {
236 $fmtLimit = $wgLang->formatNum( $limit );
237 $prev = wfMsg( 'prevn', $fmtLimit );
238 $next = wfMsg( 'nextn', $fmtLimit );
240 if ( 0 != $prevId ) {
241 $prevLink = $this->makeSelfLink( $prev, "limit={$limit}&from={$prevId}&dir=prev" );
245 if ( 0 != $nextId ) {
246 $nextLink = $this->makeSelfLink( $next, "limit={$limit}&from={$nextId}" );
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 );
256 return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
259 function numLink( $limit, $from ) {
261 $query = "limit={$limit}&from={$from}";
262 $fmtLimit = $wgLang->formatNum( $limit );
263 return $this->makeSelfLink( $fmtLimit, $query );