* (bug 12543) API should support new protected titles system
authorRoan Kattouw <catrope@users.mediawiki.org>
Tue, 8 Jan 2008 16:31:50 +0000 (16:31 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Tue, 8 Jan 2008 16:31:50 +0000 (16:31 +0000)
* 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
includes/api/ApiQueryInfo.php

index 91fa57d..02d3e84 100644 (file)
@@ -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;
index 8bb80ef..10ac813 100644 (file)
@@ -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()];
+                       }
                }
        }