Extension:CategoryHistory

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
CategoryHistory

Release status: beta

Implementation Skin, Special page, Data extraction
Description Shows changes of all articles belonging to a category.
Author(s) Josef Martiňák (Jossmarttalk)
Last version 0.3 (Jul 30 2012)
MediaWiki 1.19+
Database changes no
License GPL
Download This page
Hooks used
SkinSubPageSubtitle
Check usage and version matrix; stats


Special page called "CategoryHistory" shows changes of all articles belonging to a category, which name is displayed at the end of the special page's URL.

The link for the history is available at every Category page (under the title).


Contents

Installation[edit]

  • Create folder extensions/CategoryHistory and put in the CategoryHistory.php, CategoryHistory.i18n.php and CategoryHistory_body.php
  • In LocalSettings.php add this line
require_once( "$IP/extensions/CategoryHistory/CategoryHistory.php" );
  • There are 2 constants in CategoryHistory.php for configuration:
    • CATHIST_PAGE_LIMIT - number of items in the table - default 500
    • CATHIST_NAME_STYLE - the style of diplayed name in the table. Options are: REALNAME (if set), USERNAME, BOTH

Code[edit]

CategoryHistory.php[edit]

<?php
 
#####################################################################################################
# Extension displays recent changes of the category, which name is displayed at the end of the URL. #
# The link for the history is available at every Category page (under the title)                    #
#####################################################################################################

 
if (!defined('MEDIAWIKI')) {
echo <<<EOT
        To install my extension, put the following line in LocalSettings.php:
        require_once( "\$IP/extensions/CategoryHistory/CategoryHistory.php" );
EOT;
exit( 1 );
}
 
$wgExtensionCredits['specialpage'][] = array(
        'name' => 'CategoryHistory',
        'author' => 'Josef Martiňák, Petr Kajzar',
        'url' => "http://www.mediawiki.org/wiki/Extension:CategoryHistory",
        'descriptionmsg' => 'categoryhistory-desc',
        'version' => '0.3.1',
);
 
 
################################# SETUP ##############################################

# Number of items in the table
define('CATHIST_PAGE_LIMIT',500);
# The style of the name in the table. Options are: REALNAME (if set), USERNAME, BOTH
define('CATHIST_NAME_STYLE','REALNAME');
 
######################################################################################

 
$dir = dirname(__FILE__) . '/';
 
$wgAutoloadClasses['CategoryHistory'] = $dir . 'CategoryHistory_body.php';
$wgExtensionMessagesFiles['CategoryHistory'] = $dir . 'CategoryHistory.i18n.php';
$wgSpecialPages['CategoryHistory'] = 'CategoryHistory';
$wgHooks['SkinSubPageSubtitle'][] = 'AddLinkToCategory';
 
 
 
function AddLinkToCategory(&$subpages,$skin) { 
 
        $title = $skin->getRelevantTitle();
        if($title->getNamespace() == 14) {
                // category page
                $url = str_replace("/".$title->getSubjectNsText().":","/Special:CategoryHistory/",
                                                        $title->getFullURL());
                $subpages .= Html::element('a',array('href' => $url),$skin->msg('mwe-cathist-link')->text());
        }
        return true; 
}

CategoryHistory_body.php[edit]

<?php
 
#####################################################################################################
# This page displays recent changes of the category, which name is displayed at the end of the URL. #
# The link for the history is available at every Category page (under the title)                    #
#####################################################################################################

class CategoryHistory extends SpecialPage {
 
        function __construct() {
                parent::__construct( 'CategoryHistory' );
                wfLoadExtensionMessages('CategoryHistory');
        }
 
        function execute($category) {
                $this->setHeaders();
                $request = $this->getRequest();
                $out = $this->getOutput();
 
                if(!$category) {
                        $out->mBodytext .= $this->msg('categoryhistory-desc')->parse();
                }
                else {
                        $category = mysql_real_escape_string($category);
                        $ch_type = $request->getVal('ch_type');
                        if(empty($ch_type)) {
                                $ch_type = "showtable";
                        }
                        $out->setPagetitle($this->msg('categoryhistory')->text()." - $category");
                        $page = $request->getInt('catHistoryPageNumber',1);
 
                        // parse all revisions of categories' articles
                        $dbr = wfGetDB(DB_SLAVE);
                        $res = $dbr->select(
                                "categorylinks",
                            array("cl_from"),
                            "cl_to = '$category'"
                        );
 
                        if($res->numRows()>0) { 
                                // category has some articles
 
                                // preparing conditions and options for the final SELECT
                                $conds = "";
                                foreach($res as $row) {
                                        if($conds) {
                                                $conds .= " OR ";
                                        }
                                        $conds .= "rev_page = ".$row->cl_from;
                                }
                                $options = array("ORDER BY" => "rev_timestamp DESC","LIMIT" => CATHIST_PAGE_LIMIT,
                                        "OFFSET" => (($page-1)*CATHIST_PAGE_LIMIT));
 
                                // display pager
                                $output = "<style>";
                                $output .= "input {background-color:white;border-width:0px;}";
                                $output .= "</style>";
                                $output .= "<form id='catHistMenu' name='catHistMenu' method='post' action=''>\n";
                                // previous
                                if($page==1) {
                                        $prev = 1; 
                                }
                                else {
                                        $prev = $page-1;
                                }
                                if($page > 1) {
                                        $output .= "<input type='button' onclick=";
                                        $output .= "\"document.getElementById('catHistoryPageNumber').value=$prev;";
                                        $output .= "this.form.submit();\" ";
                                        $output .= "value='<&nbsp;' title='";
                                        $output .= $this->msg('mwe-cathist-previous')->text();
                                        $output .= "'/>\n";
                                }
                                else {
                                        $output .= "<input type='button' value='<'/>";
                                }
                                // show the number of pages
                                $res = $dbr->select(
                                        "revision",
                                        array("rev_page"),
                                        $conds
                                );
                                $nm = 1 + floor($res->numRows()/CATHIST_PAGE_LIMIT);
                                for($i=1;$i<=$nm;$i++){
                                        if($i==$page) {
                                                $output .= "<input type='button' value='$i&nbsp;' style='color:red'/>";
                                        }
                                        else {
                                                $output .= "<input type='button' ";
                                                $output .= "onclick=\"document.getElementById('catHistoryPageNumber')";
                                                $output .= ".value=$i;this.form.submit();\" value='$i&nbsp;'/>\n";
                                        }
                                }
                                // next
                                if($page<$nm) {
                                        $next = $page+1; 
                                }
                                else {
                                        $next = $nm;
                                }
                                if($page < $nm) {
                                        $output .= "<input type='button' ";
                                        $output .= "onclick=\"document.getElementById('catHistoryPageNumber')";
                                        $output .= ".value=$next;this.form.submit();\" ";
                                        $output .= "value='>' title='".$this->msg('mwe-cathist-next')->text()."'/>\n";
                                }
                                else {
                                        $output .= "<input type='button' value='>'/>";
                                }
                                $output .= "<input id='catHistoryPageNumber' name='catHistoryPageNumber' ";
                                $output .= "type='hidden' value='1'/>\n";
                                $output .= "</form><br/>\n";
                                $out->mBodytext .= $output;
 
 
                                // get a page of changes
                                $res = $dbr->select(
                                        "revision",
                                        array("rev_page","rev_comment","rev_user","rev_timestamp","rev_minor_edit",
                                                  "rev_deleted","rev_user_text"),
                                        $conds,
                                        __METHOD__,
                                        $options
                                );
 
 
                                // table header
                                $table = "{| class = 'wikitable'\n";
                                $table .= "|- \n";
                                $table .= "! width='110'|".$this->msg('mwe-cathist-time')->text()."\n";
                                $table .= "! width='300'|".$this->msg('mwe-cathist-pagename')->text()."\n";
                                $table .= "! width='230'|".$this->msg('mwe-cathist-author')->text()."\n";
                                $table .= "! ".$this->msg('mwe-cathist-desc')->text()."\n";
                                $table .= "! width='40'|".$this->msg('mwe-cathist-flags')->text()."\n";
                                if($ch_type=="wikicode"){
                                        $table .= "! ".$this->msg('mwe-cathist-notice')->text()." 1\n";
                                        $table .= "! ".$this->msg('mwe-cathist-notice')->text()." 2\n";
                                }
 
                                // display rows with changes
                                foreach($res as $row) {
                                        $table .= "|- \n";
                                        // datum
                                        $table .= "| ".substr($row->rev_timestamp,0,4)."-";
                                        $table .= substr($row->rev_timestamp,4,2)."-";
                                        $table .= substr($row->rev_timestamp,6,2)." ";
                                        $table .= substr($row->rev_timestamp,8,2).":";
                                        $table .= substr($row->rev_timestamp,10,2)."\n";
 
                                        // get article's name
                                        $rw = $dbr->selectRow(
                                                "page",
                                                array("page_title","page_namespace"),
                                                "page_id = ".$row->rev_page
                                        );
                                        $name = str_replace("_"," ",$rw->page_title);
                                        if(!empty($rw->page_namespace)) {
                                                $name = "{{ns:".$rw->page_namespace."}}:$name";
                                        }
                                        $name="[{{fullurl:$name|action=history}} $name]";
                                        $table .= "| $name\n";
 
                                        // get user's real name for the link to his profile
                                        $rw = $dbr->selectRow(
                                                "user",
                                                array("user_name","user_real_name"),
                                                "user_id = ".$row->rev_user
                                        );
                                        if(CATHIST_NAME_STYLE != "REALNAME") {
                                                if(!empty($rw->user_name)) {
                                                        $user = "[[User:".$rw->user_name."|".$rw->user_name."]]";
                                                }
                                                else {
                                                        $user = $row->rev_user_text;
                                                }
                                                if(CATHIST_NAME_STYLE == "BOTH" && !empty($rw->user_real_name)) {
                                                        $user .= " (".$rw->user_real_name.")";
                                                }
                                        }
                                        else{
                                                if(!empty($rw->user_real_name)) {
                                                        $user = "[[User:".$rw->user_name."|".$rw->user_real_name."]]";
                                                }
                                                elseif(!empty($rw->user_name)) {
                                                        $user = "[[User:".$rw->user_name."|".$rw->user_name."]]";
                                                }
                                                else {
                                                        $user = $row->rev_user_text;
                                                }
                                        }                               
                                        $table .= "| $user\n";
 
                                        // comment
                                        if(!empty($row->rev_comment)) {
                                                $comment = $row->rev_comment;
                                        }
                                        else {
                                                $comment = "";
                                        }
                                        //$comment = "<nowiki>$comment</nowiki>";
                                        $comment = "$comment";
                                        $table .= "| $comment\n";
 
                                        // article deleted?
                                        if(!empty($row->rev_deleted)) {
                                                $deleted = "d"; 
                                        }
                                        else {
                                                $deleted = "";
                                        }
 
                                        // minor edit
                                        if(!empty($row->rev_minor_edit)) {
                                                $me = "me"; 
                                        }
                                        else {
                                                $me = "";
                                        }
                                        $table .= "| $me";
                                        if($deleted) {
                                                $table .= "-$deleted";
                                        }
                                        $table .= "\n";
                                        if($ch_type=="wikicode"){
                                                $table .= "| \n| \n";
                                        }
                                }
                                $table .= "|} \n\n";
 
                                if($ch_type=="wikicode"){
                                        // display wikicode of the table
                                        $out->mBodytext .= nl2br($table);
                                        $out->mBodytext .= $this->msg('mwe-cathist-legend')->parse()."<br/>";
                                        $out->mBodytext .= $this->msg('mwe-cathist-legend-flags')->parse()."<br/>";
                                        $out->mBodytext .= "<br/><p><a href='./$category?catHistoryPageNumber=$page&ch_type=showtable'>";
                                        $out->mBodytext .= $this->msg('mwe-cathist-showtable')->text()."</a></p>";
                                }
                                else{
                                        // display table
                                        $out->addWikiText($table);
                                        $out->mBodytext .= $this->msg('mwe-cathist-legend')->parse()."<br/>";
                                        $out->mBodytext .= $this->msg('mwe-cathist-legend-flags')->parse()."<br/>";
                                        $out->mBodytext .= "<br/><p><a href='./$category?catHistoryPageNumber=$page&ch_type=wikicode'>";
                                        $out->mBodytext .= $this->msg('mwe-cathist-showwiki')->text()."</a></p>";
                                }
 
                        }
 
                }
 
        }
}

