From 7523ddffb42164d028d358286b05bf3aaefc9eb9 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 8 Jan 2008 16:31:50 +0000 Subject: [PATCH] * (bug 12543) API should support new protected titles system * Changing ApiProtect to return ISO 8601 timestamps * This doesn't really need a RELEASE-NOTES entry, as the protected titles system is already mentioned in RELEASE-NOTES --- includes/api/ApiProtect.php | 15 +++++++---- includes/api/ApiQueryInfo.php | 51 ++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index 91fa57d55e..02d3e84bb7 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -59,11 +59,8 @@ class ApiProtect extends ApiBase { $titleObj = Title::newFromText($params['title']); if(!$titleObj) $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle'); - if(!$titleObj->exists()) - $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle'); if(!$titleObj->userCan('protect')) $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied'); - $articleObj = new Article($titleObj); if(in_array($params['expiry'], array('infinite', 'indefinite', 'never'))) $expiry = Block::infinity(); @@ -83,16 +80,24 @@ class ApiProtect extends ApiBase { { $p = explode('=', $prot); $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]); + if($titleObj->exists() && $p[0] == 'create') + $this->dieUsage("Existing titles can't be protected with 'create'", 'create-titleexists'); + if(!$titleObj->exists() && $p[0] != 'create') + $this->dieUsage("Missing titles can only be protected with 'create'", 'create-missingtitle'); } $dbw = wfGetDb(DB_MASTER); $dbw->begin(); - $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry); + if($titleObj->exists()) { + $articleObj = new Article($titleObj); + $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry); + } else + $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry); if(!$ok) // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime? $this->dieUsage('Unknown error', 'unknownerror'); $dbw->commit(); - $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => $expiry); + $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => wfTimestamp(TS_ISO_8601, $expiry)); if($params['cascade']) $res['cascade'] = ''; $res['protections'] = $protections; diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index 8bb80ef0a5..10ac8139f0 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -72,6 +72,7 @@ class ApiQueryInfo extends ApiQueryBase { $pageSet = $this->getPageSet(); $titles = $pageSet->getGoodTitles(); + $missing = $pageSet->getMissingTitles(); $result = $this->getResult(); $pageRestrictions = $pageSet->getCustomField('page_restrictions'); @@ -82,23 +83,42 @@ class ApiQueryInfo extends ApiQueryBase { $pageLatest = $pageSet->getCustomField('page_latest'); $pageLength = $pageSet->getCustomField('page_len'); - if ($fld_protection && count($titles) > 0) { + $db = $this->getDB(); + if ($fld_protection && !empty($titles)) { $this->addTables('page_restrictions'); $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry')); $this->addWhereFld('pr_page', array_keys($titles)); - $db = $this->getDB(); $res = $this->select(__METHOD__); while($row = $db->fetchObject($res)) { $protections[$row->pr_page][] = array( - 'type' => $row->pr_type, - 'level' => $row->pr_level, - 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ) - ); + 'type' => $row->pr_type, + 'level' => $row->pr_level, + 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ) + ); } $db->freeResult($res); } - + // We don't need to check for pt stuff if there are no nonexistent titles + if($fld_protection && !empty($missing)) + { + $this->resetQueryParams(); + // Construct a custom WHERE clause that matches all titles in $missing + $lb = new LinkBatch($missing); + $this->addTables('protected_titles'); + $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry')); + $this->addWhere($lb->constructSet('pt', $db)); + $res = $this->select(__METHOD__); + while($row = $db->fetchObject($res)) { + $prottitles[$row->pt_namespace][$row->pt_title] = array( + 'type' => 'create', + 'level' => $row->pt_create_perm, + 'expiry' => Block::decodeExpiry($row->pt_expiry, TS_ISO_8601) + ); + } + $db->freeResult($res); + } + foreach ( $titles as $pageid => $title ) { $pageInfo = array ( 'touched' => wfTimestamp(TS_ISO_8601, $pageTouched[$pageid]), @@ -169,14 +189,19 @@ class ApiQueryInfo extends ApiQueryBase { ), $pageid, $pageInfo); } - // Get edit tokens for missing titles if requested - // Delete, protect and move tokens are N/A for missing titles anyway - if($tok_edit) + // Get edit/protect tokens and protection data for missing titles if requested + // Delete and move tokens are N/A for missing titles anyway + if($tok_edit || $tok_protect || $fld_protection) { - $missing = $pageSet->getMissingTitles(); $res = &$result->getData(); - foreach($missing as $pageid => $title) - $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken(); + foreach($missing as $pageid => $title) { + if($tok_edit) + $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken(); + if($tok_protect) + $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken(); + if($fld_protection) + $res['query']['pages'][$pageid]['protection'] = $prottitles[$title->getNamespace()][$title->getDbKey()]; + } } } -- 2.20.1