User:Meffiem~metawiki/Newpages

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Blue Glass Arrow.svg MediaWiki logo.png
This page should be moved to MediaWiki.org.
Please do not move the page by hand. It will be imported by a MediaWiki.org administrator with the full edit history. In the meantime, you may continue to edit the page as normal.

Description[edit]

Newpages is a Mediawiki extension to display a list of links to newly created pages in the main namespace. This could be useful on the Main Page of a small project, where the number of new pages is not overwhelming, but enough that you want the list to be automatically updated.

The wiki code syntax is,

   <newpages>
      limit={a positive number}
   </newpages>

where limit is the maximum number of links to new pages generated. The default value is 5 if the limit parameter is not present. If there are more newpages on the site than the limit then a link to the Newpages special page is also generated, labelled as more....

The creation date is appended to each new page link.

Example[edit]

The following wiki code will generate a list of the three most recent new pages in the main namespace.

   <newpages> limit=3 </newpages>

The output will be something like the following (but with links to the pages);

  • Yet another page, 18 January 2006
  • Some other page, 17 January 2006
  • About something, 15 Janurary 2006
  • more ...

Installation[edit]

This extension was developed and tested on MediaWiki v1.5.3.

  1. Copy the source code to a file called NewPages.php into the mediawiki extensions directory.
  2. Add the following line to your LocalSettings.php file:
require_once("extensions/NewPages.php");

Code[edit]

Copy this code to extensions/NewPages.php.

<?php
#
# This Mediawiki extension creates a bullet list of the most
# recent new pages.  This can be useful for a small project's
# main page to give visitors a quick view of the new pages 
# created since the last visit.
#
# The wiki syntax is,
# 
#  <newpages>
#     limit=10
#  </newpages>
#
# where limit is the maximum number of new pages to show.
# 
# To activate the extension, include it from your LocalSettings.php
# with: require_once("extensions/NewPages.php"); 
#
# Author: Michael Meffie
# Date: Jan 17 2006
# Credits: This extension was derived from SpecialNewpages.php.
# License: GPL v2.0
#

$wgExtensionFunctions[] = "wfNewPagesExtension";

$wgExtensionCredits['parserhook'][] = array(
    'name' => 'NewPages',
    'author' => 'Michael Meffie',
    'url' => 'http://meta.wikimedia.org/wiki/User:Meffiem',
);

function wfNewPagesExtension() {
    global $wgParser;
    $wgParser->setHook( "newpages", "renderNewPages" );
}

function renderNewPages( $input ) {
    global $wgOut;
    $output = "<br />No new pages since last database cleaning<br />";
    $limit = 5;    
    getBoxOption($limit,$input,'limit',true);    

    $dbr =& wfGetDB( DB_SLAVE );
    extract( $dbr->tableNames( 'recentchanges', 'page' ) );

    $query_limit = $limit + 1;  # to determine if we should display (more...)
    $sql = "SELECT  rc_namespace AS namespace,
                    rc_title AS title,
                    rc_cur_id AS value,
                    rc_user AS user,
                    rc_user_text AS user_text,
                    rc_comment as comment,
                    rc_timestamp AS timestamp,
                    rc_id AS rcid,
                    page_len as length,
                    page_latest as rev_id
            FROM $recentchanges,$page
            WHERE rc_cur_id=page_id AND rc_new=1
              AND rc_namespace=".NS_MAIN." AND page_is_redirect=0
              ORDER BY value DESC
              LIMIT $query_limit";

    $result = $dbr->query( $sql );
    $num = $dbr->numRows( $result );
    if ($num > 0) {
        $output = "<ul>\n";
        for ($i=0; $i<$num && $i<$limit; $i++) {
           $row = $dbr->fetchObject( $result );
           $s = formatRow( $row );
           $output .= "<li>$s</li>\n";
        }
        if ($num > $limit) {
           $more = $wgOut->parse("[[Special:Newpages|more...]]", false);  
           $output .= "<li>$more</li>\n";
        }
        $output .= "</ul>\n";
    }    

    $dbr->freeResult( $result );
    return $output;
}

