Matthias Mullie has uploaded a new change for review.
https://gerrit.wikimedia.org/r/64620
Change subject: Make it slightly easier for extensions to hook into page
protection.
......................................................................
Make it slightly easier for extensions to hook into page protection.
Split parts of doUpdateRestrictions() into several more concise functions.
The biggest drawback is increasing $dbw->encodeExpiry() calls times 3. This is
not much of a problem given that's a very inexpensive function and it is only
called very few times ($limit holds 2 values in current codebase)
Added $reason to the ProtectionForm::save hook, so that other code hooking into
and adding their own settings into page protection form can also process the
reason for the protection change.
Change-Id: I879290ed83e4e47e9561d4c352fbd50c07d7e18a
---
M RELEASE-NOTES-1.22
M docs/hooks.txt
M includes/ProtectionForm.php
M includes/WikiPage.php
4 files changed, 149 insertions(+), 91 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/20/64620/1
diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22
index cd7971b..a9cb84e 100644
--- a/RELEASE-NOTES-1.22
+++ b/RELEASE-NOTES-1.22
@@ -80,6 +80,8 @@
which can be cascading (previously 'sysop' was hard-coded as the only one).
* XHTML5 support has been improved. If you set $wgMimeType =
'application/xhtml+xml'
MediaWiki will try outputting markup acording to XHTML5 rules.
+* Altered hook 'ProtectionForm::save', adding the reason page protection is
+ changed as third parameter.
=== Bug fixes in 1.22 ===
* Disable Special:PasswordReset when $wgEnableEmail is false. Previously one
diff --git a/docs/hooks.txt b/docs/hooks.txt
index a292997..4e8334e 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -1876,9 +1876,10 @@
$output: a string of the form HTML so far
'ProtectionForm::save': Called when a protection form is submitted.
-$article: the title being (un)protected
-$errorMsg: an html message string of an error or an array of message name and
+$article: the Page being (un)protected
+&$errorMsg: an html message string of an error or an array of message name and
its parameters
+$reasonstr: a string describing the reason page protection level is altered
'ProtectionForm::showLogExtract': Called after the protection log extract is
shown.
diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php
index 6f444ee..6264394 100644
--- a/includes/ProtectionForm.php
+++ b/includes/ProtectionForm.php
@@ -320,7 +320,7 @@
* you can also return an array of message name and
its parameters
*/
$errorMsg = '';
- if ( !wfRunHooks( 'ProtectionForm::save', array(
$this->mArticle, &$errorMsg ) ) ) {
+ if ( !wfRunHooks( 'ProtectionForm::save', array(
$this->mArticle, &$errorMsg, $reasonstr ) ) ) {
if ( $errorMsg == '' ) {
$errorMsg = array( 'hookaborted' );
}
diff --git a/includes/WikiPage.php b/includes/WikiPage.php
index b8f4911..eb6c18a 100644
--- a/includes/WikiPage.php
+++ b/includes/WikiPage.php
@@ -2214,14 +2214,14 @@
* This works for protection both existing and non-existing pages.
*
* @param array $limit set of restriction keys
- * @param $reason String
- * @param &$cascade Integer. Set to false if cascading protection isn't
allowed.
* @param array $expiry per restriction type expiration
+ * @param &$cascade Integer. Set to false if cascading protection isn't
allowed.
+ * @param $reason String
* @param $user User The user updating the restrictions
* @return Status
*/
public function doUpdateRestrictions( array $limit, array $expiry,
&$cascade, $reason, User $user ) {
- global $wgContLang, $wgCascadingRestrictionLevels;
+ global $wgCascadingRestrictionLevels;
if ( wfReadOnly() ) {
return Status::newFatal( 'readonlytext',
wfReadOnlyReason() );
@@ -2294,51 +2294,6 @@
$logAction = 'protect';
}
- $encodedExpiry = array();
- $protectDescription = '';
- # Some bots may parse IRC lines, which are generated from log
entries which contain plain
- # protect description text. Keep them in old format to avoid
breaking compatibility.
- # TODO: Fix protection log to store structured description and
format it on-the-fly.
- $protectDescriptionLog = '';
- foreach ( $limit as $action => $restrictions ) {
- $encodedExpiry[$action] = $dbw->encodeExpiry(
$expiry[$action] );
- if ( $restrictions != '' ) {
- $protectDescriptionLog .=
$wgContLang->getDirMark() . "[$action=$restrictions] (";
- # $action is one of $wgRestrictionTypes =
array( 'create', 'edit', 'move', 'upload' ).
- # All possible message keys are listed here for
easier grepping:
- # * restriction-create
- # * restriction-edit
- # * restriction-move
- # * restriction-upload
- $actionText = wfMessage( 'restriction-' .
$action )->inContentLanguage()->text();
- # $restrictions is one of $wgRestrictionLevels
= array( '', 'autoconfirmed', 'sysop' ),
- # with '' filtered out. All possible message
keys are listed below:
- # * protect-level-autoconfirmed
- # * protect-level-sysop
- $restrictionsText = wfMessage( 'protect-level-'
. $restrictions )->inContentLanguage()->text();
- if ( $encodedExpiry[$action] != 'infinity' ) {
- $expiryText = wfMessage(
- 'protect-expiring',
- $wgContLang->timeanddate(
$expiry[$action], false, false ),
- $wgContLang->date(
$expiry[$action], false, false ),
- $wgContLang->time(
$expiry[$action], false, false )
- )->inContentLanguage()->text();
- } else {
- $expiryText = wfMessage(
'protect-expiry-indefinite' )
- ->inContentLanguage()->text();
- }
-
- if ( $protectDescription !== '' ) {
- $protectDescription .= wfMessage(
'word-separator' )->inContentLanguage()->text();
- }
- $protectDescription .= wfMessage(
'protect-summary-desc' )
- ->params( $actionText,
$restrictionsText, $expiryText )
- ->inContentLanguage()->text();
- $protectDescriptionLog .= $expiryText . ') ';
- }
- }
- $protectDescriptionLog = trim( $protectDescriptionLog );
-
if ( $id ) { // Protection of existing page
if ( !wfRunHooks( 'ArticleProtect', array( &$this,
&$user, $limit, $reason ) ) ) {
return Status::newGood();
@@ -2366,7 +2321,7 @@
'pr_type' => $action,
'pr_level' =>
$restrictions,
'pr_cascade' => (
$cascade && $action == 'edit' ) ? 1 : 0,
- 'pr_expiry' =>
$encodedExpiry[$action]
+ 'pr_expiry' =>
$dbw->encodeExpiry( $expiry[$action] )
),
__METHOD__
);
@@ -2376,42 +2331,8 @@
}
}
- // Prepare a null revision to be added to the history
- $editComment = $wgContLang->ucfirst(
- wfMessage(
- $revCommentMsg,
- $this->mTitle->getPrefixedText()
- )->inContentLanguage()->text()
- );
- if ( $reason ) {
- $editComment .= wfMessage( 'colon-separator'
)->inContentLanguage()->text() . $reason;
- }
- if ( $protectDescription ) {
- $editComment .= wfMessage( 'word-separator'
)->inContentLanguage()->text();
- $editComment .= wfMessage( 'parentheses'
)->params( $protectDescription )->inContentLanguage()->text();
- }
- if ( $cascade ) {
- $editComment .= wfMessage( 'word-separator'
)->inContentLanguage()->text();
- $editComment .= wfMessage( 'brackets' )->params(
- wfMessage( 'protect-summary-cascade'
)->inContentLanguage()->text()
- )->inContentLanguage()->text();
- }
-
- // Insert a null revision
- $nullRevision = Revision::newNullRevision( $dbw, $id,
$editComment, true );
- $nullRevId = $nullRevision->insertOn( $dbw );
-
$latest = $this->getLatest();
- // Update page record
- $dbw->update( 'page',
- array( /* SET */
- 'page_touched' => $dbw->timestamp(),
- 'page_restrictions' => '',
- 'page_latest' => $nullRevId
- ), array( /* WHERE */
- 'page_id' => $id
- ), __METHOD__
- );
+ $nullRevision = $this->insertNullRevision(
$revCommentMsg, $limit, $expiry, $cascade, $reason, $user );
wfRunHooks( 'NewRevisionFromEditComplete', array(
$this, $nullRevision, $latest, $user ) );
wfRunHooks( 'ArticleProtectComplete', array( &$this,
&$user, $limit, $reason ) );
@@ -2427,7 +2348,7 @@
'pt_title' =>
$this->mTitle->getDBkey(),
'pt_create_perm' =>
$limit['create'],
'pt_timestamp' =>
$dbw->encodeExpiry( wfTimestampNow() ),
- 'pt_expiry' =>
$encodedExpiry['create'],
+ 'pt_expiry' =>
$dbw->encodeExpiry( $expiry['create'] ),
'pt_user' => $user->getId(),
'pt_reason' => $reason,
), __METHOD__
@@ -2445,19 +2366,153 @@
$this->mTitle->flushRestrictions();
if ( $logAction == 'unprotect' ) {
- $logParams = array();
+ $params = array();
} else {
- $logParams = array( $protectDescriptionLog, $cascade ?
'cascade' : '' );
+ $protectDescriptionLog = $this->protectDescriptionLog(
$limit, $expiry );
+ $params = array( $protectDescriptionLog, $cascade ?
'cascade' : '' );
}
// Update the protection log
$log = new LogPage( 'protect' );
- $log->addEntry( $logAction, $this->mTitle, trim( $reason ),
$logParams, $user );
+ $log->addEntry( $logAction, $this->mTitle, trim( $reason ),
$params, $user );
return Status::newGood();
}
/**
+ * Insert a new null revision for this page
+ *
+ * @param string $revCommentMsg comment message key for the revision
+ * @param array $limit set of restriction keys
+ * @param array $expiry per restriction type expiration
+ * @param $cascade Integer. Set to false if cascading protection isn't
allowed.
+ * @param $reason String
+ * @param $user User The user updating the restrictions
+ */
+ public function insertNullRevision( $revCommentMsg, array $limit, array
$expiry, $cascade, $reason, User $user ) {
+ global $wgContLang;
+ $dbw = wfGetDB( DB_MASTER );
+
+ // Prepare a null revision to be added to the history
+ $editComment = $wgContLang->ucfirst(
+ wfMessage(
+ $revCommentMsg,
+ $this->mTitle->getPrefixedText()
+ )->inContentLanguage()->text()
+ );
+ if ( $reason ) {
+ $editComment .= wfMessage( 'colon-separator'
)->inContentLanguage()->text() . $reason;
+ }
+ $protectDescription = $this->protectDescription( $limit,
$expiry );
+ if ( $protectDescription ) {
+ $editComment .= wfMessage( 'word-separator'
)->inContentLanguage()->text();
+ $editComment .= wfMessage( 'parentheses' )->params(
$protectDescription )->inContentLanguage()->text();
+ }
+ if ( $cascade ) {
+ $editComment .= wfMessage( 'word-separator'
)->inContentLanguage()->text();
+ $editComment .= wfMessage( 'brackets' )->params(
+ wfMessage( 'protect-summary-cascade'
)->inContentLanguage()->text()
+ )->inContentLanguage()->text();
+ }
+
+ $nullRev = Revision::newNullRevision( $dbw, $this->getId(),
$editComment, true );
+ $nullRev->insertOn( $dbw );
+
+ // Update page record and touch page
+ $oldLatest = $nullRev->getParentId();
+ $this->updateRevisionOn( $dbw, $nullRev, $oldLatest );
+
+ return $nullRev;
+ }
+
+ /**
+ * @param string $expiry 14-char timestamp or "infinity", or false if
the input was invalid
+ * @return string
+ */
+ protected function formatExpiry( $expiry ) {
+ global $wgContLang;
+ $dbr = wfGetDB( DB_SLAVE );
+
+ $encodedExpiry = $dbr->encodeExpiry( $expiry );
+ if ( $encodedExpiry != 'infinity' ) {
+ return wfMessage(
+ 'protect-expiring',
+ $wgContLang->timeanddate( $expiry, false, false
),
+ $wgContLang->date( $expiry, false, false ),
+ $wgContLang->time( $expiry, false, false )
+ )->inContentLanguage()->text();
+ } else {
+ return wfMessage( 'protect-expiry-indefinite' )
+ ->inContentLanguage()->text();
+ }
+ }
+
+ /**
+ * Builds the description to serve as comment for the edit.
+ *
+ * @param array $limit set of restriction keys
+ * @param array $expiry per restriction type expiration
+ * @return string
+ */
+ public function protectDescription( array $limit, array $expiry ) {
+ $protectDescription = '';
+
+ foreach ( $limit as $action => $restrictions ) {
+ if ( $restrictions != '' ) {
+ # $action is one of $wgRestrictionTypes =
array( 'create', 'edit', 'move', 'upload' ).
+ # All possible message keys are listed here for
easier grepping:
+ # * restriction-create
+ # * restriction-edit
+ # * restriction-move
+ # * restriction-upload
+ $actionText = wfMessage( 'restriction-' .
$action )->inContentLanguage()->text();
+ # $restrictions is one of $wgRestrictionLevels
= array( '', 'autoconfirmed', 'sysop' ),
+ # with '' filtered out. All possible message
keys are listed below:
+ # * protect-level-autoconfirmed
+ # * protect-level-sysop
+ $restrictionsText = wfMessage( 'protect-level-'
. $restrictions )->inContentLanguage()->text();
+
+ $expiryText = $this->formatExpiry(
$expiry[$action] );
+
+ if ( $protectDescription !== '' ) {
+ $protectDescription .= wfMessage(
'word-separator' )->inContentLanguage()->text();
+ }
+ $protectDescription .= wfMessage(
'protect-summary-desc' )
+ ->params( $actionText,
$restrictionsText, $expiryText )
+ ->inContentLanguage()->text();
+ }
+ }
+
+ return $protectDescription;
+ }
+
+ /**
+ * Builds the description to serve as comment for the log entry.
+ *
+ * Some bots may parse IRC lines, which are generated from log entries
which contain plain
+ * protect description text. Keep them in old format to avoid breaking
compatibility.
+ * TODO: Fix protection log to store structured description and format
it on-the-fly.
+ *
+ * @param array $limit set of restriction keys
+ * @param array $expiry per restriction type expiration
+ * @return string
+ */
+ public function protectDescriptionLog( array $limit, array $expiry ) {
+ global $wgContLang;
+
+ $protectDescriptionLog = '';
+
+ foreach ( $limit as $action => $restrictions ) {
+ if ( $restrictions != '' ) {
+ $expiryText = $this->formatExpiry(
$expiry[$action] );
+ $protectDescriptionLog .=
$wgContLang->getDirMark() . "[$action=$restrictions] ($expiryText)";
+ }
+ }
+
+ return trim( $protectDescriptionLog );
+ }
+
+ /**
* Take an array of page restrictions and flatten it to a string
* suitable for insertion into the page_restrictions field.
* @param $limit Array
--
To view, visit https://gerrit.wikimedia.org/r/64620
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I879290ed83e4e47e9561d4c352fbd50c07d7e18a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits