r99558 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99557‎ | r99558 | r99559 >
Date:20:15, 11 October 2011
Author:raindrift
Status:deferred (Comments)
Tags:
Comment:
Created PageTriage API for checking in/out pages.
Modified paths:
  • /trunk/extensions/PageTriage/PageTriage.i18n.php (modified) (history)
  • /trunk/extensions/PageTriage/api (added) (history)
  • /trunk/extensions/PageTriage/api/ApiQueryPageTriage.php (added) (history)
  • /trunk/extensions/PageTriage/sql/PageTriage.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/PageTriage/sql/PageTriage.sql
@@ -23,4 +23,8 @@
2424 ptc_timestamp varbinary(14) NOT NULL
2525 ) /*$wgDBTableOptions*/;
2626
27 -CREATE UNIQUE INDEX /*i*/ptc_user_rc ON /*_*/pagetriage_checkouts (ptc_user,ptc_recentchanges_id);
 27+-- this index is for retrieving data
 28+CREATE INDEX /*i*/ptc_user_rc ON /*_*/pagetriage_checkouts (ptc_user,ptc_recentchanges_id);
 29+
 30+-- this index is for enforcing one checkout per page.
 31+CREATE UNIQUE INDEX /*i*/ptc_recentchanges_id ON /*_*/pagetriage_checkouts (ptc_recentchanges_id);
Index: trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
@@ -0,0 +1,136 @@
 2+<?php
 3+/**
 4+ * PageTriage extension API
 5+ *
 6+ * Copyright © 2011 Wikimedia Foundation and Ian Baker <ian@wikimedia.org>
 7+ * Based on code by Victor Vasiliev, Bryan Tong Minh, Roan Kattouw, and Alex Z.
 8+ *
 9+ * This program is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * This program is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License along
 20+ * with this program; if not, write to the Free Software Foundation, Inc.,
 21+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22+ * http://www.gnu.org/copyleft/gpl.html
 23+ */
 24+
 25+/**
 26+ * Query module to checkout and checkin pages for PageTriage
 27+ *
 28+ * @ingroup API
 29+ * @ingroup Extensions
 30+ */
 31+class ApiQueryPageTriage extends ApiBase {
 32+
 33+ public function __construct( $query, $moduleName ) {
 34+ parent::__construct( $query, $moduleName, 'ptr' );
 35+ }
 36+
 37+ public function execute() {
 38+ # get the current user.
 39+ $context = $this->createContext();
 40+ $userId = $context->getUser()->getId();
 41+
 42+ $params = $this->extractRequestParams();
 43+ $action = $params['action'];
 44+
 45+ if( !preg_match('/^\D+$/', $params['id'] ) ) {
 46+ $this->dieUsageMsg( array( 'pagetriage-api-invalidid', $params['id'] ) );
 47+ }
 48+
 49+ // expire old checkouts.
 50+ // TODO: make the time configurable.
 51+ wfDebug( __METHOD__ . " expiring PageTriage checkouts older than 15 minutes\n" );
 52+ $dbw = $this->repo->getMasterDb();
 53+ $dbw->delete(
 54+ 'pagetriage_checkouts',
 55+ 'ptc_timestamp < ' . $dbw->timestamp( time() - 15 * 60 ),
 56+ __METHOD__
 57+ );
 58+
 59+ $res = $this->getResult();
 60+
 61+ if( $action === 'checkout' ) {
 62+ // the unique index on ptc_recentchanges_id ensures that this will fail if there's an existing row.
 63+ // doing it this way allows for atomic checking w/o starting a transaction.
 64+ //
 65+ // this happens on the master because we expect even a small amount of lag to
 66+ // entirely break it. it's a small table and a small number of people will be using it.
 67+ $dbw->insert(
 68+ 'pagetriage_checkouts',
 69+ array(
 70+ 'ptc_user' => $userId,
 71+ 'ptc_recentchanges_id' => $params['id'],
 72+ 'ptc_timestamp' => $dbw->timestamp()
 73+ ),
 74+ __METHOD__
 75+ );
 76+
 77+ // this won't be set if the insert failed.
 78+ $checkoutId = $dbw->insertId();
 79+
 80+ if( $checkoutId ) {
 81+ $res->addValue( 'pagetriage', 'checkout-id', $checkoutId );
 82+ $res->addValue( 'pagetriage', 'result', 'ok' );
 83+ } else {
 84+ $res->addValue( 'pagetriage', 'result', 'already-checked-out' );
 85+ }
 86+ } elseif ( $action === 'checkin' ) {
 87+ // delete this user's row, if any.
 88+ $dbw->delete(
 89+ 'pagetriage_checkouts',
 90+ array(
 91+ 'ptc_user' => $userId,
 92+ 'ptc_recentchanges_id' => $params['id'],
 93+ ),
 94+ __METHOD__
 95+ );
 96+
 97+ $res->addValue( 'pagetriage', 'result', 'ok' );
 98+ }
 99+ }
 100+
 101+ public function getAllowedParams() {
 102+ return array(
 103+ 'id' => array(
 104+ ApiBase::PARAM_REQUIRED => true,
 105+ ),
 106+ 'action' => array(
 107+ ApiBase::PARAM_DFLT => 'checkout',
 108+ ApiBase::PARAM_ISMULTI => false,
 109+ ApiBase::PARAM_TYPE => array(
 110+ 'checkout', 'checkin',
 111+ ),
 112+ )
 113+ );
 114+ }
 115+
 116+ public function getParamDescription() {
 117+ return array(
 118+ 'id' => 'The ID of the recentchanges entry you\'d like to check out/in',
 119+ 'action' => 'What you\'d like to do',
 120+ );
 121+ }
 122+
 123+ public function getDescription() {
 124+ return 'Check out or check in a page for page triage.';
 125+ }
 126+
 127+ public function getExamples() {
 128+ return array(
 129+ 'api.php?action=pagetriage&ptrid=12345',
 130+ 'api.php?action=pagetriage&ptrid=12345&action=checkin',
 131+ );
 132+ }
 133+
 134+ public function getVersion() {
 135+ return __CLASS__ . ': $Id: $';
 136+ }
 137+}
Property changes on: trunk/extensions/PageTriage/api/ApiQueryPageTriage.php
___________________________________________________________________
Added: svn:eol-style
1138 + native
Index: trunk/extensions/PageTriage/PageTriage.i18n.php
@@ -15,6 +15,7 @@
1616 'pagetriage-desc' => 'Facilitates reviewing and approving new pages',
1717 'pagetriage' => 'Page triage',
1818 'pagetriagelist' => 'Page triage list',
 19+ 'pagetriage-api-invalidid' => 'The ID you provided ($1) is not valid.',
1920 );
2021
2122 /**
@@ -22,4 +23,5 @@
2324 */
2425 $messages['qqq'] = array(
2526 'pagetriage-desc' => '{{desc}}',
 27+ 'pagetriage-api-invalidid' => 'Invalid title error message for pagetriage API',
2628 );
\ No newline at end of file

Comments

#Comment by Siebrand (talk | contribs)   21:45, 11 October 2011

Please document variables explicitly, despite how obvious they may appear to you.

Status & tagging log