Avoid code duplication
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 4 May 2008 16:24:05 +0000 (16:24 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 4 May 2008 16:24:05 +0000 (16:24 +0000)
includes/api/ApiQueryInfo.php

index b25d9af..f1e16ba 100644 (file)
@@ -108,73 +108,59 @@ class ApiQueryInfo extends ApiQueryBase {
                        foreach ($titles as $id => $title)
                                if ($title->getNamespace() == NS_IMAGE)
                                        $imageIds[] = $id;
+                       // To avoid code duplication
+                       $cascadeTypes = array(
+                               array(
+                                       'prefix' => 'tl',
+                                       'table' => 'templatelinks',
+                                       'ns' => 'tl_namespace',
+                                       'title' => 'tl_title',
+                                       'ids' => array_diff(array_keys($titles), $imageIds)
+                               ),
+                               array(
+                                       'prefix' => 'il',
+                                       'table' => 'imagelinks',
+                                       'ns' => NS_IMAGE,
+                                       'title' => 'il_to',
+                                       'ids' => $imageIds
+                               )
+                       );
                        
-                       if (count($imageIds) != count($titles)) {
-                               // Check for cascading protection for non images
-                               $this->resetQueryParams();
-                               $this->addTables(array('page_restrictions', 'templatelinks'));
-                               $this->addTables('page', 'page_source');
-                               $this->addTables('page', 'page_target');
-                               $this->addFields(array('pr_type', 'pr_level', 'pr_expiry', 
-                                               'page_target.page_id AS page_target_id',
-                                               'page_source.page_namespace AS page_source_namespace',
-                                               'page_source.page_title AS page_source_title'));
-                               $this->addWhere(array('tl_from = pr_page', 
-                                               'page_target.page_namespace = tl_namespace', 
-                                               'page_target.page_title = tl_title',
-                                               'page_source.page_id = pr_page'
-                               ));
-                               $this->addWhereFld('pr_cascade', 1);
-                               $this->addWhereFld('page_target.page_id', array_diff(array_keys($titles), $imageIds));
-                               
-                               $res = $this->select(__METHOD__);
-                               while($row = $db->fetchObject($res)) {
-                                       $source = Title::makeTitle($row->page_source_namespace, $row->page_source_title);
-                                       $a = array(
-                                               'type' => $row->pr_type,
-                                               'level' => $row->pr_level,
-                                               'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
-                                               'source' => $source->getPrefixedText()
-                                       );
-                                       $protections[$row->page_target_id][] = $a;
-                               }
-                               $db->freeResult($res);
-                       }
-
-                       if (count($imageIds) != 0) {
-                               // Check for cascading protection for non images
-                               $this->resetQueryParams();
-                               $this->addTables(array('page_restrictions', 'imagelinks'));
-                               $this->addTables('page', 'page_source');
-                               $this->addTables('page', 'page_target');
-                               $this->addFields(array('pr_type', 'pr_level', 'pr_expiry', 
-                                               'page_target.page_id AS page_target_id',
-                                               'page_source.page_namespace AS page_source_namespace',
-                                               'page_source.page_title AS page_source_title'));
-                               $this->addWhere(array('il_from = pr_page', 
-                                               'page_target.page_namespace = '.NS_IMAGE, 
-                                               'page_target.page_title = il_to',
-                                               'page_source.page_id = pr_page'
-                               ));
-                               $this->addWhereFld('pr_cascade', 1);
-                               $this->addWhereFld('page_target.page_id', $imageIds);
+                       foreach ($cascadeTypes as $type)
+                       {
+                               if (count($type['ids']) != 0) {
+                                       $this->resetQueryParams();
+                                       $this->addTables(array('page_restrictions', $type['table']));
+                                       $this->addTables('page', 'page_source');
+                                       $this->addTables('page', 'page_target');
+                                       $this->addFields(array('pr_type', 'pr_level', 'pr_expiry', 
+                                                       'page_target.page_id AS page_target_id',
+                                                       'page_source.page_namespace AS page_source_namespace',
+                                                       'page_source.page_title AS page_source_title'));
+                                       $this->addWhere(array("{$type['prefix']}_from = pr_page", 
+                                                       'page_target.page_namespace = '.$type['ns'], 
+                                                       'page_target.page_title = '.$type['title'],
+                                                       'page_source.page_id = pr_page'
+                                       ));
+                                       $this->addWhereFld('pr_cascade', 1);
+                                       $this->addWhereFld('page_target.page_id', $type['ids']);
                                
-                               $res = $this->select(__METHOD__);
-                               while($row = $db->fetchObject($res)) {
-                                       $source = Title::makeTitle($row->page_source_namespace, $row->page_source_title);
-                                       $a = array(
-                                               'type' => $row->pr_type,
-                                               'level' => $row->pr_level,
-                                               'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
-
-                                               'source' => $source->getPrefixedText()
-                                       );
-                                       $protections[$row->page_target_id][] = $a;
+                                       $res = $this->select(__METHOD__);
+                                       while($row = $db->fetchObject($res)) {
+                                               $source = Title::makeTitle($row->page_source_namespace, $row->page_source_title);
+                                               $a = array(
+                                                       'type' => $row->pr_type,
+                                                       'level' => $row->pr_level,
+                                                       'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
+                                                       'source' => $source->getPrefixedText()
+                                               );
+                                               $protections[$row->page_target_id][] = $a;
+                                       }
+                                       $db->freeResult($res);
                                }
-                               $db->freeResult($res);
                        }
-                       
                }
+
                // We don't need to check for pt stuff if there are no nonexistent titles
                if($fld_protection && !empty($missing))
                {