Extension:NotifyCategoryTalkOnEdit

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
NotifyCategoryTalkOnEdit
Release status: beta
Implementation Notify
Description Makes a note on a category's talk page when a user modifies or creates a page in that category. The result is that users watching the category will then be notified (by the usual means).
Author(s) Justin Gregory (jmgregorytalk)
Latest version 0.9 (2007-10-08)
MediaWiki tested on MediaWiki 1.11.0, probably works on other versions
License GPL
Download Extension:NotifyCategoryTalkOnEdit#Code
Parameters
$wgNCTOENotifyOnCreate, $wgNCTOENotifyOnEdit
Hooks used
ArticleInsertComplete
ArticleSaveComplete
Translate the NotifyCategoryTalkOnEdit extension if it is available at translatewiki.net
Check usage and version matrix.

This extension addresses the issue where watching a category page will not notify you of additions to the category. When a new page is created or an existing page is edited, this extension will see which categories the page is in, and then add a notification to the talk pages of those categories. If the category page is on the user's watchlist, they'll be notified by whatever means they have chosen in their preferences.

Download instructions[edit]

Please cut and paste the code snippets found below and place them in $IP/extensions/NotifyCategoryTalkOnEdit/NotifyCategoryTalkOnEdit.php, $IP/extensions/NotifyCategoryTalkOnEdit/NotifyCategoryTalkOnEdit.body.php, and $IP/extensions/NotifyCategoryTalkOnEdit/NotifyCategoryTalkOnEdit.i18n.php.

Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php .

Installation[edit]

To install this extension, add the following to LocalSettings.php :

require_once("$IP/extensions/NotifyCategoryTalkOnEdit/NotifyCategoryTalkOnEdit.php");

Optionally, add configuration parameters to LocalSettings.php to disable certain notifications.

Configuration parameters[edit]

  • $wgNCTOENotifyOnCreate - Default: true. Set to false to disable notifications for new pages in a category.
  • $wgNCTOENotifyOnEdit - Default: true. Set to false to disable notifications for page modifications in a category.

Code[edit]

NotifyCategoryTalkOnEdit.php[edit]

<?php

if( !defined( 'MEDIAWIKI' ) )
die();

$wgExtensionCredits['other'][] = array(
        'name' => "Notify Category Talk On Edit",
        'description' => "Makes a note on a category's talk page when a user modifies or creates a page in that category.  The result is that users watching the category will then be notified (by the usual means).",
        'version' => 0.9,
        'author' => "Justin Gregory",
        'url' => "http://www.mediawiki.org/wiki/Extension:NotifyCategoryTalkOnEdit",
 );

$wgAutoloadClasses['NotifyCategoryTalkOnEdit'] = dirname(__FILE__) . '/NotifyCategoryTalkOnEdit.body.php';

$wgExtensionFunctions[] = 'wfNCTOESetup';

function wfNCTOESetup()
{
        global $wgMessageCache;
        require(dirname( __FILE__ ) . '/NotifyCategoryTalkOnEdit.i18n.php');
        foreach ($NCTOEMessages as $lang => $langMessages)
        {
            $wgMessageCache->addMessages($langMessages, $lang);
        }
}


$wgHooks['ArticleInsertComplete'][] = 'NotifyCategoryTalkOnEdit::NCTOnAddPage';
$wgHooks['ArticleSaveComplete'][] = 'NotifyCategoryTalkOnEdit::NCTOnSavePage';

$wgNCTOENotifyOnCreate = true;
$wgNCTOENotifyOnEdit = true;

NotifyCategoryTalkOnEdit.body.php[edit]

<?php

if( !defined( 'MEDIAWIKI' ) )
die();

