* New properties: links, templates, images, langlinks
[lhc/web/wiklou.git] / includes / api / ApiQueryLinks.php
1 <?php
2
3 /*
4 * Created on May 12, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiQueryBase.php");
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiQueryLinks extends ApiQueryGeneratorBase {
35
36 const LINKS = 'links';
37 const TEMPLATES = 'templates';
38
39 private $table, $prefix, $description;
40
41 public function __construct($query, $moduleName) {
42
43 switch ($moduleName) {
44 case self::LINKS :
45 $this->table = 'pagelinks';
46 $this->prefix = 'pl';
47 $this->description = 'link';
48 break;
49 case self::TEMPLATES :
50 $this->table = 'templatelinks';
51 $this->prefix = 'tl';
52 $this->description = 'template';
53 break;
54 default :
55 ApiBase :: dieDebug(__METHOD__, 'Unknown module name');
56 }
57
58 parent :: __construct($query, $moduleName, $this->prefix);
59 }
60
61 public function execute() {
62 $this->run();
63 }
64
65 public function executeGenerator($resultPageSet) {
66 $this->run($resultPageSet);
67 }
68
69 private function run($resultPageSet = null) {
70
71 $this->addFields(array (
72 $this->prefix . '_from pl_from',
73 $this->prefix . '_namespace pl_namespace',
74 $this->prefix . '_title pl_title'
75 ));
76
77 $this->addTables($this->table);
78 $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
79 $this->addOption('ORDER BY', str_replace('pl_', $this->prefix . '_', 'pl_from, pl_namespace, pl_title'));
80
81 $db = $this->getDB();
82 $res = $this->select(__METHOD__);
83
84 if (is_null($resultPageSet)) {
85
86 $data = array();
87 $lastId = 0; // database has no ID 0
88 while ($row = $db->fetchObject($res)) {
89 if ($lastId != $row->pl_from) {
90 if($lastId != 0) {
91 $this->addPageSubItems($lastId, $data);
92 $data = array();
93 }
94 $lastId = $row->pl_from;
95 }
96 $vals = $this->addRowInfo('pl', $row);
97 if ($vals)
98 $data[] = $vals;
99 }
100
101 if($lastId != 0) {
102 $this->addPageSubItems($lastId, $data);
103 }
104
105 } else {
106
107 $titles = array();
108 while ($row = $db->fetchObject($res)) {
109 $titles[] = Title :: makeTitle($row->pl_namespace, $row->pl_title);
110 }
111 $resultPageSet->populateFromTitles($titles);
112 }
113
114 $db->freeResult($res);
115 }
116
117 private function addPageSubItems($pageId, $data) {
118 $result = $this->getResult();
119 $result->setIndexedTagName($data, $this->prefix);
120 $result->addValue(array ('query', 'pages', intval($pageId)),
121 $this->getModuleName(),
122 $data);
123 }
124
125 protected function getDescription() {
126 return "Returns all {$this->description}s from the given page(s)";
127 }
128
129 protected function getExamples() {
130 return array (
131 "Get {$this->description}s from the [[Main Page]]:",
132 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
133 "Get information about the {$this->description} pages in the [[Main Page]]:",
134 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info"
135 );
136 }
137
138 public function getVersion() {
139 return __CLASS__ . ': $Id$';
140 }
141 }
142 ?>