Extension talk:HistoryOfPage

Latest comment: 12 years ago by 217.158.90.2

Running under mediawiki 1.16 (on Windows Server 2008), the posted code gives an error.

This has been fixed - new code below

Note Note: I'm not a maintainer, but it was important to me to get this working.

Note Note: This version changes the basic functionality - no longer returns links to all versions, just the history of the page prior to and including this version. Also strongly formatted the output (HTML4 transitional or previous).

Note Note: I've not attempted to take over this extension or re-post the code, as the function of the extension has changed in my version. Rather than re-issue a new extension completely, I have posted this as comments for those who want it to work the way that I do.

Note Note: To get this this working with the PdfBook extension, you need to add the top line to PdfBook as follows:

                    $text    = $wgParser->preprocess($text, $title, $opt);
                    $out     = $wgParser->parse($text, $title, $opt, true, true);

Note Note: To get PdfBook working with historical revisions of documents takes some effort - head over to the extension talk to see my comments there.

Note Note: I'll also convert PdfBook to work with WkHtmlToPdf rather than HtmlDoc, as the results from that program are 1) better, 2) working in latest version and 3) maintained by someone who hasn't gone out of business.

<?php
/**
 * HistoryOfPage extension
 * - An extension that returns the formatted non-minor changes to a page prior to and including this revision
 *
 * See http://www.mediawiki.org/wiki/Extension:HistoryOfPage for installation and usage details
 * See http://www.mediawiki.org/w/index.php?title=Extension_talk:HistoryOfPage for development notes and discussion
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author M. Deri Taufan [http://wiki.science.ru.nl/mms]
 * @copyright © 2011 M. Deri Taufan
 * @licence GNU General Public Licence 2.0 or later
 *
 * Usage:
 *  Minor comments are ignored.
 *  Blank comment strings are replaced with 'No Details'
 *  Section comments are italicised as 'Edited section "XYZ"'
 *  Comments beginning with a version pattern (vX.Y) will be extracted and the revision marked as a major issued version.
 * 
 * Version history:
 *  0.1 - Initial Creation
 *  0.2 - Overhaul
 *      - This version changes the basic functionality - no longer returns links to all versions, just the history
 *        of the page prior to and including this version. Also strongly formatted the output (HTML4 transitional or previous).
 * 
 */

if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

$wgExtensionCredits['parserhook'][] = array(
        'path' => __FILE__,
        'name' => "HistoryOfPage",
        'description' => "An extension that returns the formatted non-minor changes to a page prior to and including this revision",
        'version' => 0.2,
        'author' => "M. Deri Taufan, Tony Walker",
        'url' => "http://wiki.science.ru.nl/mms",
);
# Define a setup function
$wgHooks['ParserFirstCallInit'][] = 'hpParserFunction_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][] = 'hpParserFunction_Magic';

function hpParserFunction_Setup ( &$parser ) {
    $parser->setFunctionHook( 'hop', 'hpParserFunction_Render' );
    return true;
}

function hpParserFunction_Magic ( &$magicWords, $langCode ) {
    $magicWords['hop'] = array( 0, 'hop' );
    return true;
}

function hpParserFunction_Render ( $parser, $param1='' ) {
    $revPage = '';
    $outputLink = '';
    $pageTitle = '';
    $versParam = intval($param1);
    
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    $url = $protocol.'://'.$_SERVER['HTTP_HOST'];
    
    $dbr = wfGetDB( DB_SLAVE );
    $res = $dbr->selectRow(
        'revision', // $table
        array( 'rev_page','rev_id' ), // $vars (columns of the table)
        "rev_id=$versParam", // &conds
        __METHOD__, // $fname = 'Database::select',
        array( 'ORDER BY' => "rev_id ASC" ) // $options = array()
    );
        $revPage = intval($res->rev_page);

    //query the page title based on page_id
    $res = $dbr->selectRow(
        'page',
        array( 'page_id','page_title' ),
        "page_id=$revPage",
        __METHOD__,
        array( 'ORDER BY' => "page_id ASC" )
    );
    $pageTitle = $res->page_title;
    
    //query timestamp of a non-minor change, only for changes on or before the version requested.
    $res = $dbr->select(
        'revision',
        array( 'rev_timestamp','rev_id','rev_user_text','rev_comment' ),
        "rev_page=$revPage AND rev_minor_edit=0 AND rev_id<=$versParam",
        __METHOD__,
        array( 'ORDER BY' => "rev_id ASC" )
    );
    $outVers='';
    $outStatus='';
    $outComment='';
    $versions='';
    $outputLink='<table border="1" width="100%"><tr bgcolor="silver"><td width="5%">\'\'\'Version\'\'\'</td><td width="25%">\'\'\'Date\'\'\'</td><td width="10%">\'\'\'Status\'\'\'</td><td width="45%">\'\'\'Reason\'\'\'</td><td width="10%">\'\'\'By\'\'\'</td></tr>';
    foreach( $res as $row ) {
        // Formatting of lines
        // Extract the version, if there is one
        if (preg_match('(^v+\d*+\.+\d*)', $row->rev_comment, $versions) == 1) {
            $outVers=$versions[0];
            $outStatus='Issue';
        } else {
            $outVers=' ';
            $outStatus='Draft';
        }
        
        // Format the comment string
        if (strpos($row->rev_comment,'Created page with') !== FALSE) {
            // Simple commnent to show page initially created
            $outComment='Initial Creation';
        } elseif ($row->rev_comment==NULL) {
            // Basic comment to show someone didn't enter comments when updating (tut, tut)
            $outComment='No Details';
        } elseif (preg_match('(.*\/\*.*\*\/.*)', $row->rev_comment)) {
            // When sections edited, format the comment string into a basic structure 'Edited section "XYZ"'
            $outComment=preg_replace('(.*(\/\*)\s*(.*)(\s+\*\/).*)','<i>Edited section "$2" </i>', $row->rev_comment);
        } else {
            if (strlen($outVers)!=0) {
                $outComment=preg_replace('(^v+\d*+\.+\d*\s*(\-\s*)*)','', $row->rev_comment);
            } else {
                $outComment="$row->rev_comment";
            }
        }
        // Build the line, with formatting (first is failed attempt to 
        // $outputLink = $outputLink."<tr><td>$outVers</td><td>".wfTimestamp(TS_DB,$row->rev_timestamp)."</td><td>$outStatus</td><td>[{{SERVER}}/REQ_290107_Nightfreight_RF_Requirements&oldid=$row->rev_id $outComment]</td><td>$row->rev_user_text</td></tr>";
        $outputLink = $outputLink."<tr><td>$outVers</td><td>".wfTimestamp(TS_DB,$row->rev_timestamp)."</td><td>$outStatus</td><td>$outComment</td><td>$row->rev_user_text</td></tr>";
        
        // http://172.198.45.54/calidus-assist/OBS/index.php?title=REQ_290107_Nightfreight_RF_Requirements&oldid=137
    }
    $outputLink=$outputLink.'</table>';

    //return $param1."<br>".$versParam."<br>".$outputLink;
    return $outputLink;
}

--217.158.90.2 14:18, 31 January 2012 (UTC)Reply

Return to "HistoryOfPage" page.