class NotifyCategoryTalkOnEdit
{

function NCTOnAddPage(&$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor, &$flags)
{
        // Make sure this feature hasn't been turned off in LocalSettings.php:
        global $wgNCTOENotifyOnCreate;
        if ($wgNCTOENotifyOnCreate != true)
        {
                return true;
        }

        // If this is a minor edit, be sure to include the appropriate flag:
        $minorflag = 0;
        if ($minoredit)
        {
                $minorflag = EDIT_MINOR;
        }

        global $wgCanonicalNamespaceNames;

        // the Categoryfinder will be used to search the article for categories
        $cf = new Categoryfinder;

        // Get a list of all known categories from the database
        $dbr =& wfGetDB(DB_SLAVE);
        $res = $dbr->select('categorylinks', array('cl_to', 'count(*) AS count'), '', 'NotifyCategoryTalkOnEdit::NCTOnAddPage()', array('GROUP BY' => 'cl_to'));

        // For each category...
        while ( $row = $dbr->fetchObject( $res ) )
        {
                // ... look for it in the article
                $cf->seed(array($article->getID()), array($row->cl_to)); 
                $a = $cf->run(); 
                
                // Did we find it?
                foreach ($a as $pageID)
                {
                        // If so, edit the category's talk page to add the notification message
                        $talktitle = Title::newFromText($wgCanonicalNamespaceNames[NS_CATEGORY_TALK] . ':' . $row->cl_to);
                        $talkpage = new Article($talktitle);
                        $oldcontent = $talkpage->getContent();
                        $newcontent = $oldcontent . wfMsg('nctoenewpagemessage', Title::newFromID($pageID)->getPrefixedText(), $row->cl_to, $wgCanonicalNamespaceNames[NS_CATEGORY]); 
                        $summary = wfMsg('nctoenewpagesummary', Title::newFromID($pageID)->getPrefixedText());
                        $talkpage->doEdit($newcontent, $summary, EDIT_AUTOSUMMARY | EDIT_FORCE_BOT | $minorflag);
                }
        }

        // Clean up
        $dbr->freeResult($res);
        return true;
}


function NCTOnSavePage(&$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor, &$flags)
{
        // Make sure this feature hasn't been turned off in LocalSettings.php:
        global $wgNCTOENotifyOnEdit;
        if ($wgNCTOENotifyOnEdit != true)
        {
                return true;
        }

        // Make sure this is not an NCTOE auto-notification on a talk page, and that it is not a new page, which
        // would have already been handled by NCTOnAddPage()
        if (($flags & EDIT_FORCE_BOT) | ($flags & EDIT_NEW))
        {
                return true;
        }

        // If this is a minor edit, be sure to include the appropriate flag:
        $minorflag = 0;
        if ($minoredit)
        {
                $minorflag = EDIT_MINOR;
        }

        global $wgCanonicalNamespaceNames;

        // the Categoryfinder will be used to search the article for categories
        $cf = new Categoryfinder;

        // Get a list of all known categories from the database
        $dbr =& wfGetDB(DB_SLAVE);
        $res = $dbr->select('categorylinks', array('cl_to', 'count(*) AS count'), '', 'NotifyCategoryTalkOnEdit::NCTOnSavePage()', array('GROUP BY' => 'cl_to'));

        // For each category...
        while ( $row = $dbr->fetchObject( $res ) )
        {
                // ... look for it in the article
                $cf->seed(array($article->getID()), array($row->cl_to)); 
                $a = $cf->run(); 

                // Did we find it?
                foreach ($a as $pageID)
                {
                        // If so, edit the category's talk page to add the notification message
                        $talktitle = Title::newFromText($wgCanonicalNamespaceNames[NS_CATEGORY_TALK] . ':' . $row->cl_to);
                        $talkpage = new Article($talktitle);
                        $oldcontent = $talkpage->getContent();
                        $newcontent = $oldcontent . wfMsg('nctoepageeditmessage', Title::newFromID($pageID)->getPrefixedText(), $row->cl_to, $wgCanonicalNamespaceNames[NS_CATEGORY]);
                        $summary = wfMsg('nctoepageeditsummary', Title::newFromID($pageID)->getPrefixedText());
                        $talkpage->doEdit($newcontent, $summary, EDIT_AUTOSUMMARY | EDIT_FORCE_BOT | $minorflag);
                }
        }

        // Clean up
        $dbr->freeResult($res);
        return true;
}

}

NotifyCategoryTalkOnEdit.i18n.php[edit]

<?php

if( !defined( 'MEDIAWIKI' ) )
die();

$NCTOEMessages = array('en' => array(   'nctoenewpagemessage' => '

== Automatic New Page Notification: $1 ==

The page [[:$1]] was recently created, and has been included in the category [[:$3:$2|$2]].',
                                        'nctoenewpagesummary' => 'Auto-notify creation of page $1.',
                                        'nctoepageeditmessage' => '

== Automatic Page Modification Notification: $1 ==

The page [[:$1]] has recently been modified, and is a member of the category [[:$3:$2|$2]].', 
                                        'nctoepageeditsummary' => 'Auto-notify modification of page $1.')

// TODO: Add other languages...

);

Issues[edit]

  • This extension can be very useful for a small wiki, but the category talk pages can get crowded if there are many modifications. They will need to be cleaned up periodically.
  • The extension should check for minor edits and mark its own edits as minor. A user who doesn't wish to see minor edits, then, shouldn't see the notifications. However, although this is implemented in the code, users still seem to see all notifications...