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() {
|
2011-10-26 23:27:01 +00:00
|
|
|
global $wgRestrictionLevels;
|
2007-12-06 16:06:22 +00:00
|
|
|
$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 {
|
2010-01-11 15:55:52 +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
|
|
|
}
|
|
|
|
|
if ( !in_array( $p[1], $wgRestrictionLevels ) && $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
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', '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;
|
|
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$resultProtections[] = array( $p[0] => $protections[$p[0]],
|
2012-05-16 17:22:36 +00:00
|
|
|
'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity() ?
|
2008-10-04 14:58:13 +00:00
|
|
|
'infinite' :
|
2010-01-11 15:55:52 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
$watch = $params['watch'] ? 'watch' : $params['watchlist'];
|
|
|
|
|
$this->setWatch( $watch, $titleObj );
|
2010-03-25 22:15:08 +00:00
|
|
|
|
2011-12-18 16:01:31 +00:00
|
|
|
$status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
|
|
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
$this->dieUsageMsg( $errors[0] );
|
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
|
|
|
),
|
2012-07-18 17:24:38 +00:00
|
|
|
'token' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
|
|
|
|
),
|
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();
|
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",
|
2007-12-06 16:06:22 +00:00
|
|
|
'token' => 'A protect token previously retrieved through prop=info',
|
2012-07-29 08:48:52 +00:00
|
|
|
'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
|
2010-01-11 15:55:52 +00:00
|
|
|
'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
|
2013-03-11 03:45:51 +00:00
|
|
|
'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.' ),
|
2012-07-07 07:12:04 +00:00
|
|
|
'reason' => 'Reason for (un)protecting',
|
2010-01-11 15:55:52 +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',
|
2010-03-25 22:15:08 +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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
public function getResultProperties() {
|
|
|
|
|
return array(
|
|
|
|
|
'' => array(
|
|
|
|
|
'title' => 'string',
|
|
|
|
|
'reason' => 'string',
|
|
|
|
|
'cascade' => 'boolean'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2010-05-25 20:46:09 +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-02-14 14:29:24 +00:00
|
|
|
public function getPossibleErrors() {
|
2012-04-07 19:31:27 +00:00
|
|
|
return array_merge( parent::getPossibleErrors(),
|
2012-04-07 21:47:06 +00:00
|
|
|
$this->getTitleOrPageIdErrorMessage(),
|
2012-04-07 19:31:27 +00:00
|
|
|
array(
|
|
|
|
|
array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
|
|
|
|
|
array( 'create-titleexists' ),
|
|
|
|
|
array( 'missingtitle-createonly' ),
|
|
|
|
|
array( 'protect-invalidaction', 'action' ),
|
|
|
|
|
array( 'protect-invalidlevel', 'level' ),
|
|
|
|
|
array( 'invalidexpiry', 'expiry' ),
|
|
|
|
|
array( 'pastexpiry', 'expiry' ),
|
|
|
|
|
)
|
|
|
|
|
);
|
2010-02-13 00:28:27 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 23:53:43 +00:00
|
|
|
public function getTokenSalt() {
|
2010-10-01 20:12:50 +00:00
|
|
|
return '';
|
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(
|
2010-11-23 22:05:27 +00:00
|
|
|
'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
|
2007-12-06 16:06:22 +00:00
|
|
|
'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|