WhatLinksHereFooter/Source

Aus Free Software
Zur Navigation springen Zur Suche springen
<?php
# hook definition
$wgHooks['ArticleAfterFetchContent'][] = array('WhatLinksHereFooter');

/**
 * Function called by the hook.
 * Adds a list of Linked-To Pages or an error text if nothing links here.
 */
function WhatLinksHereFooter ( $article, $content )
{
        global $wgTitle, $wgContLang;
        global $WlhF_enable, $WlhF_header, $WlhF_success, $WlhF_error;
        global $WlhF_Namespaces, $WlhF_Categories;
        $linkList = "";

        # Check if we are not enabled or if this is an edit-Page.
        # The latter is a dirty hack, but seems to be working.
        if ( ! $WlhF_enable || isset($article->mLastRevision) )
                return;

        # See if Article is in defined namespace or category.
        if ( isset( $WlhF_Namespaces ) ) {
                if (! array_key_exists ($article->mTitle->getNamespace(), $WlhF_Namespaces) )
                        return;
        }
        if ( isset( $WlhF_Categories) ) {
                $articleInCategory = false;
                $categories = $wgTitle->getParentCategories();

                $categoryNSText = $wgContLang->getNSText ( NS_CATEGORY );
                foreach ($WlhF_Categories as $WlhF_Category) {
                        if ( array_key_exists ("$categoryNSText:$WlhF_Category", (array) $categories) )
                                $articleInCategory = true;
                }
                if (! $articleInCategory )
                        return;
        }

        $article->mContent .= "\n$WlhF_header\n";
        $linkList .= getLinks(0, $wgTitle, 10);

        if ( $linkList != "" )
                $article->mContent .= $WlhF_success . "\n" . $linkList;
        else
                $article->mContent .= $WlhF_error;
}

function getLinks($level, $target, $limit, $from = 0, $dir = 'next' )
{
        global $wgTitle, $wgUser, $wgParser, $wgOut;

        $result = "";

        // Some extra validation
        $from = intval( $from );
        if ( !$from && $dir == 'prev' ) {
                // Before start? No make sense
                $dir = 'next';
        }

        $dbr =& wfGetDB( DB_READ );

        // Make the query
        $plConds = array(
                'page_id=pl_from',
                'pl_namespace' => $target->getNamespace(),
                'pl_title' => $target->getDBkey(),
        );

        $tlConds = array(
                'page_id=tl_from',
                'tl_namespace' => $target->getNamespace(),
                'tl_title' => $target->getDBkey(),
        );

        if ( $from ) {
                if ( 'prev' == $dir ) {
                        $offsetCond = "page_id < $from";
                        $options = array( 'ORDER BY page_id DESC' );
                } else {
                        $offsetCond = "page_id >= $from";
                        $options = array( 'ORDER BY page_id' );
                }
        } else {
                $offsetCond = false;
                $options = array( 'ORDER BY page_id,is_template DESC' );
        }

        // Read an extra row as an at-end check
        $queryLimit = $limit + 1;
        $options['LIMIT'] = $queryLimit;
        if ( $offsetCond ) {
                $tlConds[] = $offsetCond;
                $plConds[] = $offsetCond;
        }
$fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
                $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
                $plConds, $fname, $options );
        $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
                $tlConds, $fname, $options );
        if ( !$dbr->numRows( $plRes ) && !$dbr->numRows( $tlRes ) ) {
                return "";
        }

        // 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;
        }
        $dbr->freeResult( $plRes );
        while ( $row = $dbr->fetchObject( $tlRes ) ) {
                $row->is_template = 1;
                $rows[$row->page_id] = $row;
        }
        $dbr->freeResult( $tlRes );

        // Sort by key and then change the keys to 0-based indices
        ksort( $rows );
        $rows = array_values( $rows );
        $numRows = count( $rows );

        // Work out the start and end IDs, for prev/next links
        if ( $dir == 'prev' ) {
                // Descending order
                if ( $numRows > $limit ) {
                        // More rows available before these ones
                        // Get the ID from the next row past the end of the displayed set
                        $prevId = $rows[$limit]->page_id;
                        // Remove undisplayed rows
                        $rows = array_slice( $rows, 0, $limit );
                } else {
                        // No more rows available before
                        $prevId = 0;
                }
                // Assume that the ID specified in $from exists, so there must be another page
                $nextId = $from;

                // Reverse order ready for display
                $rows = array_reverse( $rows );
        } else {
                // Ascending
                if ( $numRows > $limit ) {
                        // More rows available after these ones
                        // Get the ID from the last row in the result set
                        $nextId = $rows[$limit]->page_id;
                        // Remove undisplayed rows
                        $rows = array_slice( $rows, 0, $limit );
                } else {
                        // No more rows after
                        $nextId = false;
                }
                $prevId = $from;
        }

        global $WlhF_ListNamespaces, $WlhF_ListCategories, $wgContLang;
        foreach ( $rows as $row ) {
                $nt = Title::makeTitle( $row->page_namespace, $row->page_title );

                /* Articles are only included if their NamespaceID is in ListNamespaces.
                 * To invert this behaviour negate the second expression */
                if ( isset( $WlhF_ListNamespaces ) && ! array_key_exists ( $nt->getNamespace(), $WlhF_ListNamespaces) )
                        continue;

                if ( isset ( $WlhF_ListCategories ) ) {
                        $categories = $nt->getParentCategories();
                        $categoryNSText = $wgContLang->getNSText ( NS_CATEGORY );

                        foreach ($WlhF_ListCategories as $WlhF_ListCategory) {
                                if ( ! array_key_exists ("$categoryNSText:$WlhF_ListCategory", (array) $categories) )
                                        continue 2;
                        }
                }

                if ( $row->page_is_redirect ) {
                        $extra = 'redirect=no';
                } else {
                        $extra = '';
                }

                $result .= '* [[' . $nt->getPrefixedText() . "]]\n";
                // Display properties (redirect or template)
                $props = array();
                if ( $row->page_is_redirect ) {
                        $props[] = $isredir;
                }

                if ( $row->is_template ) {
                        $props[] = $istemplate;
                }
                if ( count( $props ) ) {
                        // FIXME? Cultural assumption, hard-coded punctuation
                        $result .= ' (' . implode( ', ', $props ) . ') ';
                }

        }
        return $result;
}

?>