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 $this->addFields(array (
72 $this->prefix
. '_from pl_from',
73 $this->prefix
. '_namespace pl_namespace',
74 $this->prefix
. '_title pl_title'
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'));
82 $res = $this->select(__METHOD__
);
84 if (is_null($resultPageSet)) {
87 $lastId = 0; // database has no ID 0
88 while ($row = $db->fetchObject($res)) {
89 if ($lastId != $row->pl_from
) {
91 $this->addPageSubItems($lastId, $data);
94 $lastId = $row->pl_from
;
96 $vals = $this->addRowInfo('pl', $row);
102 $this->addPageSubItems($lastId, $data);
108 while ($row = $db->fetchObject($res)) {
109 $titles[] = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
111 $resultPageSet->populateFromTitles($titles);
114 $db->freeResult($res);
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(),
125 protected function getDescription() {
126 return "Returns all {$this->description}s from the given page(s)";
129 protected function getExamples() {
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"
138 public function getVersion() {
139 return __CLASS__
. ': $Id$';