9119a0f0e930847f7bc63951d1bd94bfebb52fe4
4 * Created on May 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiQueryBase.php");
34 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
36 const LINKS
= 'links';
37 const TEMPLATES
= 'templates';
39 private $table, $prefix, $description;
41 public function __construct($query, $moduleName) {
43 switch ($moduleName) {
45 $this->table
= 'pagelinks';
47 $this->description
= 'link';
49 case self
::TEMPLATES
:
50 $this->table
= 'templatelinks';
52 $this->description
= 'template';
55 ApiBase
:: dieDebug(__METHOD__
, 'Unknown module name');
58 parent
:: __construct($query, $moduleName, $this->prefix
);
61 public function execute() {
65 public function executeGenerator($resultPageSet) {
66 $this->run($resultPageSet);
69 private function run($resultPageSet = null) {
71 if ($this->getPageSet()->getGoodTitleCount() == 0)
72 return; // nothing to do
74 $this->addFields(array (
75 $this->prefix
. '_from pl_from',
76 $this->prefix
. '_namespace pl_namespace',
77 $this->prefix
. '_title pl_title'
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'));
85 $res = $this->select(__METHOD__
);
87 if (is_null($resultPageSet)) {
90 $lastId = 0; // database has no ID 0
91 while ($row = $db->fetchObject($res)) {
92 if ($lastId != $row->pl_from
) {
94 $this->addPageSubItems($lastId, $data);
97 $lastId = $row->pl_from
;
100 $title = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
102 ApiQueryBase
:: addTitleInfo($vals, $title);
107 $this->addPageSubItems($lastId, $data);
113 while ($row = $db->fetchObject($res)) {
114 $title = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
115 if($title->userCanRead())
118 $resultPageSet->populateFromTitles($titles);
121 $db->freeResult($res);
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(),
132 protected function getDescription() {
133 return "Returns all {$this->description}s from the given page(s)";
136 protected function getExamples() {
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"
145 public function getVersion() {
146 return __CLASS__
. ': $Id$';