2008-08-25 17:57:20 +00:00
|
|
|
<?php
|
2010-02-26 13:18:56 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2008-08-25 17:57:20 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Jan 4, 2008
|
|
|
|
|
*
|
2010-02-26 13:18:56 +00:00
|
|
|
* Copyright © 2008 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
|
2008-08-25 17:57:20 +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.
|
2008-08-25 17:57:20 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2008-08-25 17:57:20 +00:00
|
|
|
*/
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2008-08-25 17:57:20 +00:00
|
|
|
// Eclipse helper - will be ignored in production
|
2010-02-26 13:18:56 +00:00
|
|
|
require_once( 'ApiBase.php' );
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-01-14 00:00:40 +00:00
|
|
|
* API module to allow users to watch a page
|
2008-08-25 17:57:20 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiWatch extends ApiBase {
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function __construct( $main, $action ) {
|
2010-02-26 13:18:56 +00:00
|
|
|
parent::__construct( $main, $action );
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
global $wgUser;
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( !$wgUser->isLoggedIn() ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2010-01-11 15:55:52 +00:00
|
|
|
$title = Title::newFromText( $params['title'] );
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( !$title ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$article = new Article( $title );
|
|
|
|
|
$res = array( 'title' => $title->getPrefixedText() );
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( $params['unwatch'] ) {
|
2008-08-25 17:57:20 +00:00
|
|
|
$res['unwatched'] = '';
|
2010-04-27 15:09:04 +00:00
|
|
|
$res['message'] = wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
|
2011-04-14 10:38:29 +00:00
|
|
|
$success = Action::factory( 'unwatch', $article )->execute();
|
2010-02-26 13:18:56 +00:00
|
|
|
} else {
|
2008-08-25 17:57:20 +00:00
|
|
|
$res['watched'] = '';
|
2010-04-27 15:09:04 +00:00
|
|
|
$res['message'] = wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
|
2011-04-14 10:38:29 +00:00
|
|
|
$success = Action::factory( 'watch', $article )->execute();
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
2010-02-26 13:18:56 +00:00
|
|
|
if ( !$success ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'hookaborted' );
|
2010-02-26 13:18:56 +00:00
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $res );
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
2010-01-11 15:55:52 +00:00
|
|
|
}
|
2009-03-06 13:49:44 +00:00
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-26 13:18:56 +00:00
|
|
|
return array(
|
2010-08-04 20:47:58 +00:00
|
|
|
'title' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
|
|
|
|
),
|
|
|
|
|
|
2008-08-25 17:57:20 +00:00
|
|
|
'unwatch' => false,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2010-02-26 13:18:56 +00:00
|
|
|
return array(
|
2008-08-25 17:57:20 +00:00
|
|
|
'title' => 'The page to (un)watch',
|
|
|
|
|
'unwatch' => 'If set the page will be unwatched rather than watched',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDescription() {
|
2010-05-25 20:46:09 +00:00
|
|
|
return 'Add or remove a page from/to the current user\'s watchlist';
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
2010-02-26 13:18:56 +00:00
|
|
|
|
2010-02-13 00:28:27 +00:00
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
|
|
|
|
|
array( 'invalidtitle', 'title' ),
|
|
|
|
|
array( 'hookaborted' ),
|
|
|
|
|
) );
|
|
|
|
|
}
|
2008-08-25 17:57:20 +00:00
|
|
|
|
|
|
|
|
protected function getExamples() {
|
|
|
|
|
return array(
|
|
|
|
|
'api.php?action=watch&title=Main_Page',
|
2010-11-23 22:05:27 +00:00
|
|
|
'api.php?action=watch&title=Main_Page&unwatch=',
|
2008-08-25 17:57:20 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
2008-09-04 22:20:32 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2008-08-25 17:57:20 +00:00
|
|
|
}
|
|
|
|
|
}
|