Extension:EmailDeletedPages

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
EmailDeletedPages
Release status: beta
EmailDeletedPages.png
Implementation Page action
Description Gives users the option of receiving an emailed copy of deleted pages they created.
Author(s) Nathan Larson (Leucostictetalk)
Latest version 1.0.0 (2013-12-10)
MediaWiki 1.23+
PHP 5.3+
Database changes No
License GPL
Download See the code section
Hooks used
GetPreferences
ArticleDelete
Translate the EmailDeletedPages extension if it is available at translatewiki.net
Check usage and version matrix.

The EmailDeletedPages extension gives users the option of receiving an emailed copy of deleted pages they created.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/EmailDeletedPages/EmailDeletedPages.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

EmailDeletedPages.php[edit]

<?php
/**
 * EmailDeletedPages MediaWiki extension.
 *
 * This extension gives users the option of receiving an emailed copy of deleted pages they created.
 *
 * Written by Leucosticte
 * http://www.mediawiki.org/wiki/User:Leucosticte
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Extensions
 */

# Alert the user that this is not a valid entry point to MediaWiki if the user tries to access the
# extension file directly.
if (!defined('MEDIAWIKI')) {
	die( 'This file is a MediaWiki extension. It is not a valid entry point' );
}

$wgExtensionCredits['other'][] = array(
	'path' => __FILE__,
	'name' => 'EmailDeletedPages',
	'author' => 'Nathan Larson',
	'url' => 'https://www.mediawiki.org/wiki/Extension:EmailDeletedPages',
	'descriptionmsg' => 'emaildeletedpages-desc',
	'version' => '1.0.0',
);

$wgAutoloadClasses['EmailDeletedPages'] = __DIR__ . '/EmailDeletedPages.classes.php';
$wgExtensionMessagesFiles['EmailDeletedPages'] = __DIR__ . '/EmailDeletedPages.i18n.php';
$wgHooks['GetPreferences'][] = 'EmailDeletedPages::togglify';
$wgHooks['ArticleDelete'][] = 'EmailDeletedPages::onArticleDelete';

EmailDeletedPages.classes.php[edit]

<?php
class EmailDeletedPages {
        /** Add our toggle
         * @param $user
         * @param &$preferences
         * @public
         */
        public static function togglify( $user, &$preferences )  {
                global $wgEmailAuthentication;
                $disableEmailPrefs = true;
                if ( $wgEmailAuthentication ) {
                        $emailauthenticationclass = 'mw-email-not-authenticated';
			if ( $user->getEmail() ) {
				if ( $user->getEmailAuthenticationTimestamp() ) {
                                        $disableEmailPrefs = false;
                                }
                        }
                }
                // A checkbox
                $preferences['emaildeletedpages'] = array(
                        'type' => 'toggle',
                        'label-message' => 'tog-emaildeletedpages', // a system message
                        'section' => 'personal/email',
                        'disabled' => $disableEmailPrefs
                );
                return true;
        }

        public static function onArticleDelete( &$article, &$user, &$reason, &$error ) {
                // TODO: What about the File: namespace?
                global $wgSitename;

                $creator = $article->getCreator();
                if ( !$creator->isEmailConfirmed() ) {
                        return true;
                }
                // If the creator hasn't asked that pages he creates be emailed to him when they're
                // deleted, send him nothing
                if ( !$creator->getBoolOption( 'emaildeletedpages' ) ) {
                        return true;
                }
                $userPageCanonicalURL = $user->getUserPage()->getCanonicalURL();
                $userName = $user->getName();
                $titlePrefixedDBKey = $article->getTitle()->getPrefixedDBKey();
                $body = wfMessage ( 'emaildeletedpages-email-body-intro' )->text();
                $content = $article->getContent ( Revision::FOR_THIS_USER, $creator );
                $revid = $article->getLatest();
                // If the most recent revision is unavailable to this user, fall back to the most
                // recent revision by this user
                if ( !$content ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $res = $dbr->selectRow( 'revision', 'rev_id', array (
                                        'rev_user' => $creator->getId(),
                                        'rev_page' => $article->getId()
                                ),
                                __METHOD__,
                                array ( 'ORDER BY' => 'rev_id DESC' )
                        );
                        if ( $res ) {
                                $revision = Revision::loadFromId ( $res->rev_id );
                                $text = $revision->getContent( Revision::FOR_THIS_USER, $creator );
                                if ( $text ) {
                                        $body = wfMessage (
                                                'emaildeletedpages-email-body-intro-fallback' )->text()
                                                . $text;
                                } else {
                                        if ( $res->rev_id == $revid ) {
                                                $body = wfMessage
                                                        ( 'emaildeletedpages-text-samerev-unavailable' )->text();
                                        } else {
                                                $body = wfMessage
                                                        ( 'emaildeletedpages-text-unavailable' )->text();
                                        }
                                }
                        }
                } else {
                        $body .= ContentHandler::getContentText( $content );
                }
                $opening = wfMessage ( 'emaildeletedpages-email-opening', $userName,
                        $userPageCanonicalURL, $titlePrefixedDBKey, $reason )->text();
                $subject = wfMessage ( 'emaildeletedpages-email-subject',
                        $titlePrefixedDBKey, $wgSitename )->text();
                $closing = wfMessage ( 'emaildeletedpages-email-closing' )->text();
                $creator->sendMail ( $subject, $opening . $body . $closing );
                return true;
        }
}

EmailDeletedPages.i18n.php[edit]

<?php
/**
 * Internationalisation for EmailDeletedPages
 *
 * @file
 * @ingroup Extensions
 */
$messages = array();

/** English
 * @author Leucosticte
 */
$messages['en'] = array(
	'emaildeletedpages-desc' => 'Gives users the option of receiving an emailed copy of deleted pages they created.',
        'emaildeletedpages-email-subject' => '$1 was deleted from $2',
        'emaildeletedpages-email-opening' => '$1<$2> deleted $3, a page you created. The deletion reason given was:

$4

',
        'emaildeletedpages-email-body-intro' => 'Here is the text of the most recent revision made to the page before it was deleted:

',
        'emaildeletedpages-email-bodyintro-fallback' => 'The most recent revision made to the page before it was deleted was revisiondeleted and therefore
could not be provided to you. Here is the text of the most recent revision that you made to the page:',
        'emaildeletedpages-text-samerev-unavailable' => 'The most recent revision made to the page, which was made by you, was revisiondeleted
and therefore could not be provided to you.',
        'emaildeletedpages-email-text-unavailable' => 'Both the most recent revision made to the page and the most recent revision made to the
page by you were revisiondeleted and therefore could not be provided to you.',
        'emaildeletedpages-email-closing' => '

You are receiving this email because you opted in to be emailed when a page you created
is deleted. To unsubscribe from these emails, visit the User profile tab of
Special:Preferences and change the setting.
<{{canonicalurl:{{#special:Preferences}}}}>',
        'tog-emaildeletedpages' => 'Email me when a page I created is deleted',
);

/** Message documentation
 * @author Leucosticte
 */
$messages['qqq'] = array(
	'emaildeletedpages-desc' => '{{desc}}',
        'emaildeletedpages-email-subject' => 'The subject line of the email that gets sent to the user informing him of what page was deleted from what wiki
Parameters:
* $1 is the title of the deleted page
* $2 is the wiki name',
        'emaildeletedpages-email-opening' => 'The beginning line of the email that gets sent to the user when a page he created is deleted
Parameters:
* $1 is the user name of the user who deleted the page
* $2 is the URL of the talk page of the deleting user
* $3 is the title of the deleted page
* $4 is the deletion reason',
        'emaildeletedpages-email-body-intro' => 'The message informing the user that what will follow is the text of the most recent revision that was made before the page was deleted.',
        'emaildeletedpages-email-bodyintro-fallback' => 'The message informing the user that the most recent revision made to the page before it was deleted was revisiondeleted and therefore
he will instead be given the text of the most recent revision that the user made to the page',
        'emaildeletedpages-text-samerev-unavailable' => 'The message informing the user that the most recent revision made to the page, which was made by him, was revisiondeleted and
therefore could not be provided to him',
        'emaildeletedpages-email-text-unavailable' => 'The message informing the user that both the most recent revision made to the page and the most recent revision made to the page by him
were revisiondeleted and therefore could not be provided to him',
        'emaildeletedpages-email-closing' => 'The message informing the user that he is receiving this email because he opted in to be emailed when a page he created is deleted; and that to
unsubscribe from these emails, he can visit the User profile tab of Special:Preferences and change the setting',
        'tog-emaildeletedpages' => 'The preferences toggle to have the user be emailed when when a page he created is deleted',
);

See also[edit]