(bug 13718) Return the proper continue parameter for cmsort=timestamp
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
index a11e9d4..711290b 100644 (file)
@@ -22,12 +22,13 @@ class WhatLinksHerePage {
        var $request, $par;
        var $limit, $from, $back, $target;
        var $selfTitle, $skin;
+       var $hideredirs, $hidetrans, $hidelinks, $fetchlinks;
 
        private $namespace;
 
-       function WhatLinksHerePage( &$request, $par = null ) {
+       function WhatLinksHerePage( $request, $par = null ) {
                global $wgUser;
-               $this->request =& $request;
+               $this->request = $request;
                $this->skin = $wgUser->getSkin();
                $this->par = $par;
        }
@@ -42,24 +43,30 @@ class WhatLinksHerePage {
                $this->from = $this->request->getInt( 'from' );
                $this->back = $this->request->getInt( 'back' );
 
+               $this->hideredirs = $this->request->getInt( 'hideredirs' );
+               $this->hidetrans = $this->request->getInt( 'hidetrans' );
+               $this->hidelinks = $this->request->getInt( 'hidelinks' );
+               $this->fetchlinks = !$this->hidelinks || !$this->hideredirs;
+
                $targetString = isset($this->par) ? $this->par : $this->request->getVal( 'target' );
 
-               if (is_null($targetString)) {
-                       $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
+               if ( is_null( $targetString ) ) {
+                       $wgOut->addHTML( $this->whatlinkshereForm() );
                        return;
                }
 
                $this->target = Title::newFromURL( $targetString );
                if( !$this->target ) {
-                       $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
+                       $wgOut->addHTML( $this->whatlinkshereForm() );
                        return;
                }
                $this->selfTitle = Title::makeTitleSafe( NS_SPECIAL,
                        'Whatlinkshere/' . $this->target->getPrefixedDBkey() );
-               $wgOut->setPagetitle( $this->target->getPrefixedText() );
+
+               $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
                $wgOut->setSubtitle( wfMsg( 'linklistsub' ) );
 
-               $wgOut->addHTML( wfMsg( 'whatlinkshere-barrow' ) . ' '  .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
+               $wgOut->addHTML( wfMsgExt( 'whatlinkshere-barrow', array( 'escapenoentities') ) . ' '  .$this->skin->makeLinkObj($this->target, '', 'redirect=no' )."<br />\n");
 
                $this->showIndirectLinks( 0, $this->target, $this->limit, $this->from, $this->back );
        }
@@ -73,9 +80,9 @@ class WhatLinksHerePage {
         * @private
         */
        function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
-               global $wgOut;
+               global $wgOut, $wgMaxRedirectLinksRetrieved;
                $fname = 'WhatLinksHerePage::showIndirectLinks';
-               $dbr = wfGetDB( DB_READ );
+               $dbr = wfGetDB( DB_SLAVE );
                $options = array();
 
                $ns = $this->request->getIntOrNull( 'namespace' );
@@ -112,34 +119,39 @@ class WhatLinksHerePage {
 
                // Read an extra row as an at-end check
                $queryLimit = $limit + 1;
-               
+
                // enforce join order, sometimes namespace selector may 
                // trigger filesorts which are far less efficient than scanning many entries
                $options[] = 'STRAIGHT_JOIN';
-               
+
                $options['LIMIT'] = $queryLimit;
                $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
 
-               $options['ORDER BY'] = 'pl_from';
-               $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
-                       $plConds, $fname, $options );
-                       
-               $options['ORDER BY'] = 'tl_from';
-               $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
-                       $tlConds, $fname, $options );
-               
-               if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
-                       if ( 0 == $level && !isset( $this->namespace ) ) {
-                               // really no links to here
-                               $wgOut->addWikiText( wfMsg( 'nolinkshere', $this->target->getPrefixedText() ) );
-                       } elseif ( 0 == $level && isset( $this->namespace ) ) {
-                               // no links from requested namespace to here
+               if( $this->fetchlinks ) {
+                       $options['ORDER BY'] = 'pl_from';
+                       $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
+                               $plConds, $fname, $options );
+               }
+
+               if( !$this->hidetrans ) {
+                       $options['ORDER BY'] = 'tl_from';
+                       $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
+                               $tlConds, $fname, $options );
+               }
+
+               if( ( !$this->fetchlinks || !$dbr->numRows( $plRes ) ) && ( $this->hidetrans || !$dbr->numRows( $tlRes ) ) ) {
+                       if ( 0 == $level ) {
                                $options = array(); // reinitialize for a further namespace search
+                               // really no links to here
                                $options['namespace'] = $this->namespace;
                                $options['target'] = $this->target->getPrefixedText();
                                list( $options['limit'], $options['offset']) = wfCheckLimits();
                                $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
-                               $wgOut->addWikiText( wfMsg( 'nolinkshere-ns', $this->target->getPrefixedText() ) );
+                               $errMsg = isset( $this->namespace ) ? 'nolinkshere-ns' : 'nolinkshere';
+                               $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
+                               // Show filters only if there are links
+                               if( $this->hidelinks || $this->hidetrans || $this->hideredirs )
+                                       $wgOut->addHTML( $this->getFilterPanel() );
                        }
                        return;
                }
@@ -156,22 +168,27 @@ class WhatLinksHerePage {
                $options['offset'] = $this->request->getVal( 'offset' );
                /* Offset must be an integral. */
                if ( !strlen( $options['offset'] ) || !preg_match( '/^[0-9]+$/', $options['offset'] ) )
-               $options['offset'] = '';
-               $options['target'] = $this->target->getPrefixedDBkey();
+                       $options['offset'] = '';
+               $options['target'] = $this->target->getPrefixedText();
 
                // Read the rows into an array and remove duplicates
                // templatelinks comes second so that the templatelinks row overwrites the
                // pagelinks row, so we get (inclusion) rather than nothing
-               while ( $row = $dbr->fetchObject( $plRes ) ) {
-                       $row->is_template = 0;
-                       $rows[$row->page_id] = $row;
+               if( $this->fetchlinks ) {
+                       while ( $row = $dbr->fetchObject( $plRes ) ) {
+                               $row->is_template = 0;
+                               $rows[$row->page_id] = $row;
+                       }
+                       $dbr->freeResult( $plRes );
+                       
                }
-               $dbr->freeResult( $plRes );
-               while ( $row = $dbr->fetchObject( $tlRes ) ) {
-                       $row->is_template = 1;
-                       $rows[$row->page_id] = $row;
+               if( !$this->hidetrans ) {
+                       while ( $row = $dbr->fetchObject( $tlRes ) ) {
+                               $row->is_template = 1;
+                               $rows[$row->page_id] = $row;
+                       }
+                       $dbr->freeResult( $tlRes );
                }
-               $dbr->freeResult( $tlRes );
 
                // Sort by key and then change the keys to 0-based indices
                ksort( $rows );
@@ -194,12 +211,9 @@ class WhatLinksHerePage {
 
                if ( $level == 0 ) {
                        $wgOut->addHTML( $this->whatlinkshereForm( $options ) );
-                       $wgOut->addWikiText( wfMsg( 'linkshere', $this->target->getPrefixedText() ) );
-               }
-               $isredir = wfMsg( 'isredirect' );
-               $istemplate = wfMsg( 'istemplate' );
+                       $wgOut->addHTML( $this->getFilterPanel() );
+                       $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
 
-               if( $level == 0 ) {
                        $prevnext = $this->getPrevNext( $limit, $prevId, $nextId, $options['namespace'] );
                        $wgOut->addHTML( $prevnext );
                }
@@ -208,6 +222,11 @@ class WhatLinksHerePage {
                foreach ( $rows as $row ) {
                        $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
 
+                       if( $this->hideredirs && $row->page_is_redirect )
+                               continue;
+                       if( $this->hidelinks && ( !$row->page_is_redirect && !$row->is_template ) )
+                               continue;
+
                        if ( $row->page_is_redirect ) {
                                $extra = 'redirect=no';
                        } else {
@@ -220,19 +239,27 @@ class WhatLinksHerePage {
                        // Display properties (redirect or template)
                        $props = array();
                        if ( $row->page_is_redirect ) {
-                               $props[] = $isredir;
+                               $props[] = wfMsgHtml( 'isredirect' );
                        }
                        if ( $row->is_template ) {
-                               $props[] = $istemplate;
+                               $props[] = wfMsgHtml( 'istemplate' );
                        }
                        if ( count( $props ) ) {
-                               // FIXME? Cultural assumption, hard-coded punctuation
-                               $wgOut->addHTML( ' (' . implode( ', ', $props ) . ') ' );
+                               $list = implode( wfMsgHtml( 'semicolon-separator' ), $props );
+                               $wgOut->addHTML( " ($list) " );
                        }
 
+                       # Space for utilities links, with a what-links-here link provided
+                       $wlh = $this->skin->makeKnownLinkObj(
+                               SpecialPage::getTitleFor( 'Whatlinkshere' ),
+                               wfMsgHtml( 'whatlinkshere-links' ),
+                               'target=' . $nt->getPrefixedUrl()
+                       );
+                       $wgOut->addHtml( ' <span class="mw-whatlinkshere-tools">(' . $wlh . ')</span>' );
+                       
                        if ( $row->page_is_redirect ) {
                                if ( $level < 2 ) {
-                                       $this->showIndirectLinks( $level + 1, $nt, 500 );
+                                       $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
                                }
                        }
                        $wgOut->addHTML( "</li>\n" );
@@ -275,7 +302,7 @@ class WhatLinksHerePage {
                  $this->numLink( 250, $prevId ) . ' | ' .
                  $this->numLink( 500, $prevId );
 
-               return wfMsg( 'viewprevnext', $prevLink, $nextLink, $nums );
+               return wfMsgHtml( 'viewprevnext', $prevLink, $nextLink, $nums );
        }
 
        function numLink( $limit, $from, $ns = null ) {
@@ -286,24 +313,26 @@ class WhatLinksHerePage {
                return $this->makeSelfLink( $fmtLimit, $query );
        }
 
-       function whatlinkshereForm( $options ) {
+       function whatlinkshereForm( $options = array( 'target' => '', 'namespace' => '' ) ) {
                global $wgScript, $wgTitle;
 
                $options['title'] = $wgTitle->getPrefixedText();
 
-               $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => "$wgScript" ) ) .
-                       '<fieldset>' .
-                       Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) );
+               $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', array(), wfMsg( 'whatlinkshere' ) ) .
+                       Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target', 'mw-whatlinkshere-target', 40, $options['target'] ) . ' ';
 
                foreach ( $options as $name => $value ) {
-                       if( $name === 'namespace') continue;
+                       if( $name === 'namespace' || $name === 'target' )
+                               continue;
                        $f .= "\t" . Xml::hidden( $name, $value ). "\n";
                }
 
                $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
                        Xml::namespaceSelector( $options['namespace'], '' ) .
                        Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
-                       '</fieldset>' .
+                       Xml::closeElement( 'fieldset' ) .
                        Xml::closeElement( 'form' ) . "\n";
 
                return $f;
@@ -314,6 +343,17 @@ class WhatLinksHerePage {
                $this->namespace = $ns;
        }
 
-}
+       function getFilterPanel() {
+               $show = wfMsgHtml( 'show' );
+               $hide = wfMsgHtml( 'hide' );
+               $links = array();
+               foreach( array( 'hidetrans', 'hidelinks', 'hideredirs' ) as $type ) {
+                       $chosen = $this->$type;
+                       $msg = wfMsgHtml( "whatlinkshere-{$type}", $chosen ? $show : $hide );
+                       $url = $this->request->appendQueryValue( $type, intval( !$chosen ), true );
+                       $links[] = $this->makeSelfLink( $msg, $url );
+               }
+               return '<p>' . implode( '&nbsp;|&nbsp;', $links ) . '</p>';
+       }
 
-?>
+}