function formatRow( $row ) {
    global $wgLang, $wgUser;
    
    $skin = $wgUser->getSkin();
    $link = $skin->makeKnownLink( $row->title, '' );
    $d = $wgLang->date( $row->timestamp, true );

    $s = "$link, $d";
    return $s;
}

function getBoxOption(&$value,&$input,$name,$isNumber=false) {
    if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
         if($isNumber) {
            $value=intval($matches[1]);
        } else {
            $value=htmlspecialchars($matches[1]);
        }
    }
}

?>


QINU problem[edit]

In case of inproper output, similar to:

UNIQ67b7fa85308204de-newpages-00000001-QINU

please use below version of NewPages.php:

<?php
#
# This Mediawiki extension creates a bullet list of the most
# recent new pages.  This can be useful for a small project's
# main page to give visitors a quick view of the new pages 
# created since the last visit.
#
# The wiki syntax is,
# 
#  <newpages>
#     limit=10
#  </newpages>
#
# where limit is the maximum number of new pages to show.
# 
# To activate the extension, include it from your LocalSettings.php
# with: require_once("extensions/NewPages.php"); 
#
# Author: Michael Meffie
# Date: Jan 17 2006
# Credits: This extension was derived from SpecialNewpages.php.
# License: GPL v2.0
#

$wgExtensionFunctions[] = "wfNewPagesExtension";

$wgExtensionCredits['parserhook'][] = array(
    'name' => 'NewPages',
    'author' => 'Michael Meffie',
    'url' => 'http://meta.wikimedia.org/wiki/User:Meffiem',
);

function wfNewPagesExtension() {
    global $wgParser;
    $wgParser->setHook( "newpages", "renderNewPages" );
}

function renderNewPages( $input, $args=null, &$parser) {
    $localParser = new Parser();

    $output = "<br />No new pages since last database cleaning<br />";
    $limit = 5;    
    getBoxOption($limit,$input,'limit',true);    

    $dbr =& wfGetDB( DB_SLAVE );
    extract( $dbr->tableNames( 'recentchanges', 'page' ) );

    $query_limit = $limit + 1;  # to determine if we should display (more...)
    $sql = "SELECT  rc_namespace AS namespace,
                    rc_title AS title,
                    rc_cur_id AS value,
                    rc_user AS user,
                    rc_user_text AS user_text,
                    rc_comment as comment,
                    rc_timestamp AS timestamp,
                    rc_id AS rcid,
                    page_len as length,
                    page_latest as rev_id
            FROM $recentchanges,$page
            WHERE rc_cur_id=page_id AND rc_new=1
              AND rc_namespace=".NS_MAIN." AND page_is_redirect=0
              ORDER BY value DESC
              LIMIT $query_limit";

    $result = $dbr->query( $sql );
    $num = $dbr->numRows( $result );
    if ($num > 0) {
        $output = "<ul>\n";
        for ($i=0; $i<$num && $i<$limit; $i++) {
           $row = $dbr->fetchObject( $result );           
	   $s = formatRow( $row );
           $output .= "<li>$s</li>\n";
        }
        if ($num > $limit) {
           $more = $localParser->parse("[[Special:Newpages|more...]]", $parser->mTitle, $parser->mOptions);  
	         $output .= "<li>".$more->getText()."</li>\n";
        }
        $output .= "</ul>\n";
    }    

    $dbr->freeResult( $result );
     
return $output;
}

function formatRow( $row ) {
    global $wgLang, $wgUser;
    
    $skin = $wgUser->getSkin();
    $link = $skin->makeKnownLink( $row->title, '' );
    $d = $wgLang->date( $row->timestamp, true );

    $s = "$link, $d";
    return $s;
}

function getBoxOption(&$value,&$input,$name,$isNumber=false) {
    if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
         if($isNumber) {
            $value=intval($matches[1]);
        } else {
            $value=htmlspecialchars($matches[1]);
        }
    }
}

?>


Credits[edit]

The newpage query was adapted from SpecialNewpages.php and QueryPage.php. The getOption function was adapted from the inputbox extension.