2007-12-06 16:06:22 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2007-12-06 16:06:22 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Sep 1, 2007
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
|
2007-12-06 16:06:22 +00:00
|
|
|
*
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-12-06 16:06:22 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
class ApiProtect extends ApiBase {
|
|
|
|
|
public function execute() {
|
|
|
|
|
$params = $this->extractRequestParams();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-04-28 13:45:37 +00:00
|
|
|
$pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
|
|
|
|
|
$titleObj = $pageObj->getTitle();
|
2010-01-11 15:55:52 +00:00
|
|
|
|
2011-10-26 23:27:01 +00:00
|
|
|
$errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $errors ) {
|
2008-01-18 16:34:40 +00:00
|
|
|
// We don't care about multiple errors, just report one of them
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( reset( $errors ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-10-04 14:58:13 +00:00
|
|
|
$expiry = (array)$params['expiry'];
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( count( $expiry ) != count( $params['protections'] ) ) {
|
|
|
|
|
if ( count( $expiry ) == 1 ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
|
2010-02-23 18:05:46 +00:00
|
|
|
} else {
|
2013-11-14 18:03:09 +00:00
|
|
|
$this->dieUsageMsg( array(
|
|
|
|
|
'toofewexpiries',
|
|
|
|
|
count( $expiry ),
|
|
|
|
|
count( $params['protections'] )
|
|
|
|
|
) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2009-11-09 12:05:30 +00:00
|
|
|
$restrictionTypes = $titleObj->getRestrictionTypes();
|
2012-05-16 17:22:36 +00:00
|
|
|
$db = $this->getDB();
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2007-12-06 16:06:22 +00:00
|
|
|
$protections = array();
|
2008-09-18 21:30:51 +00:00
|
|
|
$expiryarray = array();
|
2008-10-04 14:58:13 +00:00
|
|
|
$resultProtections = array();
|
2010-02-23 18:05:46 +00:00
|
|
|
foreach ( $params['protections'] as $i => $prot ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$p = explode( '=', $prot );
|
|
|
|
|
$protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
|
2010-01-23 22:47:49 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $titleObj->exists() && $p[0] == 'create' ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'create-titleexists' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
if ( !$titleObj->exists() && $p[0] != 'create' ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'missingtitle-createonly' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-01-23 22:47:49 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2014-01-24 02:51:11 +00:00
|
|
|
if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
|
2013-12-20 09:06:51 +00:00
|
|
|
if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'infinity', 'never' ) ) ) {
|
2012-05-16 17:22:36 +00:00
|
|
|
$expiryarray[$p[0]] = $db->getInfinity();
|
2010-02-23 18:05:46 +00:00
|
|
|
} else {
|
2010-01-11 15:55:52 +00:00
|
|
|
$exp = strtotime( $expiry[$i] );
|
2010-06-09 11:44:05 +00:00
|
|
|
if ( $exp < 0 || !$exp ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2008-10-04 14:58:13 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$exp = wfTimestamp( TS_MW, $exp );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $exp < wfTimestampNow() ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2008-10-04 14:58:13 +00:00
|
|
|
$expiryarray[$p[0]] = $exp;
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
$resultProtections[] = array(
|
|
|
|
|
$p[0] => $protections[$p[0]],
|
|
|
|
|
'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity()
|
|
|
|
|
? 'infinite'
|
|
|
|
|
: wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] )
|
|
|
|
|
)
|
|
|
|
|
);
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-10 22:39:41 +00:00
|
|
|
$cascade = $params['cascade'];
|
2010-04-10 06:11:02 +00:00
|
|
|
|
2014-08-14 18:15:23 +00:00
|
|
|
if ( $params['watch'] ) {
|
|
|
|
|
$this->logFeatureUsage( 'action=protect&watch' );
|
|
|
|
|
}
|
2010-04-10 06:11:02 +00:00
|
|
|
$watch = $params['watch'] ? 'watch' : $params['watchlist'];
|
2013-11-08 18:01:28 +00:00
|
|
|
$this->setWatch( $watch, $titleObj, 'watchdefault' );
|
2010-03-25 22:15:08 +00:00
|
|
|
|
2013-11-14 18:03:09 +00:00
|
|
|
$status = $pageObj->doUpdateRestrictions(
|
|
|
|
|
$protections,
|
|
|
|
|
$expiryarray,
|
|
|
|
|
$cascade,
|
|
|
|
|
$params['reason'],
|
|
|
|
|
$this->getUser()
|
|
|
|
|
);
|
2011-12-18 16:01:31 +00:00
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
2013-06-13 17:56:29 +00:00
|
|
|
$this->dieStatus( $status );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
$res = array(
|
|
|
|
|
'title' => $titleObj->getPrefixedText(),
|
|
|
|
|
'reason' => $params['reason']
|
|
|
|
|
);
|
|
|
|
|
if ( $cascade ) {
|
2007-12-06 16:06:22 +00:00
|
|
|
$res['cascade'] = '';
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2008-10-04 14:58:13 +00:00
|
|
|
$res['protections'] = $resultProtections;
|
2011-06-30 01:06:17 +00:00
|
|
|
$result = $this->getResult();
|
|
|
|
|
$result->setIndexedTagName( $res['protections'], 'protection' );
|
|
|
|
|
$result->addValue( null, $this->getModuleName(), $res );
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-14 21:12:11 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-01-18 20:43:59 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2010-08-04 14:15:33 +00:00
|
|
|
'title' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2012-04-07 19:31:27 +00:00
|
|
|
),
|
|
|
|
|
'pageid' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
2010-08-04 14:15:33 +00:00
|
|
|
),
|
2007-12-06 16:06:22 +00:00
|
|
|
'protections' => array(
|
2010-08-04 14:15:33 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2010-08-04 14:29:39 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2007-12-06 16:06:22 +00:00
|
|
|
),
|
2008-10-04 14:58:13 +00:00
|
|
|
'expiry' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
|
ApiBase::PARAM_ALLOW_DUPLICATES => true,
|
|
|
|
|
ApiBase::PARAM_DFLT => 'infinite',
|
2008-10-04 14:58:13 +00:00
|
|
|
),
|
2007-12-06 16:06:22 +00:00
|
|
|
'reason' => '',
|
2009-02-02 16:38:40 +00:00
|
|
|
'cascade' => false,
|
2010-03-25 22:15:08 +00:00
|
|
|
'watch' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
|
|
|
|
),
|
|
|
|
|
'watchlist' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => 'preferences',
|
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
|
|
|
|
'watch',
|
|
|
|
|
'unwatch',
|
|
|
|
|
'preferences',
|
|
|
|
|
'nochange'
|
|
|
|
|
),
|
|
|
|
|
),
|
2007-12-06 16:06:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2012-04-07 19:31:27 +00:00
|
|
|
$p = $this->getModulePrefix();
|
2013-11-14 12:47:20 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2012-04-07 19:31:27 +00:00
|
|
|
'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
|
|
|
|
|
'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
|
2012-07-29 08:48:52 +00:00
|
|
|
'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
|
2013-11-14 18:03:09 +00:00
|
|
|
'expiry' => array(
|
|
|
|
|
'Expiry timestamps. If only one timestamp is ' .
|
|
|
|
|
'set, it\'ll be used for all protections.',
|
2013-12-20 09:06:51 +00:00
|
|
|
'Use \'infinite\', \'indefinite\', \'infinity\' or \'never\', for a never-expiring protection.'
|
2013-11-14 18:03:09 +00:00
|
|
|
),
|
2012-07-07 07:12:04 +00:00
|
|
|
'reason' => 'Reason for (un)protecting',
|
2013-11-14 18:03:09 +00:00
|
|
|
'cascade' => array(
|
|
|
|
|
'Enable cascading protection (i.e. protect pages included in this page)',
|
|
|
|
|
'Ignored if not all protection levels are \'sysop\' or \'protect\''
|
|
|
|
|
),
|
2009-02-02 16:38:40 +00:00
|
|
|
'watch' => 'If set, add the page being (un)protected to your watchlist',
|
2013-11-14 18:03:09 +00:00
|
|
|
'watchlist' => 'Unconditionally add or remove the page from your ' .
|
|
|
|
|
'watchlist, use preferences or do not change watch',
|
2007-12-06 16:06:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2014-03-09 20:22:47 +00:00
|
|
|
return 'Change the protection level of a page.';
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2013-11-14 18:03:09 +00:00
|
|
|
'api.php?action=protect&title=Main%20Page&token=123ABC&' .
|
|
|
|
|
'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
|
|
|
|
|
'api.php?action=protect&title=Main%20Page&token=123ABC&' .
|
|
|
|
|
'protections=edit=all|move=all&reason=Lifting%20restrictions'
|
2007-12-06 16:06:22 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Protect';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|