[MediaWiki-commits] [Gerrit] mediawiki...AuthorProtect[master]: Convert AuthorProtect to use extension registration

2017-11-25 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/373563 )

Change subject: Convert AuthorProtect to use extension registration
..


Convert AuthorProtect to use extension registration

Bug: T174042
Change-Id: I27fec954e8a5c8ea46c8d040051be2288161bcd8
---
A AuthorProtect.hooks.php
M AuthorProtect.php
A AuthorProtectAction.php
A extension.json
M i18n/en.json
M i18n/qqq.json
6 files changed, 281 insertions(+), 261 deletions(-)

Approvals:
  Skizzerz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/AuthorProtect.hooks.php b/AuthorProtect.hooks.php
new file mode 100644
index 000..22d3448
--- /dev/null
+++ b/AuthorProtect.hooks.php
@@ -0,0 +1,62 @@
+getTitle();
+
+   if ( self::UserIsAuthor( $user, $title ) ) {
+   $aRights[] = 'author';
+   $aRights = array_unique( $aRights );
+   }
+
+   return true;
+   }
+
+   public static function MakeContentAction( $skin, &$links ) {
+   $title = $skin->getTitle();
+   $user = $skin->getUser();
+   $request = $skin->getRequest();
+
+   if ( self::UserIsAuthor( $user, $title ) && $user->isAllowed( 
'authorprotect' ) && !$user->isAllowed( 'protect' ) ) {
+   $action = $request->getText( 'action' );
+   $links['actions']['authorprotect'] = [
+   'class' => $action == 'authorprotect' ? 
'selected' : false,
+   'text' => wfMessage( 
self::AuthorProtectMessage( $title ) ),
+   'href' => $title->getLocalUrl( 
'action=authorprotect' ),
+   ];
+   }
+
+   return true;
+   }
+
+   public static function UserIsAuthor( $user, $title, $checkMaster = 
false ) {
+   if ( !$title instanceOf Title ) {
+   return false; // quick hack to prevent the API from 
messing up.
+   }
+
+   if ( $user->getID() === 0 ) {
+   return false; // don't allow anons, they shouldn't even 
get this far but just in case...
+   }
+
+   $id = $title->getArticleID();
+   $dbr = wfGetDB( $checkMaster ? DB_MASTER : DB_SLAVE );
+   $aid = $dbr->selectField(
+   'revision',
+   'rev_user',
+   [ 'rev_page' => $id ],
+   __METHOD__,
+   [ 'ORDER BY' => 'rev_timestamp ASC' ]
+   );
+
+   return $user->getID() == $aid;
+   }
+
+   private static function AuthorProtectMessage( $title ) {
+   foreach ( $title->getRestrictionTypes() as $type ) {
+   if ( in_array( 'author', $title->getRestrictions( $type 
) ) ) {
+   return 'unprotect';
+   }
+   }
+   return 'protect';
+   }
+}
diff --git a/AuthorProtect.php b/AuthorProtect.php
index cd4ac49..cbaafb7 100644
--- a/AuthorProtect.php
+++ b/AuthorProtect.php
@@ -4,261 +4,14 @@
  * See http://www.mediawiki.org/wiki/Extension:AuthorProtect for more details
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-   echo "This file is an extension to MediaWiki and cannot be run 
externally\n";
-   die( 1 );
-}
-
-$wgExtensionCredits['other'][] = array(
-   'path' => __FILE__,
-   'name' => 'Author Protect',
-   'author' => 'Ryan Schmidt',
-   'url' => 'https://www.mediawiki.org/wiki/Extension:AuthorProtect',
-   'version' => '1.4.0',
-   'descriptionmsg' => 'authorprotect-desc',
-   'license-name' => 'GPL-2.0+'
-);
-
-$wgAvailableRights[] = 'author'; // dynamically assigned to the author of a 
page, but can be set w/ wgGroupPermissions too
-$wgAvailableRights[] = 'authorprotect'; // users without this right cannot 
protect pages they author
-$wgMessagesDirs['AuthorProtect'] = __DIR__ . '/i18n';
-$wgGroupPermissions['sysop']['author'] = true; // sysops can edit every page 
despite author protection
-$wgGroupPermissions['user']['authorprotect'] = true; // registered users can 
protect pages they author
-$wgHooks['SkinTemplateNavigation::Universal'][] = 
'AuthorProtect::MakeContentAction';
-$wgHooks['UnknownAction'][] = 'AuthorProtect::AuthorProtectForm';
-$wgHooks['userCan'][] = 'AuthorProtect::AuthorProtectDelay';
-$wgHooks['UserGetRights'][] = 'AuthorProtect::AssignAuthor';
-$wgRestrictionLevels[] = 'author'; // so sysops, etc. using the normal 
protection interface can protect and unprotect it at the author level
-$wgAvailableRights[] = 'author';
-$wgAvailableRights[] = 'authorprotect';
-
-// internal variables, do not modify
-$wgAuthorProtectDoProtect = false;
-$wgAuthorProtectDelayRun = true;
-
-class AuthorProtect {
-   /**
-* Extensions like ConfirmAc

[MediaWiki-commits] [Gerrit] mediawiki...AuthorProtect[master]: Convert AuthorProtect to use extension registration.

2017-08-24 Thread Reception123 (Code Review)
Reception123 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373563 )

Change subject: Convert AuthorProtect to use extension registration.
..

Convert AuthorProtect to use extension registration.

Bug: T174042
Change-Id: I27fec954e8a5c8ea46c8d040051be2288161bcd8
---
A AuthorProtect.hooks.php
M AuthorProtect.php
A extension.json
3 files changed, 286 insertions(+), 257 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/AuthorProtect 
refs/changes/63/373563/1

diff --git a/AuthorProtect.hooks.php b/AuthorProtect.hooks.php
new file mode 100644
index 000..312cc18
--- /dev/null
+++ b/AuthorProtect.hooks.php
@@ -0,0 +1,232 @@
+mRights = null;
+   $user->getRights(); // delay hook execution for 
compatibility w/ ConfirmAccount
+   $act = ( $action == '' || $action == 'view' ) ? 'edit' 
: $action;
+   $wgAuthorProtectDelayRun = false;
+   if ( self::userIsAuthor( $title ) && 
self::isAuthorProtected( $title, $act ) ) {
+   $result = true;
+   return false;
+   }
+   }
+   $result = null;
+   return true;
+   }
+
+   public static function AssignAuthor( $user, &$aRights ) {
+   global $wgTitle;
+
+   // don't assign author to anons... messes up logging stuff.
+   // plus it's all user_id based so it is impossible to 
differentiate one anon from another
+   if ( self::userIsAuthor( $wgTitle ) && $user->isLoggedIn() ) {
+   $aRights[] = 'author';
+   $aRights = array_unique( $aRights );
+   }
+   // assign protect too if we need to
+   global $wgAuthorProtectDoProtect;
+   if ( $wgAuthorProtectDoProtect ) {
+   $aRights[] = 'protect';
+   $aRights = array_unique( $aRights );
+   }
+   return true;
+   }
+
+   private static function AuthorProtectAssignProtect() {
+   global $wgAuthorProtectDoProtect, $wgUser;
+   $wgAuthorProtectDoProtect = true;
+   $wgUser->mRights = null;
+   $wgUser->getRights(); // re-trigger the above function to 
assign the protect right
+   $wgAuthorProtectDoProtect = false;
+   }
+
+   private static function AuthorProtectUnassignProtect() {
+   global $wgUser;
+   $wgUser->mRights = null;
+   $wgUser->getRights();
+   }
+
+   public static function MakeContentAction( $skin, &$links ) {
+   global $wgUser, $wgRequest;
+
+   $title = $skin->getTitle();
+   if ( self::userIsAuthor( $title ) && $wgUser->isAllowed( 
'authorprotect' ) && !$wgUser->isAllowed( 'protect' ) ) {
+   $action = $wgRequest->getText( 'action' );
+   $links['actions']['authorprotect'] = array(
+   'class' => $action == 'authorprotect' ? 
'selected' : false,
+   'text' => wfMessage( 
self::AuthorProtectMessage( $title ) ),
+   'href' => $title->getLocalUrl( 
'action=authorprotect' ),
+   );
+   }
+   return true;
+   }
+
+   public static function AuthorProtectForm( $action, $article ) {
+   if ( $action != 'authorprotect' ) {
+   return true; // unknown action, so state that the 
action doesn't exist
+   }
+   global $wgOut, $wgUser, $wgRequest, $wgRestrictionTypes;
+   if ( !$wgUser->isAllowed( 'authorprotect' ) ) {
+   throw new PermissionsError( 'authorprotect' );
+   }
+   if ( !self::userIsAuthor( $article->getTitle() ) ) {
+   $wgOut->setPageTitle( wfMessage( 'errorpagetitle' ) );
+   $wgOut->addWikiMsg( 'authorprotect-notauthor', 
$user->getName() );
+   return false;
+   }
+   $wgOut->setPageTitle( wfMessage( 'authorprotect' ) );
+   if ( !$wgRequest->wasPosted() ) {
+   $wgOut->addHTML( self::AuthorProtectMakeProtectForm( 
$article->getTitle() ) );
+   } else {
+   if ( !$wgUser->matchEditToken( $wgRequest->getText( 
'wpToken' ) ) ) {
+   $wgOut->setPageTitle( wfMessage( 
'errorpagetitle' ) );
+   $wgOut->addWikiMsg( 'sessionfailure' );
+   return false;
+   }
+   $restrictions = array();
+   $expiration = array();
+   $expiry = self::AuthorProtectExpiry( 
$