http://www.mediawiki.org/wiki/Special:Code/MediaWiki/99558
Revision: 99558
Author: raindrift
Date: 2011-10-11 20:15:00 +0000 (Tue, 11 Oct 2011)
Log Message:
-----------
Created PageTriage API for checking in/out pages.
Modified Paths:
--------------
trunk/extensions/PageTriage/PageTriage.i18n.php
trunk/extensions/PageTriage/sql/PageTriage.sql
Added Paths:
-----------
trunk/extensions/PageTriage/api/
trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
Modified: trunk/extensions/PageTriage/PageTriage.i18n.php
===================================================================
--- trunk/extensions/PageTriage/PageTriage.i18n.php 2011-10-11 20:13:37 UTC
(rev 99557)
+++ trunk/extensions/PageTriage/PageTriage.i18n.php 2011-10-11 20:15:00 UTC
(rev 99558)
@@ -15,6 +15,7 @@
'pagetriage-desc' => 'Facilitates reviewing and approving new pages',
'pagetriage' => 'Page triage',
'pagetriagelist' => 'Page triage list',
+ 'pagetriage-api-invalidid' => 'The ID you provided ($1) is not valid.',
);
/**
@@ -22,4 +23,5 @@
*/
$messages['qqq'] = array(
'pagetriage-desc' => '{{desc}}',
+ 'pagetriage-api-invalidid' => 'Invalid title error message for
pagetriage API',
);
\ No newline at end of file
Added: trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
===================================================================
--- trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
(rev 0)
+++ trunk/extensions/PageTriage/api/ApiQueryPageTriage.php 2011-10-11
20:15:00 UTC (rev 99558)
@@ -0,0 +1,136 @@
+<?php
+/**
+ * PageTriage extension API
+ *
+ * Copyright © 2011 Wikimedia Foundation and Ian Baker <[email protected]>
+ * Based on code by Victor Vasiliev, Bryan Tong Minh, Roan Kattouw, and Alex Z.
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+/**
+ * Query module to checkout and checkin pages for PageTriage
+ *
+ * @ingroup API
+ * @ingroup Extensions
+ */
+class ApiQueryPageTriage extends ApiBase {
+
+ public function __construct( $query, $moduleName ) {
+ parent::__construct( $query, $moduleName, 'ptr' );
+ }
+
+ public function execute() {
+ # get the current user.
+ $context = $this->createContext();
+ $userId = $context->getUser()->getId();
+
+ $params = $this->extractRequestParams();
+ $action = $params['action'];
+
+ if( !preg_match('/^\D+$/', $params['id'] ) ) {
+ $this->dieUsageMsg( array( 'pagetriage-api-invalidid',
$params['id'] ) );
+ }
+
+ // expire old checkouts.
+ // TODO: make the time configurable.
+ wfDebug( __METHOD__ . " expiring PageTriage checkouts older
than 15 minutes\n" );
+ $dbw = $this->repo->getMasterDb();
+ $dbw->delete(
+ 'pagetriage_checkouts',
+ 'ptc_timestamp < ' . $dbw->timestamp( time() - 15 * 60
),
+ __METHOD__
+ );
+
+ $res = $this->getResult();
+
+ if( $action === 'checkout' ) {
+ // the unique index on ptc_recentchanges_id ensures
that this will fail if there's an existing row.
+ // doing it this way allows for atomic checking w/o
starting a transaction.
+ //
+ // this happens on the master because we expect even a
small amount of lag to
+ // entirely break it. it's a small table and a small
number of people will be using it.
+ $dbw->insert(
+ 'pagetriage_checkouts',
+ array(
+ 'ptc_user' => $userId,
+ 'ptc_recentchanges_id' => $params['id'],
+ 'ptc_timestamp' => $dbw->timestamp()
+ ),
+ __METHOD__
+ );
+
+ // this won't be set if the insert failed.
+ $checkoutId = $dbw->insertId();
+
+ if( $checkoutId ) {
+ $res->addValue( 'pagetriage', 'checkout-id',
$checkoutId );
+ $res->addValue( 'pagetriage', 'result', 'ok' );
+ } else {
+ $res->addValue( 'pagetriage', 'result',
'already-checked-out' );
+ }
+ } elseif ( $action === 'checkin' ) {
+ // delete this user's row, if any.
+ $dbw->delete(
+ 'pagetriage_checkouts',
+ array(
+ 'ptc_user' => $userId,
+ 'ptc_recentchanges_id' => $params['id'],
+ ),
+ __METHOD__
+ );
+
+ $res->addValue( 'pagetriage', 'result', 'ok' );
+ }
+ }
+
+ public function getAllowedParams() {
+ return array(
+ 'id' => array(
+ ApiBase::PARAM_REQUIRED => true,
+ ),
+ 'action' => array(
+ ApiBase::PARAM_DFLT => 'checkout',
+ ApiBase::PARAM_ISMULTI => false,
+ ApiBase::PARAM_TYPE => array(
+ 'checkout', 'checkin',
+ ),
+ )
+ );
+ }
+
+ public function getParamDescription() {
+ return array(
+ 'id' => 'The ID of the recentchanges entry you\'d like
to check out/in',
+ 'action' => 'What you\'d like to do',
+ );
+ }
+
+ public function getDescription() {
+ return 'Check out or check in a page for page triage.';
+ }
+
+ public function getExamples() {
+ return array(
+ 'api.php?action=pagetriage&ptrid=12345',
+ 'api.php?action=pagetriage&ptrid=12345&action=checkin',
+ );
+ }
+
+ public function getVersion() {
+ return __CLASS__ . ': $Id: $';
+ }
+}
Property changes on: trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/extensions/PageTriage/sql/PageTriage.sql
===================================================================
--- trunk/extensions/PageTriage/sql/PageTriage.sql 2011-10-11 20:13:37 UTC
(rev 99557)
+++ trunk/extensions/PageTriage/sql/PageTriage.sql 2011-10-11 20:15:00 UTC
(rev 99558)
@@ -23,4 +23,8 @@
ptc_timestamp varbinary(14) NOT NULL
) /*$wgDBTableOptions*/;
-CREATE UNIQUE INDEX /*i*/ptc_user_rc ON /*_*/pagetriage_checkouts
(ptc_user,ptc_recentchanges_id);
+-- this index is for retrieving data
+CREATE INDEX /*i*/ptc_user_rc ON /*_*/pagetriage_checkouts
(ptc_user,ptc_recentchanges_id);
+
+-- this index is for enforcing one checkout per page.
+CREATE UNIQUE INDEX /*i*/ptc_recentchanges_id ON /*_*/pagetriage_checkouts
(ptc_recentchanges_id);
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs