46549ca2b05dada4f3b98051021c4acecda3e8f8
4 * Created on May 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@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");
32 * A query module to list all wiki links on a given set of pages.
36 class ApiQueryLinks
extends ApiQueryGeneratorBase
{
38 const LINKS
= 'links';
39 const TEMPLATES
= 'templates';
41 private $table, $prefix, $description;
43 public function __construct($query, $moduleName) {
45 switch ($moduleName) {
47 $this->table
= 'pagelinks';
49 $this->description
= 'link';
51 case self
::TEMPLATES
:
52 $this->table
= 'templatelinks';
54 $this->description
= 'template';
57 ApiBase
:: dieDebug(__METHOD__
, 'Unknown module name');
60 parent
:: __construct($query, $moduleName, $this->prefix
);
63 public function execute() {
67 public function executeGenerator($resultPageSet) {
68 $this->run($resultPageSet);
71 private function run($resultPageSet = null) {
73 if ($this->getPageSet()->getGoodTitleCount() == 0)
74 return; // nothing to do
76 $this->addFields(array (
77 $this->prefix
. '_from pl_from',
78 $this->prefix
. '_namespace pl_namespace',
79 $this->prefix
. '_title pl_title'
82 $this->addTables($this->table
);
83 $this->addWhereFld($this->prefix
. '_from', array_keys($this->getPageSet()->getGoodTitles()));
84 $this->addOption('ORDER BY', str_replace('pl_', $this->prefix
. '_', 'pl_from, pl_namespace, pl_title'));
87 $res = $this->select(__METHOD__
);
89 if (is_null($resultPageSet)) {
92 $lastId = 0; // database has no ID 0
93 while ($row = $db->fetchObject($res)) {
94 if ($lastId != $row->pl_from
) {
96 $this->addPageSubItems($lastId, $data);
99 $lastId = $row->pl_from
;
102 $title = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
104 ApiQueryBase
:: addTitleInfo($vals, $title);
109 $this->addPageSubItems($lastId, $data);
115 while ($row = $db->fetchObject($res)) {
116 $title = Title
:: makeTitle($row->pl_namespace
, $row->pl_title
);
117 if($title->userCanRead())
120 $resultPageSet->populateFromTitles($titles);
123 $db->freeResult($res);
126 private function addPageSubItems($pageId, $data) {
127 $result = $this->getResult();
128 $result->setIndexedTagName($data, $this->prefix
);
129 $result->addValue(array ('query', 'pages', intval($pageId)),
130 $this->getModuleName(),
134 protected function getDescription() {
135 return "Returns all {$this->description}s from the given page(s)";
138 protected function getExamples() {
140 "Get {$this->description}s from the [[Main Page]]:",
141 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
142 "Get information about the {$this->description} pages in the [[Main Page]]:",
143 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info"
147 public function getVersion() {
148 return __CLASS__
. ': $Id$';