9119a0f0e930847f7bc63951d1bd94bfebb52fe4
[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 if ($this->getPageSet()->getGoodTitleCount() == 0)
72 return; // nothing to do
73
74 $this->addFields(array (
75 $this->prefix . '_from pl_from',
76 $this->prefix . '_namespace pl_namespace',
77 $this->prefix . '_title pl_title'
78 ));
79
80 $this->addTables($this->table);
81 $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
82 $this->addOption('ORDER BY', str_replace('pl_', $this->prefix . '_', 'pl_from, pl_namespace, pl_title'));
83
84 $db = $this->getDB();
85 $res = $this->select(__METHOD__);
86
87 if (is_null($resultPageSet)) {
88
89 $data = array();
90 $lastId = 0; // database has no ID 0
91 while ($row = $db->fetchObject($res)) {
92 if ($lastId != $row->pl_from) {
93 if($lastId != 0) {
94 $this->addPageSubItems($lastId, $data);
95 $data = array();
96 }
97 $lastId = $row->pl_from;
98 }
99
100 $title = Title :: makeTitle($row->pl_namespace, $row->pl_title);
101 $vals = array();
102 ApiQueryBase :: addTitleInfo($vals, $title);
103 $data[] = $vals;
104 }
105
106 if($lastId != 0) {
107 $this->addPageSubItems($lastId, $data);
108 }
109
110 } else {
111
112 $titles = array();
113 while ($row = $db->fetchObject($res)) {
114 $title = Title :: makeTitle($row->pl_namespace, $row->pl_title);
115 if($title->userCanRead())
116 $titles[] = $title;
117 }
118 $resultPageSet->populateFromTitles($titles);
119 }
120
121 $db->freeResult($res);
122 }
123
124 private function addPageSubItems($pageId, $data) {
125 $result = $this->getResult();
126 $result->setIndexedTagName($data, $this->prefix);
127 $result->addValue(array ('query', 'pages', intval($pageId)),
128 $this->getModuleName(),
129 $data);
130 }
131
132 protected function getDescription() {
133 return "Returns all {$this->description}s from the given page(s)";
134 }
135
136 protected function getExamples() {
137 return array (
138 "Get {$this->description}s from the [[Main Page]]:",
139 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
140 "Get information about the {$this->description} pages in the [[Main Page]]:",
141 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info"
142 );
143 }
144
145 public function getVersion() {
146 return __CLASS__ . ': $Id$';
147 }
148 }
149 ?>