CategoryHistory.i18n.php[edit]

<?php
 
$messages = array();
 
/* *** English *** */
$messages['en'] = array( 
        'categoryhistory' => 'Category history',
        'categoryhistory-desc' => "This page displays recent changes of the category, 
                which name is displayed at the end of the URL. 
                The link for the history is available at every Category page (under the title)",
        'mwe-cathist-legend' => "'''Explanatory Notes'''",
        'mwe-cathist-legend-flags' => "Flags: me - minor edit, d - deleted.",
        'mwe-cathist-wikikod' => "Wikicode",
        'mwe-cathist-showwiki' => 'DISPLAY THE WIKICODE OF THIS TABLE',
        'mwe-cathist-showtable' => 'DISPLAY THE TABLE',
        'mwe-cathist-previous' => 'Previous',
        'mwe-cathist-next' => 'Next',
        'mwe-cathist-link' => 'Show changes',
        'mwe-cathist-time' => 'Time',
        'mwe-cathist-pagename' => 'Page Name',
        'mwe-cathist-author' => 'Author of this revision',
        'mwe-cathist-desc' => 'Description',
        'mwe-cathist-flags' => 'Flags',
        'mwe-cathist-notice' => 'Notice'
);
 
/* *** Czech *** */
$messages['cs'] = array(
        'categoryhistory' => 'Historie kategorie',
        'categoryhistory-desc' => 'Stránka zobrazí poslední změny v kategorii, jejíž název je uveden 
                        za lomítkem na konci URL (tedy [[Special:CategoryHistory/název_kategorie]]). 
                        Přihlášený uživatel najde odkaz na tuto historii u každé kategorie pod nadpisem.',
        'mwe-cathist-legend' => "'''Vysvětlivky'''",
        'mwe-cathist-legend-flags' => "Příznaky: me - malá editace, d - smazáno (deleted).",
        'mwe-cathist-wikikod' => "Wiki kód",
        'mwe-cathist-showwiki' => 'ZOBRAZIT WIKIKÓD TABULKY',
        'mwe-cathist-showtable' => 'ZOBRAZIT TABULKU',
        'mwe-cathist-previous' => 'Předchozí',
        'mwe-cathist-next' => 'Další',
        'mwe-cathist-link' => 'Ukázat změny',
        'mwe-cathist-time' => 'Čas',
        'mwe-cathist-pagename' => 'Název stránky',
        'mwe-cathist-author' => 'Autor revize',
        'mwe-cathist-desc' => 'Popis změn',
        'mwe-cathist-flags' => 'Příznaky',
        'mwe-cathist-notice' => 'Poznámka'
);