00001 <?php
00013 function wfSpecialWhatlinkshere($par = NULL) {
00014 global $wgRequest;
00015 $page = new WhatLinksHerePage( $wgRequest, $par );
00016 $page->execute();
00017 }
00018
00023 class WhatLinksHerePage {
00024
00025 protected $par;
00026
00027
00028 protected $opts, $target, $selfTitle;
00029
00030
00031 protected $skin, $request;
00032
00033 protected $limits = array( 20, 50, 100, 250, 500 );
00034
00035 function WhatLinksHerePage( $request, $par = null ) {
00036 global $wgUser;
00037 $this->request = $request;
00038 $this->skin = $wgUser->getSkin();
00039 $this->par = $par;
00040 }
00041
00042 function execute() {
00043 global $wgOut;
00044
00045 $opts = new FormOptions();
00046
00047 $opts->add( 'target', '' );
00048 $opts->add( 'namespace', '', FormOptions::INTNULL );
00049 $opts->add( 'limit', 50 );
00050 $opts->add( 'from', 0 );
00051 $opts->add( 'back', 0 );
00052 $opts->add( 'hideredirs', false );
00053 $opts->add( 'hidetrans', false );
00054 $opts->add( 'hidelinks', false );
00055 $opts->add( 'hideimages', false );
00056
00057 $opts->fetchValuesFromRequest( $this->request );
00058 $opts->validateIntBounds( 'limit', 0, 5000 );
00059
00060
00061 if ( isset($this->par) ) {
00062 $opts->setValue( 'target', $this->par );
00063 }
00064
00065
00066 $this->opts = $opts;
00067
00068 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
00069 if( !$this->target ) {
00070 $wgOut->addHTML( $this->whatlinkshereForm() );
00071 return;
00072 }
00073
00074 $this->selfTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->target->getPrefixedDBkey() );
00075
00076 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
00077 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin->link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
00078
00079 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
00080 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
00081 }
00082
00091 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
00092 global $wgOut, $wgMaxRedirectLinksRetrieved;
00093 $dbr = wfGetDB( DB_SLAVE );
00094 $options = array();
00095
00096 $hidelinks = $this->opts->getValue( 'hidelinks' );
00097 $hideredirs = $this->opts->getValue( 'hideredirs' );
00098 $hidetrans = $this->opts->getValue( 'hidetrans' );
00099 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
00100
00101 $fetchlinks = (!$hidelinks || !$hideredirs);
00102
00103
00104 $plConds = array(
00105 'page_id=pl_from',
00106 'pl_namespace' => $target->getNamespace(),
00107 'pl_title' => $target->getDBkey(),
00108 );
00109 if( $hideredirs ) {
00110 $plConds['page_is_redirect'] = 0;
00111 } elseif( $hidelinks ) {
00112 $plConds['page_is_redirect'] = 1;
00113 }
00114
00115 $tlConds = array(
00116 'page_id=tl_from',
00117 'tl_namespace' => $target->getNamespace(),
00118 'tl_title' => $target->getDBkey(),
00119 );
00120
00121 $ilConds = array(
00122 'page_id=il_from',
00123 'il_to' => $target->getDBkey(),
00124 );
00125
00126 $namespace = $this->opts->getValue( 'namespace' );
00127 if ( is_int($namespace) ) {
00128 $plConds['page_namespace'] = $namespace;
00129 $tlConds['page_namespace'] = $namespace;
00130 $ilConds['page_namespace'] = $namespace;
00131 }
00132
00133 if ( $from ) {
00134 $tlConds[] = "tl_from >= $from";
00135 $plConds[] = "pl_from >= $from";
00136 $ilConds[] = "il_from >= $from";
00137 }
00138
00139
00140 $queryLimit = $limit + 1;
00141
00142
00143
00144 $options[] = 'STRAIGHT_JOIN';
00145
00146 $options['LIMIT'] = $queryLimit;
00147 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
00148
00149 if( $fetchlinks ) {
00150 $options['ORDER BY'] = 'pl_from';
00151 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
00152 $plConds, __METHOD__, $options );
00153 }
00154
00155 if( !$hidetrans ) {
00156 $options['ORDER BY'] = 'tl_from';
00157 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
00158 $tlConds, __METHOD__, $options );
00159 }
00160
00161 if( !$hideimages ) {
00162 $options['ORDER BY'] = 'il_from';
00163 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
00164 $ilConds, __METHOD__, $options );
00165 }
00166
00167 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
00168 if ( 0 == $level ) {
00169 $wgOut->addHTML( $this->whatlinkshereForm() );
00170
00171
00172 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
00173 $wgOut->addHTML( $this->getFilterPanel() );
00174
00175 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
00176 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
00177 }
00178 return;
00179 }
00180
00181
00182
00183
00184 if( $fetchlinks ) {
00185 while ( $row = $dbr->fetchObject( $plRes ) ) {
00186 $row->is_template = 0;
00187 $row->is_image = 0;
00188 $rows[$row->page_id] = $row;
00189 }
00190 $dbr->freeResult( $plRes );
00191
00192 }
00193 if( !$hidetrans ) {
00194 while ( $row = $dbr->fetchObject( $tlRes ) ) {
00195 $row->is_template = 1;
00196 $row->is_image = 0;
00197 $rows[$row->page_id] = $row;
00198 }
00199 $dbr->freeResult( $tlRes );
00200 }
00201 if( !$hideimages ) {
00202 while ( $row = $dbr->fetchObject( $ilRes ) ) {
00203 $row->is_template = 0;
00204 $row->is_image = 1;
00205 $rows[$row->page_id] = $row;
00206 }
00207 $dbr->freeResult( $ilRes );
00208 }
00209
00210
00211 ksort( $rows );
00212 $rows = array_values( $rows );
00213
00214 $numRows = count( $rows );
00215
00216
00217 if ( $numRows > $limit ) {
00218
00219
00220 $nextId = $rows[$limit]->page_id;
00221
00222 $rows = array_slice( $rows, 0, $limit );
00223 } else {
00224
00225 $nextId = false;
00226 }
00227 $prevId = $from;
00228
00229 if ( $level == 0 ) {
00230 $wgOut->addHTML( $this->whatlinkshereForm() );
00231 $wgOut->addHTML( $this->getFilterPanel() );
00232 $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
00233
00234 $prevnext = $this->getPrevNext( $prevId, $nextId );
00235 $wgOut->addHTML( $prevnext );
00236 }
00237
00238 $wgOut->addHTML( $this->listStart() );
00239 foreach ( $rows as $row ) {
00240 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
00241
00242 if ( $row->page_is_redirect && $level < 2 ) {
00243 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
00244 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
00245 $wgOut->addHTML( Xml::closeElement( 'li' ) );
00246 } else {
00247 $wgOut->addHTML( $this->listItem( $row, $nt ) );
00248 }
00249 }
00250
00251 $wgOut->addHTML( $this->listEnd() );
00252
00253 if( $level == 0 ) {
00254 $wgOut->addHTML( $prevnext );
00255 }
00256 }
00257
00258 protected function listStart() {
00259 return Xml::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
00260 }
00261
00262 protected function listItem( $row, $nt, $notClose = false ) {
00263 # local message cache
00264 static $msgcache = null;
00265 if ( $msgcache === null ) {
00266 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
00267 'whatlinkshere-links', 'isimage' );
00268 $msgcache = array();
00269 foreach ( $msgs as $msg ) {
00270 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
00271 }
00272 }
00273
00274 $suppressRedirect = $row->page_is_redirect ? 'redirect=no' : '';
00275 $link = $this->skin->makeKnownLinkObj( $nt, '', $suppressRedirect );
00276
00277
00278 $propsText = '';
00279 $props = array();
00280 if ( $row->page_is_redirect )
00281 $props[] = $msgcache['isredirect'];
00282 if ( $row->is_template )
00283 $props[] = $msgcache['istemplate'];
00284 if( $row->is_image )
00285 $props[] = $msgcache['isimage'];
00286
00287 if ( count( $props ) ) {
00288 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
00289 }
00290
00291 # Space for utilities links, with a what-links-here link provided
00292 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
00293 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
00294
00295 return $notClose ?
00296 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
00297 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
00298 }
00299
00300 protected function listEnd() {
00301 return Xml::closeElement( 'ul' );
00302 }
00303
00304 protected function wlhLink( Title $target, $text ) {
00305 static $title = null;
00306 if ( $title === null )
00307 $title = SpecialPage::getTitleFor( 'Whatlinkshere' );
00308
00309 $targetText = $target->getPrefixedUrl();
00310 return $this->skin->makeKnownLinkObj( $title, $text, 'target=' . $targetText );
00311 }
00312
00313 function makeSelfLink( $text, $query ) {
00314 return $this->skin->makeKnownLinkObj( $this->selfTitle, $text, $query );
00315 }
00316
00317 function getPrevNext( $prevId, $nextId ) {
00318 global $wgLang;
00319 $currentLimit = $this->opts->getValue( 'limit' );
00320 $fmtLimit = $wgLang->formatNum( $currentLimit );
00321 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
00322 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
00323
00324 $changed = $this->opts->getChangedValues();
00325 unset($changed['target']);
00326
00327 if ( 0 != $prevId ) {
00328 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
00329 $prev = $this->makeSelfLink( $prev, wfArrayToCGI( $overrides, $changed ) );
00330 }
00331 if ( 0 != $nextId ) {
00332 $overrides = array( 'from' => $nextId, 'back' => $prevId );
00333 $next = $this->makeSelfLink( $next, wfArrayToCGI( $overrides, $changed ) );
00334 }
00335
00336 $limitLinks = array();
00337 foreach ( $this->limits as $limit ) {
00338 $prettyLimit = $wgLang->formatNum( $limit );
00339 $overrides = array( 'limit' => $limit );
00340 $limitLinks[] = $this->makeSelfLink( $prettyLimit, wfArrayToCGI( $overrides, $changed ) );
00341 }
00342
00343 $nums = $wgLang->pipeList( $limitLinks );
00344
00345 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
00346 }
00347
00348 function whatlinkshereForm() {
00349 global $wgScript, $wgTitle;
00350
00351
00352 $this->opts->consumeValue( 'target' );
00353
00354 $this->opts->consumeValues( array( 'back', 'from' ) );
00355
00356 $target = $this->target ? $this->target->getPrefixedText() : '';
00357 $namespace = $this->opts->consumeValue( 'namespace' );
00358
00359 # Build up the form
00360 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
00361
00362 # Values that should not be forgotten
00363 $f .= Xml::hidden( 'title', $wgTitle->getPrefixedText() );
00364 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
00365 $f .= Xml::hidden( $name, $value );
00366 }
00367
00368 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
00369
00370 # Target input
00371 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
00372 'mw-whatlinkshere-target', 40, $target );
00373
00374 $f .= ' ';
00375
00376 # Namespace selector
00377 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
00378 Xml::namespaceSelector( $namespace, '' );
00379
00380 $f .= ' ';
00381
00382 # Submit
00383 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
00384
00385 # Close
00386 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
00387
00388 return $f;
00389 }
00390
00391 function getFilterPanel() {
00392 global $wgLang;
00393 $show = wfMsgHtml( 'show' );
00394 $hide = wfMsgHtml( 'hide' );
00395
00396 $changed = $this->opts->getChangedValues();
00397 unset($changed['target']);
00398
00399 $links = array();
00400 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
00401 if( $this->target->getNamespace() == NS_FILE )
00402 $types[] = 'hideimages';
00403 foreach( $types as $type ) {
00404 $chosen = $this->opts->getValue( $type );
00405 $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
00406 $overrides = array( $type => !$chosen );
00407 $links[] = $this->makeSelfLink( $msg, wfArrayToCGI( $overrides, $changed ) );
00408 }
00409 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
00410 }
00411 }